@rvoh/dream 2.2.3 → 2.3.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/Dream.js +5 -5
- package/dist/cjs/src/db/dataTypes.js +1 -0
- package/dist/cjs/src/db/index.js +2 -2
- package/dist/cjs/src/dream/QueryDriver/Base.js +2 -2
- package/dist/cjs/src/dream/QueryDriver/Kysely.js +2 -2
- package/dist/cjs/src/dream/QueryDriver/helpers/kysely/checkForNeedToBeRunMigrations.js +2 -2
- package/dist/cjs/src/dream/QueryDriver/helpers/kysely/migrationFolderPath.js +6 -1
- package/dist/cjs/src/dream/QueryDriver/helpers/kysely/runMigration.js +2 -2
- package/dist/cjs/src/dream/internal/saveDream.js +1 -1
- package/dist/cjs/src/dream/internal/softDeleteDream.js +1 -1
- package/dist/cjs/src/dream-app/helpers/globalModelKeyFromPath.js +5 -2
- package/dist/cjs/src/dream-app/helpers/globalSerializerKeyFromPath.js +5 -2
- package/dist/cjs/src/dream-app/helpers/normalizeFilePath.js +3 -0
- package/dist/cjs/src/dream-app/index.js +3 -3
- package/dist/cjs/src/helpers/areEqual.js +2 -2
- package/dist/cjs/src/helpers/cli/ASTBuilder.js +2 -2
- package/dist/cjs/src/helpers/cloneDeepSafe.js +2 -2
- package/dist/cjs/src/helpers/customPgParsers.js +2 -2
- package/dist/cjs/src/helpers/loadRepl.js +2 -2
- package/dist/cjs/src/helpers/path/PathHelpers.js +27 -0
- package/dist/cjs/src/helpers/path/convertToFileURL.js +7 -0
- package/dist/cjs/src/helpers/path/windowsSafePath.js +22 -0
- package/dist/cjs/src/helpers/sortBy.js +2 -2
- package/dist/cjs/src/helpers/sqlAttributes.js +2 -2
- package/dist/cjs/src/helpers/stringCasing.js +2 -2
- package/dist/cjs/src/package-exports/errors.js +2 -1
- package/dist/cjs/src/package-exports/index.js +2 -2
- package/dist/cjs/src/package-exports/system.js +1 -0
- package/dist/cjs/src/serializer/SerializerRenderer.js +2 -2
- package/dist/cjs/src/types/calendardate.js +1 -0
- package/dist/cjs/src/types/datetime.js +1 -0
- package/dist/cjs/src/utils/datetime/CalendarDate.js +489 -0
- package/dist/cjs/src/utils/datetime/DateTime.js +1140 -0
- package/dist/cjs/src/utils/datetime/helpers/isoTimeDecimalString.js +8 -0
- package/dist/cjs/src/utils/datetime/helpers/microsecondParts.js +16 -0
- package/dist/cjs/src/utils/datetime/helpers/replaceISOMicroseconds.js +10 -0
- package/dist/esm/src/Dream.js +5 -5
- package/dist/esm/src/db/dataTypes.js +1 -0
- package/dist/esm/src/db/index.js +2 -2
- package/dist/esm/src/dream/QueryDriver/Base.js +2 -2
- package/dist/esm/src/dream/QueryDriver/Kysely.js +2 -2
- package/dist/esm/src/dream/QueryDriver/helpers/kysely/checkForNeedToBeRunMigrations.js +2 -2
- package/dist/esm/src/dream/QueryDriver/helpers/kysely/migrationFolderPath.js +6 -1
- package/dist/esm/src/dream/QueryDriver/helpers/kysely/runMigration.js +2 -2
- package/dist/esm/src/dream/internal/saveDream.js +1 -1
- package/dist/esm/src/dream/internal/softDeleteDream.js +1 -1
- package/dist/esm/src/dream-app/helpers/globalModelKeyFromPath.js +5 -2
- package/dist/esm/src/dream-app/helpers/globalSerializerKeyFromPath.js +5 -2
- package/dist/esm/src/dream-app/helpers/normalizeFilePath.js +3 -0
- package/dist/esm/src/dream-app/index.js +3 -3
- package/dist/esm/src/helpers/areEqual.js +2 -2
- package/dist/esm/src/helpers/cli/ASTBuilder.js +2 -2
- package/dist/esm/src/helpers/cloneDeepSafe.js +2 -2
- package/dist/esm/src/helpers/customPgParsers.js +2 -2
- package/dist/esm/src/helpers/loadRepl.js +2 -2
- package/dist/esm/src/helpers/path/PathHelpers.js +27 -0
- package/dist/esm/src/helpers/path/convertToFileURL.js +7 -0
- package/dist/esm/src/helpers/path/windowsSafePath.js +22 -0
- package/dist/esm/src/helpers/sortBy.js +2 -2
- package/dist/esm/src/helpers/sqlAttributes.js +2 -2
- package/dist/esm/src/helpers/stringCasing.js +2 -2
- package/dist/esm/src/package-exports/errors.js +2 -1
- package/dist/esm/src/package-exports/index.js +2 -2
- package/dist/esm/src/package-exports/system.js +1 -0
- package/dist/esm/src/serializer/SerializerRenderer.js +2 -2
- package/dist/esm/src/types/calendardate.js +1 -0
- package/dist/esm/src/types/datetime.js +1 -0
- package/dist/esm/src/utils/datetime/CalendarDate.js +489 -0
- package/dist/esm/src/utils/datetime/DateTime.js +1140 -0
- package/dist/esm/src/utils/datetime/helpers/isoTimeDecimalString.js +8 -0
- package/dist/esm/src/utils/datetime/helpers/microsecondParts.js +16 -0
- package/dist/esm/src/utils/datetime/helpers/replaceISOMicroseconds.js +10 -0
- package/dist/types/src/Dream.d.ts +1 -1
- package/dist/types/src/db/dataTypes.d.ts +2 -2
- package/dist/types/src/dream-app/helpers/normalizeFilePath.d.ts +1 -0
- package/dist/types/src/helpers/customPgParsers.d.ts +3 -2
- package/dist/types/src/helpers/path/PathHelpers.d.ts +21 -0
- package/dist/types/src/helpers/path/convertToFileURL.d.ts +1 -0
- package/dist/types/src/helpers/path/windowsSafePath.d.ts +20 -0
- package/dist/types/src/helpers/range.d.ts +2 -2
- package/dist/types/src/helpers/sort.d.ts +2 -2
- package/dist/types/src/helpers/sortBy.d.ts +2 -2
- package/dist/types/src/package-exports/errors.d.ts +2 -1
- package/dist/types/src/package-exports/index.d.ts +2 -2
- package/dist/types/src/package-exports/system.d.ts +1 -0
- package/dist/types/src/package-exports/types.d.ts +2 -0
- package/dist/types/src/types/associations/shared.d.ts +2 -2
- package/dist/types/src/types/associations/shared.ts +2 -2
- package/dist/types/src/types/calendardate.d.ts +4 -0
- package/dist/types/src/types/calendardate.ts +8 -0
- package/dist/types/src/types/datetime.d.ts +102 -0
- package/dist/types/src/types/datetime.ts +141 -0
- package/dist/types/src/types/dream.d.ts +2 -2
- package/dist/types/src/types/dream.ts +2 -2
- package/dist/types/src/types/moduleDeclarations/luxon.d.ts +5 -6
- package/dist/types/src/utils/datetime/CalendarDate.d.ts +375 -0
- package/dist/types/src/utils/datetime/DateTime.d.ts +773 -0
- package/dist/types/src/utils/datetime/helpers/isoTimeDecimalString.d.ts +4 -0
- package/dist/types/src/utils/datetime/helpers/microsecondParts.d.ts +10 -0
- package/dist/types/src/utils/datetime/helpers/replaceISOMicroseconds.d.ts +3 -0
- package/docs/assets/highlight.css +7 -0
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/db.DreamMigrationHelpers.html +9 -9
- package/docs/classes/db.KyselyQueryDriver.html +33 -33
- package/docs/classes/db.PostgresQueryDriver.html +34 -34
- package/docs/classes/db.QueryDriverBase.html +32 -32
- package/docs/classes/errors.CheckConstraintViolation.html +3 -3
- package/docs/classes/errors.ColumnOverflow.html +3 -3
- package/docs/classes/errors.CreateOrFindByFailedToCreateAndFind.html +3 -3
- package/docs/classes/errors.DataIncompatibleWithDatabaseField.html +3 -3
- package/docs/classes/errors.DataTypeColumnTypeMismatch.html +3 -3
- package/docs/classes/errors.GlobalNameNotSet.html +3 -3
- package/docs/classes/errors.InvalidCalendarDate.html +7 -2
- package/docs/classes/errors.InvalidDateTime.html +17 -0
- package/docs/classes/errors.MissingSerializersDefinition.html +3 -3
- package/docs/classes/errors.NonLoadedAssociation.html +3 -3
- package/docs/classes/errors.NotNullViolation.html +3 -3
- package/docs/classes/errors.RecordNotFound.html +3 -3
- package/docs/classes/errors.ValidationError.html +3 -3
- package/docs/classes/index.CalendarDate.html +195 -2
- package/docs/classes/index.DateTime.html +485 -0
- package/docs/classes/index.Decorators.html +19 -19
- package/docs/classes/index.Dream.html +121 -121
- package/docs/classes/index.DreamApp.html +5 -5
- package/docs/classes/index.DreamTransaction.html +2 -2
- package/docs/classes/index.Env.html +2 -2
- package/docs/classes/index.Query.html +59 -59
- package/docs/classes/system.CliFileWriter.html +2 -2
- package/docs/classes/system.DreamBin.html +2 -2
- package/docs/classes/system.DreamCLI.html +5 -5
- package/docs/classes/system.DreamImporter.html +2 -2
- package/docs/classes/system.DreamLogos.html +2 -2
- package/docs/classes/system.DreamSerializerBuilder.html +8 -8
- package/docs/classes/system.ObjectSerializerBuilder.html +8 -8
- package/docs/classes/system.PathHelpers.html +13 -0
- package/docs/classes/utils.Encrypt.html +2 -2
- package/docs/classes/utils.Range.html +2 -2
- package/docs/functions/db.closeAllDbConnections.html +1 -1
- package/docs/functions/db.dreamDbConnections.html +1 -1
- package/docs/functions/db.untypedDb.html +1 -1
- package/docs/functions/db.validateColumn.html +1 -1
- package/docs/functions/db.validateTable.html +1 -1
- package/docs/functions/errors.pgErrorType.html +1 -1
- package/docs/functions/index.DreamSerializer.html +1 -1
- package/docs/functions/index.ObjectSerializer.html +1 -1
- package/docs/functions/index.ReplicaSafe.html +1 -1
- package/docs/functions/index.STI.html +1 -1
- package/docs/functions/index.SoftDelete.html +1 -1
- package/docs/functions/utils.camelize.html +1 -1
- package/docs/functions/utils.capitalize.html +1 -1
- package/docs/functions/utils.cloneDeepSafe.html +1 -1
- package/docs/functions/utils.compact.html +2 -2
- package/docs/functions/utils.groupBy.html +1 -1
- package/docs/functions/utils.hyphenize.html +1 -1
- package/docs/functions/utils.intersection.html +1 -1
- package/docs/functions/utils.isEmpty.html +1 -1
- package/docs/functions/utils.normalizeUnicode.html +1 -1
- package/docs/functions/utils.pascalize.html +1 -1
- package/docs/functions/utils.percent.html +1 -1
- package/docs/functions/utils.range-1.html +1 -1
- package/docs/functions/utils.round.html +1 -1
- package/docs/functions/utils.sanitizeString.html +1 -1
- package/docs/functions/utils.snakeify.html +1 -1
- package/docs/functions/utils.sort.html +1 -1
- package/docs/functions/utils.sortBy.html +1 -1
- package/docs/functions/utils.sortObjectByKey.html +1 -1
- package/docs/functions/utils.sortObjectByValue.html +1 -1
- package/docs/functions/utils.uncapitalize.html +1 -1
- package/docs/functions/utils.uniq.html +1 -1
- package/docs/interfaces/openapi.OpenapiDescription.html +2 -2
- package/docs/interfaces/openapi.OpenapiSchemaProperties.html +1 -1
- package/docs/interfaces/openapi.OpenapiSchemaPropertiesShorthand.html +1 -1
- package/docs/interfaces/openapi.OpenapiTypeFieldObject.html +1 -1
- package/docs/interfaces/types.BelongsToStatement.html +2 -2
- package/docs/interfaces/types.DecoratorContext.html +2 -2
- package/docs/interfaces/types.DreamAppInitOptions.html +2 -2
- package/docs/interfaces/types.DreamAppOpts.html +2 -2
- package/docs/interfaces/types.DurationObject.html +12 -0
- package/docs/interfaces/types.EncryptOptions.html +2 -2
- package/docs/interfaces/types.InternalAnyTypedSerializerRendersMany.html +2 -2
- package/docs/interfaces/types.InternalAnyTypedSerializerRendersOne.html +2 -2
- package/docs/interfaces/types.SerializerRendererOpts.html +2 -2
- package/docs/modules/db.html +1 -1
- package/docs/modules/errors.html +2 -1
- package/docs/modules/index.html +3 -4
- package/docs/modules/openapi.html +1 -1
- package/docs/modules/system.html +2 -1
- package/docs/modules/types.html +6 -2
- package/docs/modules/utils.html +1 -1
- package/docs/types/openapi.CommonOpenapiSchemaObjectFields.html +1 -1
- package/docs/types/openapi.OpenapiAllTypes.html +1 -1
- package/docs/types/openapi.OpenapiFormats.html +1 -1
- package/docs/types/openapi.OpenapiNumberFormats.html +1 -1
- package/docs/types/openapi.OpenapiPrimitiveBaseTypes.html +1 -1
- package/docs/types/openapi.OpenapiPrimitiveTypes.html +1 -1
- package/docs/types/openapi.OpenapiSchemaArray.html +1 -1
- package/docs/types/openapi.OpenapiSchemaArrayShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaBase.html +1 -1
- package/docs/types/openapi.OpenapiSchemaBody.html +1 -1
- package/docs/types/openapi.OpenapiSchemaBodyShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaCommonFields.html +1 -1
- package/docs/types/openapi.OpenapiSchemaExpressionAllOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaExpressionAnyOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaExpressionOneOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaExpressionRef.html +1 -1
- package/docs/types/openapi.OpenapiSchemaExpressionRefSchemaShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaInteger.html +1 -1
- package/docs/types/openapi.OpenapiSchemaNull.html +1 -1
- package/docs/types/openapi.OpenapiSchemaNumber.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObject.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAllOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAllOfShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAnyOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectAnyOfShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectBase.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectBaseShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectOneOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectOneOfShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaObjectShorthand.html +1 -1
- package/docs/types/openapi.OpenapiSchemaPrimitiveGeneric.html +1 -1
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionAllOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionAnyOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionOneOf.html +1 -1
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionSerializableRef.html +1 -1
- package/docs/types/openapi.OpenapiSchemaShorthandExpressionSerializerRef.html +1 -1
- package/docs/types/openapi.OpenapiSchemaShorthandPrimitiveGeneric.html +1 -1
- package/docs/types/openapi.OpenapiSchemaString.html +1 -1
- package/docs/types/openapi.OpenapiShorthandAllTypes.html +1 -1
- package/docs/types/openapi.OpenapiShorthandPrimitiveBaseTypes.html +1 -1
- package/docs/types/openapi.OpenapiShorthandPrimitiveTypes.html +1 -1
- package/docs/types/openapi.OpenapiTypeField.html +1 -1
- package/docs/types/system.DreamAppAllowedPackageManagersEnum.html +1 -1
- package/docs/types/types.CalendarDateDurationUnit.html +1 -0
- package/docs/types/types.Camelized.html +1 -1
- package/docs/types/types.DbConnectionType.html +1 -1
- package/docs/types/types.DbTypes.html +1 -1
- package/docs/types/types.DreamAssociationMetadata.html +1 -1
- package/docs/types/types.DreamAttributes.html +1 -1
- package/docs/types/types.DreamClassAssociationAndStatement.html +1 -1
- package/docs/types/types.DreamClassColumn.html +1 -1
- package/docs/types/types.DreamColumn.html +1 -1
- package/docs/types/types.DreamColumnNames.html +1 -1
- package/docs/types/types.DreamLogLevel.html +1 -1
- package/docs/types/types.DreamLogger.html +1 -1
- package/docs/types/types.DreamModelSerializerType.html +1 -1
- package/docs/types/types.DreamOrViewModelClassSerializerKey.html +1 -1
- package/docs/types/types.DreamOrViewModelSerializerKey.html +1 -1
- package/docs/types/types.DreamParamSafeAttributes.html +1 -1
- package/docs/types/types.DreamParamSafeColumnNames.html +1 -1
- package/docs/types/types.DreamSerializable.html +1 -1
- package/docs/types/types.DreamSerializableArray.html +1 -1
- package/docs/types/types.DreamSerializerKey.html +1 -1
- package/docs/types/types.DreamSerializers.html +1 -1
- package/docs/types/types.DreamVirtualColumns.html +1 -1
- package/docs/types/types.DurationUnit.html +4 -0
- package/docs/types/types.EncryptAlgorithm.html +1 -1
- package/docs/types/types.HasManyStatement.html +1 -1
- package/docs/types/types.HasOneStatement.html +1 -1
- package/docs/types/types.Hyphenized.html +1 -1
- package/docs/types/types.Pascalized.html +1 -1
- package/docs/types/types.PrimaryKeyType.html +1 -1
- package/docs/types/types.RoundingPrecision.html +1 -1
- package/docs/types/types.SerializerCasing.html +1 -1
- package/docs/types/types.SimpleObjectSerializerType.html +1 -1
- package/docs/types/types.Snakeified.html +1 -1
- package/docs/types/types.StrictInterface.html +1 -1
- package/docs/types/types.UpdateableAssociationProperties.html +1 -1
- package/docs/types/types.UpdateableProperties.html +1 -1
- package/docs/types/types.ValidationType.html +1 -1
- package/docs/types/types.ViewModel.html +1 -1
- package/docs/types/types.ViewModelClass.html +1 -1
- package/docs/types/types.WeekdayName.html +2 -0
- package/docs/types/types.WhereStatementForDream.html +1 -1
- package/docs/types/types.WhereStatementForDreamClass.html +1 -1
- package/docs/variables/index.DreamConst.html +1 -1
- package/docs/variables/index.ops.html +1 -1
- package/docs/variables/openapi.openapiPrimitiveTypes-1.html +1 -1
- package/docs/variables/openapi.openapiShorthandPrimitiveTypes-1.html +1 -1
- package/docs/variables/system.DreamAppAllowedPackageManagersEnumValues.html +1 -1
- package/docs/variables/system.primaryKeyTypes.html +1 -1
- package/package.json +3 -2
- package/dist/cjs/src/helpers/CalendarDate.js +0 -151
- package/dist/cjs/src/helpers/DateTime.js +0 -4
- package/dist/esm/src/helpers/CalendarDate.js +0 -151
- package/dist/esm/src/helpers/DateTime.js +0 -4
- package/dist/types/src/helpers/CalendarDate.d.ts +0 -56
- package/dist/types/src/helpers/DateTime.d.ts +0 -6
- package/dist/types/src/types/luxon/_util.d.ts +0 -20
- package/dist/types/src/types/luxon/datetime.d.ts +0 -1759
- package/dist/types/src/types/luxon/duration.d.ts +0 -494
- package/dist/types/src/types/luxon/info.d.ts +0 -217
- package/dist/types/src/types/luxon/interval.d.ts +0 -357
- package/dist/types/src/types/luxon/misc.d.ts +0 -67
- package/dist/types/src/types/luxon/settings.d.ts +0 -91
- package/dist/types/src/types/luxon/zone.d.ts +0 -172
- package/docs/types/index.DateTime.html +0 -1
- package/docs/variables/index.DateTime-1.html +0 -1
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { DateTime } from './DateTime.js';
|
|
2
|
+
/**
|
|
3
|
+
* CalendarDate represents a date without time or timezone information.
|
|
4
|
+
*/
|
|
5
|
+
export default class CalendarDate {
|
|
6
|
+
dateTime;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a CalendarDate from a DateTime, year/month/day, or defaults to today in UTC.
|
|
9
|
+
* @param source - DateTime instance, year number, or null/undefined for today
|
|
10
|
+
* @param month - Month (1-12) when source is a year number
|
|
11
|
+
* @param day - Day of month when source is a year number
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* new CalendarDate() // today
|
|
15
|
+
* new CalendarDate(DateTime.now()) // from DateTime
|
|
16
|
+
* new CalendarDate(2026, 2, 7) // February 7, 2026
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
constructor(source, month = 1, day = 1) {
|
|
20
|
+
if (source instanceof DateTime && source.isValid) {
|
|
21
|
+
const isoDate = source.toISODate();
|
|
22
|
+
this.dateTime = DateTime.fromISO(isoDate, { zone: 'UTC' });
|
|
23
|
+
}
|
|
24
|
+
else if (typeof source === 'number') {
|
|
25
|
+
try {
|
|
26
|
+
this.dateTime = DateTime.utc(source, month, day);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error instanceof Error)
|
|
30
|
+
throw new InvalidCalendarDate(error);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.dateTime = CalendarDate.today().toDateTime();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Create a CalendarDate from a DateTime instance.
|
|
40
|
+
* @param dateTime - A DateTime instance
|
|
41
|
+
* @returns A CalendarDate for the date portion of the DateTime
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* CalendarDate.fromDateTime(DateTime.now())
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
static fromDateTime(dateTime) {
|
|
48
|
+
return new CalendarDate(dateTime);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create a CalendarDate from a JavaScript Date.
|
|
52
|
+
* @param javascriptDate - A JavaScript Date instance
|
|
53
|
+
* @param options - Optional zone to interpret the date in
|
|
54
|
+
* @returns A CalendarDate for the date portion
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* CalendarDate.fromJSDate(new Date())
|
|
58
|
+
* CalendarDate.fromJSDate(new Date(), { zone: 'America/New_York' })
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
static fromJSDate(javascriptDate, { zone } = {}) {
|
|
62
|
+
return new CalendarDate(DateTime.fromJSDate(javascriptDate, zone ? { zone } : {}));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Create a CalendarDate from an ISO 8601 date string.
|
|
66
|
+
* @param str - ISO date string (e.g., '2026-02-07')
|
|
67
|
+
* @param options - Optional zone to interpret the date in (only affects ISO strings that include a time component)
|
|
68
|
+
* @returns A CalendarDate for the given date
|
|
69
|
+
* @throws {InvalidCalendarDate} When the ISO string is invalid
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* CalendarDate.fromISO('2026-02-07')
|
|
73
|
+
* CalendarDate.fromISO('2026-02-07T09:00:00Z') // time portion ignored
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
static fromISO(str, { zone } = {}) {
|
|
77
|
+
let dateTime;
|
|
78
|
+
try {
|
|
79
|
+
dateTime = DateTime.fromISO(str, zone ? { zone } : { setZone: true });
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (error instanceof Error)
|
|
83
|
+
throw new InvalidCalendarDate(error);
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
return new CalendarDate(dateTime);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Create a CalendarDate from an SQL date string.
|
|
90
|
+
* @param str - SQL date string (e.g., '2026-02-07')
|
|
91
|
+
* @returns A CalendarDate for the given date
|
|
92
|
+
* @throws {InvalidCalendarDate} When the SQL string is invalid
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* CalendarDate.fromSQL('2026-02-07')
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
static fromSQL(str) {
|
|
99
|
+
let dateTime;
|
|
100
|
+
try {
|
|
101
|
+
dateTime = DateTime.fromSQL(str, { zone: 'UTC' });
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error instanceof Error)
|
|
105
|
+
throw new InvalidCalendarDate(error);
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
return new CalendarDate(dateTime);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Create a CalendarDate from an object with date units.
|
|
112
|
+
* @param obj - Object with year, month, day properties
|
|
113
|
+
* @param opts - Optional zone/locale options
|
|
114
|
+
* @returns A CalendarDate for the given components
|
|
115
|
+
* @throws {InvalidCalendarDate} When date values are invalid
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* CalendarDate.fromObject({ year: 2026, month: 2, day: 7 })
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
static fromObject(obj, opts) {
|
|
122
|
+
let dateTime;
|
|
123
|
+
try {
|
|
124
|
+
dateTime = DateTime.fromObject(obj, opts);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
if (error instanceof Error)
|
|
128
|
+
throw new InvalidCalendarDate(error);
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
return new CalendarDate(dateTime);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns a CalendarDate for today's date.
|
|
135
|
+
* @param options - Optional zone (defaults to UTC)
|
|
136
|
+
* @returns A CalendarDate for today
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* CalendarDate.today()
|
|
140
|
+
* CalendarDate.today({ zone: 'America/New_York' })
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
static today({ zone = 'UTC' } = {}) {
|
|
144
|
+
return new CalendarDate(DateTime.now().setZone(zone));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Returns a CalendarDate for tomorrow's date.
|
|
148
|
+
* @param options - Optional zone (defaults to UTC)
|
|
149
|
+
* @returns A CalendarDate for tomorrow
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* CalendarDate.tomorrow()
|
|
153
|
+
* CalendarDate.tomorrow({ zone: 'America/New_York' })
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
static tomorrow(options = {}) {
|
|
157
|
+
return CalendarDate.today(options).plus({ day: 1 });
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Returns a CalendarDate for yesterday's date.
|
|
161
|
+
* @param options - Optional zone (defaults to UTC)
|
|
162
|
+
* @returns A CalendarDate for yesterday
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* CalendarDate.yesterday()
|
|
166
|
+
* CalendarDate.yesterday({ zone: 'America/New_York' })
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
static yesterday(options = {}) {
|
|
170
|
+
return CalendarDate.today(options).minus({ day: 1 });
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Returns the date as an ISO 8601 string.
|
|
174
|
+
* @returns ISO date string (e.g., '2026-02-07')
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* CalendarDate.fromObject({ year: 2026, month: 2, day: 7 }).toISO() // '2026-02-07'
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
toISO() {
|
|
181
|
+
return this.dateTime.toISODate();
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Returns the date as an SQL date string.
|
|
185
|
+
* @returns SQL date string (e.g., '2026-02-07')
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* CalendarDate.fromObject({ year: 2026, month: 2, day: 7 }).toSQL() // '2026-02-07'
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
toSQL() {
|
|
192
|
+
return this.dateTime.toSQLDate();
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns the date as an ISO string for JSON serialization.
|
|
196
|
+
* @returns ISO date string (e.g., '2026-02-07')
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* JSON.stringify({ date: CalendarDate.today() })
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
toJSON() {
|
|
203
|
+
return this.toISO();
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Returns the epoch milliseconds for the date at midnight UTC.
|
|
207
|
+
* @returns Unix timestamp in milliseconds
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* CalendarDate.fromISO('2026-02-07').valueOf()
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
valueOf() {
|
|
214
|
+
return this.dateTime.valueOf();
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Returns the date as an ISO date string (same as toISO).
|
|
218
|
+
* @returns ISO date string (e.g., '2026-02-07')
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* CalendarDate.fromObject({ year: 2026, month: 2, day: 7 }).toISODate() // '2026-02-07'
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
toISODate() {
|
|
225
|
+
return this.toISO();
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Returns a localized string representation of the date.
|
|
229
|
+
* @param formatOpts - Intl.DateTimeFormat options for formatting
|
|
230
|
+
* @param opts - Optional locale options
|
|
231
|
+
* @returns Localized date string
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* CalendarDate.today().toLocaleString()
|
|
235
|
+
* CalendarDate.today().toLocaleString({ month: 'long', day: 'numeric', year: 'numeric' })
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
toLocaleString(formatOpts, opts) {
|
|
239
|
+
return this.dateTime.toLocaleString(formatOpts, opts);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Returns the date as an ISO string (same as toISO).
|
|
243
|
+
* @returns ISO date string (e.g., '2026-02-07')
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* String(CalendarDate.today())
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
toString() {
|
|
250
|
+
return this.toISO();
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Returns the underlying DateTime instance (at midnight UTC).
|
|
254
|
+
* @returns A DateTime representing this date at 00:00:00 UTC
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* const dt = CalendarDate.today().toDateTime()
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
toDateTime() {
|
|
261
|
+
return this.dateTime;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Returns a JavaScript Date for this date (at midnight UTC).
|
|
265
|
+
* @returns A JavaScript Date instance
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* const jsDate = CalendarDate.today().toJSDate()
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
toJSDate() {
|
|
272
|
+
return this.dateTime.toJSDate();
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Gets the year.
|
|
276
|
+
* @returns The year number
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* CalendarDate.fromISO('2026-02-07').year // 2026
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
get year() {
|
|
283
|
+
return this.dateTime.year;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Gets the month (1-12).
|
|
287
|
+
* @returns The month number (1 = January, 12 = December)
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* CalendarDate.fromISO('2026-02-07').month // 2
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
get month() {
|
|
294
|
+
return this.dateTime.month;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Gets the day of the month.
|
|
298
|
+
* @returns The day number (1-31)
|
|
299
|
+
* @example
|
|
300
|
+
* ```ts
|
|
301
|
+
* CalendarDate.fromISO('2026-02-07').day // 7
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
get day() {
|
|
305
|
+
return this.dateTime.day;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Returns the lowercase name of the weekday.
|
|
309
|
+
* @returns Weekday name: 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', or 'sunday'
|
|
310
|
+
* @example
|
|
311
|
+
* ```ts
|
|
312
|
+
* CalendarDate.fromISO('2026-02-09').weekdayName // 'monday' (Feb 9, 2026 is a Monday)
|
|
313
|
+
* CalendarDate.fromISO('2026-02-07').weekdayName // 'saturday'
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
get weekdayName() {
|
|
317
|
+
return this.dateTime.weekdayName;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Returns a new CalendarDate at the start of the given period.
|
|
321
|
+
* @param period - Unit to truncate to ('year', 'quarter', 'month', 'week', or 'day')
|
|
322
|
+
* @returns A CalendarDate at the start of the period
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* CalendarDate.fromISO('2026-02-15').startOf('month') // 2026-02-01
|
|
326
|
+
* CalendarDate.fromISO('2026-02-15').startOf('year') // 2026-01-01
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
startOf(period) {
|
|
330
|
+
return new CalendarDate(this.dateTime.startOf(period));
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Returns a new CalendarDate at the end of the given period.
|
|
334
|
+
* @param period - Unit to extend to end of ('year', 'quarter', 'month', 'week', or 'day')
|
|
335
|
+
* @returns A CalendarDate at the end of the period
|
|
336
|
+
* @example
|
|
337
|
+
* ```ts
|
|
338
|
+
* CalendarDate.fromISO('2026-02-15').endOf('month') // 2026-02-28
|
|
339
|
+
* CalendarDate.fromISO('2026-02-15').endOf('year') // 2026-12-31
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
endOf(period) {
|
|
343
|
+
return new CalendarDate(this.dateTime.endOf(period));
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Returns a new CalendarDate with the given duration added.
|
|
347
|
+
* @param duration - Duration to add (object with years, months, weeks, days, etc.)
|
|
348
|
+
* @returns A new CalendarDate
|
|
349
|
+
* @example
|
|
350
|
+
* ```ts
|
|
351
|
+
* CalendarDate.fromISO('2026-02-07').plus({ days: 5 }) // 2026-02-12
|
|
352
|
+
* CalendarDate.fromISO('2026-02-07').plus({ months: 2 }) // 2026-04-07
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
plus(duration) {
|
|
356
|
+
return new CalendarDate(this.dateTime.plus(duration));
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Returns a new CalendarDate with the given duration subtracted.
|
|
360
|
+
* @param duration - Duration to subtract (object with years, months, weeks, days, etc.)
|
|
361
|
+
* @returns A new CalendarDate
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* CalendarDate.fromISO('2026-02-07').minus({ days: 5 }) // 2026-02-02
|
|
365
|
+
* CalendarDate.fromISO('2026-02-07').minus({ months: 2 }) // 2025-12-07
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
minus(duration) {
|
|
369
|
+
return new CalendarDate(this.dateTime.minus(duration));
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Returns the latest CalendarDate from the given arguments.
|
|
373
|
+
* @param calendarDates - CalendarDates to compare
|
|
374
|
+
* @returns The latest CalendarDate
|
|
375
|
+
* @example
|
|
376
|
+
* ```ts
|
|
377
|
+
* CalendarDate.max(date1, date2, date3)
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
static max(...calendarDates) {
|
|
381
|
+
const dateTimes = calendarDates.map(calendarDate => calendarDate.toDateTime());
|
|
382
|
+
return new CalendarDate(DateTime.max(...dateTimes));
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Returns the earliest CalendarDate from the given arguments.
|
|
386
|
+
* @param calendarDates - CalendarDates to compare
|
|
387
|
+
* @returns The earliest CalendarDate
|
|
388
|
+
* @example
|
|
389
|
+
* ```ts
|
|
390
|
+
* CalendarDate.min(date1, date2, date3)
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
393
|
+
static min(...calendarDates) {
|
|
394
|
+
const dateTimes = calendarDates.map(calendarDate => calendarDate.toDateTime());
|
|
395
|
+
return new CalendarDate(DateTime.min(...dateTimes));
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Returns true if this and other are in the same unit of time.
|
|
399
|
+
* @param otherCalendarDate - CalendarDate to compare against
|
|
400
|
+
* @param period - Unit to check ('year', 'quarter', 'month', 'week', or 'day')
|
|
401
|
+
* @returns true if same period
|
|
402
|
+
* @example
|
|
403
|
+
* ```ts
|
|
404
|
+
* const d1 = CalendarDate.fromISO('2026-02-07')
|
|
405
|
+
* const d2 = CalendarDate.fromISO('2026-02-15')
|
|
406
|
+
* d1.hasSame(d2, 'month') // true
|
|
407
|
+
* d1.hasSame(d2, 'day') // false
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
hasSame(otherCalendarDate, period) {
|
|
411
|
+
const otherDateTime = otherCalendarDate.toDateTime();
|
|
412
|
+
if (otherDateTime === null)
|
|
413
|
+
return false;
|
|
414
|
+
return this.dateTime.hasSame(otherDateTime, period);
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Returns the difference between this CalendarDate and another in the specified unit.
|
|
418
|
+
* @param otherCalendarDate - CalendarDate to compare against
|
|
419
|
+
* @param duration - Unit for the difference ('years', 'quarters', 'months', 'weeks', or 'days')
|
|
420
|
+
* @returns Numeric difference in the specified unit
|
|
421
|
+
* @example
|
|
422
|
+
* ```ts
|
|
423
|
+
* const d1 = CalendarDate.fromISO('2026-02-07')
|
|
424
|
+
* const d2 = CalendarDate.fromISO('2026-02-15')
|
|
425
|
+
* d2.diff(d1, 'days') // 8
|
|
426
|
+
* d2.diff(d1, 'months') // 0
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
diff(otherCalendarDate, duration) {
|
|
430
|
+
const otherDateTime = otherCalendarDate.toDateTime();
|
|
431
|
+
// For quarters and months, use Luxon's diff directly since DateTime.diff doesn't support them
|
|
432
|
+
if (duration === 'quarters' || duration === 'months') {
|
|
433
|
+
const luxonDiff = this.dateTime.toLuxon().diff(otherDateTime.toLuxon(), duration);
|
|
434
|
+
return luxonDiff[duration] ?? 0;
|
|
435
|
+
}
|
|
436
|
+
const result = this.dateTime.diff(otherDateTime, duration);
|
|
437
|
+
return result[duration] ?? 0;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Returns the difference between this CalendarDate and today in the specified unit.
|
|
441
|
+
* @param duration - Unit for the difference ('years', 'quarters', 'months', 'weeks', or 'days')
|
|
442
|
+
* @returns Numeric difference in the specified unit (rounded up)
|
|
443
|
+
* @example
|
|
444
|
+
* ```ts
|
|
445
|
+
* const future = CalendarDate.today().plus({ days: 5 })
|
|
446
|
+
* future.diffNow('days') // approximately 5
|
|
447
|
+
* ```
|
|
448
|
+
*/
|
|
449
|
+
diffNow(duration) {
|
|
450
|
+
// For quarters and months, use Luxon's diff directly since DateTime.diffNow doesn't support them
|
|
451
|
+
if (duration === 'quarters' || duration === 'months') {
|
|
452
|
+
const luxonDiff = this.dateTime.toLuxon().diffNow(duration);
|
|
453
|
+
return Math.ceil(luxonDiff[duration] ?? 0);
|
|
454
|
+
}
|
|
455
|
+
const result = this.dateTime.diffNow(duration);
|
|
456
|
+
return Math.ceil(result[duration] ?? 0);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Returns true if this CalendarDate equals another CalendarDate.
|
|
460
|
+
* @param otherCalendarDate - CalendarDate to compare
|
|
461
|
+
* @returns true if dates are equal
|
|
462
|
+
* @example
|
|
463
|
+
* ```ts
|
|
464
|
+
* const d1 = CalendarDate.fromISO('2026-02-07')
|
|
465
|
+
* const d2 = CalendarDate.fromISO('2026-02-07')
|
|
466
|
+
* d1.equals(d2) // true
|
|
467
|
+
* ```
|
|
468
|
+
*/
|
|
469
|
+
equals(otherCalendarDate) {
|
|
470
|
+
return this.dateTime.equals(otherCalendarDate.toDateTime());
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Thrown when a CalendarDate is invalid (e.g., invalid input or date values).
|
|
475
|
+
* @param error - The original error (available as cause)
|
|
476
|
+
* @example
|
|
477
|
+
* ```ts
|
|
478
|
+
* try {
|
|
479
|
+
* CalendarDate.fromISO('invalid')
|
|
480
|
+
* } catch (e) {
|
|
481
|
+
* if (e instanceof InvalidCalendarDate) console.error(e.message)
|
|
482
|
+
* }
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
export class InvalidCalendarDate extends Error {
|
|
486
|
+
constructor(error) {
|
|
487
|
+
super((error.message ?? '').replace('DateTime', 'CalendarDate'));
|
|
488
|
+
}
|
|
489
|
+
}
|