@samline/date 1.0.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/LICENSE +21 -0
- package/README.md +141 -0
- package/dist/browser/global.d.ts +13 -0
- package/dist/browser/global.js +153 -0
- package/dist/browser/global.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +146 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.js +166 -0
- package/dist/react/index.js.map +1 -0
- package/dist/svelte/index.d.ts +19 -0
- package/dist/svelte/index.js +169 -0
- package/dist/svelte/index.js.map +1 -0
- package/dist/vanilla/index.d.ts +1 -0
- package/dist/vanilla/index.js +146 -0
- package/dist/vanilla/index.js.map +1 -0
- package/dist/vue/index.d.ts +16 -0
- package/dist/vue/index.js +167 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 samline
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# @samline/date
|
|
2
|
+
|
|
3
|
+
Small date formatting package built on top of Day.js with a shared core API, framework wrappers, and browser usage.
|
|
4
|
+
|
|
5
|
+
This package uses Day.js as its date engine. We are grateful for the existence of the package and will make good use of it in this project.
|
|
6
|
+
|
|
7
|
+
Repository: https://github.com/iamkun/dayjs
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- format dates with configurable input and output patterns
|
|
12
|
+
- default locale is English
|
|
13
|
+
- load and switch supported locales on demand
|
|
14
|
+
- create formatter instances with isolated locale state
|
|
15
|
+
- enable strict parsing globally per formatter or per call
|
|
16
|
+
- parse and validate dates with explicit result objects
|
|
17
|
+
- use the same API from core, vanilla, React, Vue, Svelte, or browser global builds
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @samline/date
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Entrypoints
|
|
26
|
+
|
|
27
|
+
| Entrypoint | Purpose |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `@samline/date` | shared core API |
|
|
30
|
+
| `@samline/date/vanilla` | utility wrapper for plain TypeScript or JavaScript |
|
|
31
|
+
| `@samline/date/react` | React hook |
|
|
32
|
+
| `@samline/date/vue` | Vue composable |
|
|
33
|
+
| `@samline/date/svelte` | Svelte store helpers |
|
|
34
|
+
| `@samline/date/browser` | browser global build |
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { createDateFormatter } from '@samline/date'
|
|
40
|
+
|
|
41
|
+
const formatter = createDateFormatter({ locale: 'es-mx', strict: true })
|
|
42
|
+
|
|
43
|
+
await formatter.ready
|
|
44
|
+
|
|
45
|
+
const date = formatter.getDate({
|
|
46
|
+
date: '23/03/2026',
|
|
47
|
+
input: 'DD/MM/YYYY',
|
|
48
|
+
output: 'MMMM D, YYYY'
|
|
49
|
+
})
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## API
|
|
53
|
+
|
|
54
|
+
### createDateFormatter
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
createDateFormatter(config?: {
|
|
58
|
+
locale?: SupportedLocale
|
|
59
|
+
strict?: boolean
|
|
60
|
+
invalid?: string
|
|
61
|
+
}): {
|
|
62
|
+
getDate(props?: GetDateOptions): string
|
|
63
|
+
parseDate(props: DateParsingOptions): ParseDateResult
|
|
64
|
+
isValidDate(props: DateParsingOptions): boolean
|
|
65
|
+
getSupportedLocales(): readonly SupportedLocale[]
|
|
66
|
+
getCurrentLocale(): SupportedLocale
|
|
67
|
+
setLocale(locale: SupportedLocale): Promise<void>
|
|
68
|
+
ready: Promise<void>
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Creates a formatter instance with its own locale state. This avoids coupling framework wrappers and utility calls to the global Day.js locale.
|
|
73
|
+
|
|
74
|
+
Use `strict: true` to make parsing fail when the input does not match the provided format exactly.
|
|
75
|
+
|
|
76
|
+
If you override `locale` per call, make sure that locale was already loaded by a formatter instance.
|
|
77
|
+
|
|
78
|
+
The formatter instance exposes:
|
|
79
|
+
|
|
80
|
+
- `getDate(props?)`
|
|
81
|
+
- `parseDate(props)`
|
|
82
|
+
- `isValidDate(props)`
|
|
83
|
+
- `getCurrentLocale()`
|
|
84
|
+
- `setLocale(locale)`
|
|
85
|
+
- `getSupportedLocales()`
|
|
86
|
+
- `ready`
|
|
87
|
+
|
|
88
|
+
### parseDate
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
formatter.parseDate({
|
|
92
|
+
date: '23/03/2026',
|
|
93
|
+
input: 'DD/MM/YYYY',
|
|
94
|
+
strict: true
|
|
95
|
+
})
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Returns a structured result.
|
|
99
|
+
|
|
100
|
+
- Valid parse: `isValid`, `date`, `iso`, `timestamp`, `format(output?)`
|
|
101
|
+
- Invalid parse: `isValid: false`, `error`, and null date fields
|
|
102
|
+
|
|
103
|
+
### isValidDate
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
formatter.isValidDate({
|
|
107
|
+
date: '1970-00-00',
|
|
108
|
+
input: 'YYYY-MM-DD',
|
|
109
|
+
strict: true
|
|
110
|
+
})
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Returns a boolean when you only need validation without formatting.
|
|
114
|
+
|
|
115
|
+
## Supported Locales
|
|
116
|
+
|
|
117
|
+
The package ships helper support for these locale keys:
|
|
118
|
+
|
|
119
|
+
- `en`
|
|
120
|
+
- `es`
|
|
121
|
+
- `es-mx`
|
|
122
|
+
- `fr`
|
|
123
|
+
- `pt`
|
|
124
|
+
- `pt-br`
|
|
125
|
+
- `de`
|
|
126
|
+
- `it`
|
|
127
|
+
- `ja`
|
|
128
|
+
|
|
129
|
+
Use `createDateFormatter({ locale: 'es-mx' })` when you need a locale other than English.
|
|
130
|
+
|
|
131
|
+
## Documentation
|
|
132
|
+
|
|
133
|
+
- [docs/vanilla.md](docs/vanilla.md)
|
|
134
|
+
- [docs/browser.md](docs/browser.md)
|
|
135
|
+
- [docs/react.md](docs/react.md)
|
|
136
|
+
- [docs/vue.md](docs/vue.md)
|
|
137
|
+
- [docs/svelte.md](docs/svelte.md)
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DateFormatterConfig, DateFormatter, SupportedLocale } from '../index.js';
|
|
2
|
+
|
|
3
|
+
declare const DateKit: {
|
|
4
|
+
createDateFormatter: (config?: DateFormatterConfig) => DateFormatter;
|
|
5
|
+
getSupportedLocales: () => readonly SupportedLocale[];
|
|
6
|
+
};
|
|
7
|
+
declare global {
|
|
8
|
+
interface Window {
|
|
9
|
+
DateKit: typeof DateKit;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { DateKit };
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// src/core/date.ts
|
|
2
|
+
import customParseFormat from "dayjs/plugin/customParseFormat.js";
|
|
3
|
+
import dayjs from "dayjs";
|
|
4
|
+
|
|
5
|
+
// src/core/locales.ts
|
|
6
|
+
var SUPPORTED_LOCALES = [
|
|
7
|
+
"en",
|
|
8
|
+
"es",
|
|
9
|
+
"es-mx",
|
|
10
|
+
"fr",
|
|
11
|
+
"pt",
|
|
12
|
+
"pt-br",
|
|
13
|
+
"de",
|
|
14
|
+
"it",
|
|
15
|
+
"ja"
|
|
16
|
+
];
|
|
17
|
+
var localeLoaders = {
|
|
18
|
+
en: null,
|
|
19
|
+
es: () => import("dayjs/locale/es.js"),
|
|
20
|
+
"es-mx": () => import("dayjs/locale/es-mx.js"),
|
|
21
|
+
fr: () => import("dayjs/locale/fr.js"),
|
|
22
|
+
pt: () => import("dayjs/locale/pt.js"),
|
|
23
|
+
"pt-br": () => import("dayjs/locale/pt-br.js"),
|
|
24
|
+
de: () => import("dayjs/locale/de.js"),
|
|
25
|
+
it: () => import("dayjs/locale/it.js"),
|
|
26
|
+
ja: () => import("dayjs/locale/ja.js")
|
|
27
|
+
};
|
|
28
|
+
var isSupportedLocale = (locale) => {
|
|
29
|
+
return SUPPORTED_LOCALES.includes(locale);
|
|
30
|
+
};
|
|
31
|
+
var ensureLocaleLoaded = async (locale) => {
|
|
32
|
+
const load = localeLoaders[locale];
|
|
33
|
+
if (!load) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
await load();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/core/date.ts
|
|
40
|
+
dayjs.extend(customParseFormat);
|
|
41
|
+
dayjs.locale("en");
|
|
42
|
+
var DEFAULT_FORMAT = "YYYY-MM-DD";
|
|
43
|
+
var DEFAULT_LOCALE = "en";
|
|
44
|
+
var DEFAULT_INVALID_DATE = "Invalid Date";
|
|
45
|
+
var getInvalidDateText = (config, props) => {
|
|
46
|
+
return props?.invalid ?? config?.invalid ?? DEFAULT_INVALID_DATE;
|
|
47
|
+
};
|
|
48
|
+
var getTargetLocale = (currentLocale, props) => {
|
|
49
|
+
return props?.locale ?? currentLocale;
|
|
50
|
+
};
|
|
51
|
+
var parseDateValue = (value, input, locale, strict) => {
|
|
52
|
+
if (!input) {
|
|
53
|
+
return dayjs(value).locale(locale);
|
|
54
|
+
}
|
|
55
|
+
if (typeof input === "string") {
|
|
56
|
+
return dayjs(value, input, locale, strict).locale(locale);
|
|
57
|
+
}
|
|
58
|
+
return dayjs(value, [...input], locale, strict).locale(locale);
|
|
59
|
+
};
|
|
60
|
+
var createFormatterParseDate = (getConfig) => {
|
|
61
|
+
return (props) => {
|
|
62
|
+
const config = getConfig();
|
|
63
|
+
const locale = getTargetLocale(config.locale, props);
|
|
64
|
+
const parsed = parseDateValue(props.date, props.input, locale, props.strict ?? config.strict);
|
|
65
|
+
if (!parsed.isValid()) {
|
|
66
|
+
return {
|
|
67
|
+
isValid: false,
|
|
68
|
+
locale,
|
|
69
|
+
date: null,
|
|
70
|
+
iso: null,
|
|
71
|
+
timestamp: null,
|
|
72
|
+
error: getInvalidDateText(config, props)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
isValid: true,
|
|
77
|
+
locale,
|
|
78
|
+
date: parsed.toDate(),
|
|
79
|
+
iso: parsed.toISOString(),
|
|
80
|
+
timestamp: parsed.valueOf(),
|
|
81
|
+
format: (output = DEFAULT_FORMAT) => parsed.format(output)
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
var createFormatterIsValidDate = (parseDate) => {
|
|
86
|
+
return (props) => parseDate(props).isValid;
|
|
87
|
+
};
|
|
88
|
+
var createFormatterGetDate = (getConfig) => {
|
|
89
|
+
const parseDate = createFormatterParseDate(getConfig);
|
|
90
|
+
return (props) => {
|
|
91
|
+
const config = getConfig();
|
|
92
|
+
const locale = getTargetLocale(config.locale, props);
|
|
93
|
+
const output = props?.output ?? DEFAULT_FORMAT;
|
|
94
|
+
if (!props) {
|
|
95
|
+
return dayjs().locale(locale).format(DEFAULT_FORMAT);
|
|
96
|
+
}
|
|
97
|
+
if (props.date === void 0) {
|
|
98
|
+
return dayjs().locale(locale).format(output);
|
|
99
|
+
}
|
|
100
|
+
const parsed = parseDate({
|
|
101
|
+
date: props.date,
|
|
102
|
+
input: props.input,
|
|
103
|
+
locale: props.locale,
|
|
104
|
+
strict: props.strict
|
|
105
|
+
});
|
|
106
|
+
if (!parsed.isValid) {
|
|
107
|
+
return props.invalid ?? parsed.error;
|
|
108
|
+
}
|
|
109
|
+
return parsed.format(output);
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
function assertSupportedLocale(locale) {
|
|
113
|
+
if (!isSupportedLocale(locale)) {
|
|
114
|
+
throw new Error(`Unsupported locale: ${locale}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
var getSupportedLocales = () => SUPPORTED_LOCALES;
|
|
118
|
+
var createDateFormatter = (config) => {
|
|
119
|
+
let currentLocale = config?.locale ?? DEFAULT_LOCALE;
|
|
120
|
+
const getConfig = () => ({
|
|
121
|
+
locale: currentLocale,
|
|
122
|
+
strict: config?.strict ?? false,
|
|
123
|
+
invalid: config?.invalid ?? DEFAULT_INVALID_DATE
|
|
124
|
+
});
|
|
125
|
+
const ready = ensureLocaleLoaded(currentLocale);
|
|
126
|
+
const parseDate = createFormatterParseDate(getConfig);
|
|
127
|
+
return {
|
|
128
|
+
getDate: createFormatterGetDate(getConfig),
|
|
129
|
+
parseDate,
|
|
130
|
+
isValidDate: createFormatterIsValidDate(parseDate),
|
|
131
|
+
getSupportedLocales,
|
|
132
|
+
getCurrentLocale: () => currentLocale,
|
|
133
|
+
setLocale: async (locale) => {
|
|
134
|
+
assertSupportedLocale(locale);
|
|
135
|
+
await ensureLocaleLoaded(locale);
|
|
136
|
+
currentLocale = locale;
|
|
137
|
+
},
|
|
138
|
+
ready
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/browser/global.ts
|
|
143
|
+
var DateKit = {
|
|
144
|
+
createDateFormatter,
|
|
145
|
+
getSupportedLocales
|
|
146
|
+
};
|
|
147
|
+
if (typeof window !== "undefined") {
|
|
148
|
+
window.DateKit = DateKit;
|
|
149
|
+
}
|
|
150
|
+
export {
|
|
151
|
+
DateKit
|
|
152
|
+
};
|
|
153
|
+
//# sourceMappingURL=global.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/date.ts","../../src/core/locales.ts","../../src/browser/global.ts"],"sourcesContent":["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","import { createDateFormatter, getSupportedLocales } from '../index.js'\n\nexport const DateKit = {\n createDateFormatter,\n getSupportedLocales\n}\n\ndeclare global {\n interface Window {\n DateKit: typeof DateKit\n }\n}\n\nif (typeof window !== 'undefined') {\n window.DateKit = DateKit\n}\n"],"mappings":";AAAA,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;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;AAEO,IAAM,oBAAoB,CAAC,WAA8C;AAC9E,SAAO,kBAAkB,SAAS,MAAyB;AAC7D;AAEO,IAAM,qBAAqB,OAAO,WAA2C;AAClF,QAAM,OAAO,cAAc,MAAM;AAEjC,MAAI,CAAC,MAAM;AACT;AAAA,EACF;A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jD;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;;;AE1LO,IAAM,UAAU;AAAA,EACrB;AAAA,EACA;AACF;AAQA,IAAI,OAAO,WAAW,aAAa;AACjC,SAAO,UAAU;AACnB;","names":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
declare const SUPPORTED_LOCALES: readonly ["en", "es", "es-mx", "fr", "pt", "pt-br", "de", "it", "ja"];
|
|
2
|
+
type SupportedLocale = (typeof SUPPORTED_LOCALES)[number];
|
|
3
|
+
|
|
4
|
+
type DateValue = string | number | Date;
|
|
5
|
+
type DateInputOptions = {
|
|
6
|
+
input?: string | readonly string[];
|
|
7
|
+
locale?: SupportedLocale;
|
|
8
|
+
strict?: boolean;
|
|
9
|
+
};
|
|
10
|
+
type DateParsingOptions = DateInputOptions & {
|
|
11
|
+
date: DateValue;
|
|
12
|
+
};
|
|
13
|
+
type GetDateOptions = DateInputOptions & {
|
|
14
|
+
date?: DateValue;
|
|
15
|
+
output?: string;
|
|
16
|
+
invalid?: string;
|
|
17
|
+
};
|
|
18
|
+
type DateFormatterConfig = {
|
|
19
|
+
locale?: SupportedLocale;
|
|
20
|
+
strict?: boolean;
|
|
21
|
+
invalid?: string;
|
|
22
|
+
};
|
|
23
|
+
type DateFormatter = {
|
|
24
|
+
getDate: (props?: GetDateOptions) => string;
|
|
25
|
+
parseDate: (props: DateParsingOptions) => ParseDateResult;
|
|
26
|
+
isValidDate: (props: DateParsingOptions) => boolean;
|
|
27
|
+
getSupportedLocales: () => readonly SupportedLocale[];
|
|
28
|
+
getCurrentLocale: () => SupportedLocale;
|
|
29
|
+
setLocale: (locale: SupportedLocale) => Promise<void>;
|
|
30
|
+
ready: Promise<void>;
|
|
31
|
+
};
|
|
32
|
+
type ParseDateSuccess = {
|
|
33
|
+
isValid: true;
|
|
34
|
+
locale: SupportedLocale;
|
|
35
|
+
date: Date;
|
|
36
|
+
iso: string;
|
|
37
|
+
timestamp: number;
|
|
38
|
+
format: (output?: string) => string;
|
|
39
|
+
};
|
|
40
|
+
type ParseDateFailure = {
|
|
41
|
+
isValid: false;
|
|
42
|
+
locale: SupportedLocale;
|
|
43
|
+
date: null;
|
|
44
|
+
iso: null;
|
|
45
|
+
timestamp: null;
|
|
46
|
+
error: string;
|
|
47
|
+
};
|
|
48
|
+
type ParseDateResult = ParseDateSuccess | ParseDateFailure;
|
|
49
|
+
declare const getSupportedLocales: () => readonly SupportedLocale[];
|
|
50
|
+
declare const createDateFormatter: (config?: DateFormatterConfig) => DateFormatter;
|
|
51
|
+
|
|
52
|
+
export { type DateFormatter, type DateFormatterConfig, type DateParsingOptions, type DateValue, type GetDateOptions, type ParseDateFailure, type ParseDateResult, type ParseDateSuccess, SUPPORTED_LOCALES, type SupportedLocale, createDateFormatter, getSupportedLocales };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// src/core/date.ts
|
|
2
|
+
import customParseFormat from "dayjs/plugin/customParseFormat.js";
|
|
3
|
+
import dayjs from "dayjs";
|
|
4
|
+
|
|
5
|
+
// src/core/locales.ts
|
|
6
|
+
var SUPPORTED_LOCALES = [
|
|
7
|
+
"en",
|
|
8
|
+
"es",
|
|
9
|
+
"es-mx",
|
|
10
|
+
"fr",
|
|
11
|
+
"pt",
|
|
12
|
+
"pt-br",
|
|
13
|
+
"de",
|
|
14
|
+
"it",
|
|
15
|
+
"ja"
|
|
16
|
+
];
|
|
17
|
+
var localeLoaders = {
|
|
18
|
+
en: null,
|
|
19
|
+
es: () => import("dayjs/locale/es.js"),
|
|
20
|
+
"es-mx": () => import("dayjs/locale/es-mx.js"),
|
|
21
|
+
fr: () => import("dayjs/locale/fr.js"),
|
|
22
|
+
pt: () => import("dayjs/locale/pt.js"),
|
|
23
|
+
"pt-br": () => import("dayjs/locale/pt-br.js"),
|
|
24
|
+
de: () => import("dayjs/locale/de.js"),
|
|
25
|
+
it: () => import("dayjs/locale/it.js"),
|
|
26
|
+
ja: () => import("dayjs/locale/ja.js")
|
|
27
|
+
};
|
|
28
|
+
var isSupportedLocale = (locale) => {
|
|
29
|
+
return SUPPORTED_LOCALES.includes(locale);
|
|
30
|
+
};
|
|
31
|
+
var ensureLocaleLoaded = async (locale) => {
|
|
32
|
+
const load = localeLoaders[locale];
|
|
33
|
+
if (!load) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
await load();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/core/date.ts
|
|
40
|
+
dayjs.extend(customParseFormat);
|
|
41
|
+
dayjs.locale("en");
|
|
42
|
+
var DEFAULT_FORMAT = "YYYY-MM-DD";
|
|
43
|
+
var DEFAULT_LOCALE = "en";
|
|
44
|
+
var DEFAULT_INVALID_DATE = "Invalid Date";
|
|
45
|
+
var getInvalidDateText = (config, props) => {
|
|
46
|
+
return props?.invalid ?? config?.invalid ?? DEFAULT_INVALID_DATE;
|
|
47
|
+
};
|
|
48
|
+
var getTargetLocale = (currentLocale, props) => {
|
|
49
|
+
return props?.locale ?? currentLocale;
|
|
50
|
+
};
|
|
51
|
+
var parseDateValue = (value, input, locale, strict) => {
|
|
52
|
+
if (!input) {
|
|
53
|
+
return dayjs(value).locale(locale);
|
|
54
|
+
}
|
|
55
|
+
if (typeof input === "string") {
|
|
56
|
+
return dayjs(value, input, locale, strict).locale(locale);
|
|
57
|
+
}
|
|
58
|
+
return dayjs(value, [...input], locale, strict).locale(locale);
|
|
59
|
+
};
|
|
60
|
+
var createFormatterParseDate = (getConfig) => {
|
|
61
|
+
return (props) => {
|
|
62
|
+
const config = getConfig();
|
|
63
|
+
const locale = getTargetLocale(config.locale, props);
|
|
64
|
+
const parsed = parseDateValue(props.date, props.input, locale, props.strict ?? config.strict);
|
|
65
|
+
if (!parsed.isValid()) {
|
|
66
|
+
return {
|
|
67
|
+
isValid: false,
|
|
68
|
+
locale,
|
|
69
|
+
date: null,
|
|
70
|
+
iso: null,
|
|
71
|
+
timestamp: null,
|
|
72
|
+
error: getInvalidDateText(config, props)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
isValid: true,
|
|
77
|
+
locale,
|
|
78
|
+
date: parsed.toDate(),
|
|
79
|
+
iso: parsed.toISOString(),
|
|
80
|
+
timestamp: parsed.valueOf(),
|
|
81
|
+
format: (output = DEFAULT_FORMAT) => parsed.format(output)
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
var createFormatterIsValidDate = (parseDate) => {
|
|
86
|
+
return (props) => parseDate(props).isValid;
|
|
87
|
+
};
|
|
88
|
+
var createFormatterGetDate = (getConfig) => {
|
|
89
|
+
const parseDate = createFormatterParseDate(getConfig);
|
|
90
|
+
return (props) => {
|
|
91
|
+
const config = getConfig();
|
|
92
|
+
const locale = getTargetLocale(config.locale, props);
|
|
93
|
+
const output = props?.output ?? DEFAULT_FORMAT;
|
|
94
|
+
if (!props) {
|
|
95
|
+
return dayjs().locale(locale).format(DEFAULT_FORMAT);
|
|
96
|
+
}
|
|
97
|
+
if (props.date === void 0) {
|
|
98
|
+
return dayjs().locale(locale).format(output);
|
|
99
|
+
}
|
|
100
|
+
const parsed = parseDate({
|
|
101
|
+
date: props.date,
|
|
102
|
+
input: props.input,
|
|
103
|
+
locale: props.locale,
|
|
104
|
+
strict: props.strict
|
|
105
|
+
});
|
|
106
|
+
if (!parsed.isValid) {
|
|
107
|
+
return props.invalid ?? parsed.error;
|
|
108
|
+
}
|
|
109
|
+
return parsed.format(output);
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
function assertSupportedLocale(locale) {
|
|
113
|
+
if (!isSupportedLocale(locale)) {
|
|
114
|
+
throw new Error(`Unsupported locale: ${locale}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
var getSupportedLocales = () => SUPPORTED_LOCALES;
|
|
118
|
+
var createDateFormatter = (config) => {
|
|
119
|
+
let currentLocale = config?.locale ?? DEFAULT_LOCALE;
|
|
120
|
+
const getConfig = () => ({
|
|
121
|
+
locale: currentLocale,
|
|
122
|
+
strict: config?.strict ?? false,
|
|
123
|
+
invalid: config?.invalid ?? DEFAULT_INVALID_DATE
|
|
124
|
+
});
|
|
125
|
+
const ready = ensureLocaleLoaded(currentLocale);
|
|
126
|
+
const parseDate = createFormatterParseDate(getConfig);
|
|
127
|
+
return {
|
|
128
|
+
getDate: createFormatterGetDate(getConfig),
|
|
129
|
+
parseDate,
|
|
130
|
+
isValidDate: createFormatterIsValidDate(parseDate),
|
|
131
|
+
getSupportedLocales,
|
|
132
|
+
getCurrentLocale: () => currentLocale,
|
|
133
|
+
setLocale: async (locale) => {
|
|
134
|
+
assertSupportedLocale(locale);
|
|
135
|
+
await ensureLocaleLoaded(locale);
|
|
136
|
+
currentLocale = locale;
|
|
137
|
+
},
|
|
138
|
+
ready
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
export {
|
|
142
|
+
SUPPORTED_LOCALES,
|
|
143
|
+
createDateFormatter,
|
|
144
|
+
getSupportedLocales
|
|
145
|
+
};
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/date.ts","../src/core/locales.ts"],"sourcesContent":["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,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;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;AAEO,IAAM,oBAAoB,CAAC,WAA8C;AAC9E,SAAO,kBAAkB,SAAS,MAAyB;AAC7D;AAEO,IAAM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AEpD,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;","names":[]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DateFormatterConfig, GetDateOptions, DateParsingOptions, ParseDateResult, SupportedLocale } from '../index.js';
|
|
2
|
+
|
|
3
|
+
type UseDateFormatterOptions = DateFormatterConfig;
|
|
4
|
+
declare const useDateFormatter: (options?: UseDateFormatterOptions) => {
|
|
5
|
+
locale: "en" | "es" | "es-mx" | "fr" | "pt" | "pt-br" | "de" | "it" | "ja";
|
|
6
|
+
currentLocale: "en" | "es" | "es-mx" | "fr" | "pt" | "pt-br" | "de" | "it" | "ja";
|
|
7
|
+
getDate: (props?: GetDateOptions) => string;
|
|
8
|
+
parseDate: (props: DateParsingOptions) => ParseDateResult;
|
|
9
|
+
isValidDate: (props: DateParsingOptions) => boolean;
|
|
10
|
+
getSupportedLocales: () => readonly SupportedLocale[];
|
|
11
|
+
ready: Promise<void>;
|
|
12
|
+
setLocale: (nextLocale: SupportedLocale) => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { type UseDateFormatterOptions, useDateFormatter };
|