@samline/date 2.1.1 → 2.2.0

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/vue/index.js CHANGED
@@ -17,6 +17,7 @@ var SUPPORTED_LOCALES = [
17
17
  "it",
18
18
  "ja"
19
19
  ];
20
+ var loadedLocales = /* @__PURE__ */ new Set(["en"]);
20
21
  var localeLoaders = {
21
22
  en: null,
22
23
  es: () => import("dayjs/locale/es.js"),
@@ -49,12 +50,20 @@ var resolveLocale = (locale) => {
49
50
  }
50
51
  return asSupportedLocale(baseLocale);
51
52
  };
53
+ var isLocaleLoaded = (locale) => {
54
+ return loadedLocales.has(locale);
55
+ };
52
56
  var ensureLocaleLoaded = async (locale) => {
57
+ if (loadedLocales.has(locale)) {
58
+ return;
59
+ }
53
60
  const load = localeLoaders[locale];
54
61
  if (!load) {
62
+ loadedLocales.add(locale);
55
63
  return;
56
64
  }
57
65
  await load();
66
+ loadedLocales.add(locale);
58
67
  };
59
68
 
60
69
  // src/core/date.ts
@@ -70,7 +79,13 @@ var createResolvedConfig = (locale, config) => ({
70
79
  invalid: config?.invalid ?? DEFAULT_INVALID_DATE
71
80
  });
72
81
  var getInvalidDateText = (config, props) => {
73
- return props?.invalid ?? config?.invalid ?? DEFAULT_INVALID_DATE;
82
+ if (typeof props === "object" && props !== null && "invalid" in props) {
83
+ const invalid = props.invalid;
84
+ if (invalid !== void 0) {
85
+ return invalid;
86
+ }
87
+ }
88
+ return config?.invalid ?? DEFAULT_INVALID_DATE;
74
89
  };
