@samline/date 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # @samline/date
2
2
 
3
- Small date formatting package built on top of Day.js with a shared core API, framework wrappers, and browser usage.
3
+ Small date formatting package built on top of Day.js with strict parsing, locale-aware formatting, and a shared API for core, vanilla, React, Vue, Svelte, and browser usage.
4
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.
5
+ This package uses Day.js as its date engine. Thanks to the Day.js project for making that foundation available.
6
6
 
7
- Repository: https://github.com/iamkun/dayjs
7
+ Day.js repository: https://github.com/iamkun/dayjs
8
8
 
9
9
  ## Features
10
10
 
@@ -16,25 +16,83 @@ Repository: https://github.com/iamkun/dayjs
16
16
  - parse and validate dates with explicit result objects
17
17
  - use the same API from core, vanilla, React, Vue, Svelte, or browser global builds
18
18
 
19
+ ## Table of Contents
20
+
21
+ - [Installation](#installation)
22
+ - [CDN / Browser](#cdn--browser)
23
+ - [Entrypoints](#entrypoints)
24
+ - [Quick Start](#quick-start)
25
+ - [API](#api)
26
+ - [Supported Locales](#supported-locales)
27
+ - [Documentation](#documentation)
28
+ - [License](#license)
29
+
19
30
  ## Installation
20
31
 
32
+ Use the package manager that matches your project. The published package targets Node 20 or newer.
33
+
21
34
  ```bash
22
35
  npm install @samline/date
23
36
  ```
24
37
 
38
+ ```bash
39
+ pnpm add @samline/date
40
+ ```
41
+
42
+ ```bash
43
+ yarn add @samline/date
44
+ ```
45
+
46
+ ```bash
47
+ bun add @samline/date
48
+ ```
49
+
50
+ ## CDN / Browser
51
+
52
+ Use the browser bundle when your project loads scripts directly in the page and cannot compile npm modules.
53
+
54
+ This is useful in environments such as Shopify themes, WordPress templates, or plain HTML pages with no build step.
55
+
56
+ ```html
57
+ <script src="https://cdn.jsdelivr.net/npm/@samline/date@2.1.2/dist/browser/date.global.js"></script>
58
+ ```
59
+
60
+ Then use it from a normal script:
61
+
62
+ ```html
63
+ <script>
64
+ ;(async () => {
65
+ const value = await window.DateKit.getDate({
66
+ date: '23/03/2026',
67
+ input: 'DD/MM/YYYY',
68
+ output: 'MMMM D, YYYY'
69
+ })
70
+
71
+ console.log(value)
72
+ console.log(window.DateKit.resolveLocale('en-us'))
73
+ })()
74
+ </script>
75
+ ```
76
+
77
+ After the CDN script loads, the browser build exposes `window.DateKit`.
78
+
79
+ Use one of the package manager commands above when your project has a build step. If you are working in Shopify, WordPress, or any browser-only template without compilation, use the browser bundle described in [docs/browser.md](docs/browser.md).
80
+
25
81
  ## Entrypoints
26
82
 
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 |
83
+ | Entrypoint | Main API | Purpose |
84
+ | --- | --- | --- |
85
+ | `@samline/date` | `createDateFormatter`, `getDate`, `parseDate`, `isValidDate` | shared core API |
86
+ | `@samline/date/vanilla` | same exports as root | utility wrapper for plain TypeScript or JavaScript |
87
+ | `@samline/date/react` | `useDateFormatter` | React hook with scoped formatter state |
88
+ | `@samline/date/vue` | `useDateFormatter` | Vue composable with reactive locale state |
89
+ | `@samline/date/svelte` | `createDateFormatterStore` | Svelte store-driven formatter API |
90
+ | `@samline/date/browser` | `DateKit` | browser global build for projects without a bundler |
35
91
 
36
92
  ## Quick Start
37
93
 
94
+ Use the one-shot helpers when you only need a single async operation.
95
+
38
96
  ```ts
39
97
  import { getDate } from '@samline/date'
40
98
 
@@ -45,22 +103,7 @@ const date = await getDate({
45
103
  })
46
104
  ```
47
105
 
48
- You can also inspect the effective locale before formatting:
49
-
50
- ```ts
51
- import { resolveLocale } from '@samline/date'
52
-
53
- resolveLocale('es-mx')
54
- // es-mx
55
-
56
- resolveLocale('en-us')
57
- // en
58
-
59
- resolveLocale('zz-zz')
60
- // null
61
- ```
62
-
63
- For repeated work with the same locale or invalid text, create one formatter instance and reuse it:
106
+ If you need repeated formatting, parsing, or locale changes, create one formatter instance and reuse it.
64
107
 
65
108
  ```ts
66
109
  import { createDateFormatter } from '@samline/date'
@@ -76,8 +119,37 @@ const date = formatter.getDate({
76
119
  })
77
120
  ```
78
121
 
122
+ You can also inspect the effective locale before formatting:
123
+
124
+ ```ts
125
+ import { getSupportedLocales, resolveLocale } from '@samline/date'
126
+
127
+ getSupportedLocales()
128
+ // ['en', 'es', 'es-mx', 'fr', 'pt', 'pt-br', 'de', 'it', 'ja']
129
+
130
+ resolveLocale('es-mx')
131
+ // es-mx
132
+
133
+ resolveLocale('en-us')
134
+ // en
135
+
136
+ resolveLocale('zz-zz')
137
+ // null
138
+ ```
139
+
79
140
  ## API
80
141
 
142
+ The shared root entrypoint exports:
143
+
144
+ - `createDateFormatter(config?)`
145
+ - `getDate(props?, config?)`
146
+ - `parseDate(props, config?)`
147
+ - `isValidDate(props, config?)`
148
+ - `getSupportedLocales()`
149
+ - `SUPPORTED_LOCALES`
150
+ - `resolveLocale(locale)`
151
+ - `isSupportedLocale(locale)`
152
+
81
153
  ### createDateFormatter
82
154
 
83
155
  ```ts
@@ -117,15 +189,19 @@ The formatter instance exposes:
117
189
  ### Locale helpers
118
190
 
119
191
  ```ts
192
+ SUPPORTED_LOCALES: readonly SupportedLocale[]
193
+ getSupportedLocales(): readonly SupportedLocale[]
120
194
  resolveLocale(locale: LocaleInput): SupportedLocale | null
121
195
  isSupportedLocale(locale: string): boolean
122
196
  ```
123
197
 
198
+ Use `SUPPORTED_LOCALES` or `getSupportedLocales()` when you need the list of locale keys exposed by the package.
199
+
124
200
  Use `resolveLocale` when you want to know the effective locale before creating a formatter or calling a helper.
125
201
 
126
202
  Use `isSupportedLocale` when you only need a boolean check after the package applies its exact-match and base-locale fallback rules.
127
203
 
128
- These helpers are also available in the browser build through `DateKit.resolveLocale(...)` and `DateKit.isSupportedLocale(...)`.
204
+ These helpers are also available in the browser build through `DateKit.getSupportedLocales()`, `DateKit.resolveLocale(...)`, and `DateKit.isSupportedLocale(...)`.
129
205
 
130
206
  ### One-shot helpers
131
207
 
@@ -137,6 +213,8 @@ isValidDate(props: DateParsingOptions, config?: DateFormatterConfig): Promise<bo
137
213
 
138
214
  Use these helpers when you only need a single operation and do not want to create a formatter instance manually.
139
215
 
216
+ All three helpers are async because they can load locale data before running the operation.
217
+
140
218
  | Helper | Returns | Use it when you need |
141
219
  | --- | --- | --- |
142
220
  | `getDate(...)` | formatted string | a final display value |
@@ -165,6 +243,8 @@ const valid = await isValidDate({
165
243
 
166
244
  They load the requested locale automatically and also use `strict: true` by default.
167
245
 
246
+ If you call `getDate()` without props, it returns the current date formatted with the default formatter settings.
247
+
168
248
  ### parseDate
169
249
 
170
250
  ```ts
@@ -180,6 +260,8 @@ Returns a structured result.
180
260
  - Valid parse: `isValid`, `date`, `iso`, `timestamp`, `format(output?)`
181
261
  - Invalid parse: `isValid: false`, `error`, and null date fields
182
262
 
263
+ This makes `parseDate` the right choice when you need validation details, an ISO value, a timestamp, or deferred formatting from the same parsed input.
264
+
183
265
  ### isValidDate
184
266
 
185
267
  ```ts
@@ -194,7 +276,7 @@ Returns a boolean when you only need validation without formatting.
194
276
 
195
277
  ## Supported Locales
196
278
 
197
- The package ships helper support for these locale keys:
279
+ The package ships helper support for these locale keys through `SUPPORTED_LOCALES` and `getSupportedLocales()`:
198
280
 
199
281
  - `en`
200
282
  - `es`
@@ -226,11 +308,11 @@ Use a simple locale like `fr` or `en` when the base language is enough. Use a re
226
308
 
227
309
  ## Documentation
228
310
 
229
- - [docs/vanilla.md](docs/vanilla.md)
230
- - [docs/browser.md](docs/browser.md)
231
- - [docs/react.md](docs/react.md)
232
- - [docs/vue.md](docs/vue.md)
233
- - [docs/svelte.md](docs/svelte.md)
311
+ - [docs/vanilla.md](docs/vanilla.md) for the shared API in plain TypeScript or JavaScript
312
+ - [docs/browser.md](docs/browser.md) for browser-only usage without a bundler
313
+ - [docs/react.md](docs/react.md) for the React hook entrypoint
314
+ - [docs/vue.md](docs/vue.md) for the Vue composable entrypoint
315
+ - [docs/svelte.md](docs/svelte.md) for the Svelte store entrypoint
234
316
 
235
317
  ## License
236
318