@samline/date 1.0.0 → 2.1.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/README.md +102 -6
- package/dist/browser/global.d.ts +6 -1
- package/dist/browser/global.js +84 -22
- package/dist/browser/global.js.map +1 -1
- package/dist/index.d.ts +10 -4
- package/dist/index.js +84 -22
- package/dist/index.js.map +1 -1
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +50 -23
- package/dist/react/index.js.map +1 -1
- package/dist/svelte/index.d.ts +2 -2
- package/dist/svelte/index.js +50 -23
- package/dist/svelte/index.js.map +1 -1
- package/dist/vanilla/index.d.ts +1 -1
- package/dist/vanilla/index.js +84 -22
- package/dist/vanilla/index.js.map +1 -1
- package/dist/vue/index.d.ts +2 -2
- package/dist/vue/index.js +50 -23
- package/dist/vue/index.js.map +1 -1
- package/package.json +1 -1
package/dist/vue/index.js
CHANGED
|
@@ -28,8 +28,26 @@ var localeLoaders = {
|
|
|
28
28
|
it: () => import("dayjs/locale/it.js"),
|
|
29
29
|
ja: () => import("dayjs/locale/ja.js")
|
|
30
30
|
};
|
|
31
|
-
var
|
|
32
|
-
return
|
|
31
|
+
var normalizeLocale = (locale) => {
|
|
32
|
+
return locale.trim().toLowerCase().replace(/_/g, "-");
|
|
33
|
+
};
|
|
34
|
+
var asSupportedLocale = (locale) => {
|
|
35
|
+
if (!SUPPORTED_LOCALES.includes(locale)) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return locale;
|
|
39
|
+
};
|
|
40
|
+
var resolveLocale = (locale) => {
|
|
41
|
+
const normalizedLocale = normalizeLocale(locale);
|
|
42
|
+
const exactLocale = asSupportedLocale(normalizedLocale);
|
|
43
|
+
if (exactLocale) {
|
|
44
|
+
return exactLocale;
|
|
45
|
+
}
|
|
46
|
+
const [baseLocale] = normalizedLocale.split("-");
|
|
47
|
+
if (!baseLocale) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return asSupportedLocale(baseLocale);
|
|
33
51
|
};
|
|
34
52
|
var ensureLocaleLoaded = async (locale) => {
|
|
35
53
|
const load = localeLoaders[locale];
|
|
@@ -45,11 +63,20 @@ dayjs.locale("en");
|
|
|
45
63
|
var DEFAULT_FORMAT = "YYYY-MM-DD";
|
|
46
64
|
var DEFAULT_LOCALE = "en";
|
|
47
65
|
var DEFAULT_INVALID_DATE = "Invalid Date";
|
|
66
|
+
var DEFAULT_STRICT = true;
|
|
67
|
+
var createResolvedConfig = (locale, config) => ({
|
|
68
|
+
locale,
|
|
69
|
+
strict: config?.strict ?? DEFAULT_STRICT,
|
|
70
|
+
invalid: config?.invalid ?? DEFAULT_INVALID_DATE
|
|
71
|
+
});
|
|
48
72
|
var getInvalidDateText = (config, props) => {
|
|
49
73
|
return props?.invalid ?? config?.invalid ?? DEFAULT_INVALID_DATE;
|
|
50
74
|
};
|
|
51
75
|
var getTargetLocale = (currentLocale, props) => {
|
|
52
|
-
|
|
76
|
+
if (!props?.locale) {
|
|
77
|
+
return currentLocale;
|
|
78
|
+
}
|
|
79
|
+
return resolveLocaleOrThrow(props.locale);
|
|
53
80
|
};
|
|
54
81
|
var parseDateValue = (value, input, locale, strict) => {
|
|
55
82
|
if (!input) {
|
|
@@ -85,11 +112,11 @@ var createFormatterParseDate = (getConfig) => {
|
|
|
85
112
|
};
|
|
86
113
|
};
|
|
87
114
|
};
|
|
88
|
-
var createFormatterIsValidDate = (
|
|
89
|
-
return (props) =>
|
|
115
|
+
var createFormatterIsValidDate = (parseDate2) => {
|
|
116
|
+
return (props) => parseDate2(props).isValid;
|
|
90
117
|
};
|
|
91
118
|
var createFormatterGetDate = (getConfig) => {
|
|
92
|
-
const
|
|
119
|
+
const parseDate2 = createFormatterParseDate(getConfig);
|
|
93
120
|
return (props) => {
|
|
94
121
|
const config = getConfig();
|
|
95
122
|
const locale = getTargetLocale(config.locale, props);
|
|
@@ -100,7 +127,7 @@ var createFormatterGetDate = (getConfig) => {
|
|
|
100
127
|
if (props.date === void 0) {
|
|
101
128
|
return dayjs().locale(locale).format(output);
|
|
102
129
|
}
|
|
103
|
-
const parsed =
|
|
130
|
+
const parsed = parseDate2({
|
|
104
131
|
date: props.date,
|
|
105
132
|
input: props.input,
|
|
106
133
|
locale: props.locale,
|
|
@@ -112,31 +139,31 @@ var createFormatterGetDate = (getConfig) => {
|
|
|
112
139
|
return parsed.format(output);
|
|
113
140
|
};
|
|
114
141
|
};
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
|
|
142
|
+
function resolveLocaleOrThrow(locale) {
|
|
143
|
+
const resolvedLocale = resolveLocale(locale);
|
|
144
|
+
if (!resolvedLocale) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
`Unsupported locale: ${locale}. The package tries an exact locale match first and then falls back to the base locale.`
|
|
147
|
+
);
|
|
118
148
|
}
|
|
149
|
+
return resolvedLocale;
|
|
119
150
|
}
|
|
120
151
|
var getSupportedLocales = () => SUPPORTED_LOCALES;
|
|
121
152
|
var createDateFormatter = (config) => {
|
|
122
|
-
let currentLocale = config?.locale
|
|
123
|
-
const getConfig = () => (
|
|
124
|
-
locale: currentLocale,
|
|
125
|
-
strict: config?.strict ?? false,
|
|
126
|
-
invalid: config?.invalid ?? DEFAULT_INVALID_DATE
|
|
127
|
-
});
|
|
153
|
+
let currentLocale = config?.locale ? resolveLocaleOrThrow(config.locale) : DEFAULT_LOCALE;
|
|
154
|
+
const getConfig = () => createResolvedConfig(currentLocale, config);
|
|
128
155
|
const ready = ensureLocaleLoaded(currentLocale);
|
|
129
|
-
const
|
|
156
|
+
const parseDate2 = createFormatterParseDate(getConfig);
|
|
130
157
|
return {
|
|
131
158
|
getDate: createFormatterGetDate(getConfig),
|
|
132
|
-
parseDate,
|
|
133
|
-
isValidDate: createFormatterIsValidDate(
|
|
159
|
+
parseDate: parseDate2,
|
|
160
|
+
isValidDate: createFormatterIsValidDate(parseDate2),
|
|
134
161
|
getSupportedLocales,
|
|
135
162
|
getCurrentLocale: () => currentLocale,
|
|
136
163
|
setLocale: async (locale) => {
|
|
137
|
-
|
|
138
|
-
await ensureLocaleLoaded(
|
|
139
|
-
currentLocale =
|
|
164
|
+
const resolvedLocale = resolveLocaleOrThrow(locale);
|
|
165
|
+
await ensureLocaleLoaded(resolvedLocale);
|
|
166
|
+
currentLocale = resolvedLocale;
|
|
140
167
|
},
|
|
141
168
|
ready
|
|
142
169
|
};
|
|
@@ -148,7 +175,7 @@ var useDateFormatter = (options) => {
|
|
|
148
175
|
const locale = ref(formatter.getCurrentLocale());
|
|
149
176
|
const setLocale = async (nextLocale) => {
|
|
150
177
|
await formatter.setLocale(nextLocale);
|
|
151
|
-
locale.value =
|
|
178
|
+
locale.value = formatter.getCurrentLocale();
|
|
152
179
|
};
|
|
153
180
|
return {
|
|
154
181
|
locale,
|
package/dist/vue/index.js.map
CHANGED
|
@@ -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 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: SupportedLocale) => {\n await formatter.setLocale(nextLocale)\n locale.value = nextLocale\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, isSupportedLocale, SUPPORTED_LOCALES, 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?: SupportedLocale\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?: 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: SupportedLocale) => 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'\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 return props?.locale ?? currentLocale\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: () => Required<DateFormatterConfig>) => {\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: () => Required<DateFormatterConfig>) => {\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 assertSupportedLocale(locale: string): asserts locale is SupportedLocale {\n if (!isSupportedLocale(locale)) {\n throw new Error(`Unsupported locale: ${locale}`)\n }\n}\n\nexport const getSupportedLocales = (): readonly SupportedLocale[] => SUPPORTED_LOCALES\n\nexport const createDateFormatter = (config?: DateFormatterConfig): DateFormatter => {\n let currentLocale = config?.locale ?? DEFAULT_LOCALE\n\n const getConfig = (): Required<DateFormatterConfig> => ({\n locale: currentLocale,\n strict: config?.strict ?? false,\n invalid: config?.invalid ?? DEFAULT_INVALID_DATE\n })\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: SupportedLocale) => {\n assertSupportedLocale(locale)\n await ensureLocaleLoaded(locale)\n currentLocale = locale\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]\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\nexport const isSupportedLocale = (locale: string): locale is SupportedLocale => {\n return SUPPORTED_LOCALES.includes(locale as SupportedLocale)\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,WAAW;;;ACDX,IAAM,oBAAoB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAIA,IAAM,gBAA0E;AAAA,EAC9E,IAAI;AAAA,EACJ,IAAI,MAAM,OAAO,oBAAoB;AAAA,EACrC,SAAS,MAAM,OAAO,uBAAuB;AAAA,EAC7C,IAAI,MAAM,OAAO,oBAAoB;AA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O,IAAM,sBAAsB,MAAkC;AAE9D,IAAM,sBAAsB,CAAC,WAAgD;AAClF,MAAI,gBAAgB,QAAQ,UAAU;AAEtC,QAAM,YAAY,OAAsC;AAAA,IACtD,QAAQ;AAAA,IACR,QAAQ,QAAQ,UAAU;AAAA,IAC1B,SAAS,QAAQ,WAAW;AAAA,EAC9B;AAEA,QAAM,QAAQ,mBAAmB,aAAa;AAC9C,QAAM,YAAY,yBAAyB,SAAS;AAEpD,SAAO;AAAA,IACL,SAAS,uBAAuB,SAAS;AAAA,IACzC;AAAA,IACA,aAAa,2BAA2B,SAAS;AAAA,IACjD;AAAA,IACA,kBAAkB,MAAM;AAAA,IACxB,WAAW,OAAO,WAA4B;AAC5C,4BAAsB,MAAM;AAC5B,YAAM,mBAAmB,MAAM;AAC/B,sBAAgB;AAAA,IAClB;AAAA,IACA;AAAA,EACF;AACF;;;AD/KO,IAAM,mBAAmB,CAAC,YAAsC;AACrE,QAAM,YAAY,oBAAoB,OAAO;AAC7C,QAAM,SAAS,IAAqB,UAAU,iBAAiB,CAAC;AAEhE,QAAM,YAAY,OAAO,eAAgC;AACvD,UAAM,UAAU,UAAU,UAAU;AACpC,WAAO,QAAQ;AAAA,EACjB;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":[]}
|
|
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,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,gBAA0E;AAAA,EAC9E,IAAI;AAAA,EACJ,IAAI,MAAM,OAAO,oBAAoB;AAAA,EACrC,SAAS,MAAM,OAAO,uBAAuB;AAAA,EAC7C,IAAI,MAAM,OAAO,oBAAoB;AAAA,EACrC,IAAI,MAAM,OAAO,oBAAoB;AAAA,EACrC,SAAS,MAAM,OAAO,uBAAuB;AAAA,EAC7C,IAAI,MAAM,OAAO,oBAAoB;AAAA,EACrC,IAAI,MAAM,OAAO,oBAAoB;AAAA,EACrC,IAAI,MAAM,OAAO,oBAAoB;AACvC;AAEA,IAAM,kBAAkB,CAAC,WAA2B;AAClD,SAAO,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,MAAM,GAAG;AACtD;AAEA,IAAM,oBAAoB,CAAC,WAA2C;AACpE,MAAI,CAAC,kBAAkB,SAAS,MAAyB,GAAG;AAC1D,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAMO,IAAM,gBAAgB,CAAC,WAAgD;AAC5E,QAAM,mBAAmB,gBAAgB,MAAM;AAC/C,QAAM,cAAc,kBAAkB,gBAAgB;AAEtD,MAAI,aAAa;AACf,WAAO;AAAA,EACT;AAEA,QAAM,CAAC,UAAU,IAAI,iBAAiB,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