75
90
  var getTargetLocale = (currentLocale, props) => {
76
91
  if (!props?.locale) {
@@ -87,29 +102,193 @@ var parseDateValue = (value, input, locale, strict) => {
87
102
  }
88
103
  return dayjs(value, [...input], locale, strict).locale(locale);
89
104
  };
105
+ var getCurrentDayjs = (locale) => {
106
+ return dayjs().locale(locale);
107
+ };
108
+ var createParseSuccess = (parsed, locale) => {
109
+ return {
110
+ isValid: true,
111
+ locale,
112
+ date: parsed.toDate(),
113
+ iso: parsed.toISOString(),
114
+ timestamp: parsed.valueOf(),
115
+ format: (output = DEFAULT_FORMAT) => parsed.format(output)
116
+ };
117
+ };
118
+ var createParseFailure = (locale, error) => {
119
+ return {
120
+ isValid: false,
121
+ locale,
122
+ date: null,
123
+ iso: null,
124
+ timestamp: null,
125
+ error
126
+ };
127
+ };
128
+ var createDateChainSuccessState = (parsed, locale) => {
129
+ return {
130
+ isValid: true,
131
+ locale,
132
+ date: parsed.toDate(),
133
+ iso: parsed.toISOString(),
134
+ timestamp: parsed.valueOf()
135
+ };
136
+ };
137
+ var createDateChainFailureState = (locale, error) => {
138
+ return {
139
+ isValid: false,
140
+ locale,
141
+ date: null,
142
+ iso: null,
143
+ timestamp: null,
144
+ error
145
+ };
146
+ };
147
+ var mapChainSetUnit = (unit) => {
148
+ if (unit === "day") {
149
+ return "date";
150
+ }
151
+ return unit;
152
+ };
153
+ var createReadyError = () => {
154
+ return new Error("Date chain is not ready. Await chain.ready before using it with locales that may need to load.");
155
+ };
156
+ var assertDateChainReady = (state) => {
157
+ if (!state.ready) {
158
+ throw createReadyError();
159
+ }
160
+ };
161
+ var getComparableDayjs = (value) => {
162
+ if (typeof value === "object" && value !== null && "toState" in value) {
163
+ const state = value.toState();
164
+ if (!state.isValid) {
165
+ return null;
166
+ }
167
+ return dayjs(state.date);
168
+ }
169
+ const comparable = dayjs(value);
170
+ if (!comparable.isValid()) {
171
+ return null;
172
+ }
173
+ return comparable;
174
+ };
175
+ var createDateChainApi = (state, ready) => {
176
+ const applyMutation = (transform) => {
177
+ assertDateChainReady(state);
178
+ if (state.current) {
179
+ state.current = transform(state.current);
180
+ }
181
+ return chain;
182
+ };
183
+ const compare = (other, comparator) => {
184
+ assertDateChainReady(state);
185
+ if (!state.current) {
186
+ return false;
187
+ }
188
+ const comparable = getComparableDayjs(other);
189
+ if (!comparable) {
190
+ return false;
191
+ }
192
+ return comparator(state.current, comparable);
193
+ };
194
+ const chain = {
195
+ ready,
196
+ add: (value, unit) => applyMutation((current) => current.add(value, unit)),
197
+ subtract: (value, unit) => applyMutation((current) => current.subtract(value, unit)),
198
+ set: (unit, value) => applyMutation((current) => current.set(mapChainSetUnit(unit), value)),
199
+ startOf: (unit) => applyMutation((current) => current.startOf(unit)),
200
+ endOf: (unit) => applyMutation((current) => current.endOf(unit)),
201
+ format: (output = DEFAULT_FORMAT) => {
202
+ assertDateChainReady(state);
203
+ if (!state.current) {
204
+ return state.invalid;
205
+ }
206
+ return state.current.format(output);
207
+ },
208
+ toDate: () => {
209
+ assertDateChainReady(state);
210
+ return state.current ? state.current.toDate() : null;
211
+ },
212
+ toISOString: () => {
213
+ assertDateChainReady(state);
214
+ return state.current ? state.current.toISOString() : null;
215
+ },
216
+ toTimestamp: () => {
217
+ assertDateChainReady(state);
218
+ return state.current ? state.current.valueOf() : null;
219
+ },
220
+ isValid: () => {
221
+ assertDateChainReady(state);
222
+ return state.current !== null;
223
+ },
224
+ isBefore: (other, unit) => compare(other, (current, comparable) => current.isBefore(comparable, unit)),
225
+ isAfter: (other, unit) => compare(other, (current, comparable) => current.isAfter(comparable, unit)),
226
+ isSame: (other, unit) => compare(other, (current, comparable) => current.isSame(comparable, unit)),
227
+ toState: () => {
228
+ assertDateChainReady(state);
229
+ if (!state.current) {
230
+ return createDateChainFailureState(state.locale, state.error ?? state.invalid);
231
+ }
232
+ return createDateChainSuccessState(state.current, state.locale);
233
+ }
234
+ };
235
+ return chain;
236
+ };
237
+ var createDateChainState = (locale, invalid, getInitialDate) => {
238
+ const state = {
239
+ locale,
240
+ invalid,
241
+ current: null,
242
+ ready: false,
243
+ error: invalid
244
+ };
245
+ const initialize = () => {
246
+ const parsed = getInitialDate();
247
+ state.ready = true;
248
+ if (!parsed.isValid()) {
249
+ state.current = null;
250
+ state.error = invalid;
251
+ return;
252
+ }
253
+ state.current = parsed;
254
+ state.error = null;
255
+ };
256
+ if (isLocaleLoaded(locale)) {
257
+ initialize();
258
+ return {
259
+ state,
260
+ ready: Promise.resolve()
261
+ };
262
+ }
263
+ return {
264
+ state,
265
+ ready: ensureLocaleLoaded(locale).then(() => {
266
+ initialize();
267
+ })
268
+ };
269
+ };
270
+ var createDateChainFromResolvedConfig = (props, config) => {
271
+ const locale = getTargetLocale(config.locale, props);
272
+ const invalid = getInvalidDateText(config, props);
273
+ const strict = props?.strict ?? config.strict;
274
+ const getInitialDate = () => {
275
+ if (props?.date === void 0) {
276
+ return getCurrentDayjs(locale);
277
+ }
278
+ return parseDateValue(props.date, props.input, locale, strict);
279
+ };
280
+ const { state, ready } = createDateChainState(locale, invalid, getInitialDate);
281
+ return createDateChainApi(state, ready);
282
+ };
90
283
  var createFormatterParseDate = (getConfig) => {
91
284
  return (props) => {
92
285
  const config = getConfig();
93
286
  const locale = getTargetLocale(config.locale, props);
94
287
  const parsed = parseDateValue(props.date, props.input, locale, props.strict ?? config.strict);
95
288
  if (!parsed.isValid()) {
96
- return {
97
- isValid: false,
98
- locale,
99
- date: null,
100
- iso: null,
101
- timestamp: null,
102
- error: getInvalidDateText(config, props)
103
- };
289
+ return createParseFailure(locale, getInvalidDateText(config, props));
104
290
  }
105
- return {
106
- isValid: true,
107
- locale,
108
- date: parsed.toDate(),
109
- iso: parsed.toISOString(),
110
- timestamp: parsed.valueOf(),
111
- format: (output = DEFAULT_FORMAT) => parsed.format(output)
112
- };
291
+ return createParseSuccess(parsed, locale);
113
292
  };
114
293
  };
115
294
  var createFormatterIsValidDate = (parseDate2) => {
@@ -122,10 +301,10 @@ var createFormatterGetDate = (getConfig) => {
122
301
  const locale = getTargetLocale(config.locale, props);
123
302
  const output = props?.output ?? DEFAULT_FORMAT;
124
303
  if (!props) {
125
- return dayjs().locale(locale).format(DEFAULT_FORMAT);
304
+ return getCurrentDayjs(locale).format(DEFAULT_FORMAT);
126
305
  }
127
306
  if (props.date === void 0) {
128
- return dayjs().locale(locale).format(output);
307
+ return getCurrentDayjs(locale).format(output);
129
308
  }
130
309
  const parsed = parseDate2({
131
310
  date: props.date,
@@ -139,6 +318,11 @@ var createFormatterGetDate = (getConfig) => {
139
318
  return parsed.format(output);
140
319
  };
141
320
  };
321
+ var createFormatterCreateDateChain = (getConfig) => {
322
+ return (props) => {
323
+ return createDateChainFromResolvedConfig(props, getConfig());
324
+ };
325
+ };
142
326
  function resolveLocaleOrThrow(locale) {
143
327
  const resolvedLocale = resolveLocale(locale);
144
328
  if (!resolvedLocale) {
@@ -158,6 +342,7 @@ var createDateFormatter = (config) => {
158
342
  getDate: createFormatterGetDate(getConfig),
159
343
  parseDate: parseDate2,
160
344
  isValidDate: createFormatterIsValidDate(parseDate2),
345
+ createDateChain: createFormatterCreateDateChain(getConfig),
161
346
  getSupportedLocales,
162
347
  getCurrentLocale: () => currentLocale,
163
348
  setLocale: async (locale) => {
@@ -180,6 +365,7 @@ var useDateFormatter = (options) => {
180
365
  return {
181
366
  locale,
182
367
  currentLocale: computed(() => locale.value),
368
+ createDateChain: (props) => formatter.createDateChain(props),
183
369
  getDate: (props) => formatter.getDate(props),
184
370
  parseDate: (props) => formatter.parseDate(props),
185
371
  isValidDate: (props) => formatter.isValidDate(props),
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/vue/index.ts","../../src/core/date.ts","../../src/core/locales.ts"],"sourcesContent":["import { computed, ref } from 'vue'\n\nimport {\n createDateFormatter,\n getSupportedLocales,\n type DateFormatterConfig,\n type DateParsingOptions,\n type GetDateOptions,\n type LocaleInput,\n type SupportedLocale\n} from '../index.js'\n\nexport type UseDateFormatterOptions = DateFormatterConfig\n\nexport const useDateFormatter = (options?: UseDateFormatterOptions) => {\n const formatter = createDateFormatter(options)\n const locale = ref<SupportedLocale>(formatter.getCurrentLocale())\n\n const setLocale = async (nextLocale: LocaleInput) => {\n await formatter.setLocale(nextLocale)\n locale.value = formatter.getCurrentLocale()\n }\n\n return {\n locale,\n currentLocale: computed(() => locale.value),\n getDate: (props?: GetDateOptions) => formatter.getDate(props),\n parseDate: (props: DateParsingOptions) => formatter.parseDate(props),\n isValidDate: (props: DateParsingOptions) => formatter.isValidDate(props),\n getSupportedLocales,\n ready: formatter.ready,\n setLocale\n }\n}\n","import customParseFormat from 'dayjs/plugin/customParseFormat.js'\nimport dayjs from 'dayjs'\nimport type { Dayjs } from 'dayjs'\n\nimport { ensureLocaleLoaded, resolveLocale, SUPPORTED_LOCALES, type LocaleInput, type SupportedLocale } from './locales.js'\n\ndayjs.extend(customParseFormat)\ndayjs.locale('en')\n\nexport type DateValue = string | number | Date\n\ntype DateInputOptions = {\n input?: string | readonly string[]\n locale?: LocaleInput\n strict?: boolean\n}\n\nexport type DateParsingOptions = DateInputOptions & {\n date: DateValue\n}\n\nexport type GetDateOptions = DateInputOptions & {\n date?: DateValue\n output?: string\n invalid?: string\n}\n\nexport type DateFormatterConfig = {\n locale?: LocaleInput\n strict?: boolean\n invalid?: string\n}\n\ntype ResolvedDateFormatterConfig = {\n locale: SupportedLocale\n strict: boolean\n invalid: string\n}\n\nexport type DateFormatter = {\n getDate: (props?: GetDateOptions) => string\n parseDate: (props: DateParsingOptions) => ParseDateResult\n isValidDate: (props: DateParsingOptions) => boolean\n getSupportedLocales: () => readonly SupportedLocale[]\n getCurrentLocale: () => SupportedLocale\n setLocale: (locale: LocaleInput) => Promise<void>\n ready: Promise<void>\n}\n\nexport type ParseDateSuccess = {\n isValid: true\n locale: SupportedLocale\n date: Date\n iso: string\n timestamp: number\n format: (output?: string) => string\n}\n\nexport type ParseDateFailure = {\n isValid: false\n locale: SupportedLocale\n date: null\n iso: null\n timestamp: null\n error: string\n}\n\nexport type ParseDateResult = ParseDateSuccess | ParseDateFailure\n\nconst DEFAULT_FORMAT = 'YYYY-MM-DD'\nconst DEFAULT_LOCALE: SupportedLocale = 'en'\nconst DEFAULT_INVALID_DATE = 'Invalid Date'\nconst DEFAULT_STRICT = true\n\nconst createResolvedConfig = (\n locale: SupportedLocale,\n config?: DateFormatterConfig\n): ResolvedDateFormatterConfig => ({\n locale,\n strict: config?.strict ?? DEFAULT_STRICT,\n invalid: config?.invalid ?? DEFAULT_INVALID_DATE\n})\n\nconst getInvalidDateText = (config?: DateFormatterConfig, props?: GetDateOptions): string => {\n return props?.invalid ?? config?.invalid ?? DEFAULT_INVALID_DATE\n}\n\nconst getTargetLocale = (currentLocale: SupportedLocale, props?: GetDateOptions): SupportedLocale => {\n if (!props?.locale) {\n return currentLocale\n }\n\n return resolveLocaleOrThrow(props.locale)\n}\n\nconst getHelperLocale = <T extends { locale?: LocaleInput }>(\n config?: DateFormatterConfig,\n props?: T\n): SupportedLocale => {\n if (props?.locale) {\n return resolveLocaleOrThrow(props.locale)\n }\n\n if (config?.locale) {\n return resolveLocaleOrThrow(config.locale)\n }\n\n return DEFAULT_LOCALE\n}\n\nconst parseDateValue = (\n value: DateValue,\n input: DateParsingOptions['input'],\n locale: SupportedLocale,\n strict: boolean\n): Dayjs => {\n if (!input) {\n return dayjs(value).locale(locale)\n }\n\n if (typeof input === 'string') {\n return dayjs(value, input, locale, strict).locale(locale)\n }\n\n return dayjs(value, [...input], locale, strict).locale(locale)\n}\n\nconst createFormatterParseDate = (getConfig: () => ResolvedDateFormatterConfig) => {\n return (props: DateParsingOptions): ParseDateResult => {\n const config = getConfig()\n const locale = getTargetLocale(config.locale, props)\n const parsed = parseDateValue(props.date, props.input, locale, props.strict ?? config.strict)\n\n if (!parsed.isValid()) {\n return {\n isValid: false,\n locale,\n date: null,\n iso: null,\n timestamp: null,\n error: getInvalidDateText(config, props)\n }\n }\n\n return {\n isValid: true,\n locale,\n date: parsed.toDate(),\n iso: parsed.toISOString(),\n timestamp: parsed.valueOf(),\n format: (output = DEFAULT_FORMAT) => parsed.format(output)\n }\n }\n}\n\nconst createFormatterIsValidDate = (parseDate: (props: DateParsingOptions) => ParseDateResult) => {\n return (props: DateParsingOptions): boolean => parseDate(props).isValid\n}\n\nconst createFormatterGetDate = (getConfig: () => ResolvedDateFormatterConfig) => {\n const parseDate = createFormatterParseDate(getConfig)\n\n return (props?: GetDateOptions): string => {\n const config = getConfig()\n const locale = getTargetLocale(config.locale, props)\n const output = props?.output ?? DEFAULT_FORMAT\n\n if (!props) {\n return dayjs().locale(locale).format(DEFAULT_FORMAT)\n }\n\n if (props.date === undefined) {\n return dayjs().locale(locale).format(output)\n }\n\n const parsed = parseDate({\n date: props.date,\n input: props.input,\n locale: props.locale,\n strict: props.strict\n })\n\n if (!parsed.isValid) {\n return props.invalid ?? parsed.error\n }\n\n return parsed.format(output)\n }\n}\n\nfunction resolveLocaleOrThrow(locale: LocaleInput): SupportedLocale {\n const resolvedLocale = resolveLocale(locale)\n\n if (!resolvedLocale) {\n throw new Error(\n `Unsupported locale: ${locale}. The package tries an exact locale match first and then falls back to the base locale.`\n )\n }\n\n return resolvedLocale\n}\n\nexport const getSupportedLocales = (): readonly SupportedLocale[] => SUPPORTED_LOCALES\n\nexport const getDate = async (props?: GetDateOptions, config?: DateFormatterConfig): Promise<string> => {\n const locale = getHelperLocale(config, props)\n const formatter = createDateFormatter({ ...config, locale })\n\n await formatter.ready\n\n return formatter.getDate(props)\n}\n\nexport const parseDate = async (\n props: DateParsingOptions,\n config?: DateFormatterConfig\n): Promise<ParseDateResult> => {\n const locale = getHelperLocale(config, props)\n const formatter = createDateFormatter({ ...config, locale })\n\n await formatter.ready\n\n return formatter.parseDate(props)\n}\n\nexport const isValidDate = async (\n props: DateParsingOptions,\n config?: DateFormatterConfig\n): Promise<boolean> => {\n const locale = getHelperLocale(config, props)\n const formatter = createDateFormatter({ ...config, locale })\n\n await formatter.ready\n\n return formatter.isValidDate(props)\n}\n\nexport const createDateFormatter = (config?: DateFormatterConfig): DateFormatter => {\n let currentLocale = config?.locale ? resolveLocaleOrThrow(config.locale) : DEFAULT_LOCALE\n\n const getConfig = (): ResolvedDateFormatterConfig => createResolvedConfig(currentLocale, config)\n\n const ready = ensureLocaleLoaded(currentLocale)\n const parseDate = createFormatterParseDate(getConfig)\n\n return {\n getDate: createFormatterGetDate(getConfig),\n parseDate,\n isValidDate: createFormatterIsValidDate(parseDate),\n getSupportedLocales,\n getCurrentLocale: () => currentLocale,\n setLocale: async (locale: LocaleInput) => {\n const resolvedLocale = resolveLocaleOrThrow(locale)\n\n await ensureLocaleLoaded(resolvedLocale)\n currentLocale = resolvedLocale\n },\n ready\n }\n}\n","export const SUPPORTED_LOCALES = [\n 'en',\n 'es',\n 'es-mx',\n 'fr',\n 'pt',\n 'pt-br',\n 'de',\n 'it',\n 'ja'\n] as const\n\nexport type SupportedLocale = (typeof SUPPORTED_LOCALES)[number]\nexport type LocaleInput = string\n\nconst localeLoaders: Record<SupportedLocale, (() => Promise<unknown>) | null> = {\n en: null,\n es: () => import('dayjs/locale/es.js'),\n 'es-mx': () => import('dayjs/locale/es-mx.js'),\n fr: () => import('dayjs/locale/fr.js'),\n pt: () => import('dayjs/locale/pt.js'),\n 'pt-br': () => import('dayjs/locale/pt-br.js'),\n de: () => import('dayjs/locale/de.js'),\n it: () => import('dayjs/locale/it.js'),\n ja: () => import('dayjs/locale/ja.js')\n}\n\nconst normalizeLocale = (locale: string): string => {\n return locale.trim().toLowerCase().replace(/_/g, '-')\n}\n\nconst asSupportedLocale = (locale: string): SupportedLocale | null => {\n if (!SUPPORTED_LOCALES.includes(locale as SupportedLocale)) {\n return null\n }\n\n return locale as SupportedLocale\n}\n\nexport const isSupportedLocale = (locale: string): boolean => {\n return resolveLocale(locale) !== null\n}\n\nexport const resolveLocale = (locale: LocaleInput): SupportedLocale | null => {\n const normalizedLocale = normalizeLocale(locale)\n const exactLocale = asSupportedLocale(normalizedLocale)\n\n if (exactLocale) {\n return exactLocale\n }\n\n const [baseLocale] = normalizedLocale.split('-')\n\n if (!baseLocale) {\n return null\n }\n\n return asSupportedLocale(baseLocale)\n}\n\nexport const ensureLocaleLoaded = async (locale: SupportedLocale): Promise<void> => {\n const load = localeLoaders[locale]\n\n if (!load) {\n return\n }\n\n await load()\n}\n"],"mappings":";AAAA,SAAS,UAAU,WAAW;;;ACA9B,OAAO,uBAAuB;AAC9B,OAAO,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AS,uBAAuB,SAAS;AAAA,IACzC,WAAAA;AAAA,IACA,aAAa,2BAA2BA,UAAS;AAAA,IACjD;AAAA,IACA,kBAAkB,MAAM;AAAA,IACxB,WAAW,OAAO,WAAwB;AACxC,YAAM,iBAAiB,qBAAqB,MAAM;AAElD,YAAM,mBAAmB,cAAc;AACvC,sBAAgB;AAAA,IAClB;AAAA,IACA;AAAA,EACF;AACF;;;ADrPO,IAAM,mBAAmB,CAAC,YAAsC;AACrE,QAAM,YAAY,oBAAoB,OAAO;AAC7C,QAAM,SAAS,IAAqB,UAAU,iBAAiB,CAAC;AAEhE,QAAM,YAAY,OAAO,eAA4B;AACnD,UAAM,UAAU,UAAU,UAAU;AACpC,WAAO,QAAQ,UAAU,iBAAiB;AAAA,EAC5C;AAEA,SAAO;AAAA,IACL;AAAA,IACA,eAAe,SAAS,MAAM,OAAO,KAAK;AAAA,IAC1C,SAAS,CAAC,UAA2B,UAAU,QAAQ,KAAK;AAAA,IAC5D,WAAW,CAAC,UAA8B,UAAU,UAAU,KAAK;AAAA,IACnE,aAAa,CAAC,UAA8B,UAAU,YAAY,KAAK;AAAA,IACvE;AAAA,IACA,OAAO,UAAU;AAAA,IACjB;AAAA,EACF;AACF;","names":["parseDate","parseDate"]}
1
+ {"version":3,"sources":["../../src/vue/index.ts","../../src/core/date.ts","../../src/core/locales.ts"],"sourcesContent":["import { computed, ref } from 'vue'\n\nimport {\n createDateFormatter,\n getSupportedLocales,\n type CreateDateChainOptions,\n type DateChain,\n type DateFormatterConfig,\n type DateParsingOptions,\n type GetDateOptions,\n type LocaleInput,\n type SupportedLocale\n} from '../index.js'\n\nexport type UseDateFormatterOptions = DateFormatterConfig\n\nexport const useDateFormatter = (options?: UseDateFormatterOptions) => {\n const formatter = createDateFormatter(options)\n const locale = ref<SupportedLocale>(formatter.getCurrentLocale())\n\n const setLocale = async (nextLocale: LocaleInput) => {\n await formatter.setLocale(nextLocale)\n locale.value = formatter.getCurrentLocale()\n }\n\n return {\n locale,\n currentLocale: computed(() => locale.value),\n createDateChain: (props?: CreateDateChainOptions): DateChain => formatter.createDateChain(props),\n getDate: (props?: GetDateOptions) => formatter.getDate(props),\n parseDate: (props: DateParsingOptions) => formatter.parseDate(props),\n isValidDate: (props: DateParsingOptions) => formatter.isValidDate(props),\n getSupportedLocales,\n ready: formatter.ready,\n setLocale\n }\n}\n","import customParseFormat from 'dayjs/plugin/customParseFormat.js'\nimport dayjs from 'dayjs'\nimport type { Dayjs } from 'dayjs'\n\nimport {\n ensureLocaleLoaded,\n isLocaleLoaded,\n resolveLocale,\n SUPPORTED_LOCALES,\n type LocaleInput,\n type SupportedLocale\n} from './locales.js'\n\ndayjs.extend(customParseFormat)\ndayjs.locale('en')\n\nexport type DateValue = string | number | Date\n\ntype DateInputOptions = {\n input?: string | readonly string[]\n locale?: LocaleInput\n strict?: boolean\n}\n\nexport type DateParsingOptions = DateInputOptions & {\n date: DateValue\n}\n\nexport type GetDateOptions = DateInputOptions & {\n date?: DateValue\n output?: string\n invalid?: string\n}\n\nexport type CreateDateChainOptions = DateInputOptions & {\n date?: DateValue\n invalid?: string\n}\n\nexport type DateFormatterConfig = {\n locale?: LocaleInput\n strict?: boolean\n invalid?: string\n}\n\nexport type DateChainManipulateUnit =\n | 'millisecond'\n | 'second'\n | 'minute'\n | 'hour'\n | 'day'\n | 'week'\n | 'month'\n | 'year'\n\nexport type DateChainBoundaryUnit =\n | 'year'\n | 'month'\n | 'week'\n | 'day'\n | 'hour'\n | 'minute'\n | 'second'\n\nexport type DateChainCompareUnit = DateChainBoundaryUnit | 'millisecond'\n\nexport type DateChainSetUnit =\n | 'year'\n | 'month'\n | 'day'\n | 'hour'\n | 'minute'\n | 'second'\n | 'millisecond'\n\ntype ResolvedDateFormatterConfig = {\n locale: SupportedLocale\n strict: boolean\n invalid: string\n}\n\nexport type DateChainSuccessState = {\n isValid: true\n locale: SupportedLocale\n date: Date\n iso: string\n timestamp: number\n}\n\nexport type DateChainFailureState = {\n isValid: false\n locale: SupportedLocale\n date: null\n iso: null\n timestamp: null\n error: string\n}\n\nexport type DateChainState = DateChainSuccessState | DateChainFailureState\n\nexport type DateChain = {\n ready: Promise<void>\n add: (value: number, unit: DateChainManipulateUnit) => DateChain\n subtract: (value: number, unit: DateChainManipulateUnit) => DateChain\n set: (unit: DateChainSetUnit, value: number) => DateChain\n startOf: (unit: DateChainBoundaryUnit) => DateChain\n endOf: (unit: DateChainBoundaryUnit) => DateChain\n format: (output?: string) => string\n toDate: () => Date | null\n toISOString: () => string | null\n toTimestamp: () => number | null\n isValid: () => boolean\n isBefore: (other: DateChainComparable, unit?: DateChainCompareUnit) => boolean\n isAfter: (other: DateChainComparable, unit?: DateChainCompareUnit) => boolean\n isSame: (other: DateChainComparable, unit?: DateChainCompareUnit) => boolean\n toState: () => DateChainState\n}\n\nexport type DateChainComparable = DateValue | DateChain\n\nexport type DateFormatter = {\n getDate: (props?: GetDateOptions) => string\n parseDate: (props: DateParsingOptions) => ParseDateResult\n isValidDate: (props: DateParsingOptions) => boolean\n createDateChain: (props?: CreateDateChainOptions) => DateChain\n getSupportedLocales: () => readonly SupportedLocale[]\n getCurrentLocale: () => SupportedLocale\n setLocale: (locale: LocaleInput) => Promise<void>\n ready: Promise<void>\n}\n\nexport type ParseDateSuccess = {\n isValid: true\n locale: SupportedLocale\n date: Date\n iso: string\n timestamp: number\n format: (output?: string) => string\n}\n\nexport type ParseDateFailure = {\n isValid: false\n locale: SupportedLocale\n date: null\n iso: null\n timestamp: null\n error: string\n}\n\nexport type ParseDateResult = ParseDateSuccess | ParseDateFailure\n\ntype InternalDateChainState = {\n locale: SupportedLocale\n invalid: string\n current: Dayjs | null\n ready: boolean\n error: string | null\n}\n\nconst DEFAULT_FORMAT = 'YYYY-MM-DD'\nconst DEFAULT_LOCALE: SupportedLocale = 'en'\nconst DEFAULT_INVALID_DATE = 'Invalid Date'\nconst DEFAULT_STRICT = true\n\nconst createResolvedConfig = (\n locale: SupportedLocale,\n config?: DateFormatterConfig\n): ResolvedDateFormatterConfig => ({\n locale,\n strict: config?.strict ?? DEFAULT_STRICT,\n invalid: config?.invalid ?? DEFAULT_INVALID_DATE\n})\n\nconst getInvalidDateText = <T>(config?: DateFormatterConfig, props?: T): string => {\n if (typeof props === 'object' && props !== null && 'invalid' in props) {\n const invalid = (props as { invalid?: string }).invalid\n\n if (invalid !== undefined) {\n return invalid\n }\n }\n\n return config?.invalid ?? DEFAULT_INVALID_DATE\n}\n\nconst getTargetLocale = <T extends { locale?: LocaleInput }>(\n currentLocale: SupportedLocale,\n props?: T\n): SupportedLocale => {\n if (!props?.locale) {\n return currentLocale\n }\n\n return resolveLocaleOrThrow(props.locale)\n}\n\nconst getHelperLocale = <T extends { locale?: LocaleInput }>(\n config?: DateFormatterConfig,\n props?: T\n): SupportedLocale => {\n if (props?.locale) {\n return resolveLocaleOrThrow(props.locale)\n }\n\n if (config?.locale) {\n return resolveLocaleOrThrow(config.locale)\n }\n\n return DEFAULT_LOCALE\n}\n\nconst parseDateValue = (\n value: DateValue,\n input: DateParsingOptions['input'],\n locale: SupportedLocale,\n strict: boolean\n): Dayjs => {\n if (!input) {\n return dayjs(value).locale(locale)\n }\n\n if (typeof input === 'string') {\n return dayjs(value, input, locale, strict).locale(locale)\n }\n\n return dayjs(value, [...input], locale, strict).locale(locale)\n}\n\nconst getCurrentDayjs = (locale: SupportedLocale): Dayjs => {\n return dayjs().locale(locale)\n}\n\nconst createParseSuccess = (parsed: Dayjs, locale: SupportedLocale): ParseDateSuccess => {\n return {\n isValid: true,\n locale,\n date: parsed.toDate(),\n iso: parsed.toISOString(),\n timestamp: parsed.valueOf(),\n format: (output = DEFAULT_FORMAT) => parsed.format(output)\n }\n}\n\nconst createParseFailure = (locale: SupportedLocale, error: string): ParseDateFailure => {\n return {\n isValid: false,\n locale,\n date: null,\n iso: null,\n timestamp: null,\n error\n }\n}\n\nconst createDateChainSuccessState = (parsed: Dayjs, locale: SupportedLocale): DateChainSuccessState => {\n return {\n isValid: true,\n locale,\n date: parsed.toDate(),\n iso: parsed.toISOString(),\n timestamp: parsed.valueOf()\n }\n}\n\nconst createDateChainFailureState = (locale: SupportedLocale, error: string): DateChainFailureState => {\n return {\n isValid: false,\n locale,\n date: null,\n iso: null,\n timestamp: null,\n error\n }\n}\n\nconst mapChainSetUnit = (unit: DateChainSetUnit): 'year' | 'month' | 'date' | 'hour' | 'minute' | 'second' | 'millisecond' => {\n if (unit === 'day') {\n return 'date'\n }\n\n return unit\n}\n\nconst createReadyError = (): Error => {\n return new Error('Date chain is not ready. Await chain.ready before using it with locales that may need to load.')\n}\n\nconst assertDateChainReady = (state: InternalDateChainState): void => {\n if (!state.ready) {\n throw createReadyError()\n }\n}\n\nconst getComparableDayjs = (value: DateChainComparable): Dayjs | null => {\n if (typeof value === 'object' && value !== null && 'toState' in value) {\n const state = value.toState()\n\n if (!state.isValid) {\n return null\n }\n\n return dayjs(state.date)\n }\n\n const comparable = dayjs(value)\n\n if (!comparable.isValid()) {\n return null\n }\n\n return comparable\n}\n\nconst createDateChainApi = (state: InternalDateChainState, ready: Promise<void>): DateChain => {\n const applyMutation = (transform: (current: Dayjs) => Dayjs): DateChain => {\n assertDateChainReady(state)\n\n if (state.current) {\n state.current = transform(state.current)\n }\n\n return chain\n }\n\n const compare = (\n other: DateChainComparable,\n comparator: (current: Dayjs, comparable: Dayjs) => boolean\n ): boolean => {\n assertDateChainReady(state)\n\n if (!state.current) {\n return false\n }\n\n const comparable = getComparableDayjs(other)\n\n if (!comparable) {\n return false\n }\n\n return comparator(state.current, comparable)\n }\n\n const chain: DateChain = {\n ready,\n add: (value, unit) => applyMutation((current) => current.add(value, unit)),\n subtract: (value, unit) => applyMutation((current) => current.subtract(value, unit)),\n set: (unit, value) => applyMutation((current) => current.set(mapChainSetUnit(unit), value)),\n startOf: (unit) => applyMutation((current) => current.startOf(unit)),\n endOf: (unit) => applyMutation((current) => current.endOf(unit)),\n format: (output = DEFAULT_FORMAT) => {\n assertDateChainReady(state)\n\n if (!state.current) {\n return state.invalid\n }\n\n return state.current.format(output)\n },\n toDate: () => {\n assertDateChainReady(state)\n\n return state.current ? state.current.toDate() : null\n },\n toISOString: () => {\n assertDateChainReady(state)\n\n return state.current ? state.current.toISOString() : null\n },\n toTimestamp: () => {\n assertDateChainReady(state)\n\n return state.current ? state.current.valueOf() : null\n },\n isValid: () => {\n assertDateChainReady(state)\n\n return state.current !== null\n },\n isBefore: (other, unit) => compare(other, (current, comparable) => current.isBefore(comparable, unit)),\n isAfter: (other, unit) => compare(other, (current, comparable) => current.isAfter(comparable, unit)),\n isSame: (other, unit) => compare(other, (current, comparable) => current.isSame(comparable, unit)),\n toState: () => {\n assertDateChainReady(state)\n\n if (!state.current) {\n return createDateChainFailureState(state.locale, state.error ?? state.invalid)\n }\n\n return createDateChainSuccessState(state.current, state.locale)\n }\n }\n\n return chain\n}\n\nconst createDateChainState = (\n locale: SupportedLocale,\n invalid: string,\n getInitialDate: () => Dayjs\n): { state: InternalDateChainState; ready: Promise<void> } => {\n const state: InternalDateChainState = {\n locale,\n invalid,\n current: null,\n ready: false,\n error: invalid\n }\n\n const initialize = () => {\n const parsed = getInitialDate()\n\n state.ready = true\n\n if (!parsed.isValid()) {\n state.current = null\n state.error = invalid\n return\n }\n\n state.current = parsed\n state.error = null\n }\n\n if (isLocaleLoaded(locale)) {\n initialize()\n\n return {\n state,\n ready: Promise.resolve()\n }\n }\n\n return {\n state,\n ready: ensureLocaleLoaded(locale).then(() => {\n initialize()\n })\n }\n}\n\nconst createDateChainFromResolvedConfig = (\n props: CreateDateChainOptions | undefined,\n config: ResolvedDateFormatterConfig\n): DateChain => {\n const locale = getTargetLocale(config.locale, props)\n const invalid = getInvalidDateText(config, props)\n const strict = props?.strict ?? config.strict\n const getInitialDate = () => {\n if (props?.date === undefined) {\n return getCurrentDayjs(locale)\n }\n\n return parseDateValue(props.date, props.input, locale, strict)\n }\n\n const { state, ready } = createDateChainState(locale, invalid, getInitialDate)\n\n return createDateChainApi(state, ready)\n}\n\nconst createFormatterParseDate = (getConfig: () => ResolvedDateFormatterConfig) => {\n return (props: DateParsingOptions): ParseDateResult => {\n const config = getConfig()\n const locale = getTargetLocale(config.locale, props)\n const parsed = parseDateValue(props.date, props.input, locale, props.strict ?? config.strict)\n\n if (!parsed.isValid()) {\n return createParseFailure(locale, getInvalidDateText(config, props))\n }\n\n return createParseSuccess(parsed, locale)\n }\n}\n\nconst createFormatterIsValidDate = (parseDate: (props: DateParsingOptions) => ParseDateResult) => {\n return (props: DateParsingOptions): boolean => parseDate(props).isValid\n}\n\nconst createFormatterGetDate = (getConfig: () => ResolvedDateFormatterConfig) => {\n const parseDate = createFormatterParseDate(getConfig)\n\n return (props?: GetDateOptions): string => {\n const config = getConfig()\n const locale = getTargetLocale(config.locale, props)\n const output = props?.output ?? DEFAULT_FORMAT\n\n if (!props) {\n return getCurrentDayjs(locale).format(DEFAULT_FORMAT)\n }\n\n if (props.date === undefined) {\n return getCurrentDayjs(locale).format(output)\n }\n\n const parsed = parseDate({\n date: props.date,\n input: props.input,\n locale: props.locale,\n strict: props.strict\n })\n\n if (!parsed.isValid) {\n return props.invalid ?? parsed.error\n }\n\n return parsed.format(output)\n }\n}\n\nconst createFormatterCreateDateChain = (getConfig: () => ResolvedDateFormatterConfig) => {\n return (props?: CreateDateChainOptions): DateChain => {\n return createDateChainFromResolvedConfig(props, getConfig())\n }\n}\n\nfunction resolveLocaleOrThrow(locale: LocaleInput): SupportedLocale {\n const resolvedLocale = resolveLocale(locale)\n\n if (!resolvedLocale) {\n throw new Error(\n `Unsupported locale: ${locale}. The package tries an exact locale match first and then falls back to the base locale.`\n )\n }\n\n return resolvedLocale\n}\n\nexport const getSupportedLocales = (): readonly SupportedLocale[] => SUPPORTED_LOCALES\n\nexport const createDateChain = (props?: CreateDateChainOptions, config?: DateFormatterConfig): DateChain => {\n const locale = getHelperLocale(config, props)\n\n return createDateChainFromResolvedConfig(props, createResolvedConfig(locale, config))\n}\n\nexport const getDate = async (props?: GetDateOptions, config?: DateFormatterConfig): Promise<string> => {\n const locale = getHelperLocale(config, props)\n const formatter = createDateFormatter({ ...config, locale })\n\n await formatter.ready\n\n return formatter.getDate(props)\n}\n\nexport const parseDate = async (\n props: DateParsingOptions,\n config?: DateFormatterConfig\n): Promise<ParseDateResult> => {\n const locale = getHelperLocale(config, props)\n const formatter = createDateFormatter({ ...config, locale })\n\n await formatter.ready\n\n return formatter.parseDate(props)\n}\n\nexport const isValidDate = async (\n props: DateParsingOptions,\n config?: DateFormatterConfig\n): Promise<boolean> => {\n const locale = getHelperLocale(config, props)\n const formatter = createDateFormatter({ ...config, locale })\n\n await formatter.ready\n\n return formatter.isValidDate(props)\n}\n\nexport const createDateFormatter = (config?: DateFormatterConfig): DateFormatter => {\n let currentLocale = config?.locale ? resolveLocaleOrThrow(config.locale) : DEFAULT_LOCALE\n\n const getConfig = (): ResolvedDateFormatterConfig => createResolvedConfig(currentLocale, config)\n\n const ready = ensureLocaleLoaded(currentLocale)\n const parseDate = createFormatterParseDate(getConfig)\n\n return {\n getDate: createFormatterGetDate(getConfig),\n parseDate,\n isValidDate: createFormatterIsValidDate(parseDate),\n createDateChain: createFormatterCreateDateChain(getConfig),\n getSupportedLocales,\n getCurrentLocale: () => currentLocale,\n setLocale: async (locale: LocaleInput) => {\n const resolvedLocale = resolveLocaleOrThrow(locale)\n\n await ensureLocaleLoaded(resolvedLocale)\n currentLocale = resolvedLocale\n },\n ready\n }\n}\n","export const SUPPORTED_LOCALES = [\n 'en',\n 'es',\n 'es-mx',\n 'fr',\n 'pt',\n 'pt-br',\n 'de',\n 'it',\n 'ja'\n] as const\n\nexport type SupportedLocale = (typeof SUPPORTED_LOCALES)[number]\nexport type LocaleInput = string\n\nconst loadedLocales = new Set<SupportedLocale>(['en'])\n\nconst localeLoaders: Record<SupportedLocale, (() => Promise<unknown>) | null> = {\n en: null,\n es: () => import('dayjs/locale/es.js'),\n 'es-mx': () => import('dayjs/locale/es-mx.js'),\n fr: () => import('dayjs/locale/fr.js'),\n pt: () => import('dayjs/locale/pt.js'),\n 'pt-br': () => import('dayjs/locale/pt-br.js'),\n de: () => import('dayjs/locale/de.js'),\n it: () => import('dayjs/locale/it.js'),\n ja: () => import('dayjs/locale/ja.js')\n}\n\nconst normalizeLocale = (locale: string): string => {\n return locale.trim().toLowerCase().replace(/_/g, '-')\n}\n\nconst asSupportedLocale = (locale: string): SupportedLocale | null => {\n if (!SUPPORTED_LOCALES.includes(locale as SupportedLocale)) {\n return null\n }\n\n return locale as SupportedLocale\n}\n\nexport const isSupportedLocale = (locale: string): boolean => {\n return resolveLocale(locale) !== null\n}\n\nexport const resolveLocale = (locale: LocaleInput): SupportedLocale | null => {\n const normalizedLocale = normalizeLocale(locale)\n const exactLocale = asSupportedLocale(normalizedLocale)\n\n if (exactLocale) {\n return exactLocale\n }\n\n const [baseLocale] = normalizedLocale.split('-')\n\n if (!baseLocale) {\n return null\n }\n\n return asSupportedLocale(baseLocale)\n}\n\nexport const markLocaleAsLoaded = (locale: SupportedLocale): void => {\n loadedLocales.add(locale)\n}\n\nexport const isLocaleLoaded = (locale: SupportedLocale): boolean => {\n return loadedLocales.has(locale)\n}\n\nexport const ensureLocaleLoaded = async (locale: SupportedLocale): Promise<void> => {\n if (loadedLocales.has(locale)) {\n return\n }\n\n const load = localeLoaders[locale]\n\n if (!load) {\n loadedLocales.add(locale)\n return\n }\n\n await load()\n loadedLocales.add(locale)\n}\n"],"mappings":";AAAA,SAAS,UAAU,WAAW;;;ACA9B,OAAO,uBAAuB;AAC9B,OAAO,WAAW;;;ACDX,IAAM,oBAAoB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAKA,IAAM,gBAAgB,oBAAI,IAAqB,CAAC,IAAI,CAAC;AAErD,IAAM,gBAA0E;AAAA,EAC9E,IAAI;AAAA,EACJ,IAAI,MAAM,OAAO,oBAAoB;AAAA,EACrC,SA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C,iBAAiB,CAAC,UAA8C,UAAU,gBAAgB,KAAK;AAAA,IAC/F,SAAS,CAAC,UAA2B,UAAU,QAAQ,KAAK;AAAA,IAC5D,WAAW,CAAC,UAA8B,UAAU,UAAU,KAAK;AAAA,IACnE,aAAa,CAAC,UAA8B,UAAU,YAAY,KAAK;AAAA,IACvE;AAAA,IACA,OAAO,UAAU;AAAA,IACjB;AAAA,EACF;AACF;","names":["parseDate","parseDate"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samline/date",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Format and localize dates with Day.js through a small multi-entrypoint API.",
5
5
  "type": "module",
6
6
  "license": "MIT",