yugajs 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +117 -2
- package/dist/declarations/fields.d.ts +62 -0
- package/dist/declarations/locale.d.ts +73 -0
- package/dist/declarations/mutable.d.ts +7 -0
- package/dist/declarations/utils.d.ts +110 -0
- package/dist/extension/index.d.ts +10 -0
- package/dist/extension/index.js +1 -0
- package/dist/index.d.ts +556 -0
- package/dist/index.js +1 -0
- package/dist/plugin/index.d.ts +10 -0
- package/dist/plugin/index.js +1 -0
- package/package.json +115 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 michaelhazan
|
|
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
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Yuga
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
⏳ Yuga is an immutable date / time / locale library built on Temporal and Intl, a modern ESM alternative to Day.js (and Moment.js) with the same API.
|
|
4
|
+
|
|
5
|
+
_Yuga_ (युग) is Sanskrit for an age of time, and the name of a throw in the ancient Indian dice game.
|
|
6
|
+
|
|
7
|
+
It has the API of [Day.js](https://day.js.org) core (and the parts of its plugins most apps load), built on the native [`Temporal`](https://tc39.es/proposal-temporal/) API, with time zones and every `Intl` locale included. See [Day.js parity](./docs/dayjs-parity.md) for every API and difference.
|
|
8
|
+
|
|
9
|
+
The [reports page](https://vitest.igan.dev/yuga/) has the latest tests, benchmarks against Day.js, Moment.js and Luxon, and a map of the bundle size, from every push to `main`.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add yugajs
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Requirements
|
|
18
|
+
|
|
19
|
+
Yuga is ESM-only (`import`, no `require`) and uses the native [`Temporal`](https://tc39.es/proposal-temporal/) and `Intl` APIs. It doesn't bundle or detect polyfills: where an API is missing, load its polyfill once, before Yuga, at the entry of your app.
|
|
20
|
+
|
|
21
|
+
| API | Native in | Polyfill |
|
|
22
|
+
| ------------------------- | ------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------- |
|
|
23
|
+
| `Temporal` | Node.js 26+, Chromium 159, Firefox 159, Safari (WebKit) 27.2 | [`temporal-polyfill`](https://www.npmjs.com/package/temporal-polyfill) (`temporal-polyfill/global`) |
|
|
24
|
+
| `Intl.Locale#getWeekInfo` | Node.js 26+, Chromium 159, Firefox 159, Safari (WebKit) 27.2 | [`@formatjs/intl-locale`](https://www.npmjs.com/package/@formatjs/intl-locale) (`@formatjs/intl-locale/polyfill.js`) |
|
|
25
|
+
|
|
26
|
+
The browser versions are the ones the test suite runs on, earlier versions may have them too. Node.js 22 and 24, older browsers and other runtimes without them need the polyfills:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// The entry of your app, before anything imports yuga.
|
|
30
|
+
import "temporal-polyfill/global"; // Defines `globalThis.Temporal` only when it's missing.
|
|
31
|
+
import "@formatjs/intl-locale/polyfill.js"; // Only installs itself when `getWeekInfo` is missing.
|
|
32
|
+
import { yuga } from "yugajs";
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **`Temporal`** is needed by everything: parsing, time zones and every instance's `$Temporal`. Without it, importing works but creating an instance throws.
|
|
36
|
+
- **`Intl.Locale#getWeekInfo`** is only needed by locales built from a language tag (`yuga().locale("fr")`), which read their first day of the week from it. Engines with only the older `weekInfo` getter (Chrome 99–129, Safari 17) need the polyfill too. A registered custom locale with its own `weekStart` doesn't need it:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
yuga.locale("en-work", {
|
|
40
|
+
name: "en-work",
|
|
41
|
+
months: ["January", "February" /* … */],
|
|
42
|
+
weekdays: ["Sunday", "Monday" /* … */],
|
|
43
|
+
weekStart: 1, // Monday.
|
|
44
|
+
ordinal: (n) => `${n}.`,
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The unit tests also run in Chromium, Firefox and WebKit (`pnpm test:browser`, with Playwright) in CI.
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { yuga } from "yugajs";
|
|
54
|
+
|
|
55
|
+
// Parsing: now, dates, numbers, ISO 8601 and Day.js's loose strings, Temporal values.
|
|
56
|
+
yuga();
|
|
57
|
+
yuga("2024-05-15 10:20");
|
|
58
|
+
yuga("15/05/2024", "DD/MM/YYYY");
|
|
59
|
+
yuga("15 mai 2024", "D MMMM YYYY", "fr");
|
|
60
|
+
yuga("2024-05-15 10:20", { utc: true });
|
|
61
|
+
yuga(Temporal.Now.zonedDateTimeISO("Asia/Tokyo"));
|
|
62
|
+
|
|
63
|
+
// Get & set, immutable.
|
|
64
|
+
yuga("2024-01-31").month(1).date(); // 29
|
|
65
|
+
yuga().year(2030).hour(9);
|
|
66
|
+
|
|
67
|
+
// Manipulate.
|
|
68
|
+
yuga("2024-01-31").add(1, "month"); // 2024-02-29
|
|
69
|
+
yuga().subtract(2, "weeks").startOf("week"); // Sunday in `en`, Monday in `en-gb`
|
|
70
|
+
yuga().endOf("month");
|
|
71
|
+
|
|
72
|
+
// Query and diff.
|
|
73
|
+
yuga("2024-05-13").isSame("2024-05-12", "week"); // true
|
|
74
|
+
yuga("2024-05-15").diff("2024-01-31", "month", true); // 3.48…
|
|
75
|
+
|
|
76
|
+
// Format, in any Intl locale.
|
|
77
|
+
yuga("2024-05-15").format("dddd, MMMM Do YYYY"); // "Wednesday, May 15th 2024"
|
|
78
|
+
yuga("2024-05-15").locale("fr").format("dddd D MMMM"); // "mercredi 15 mai"
|
|
79
|
+
yuga.locale("de"); // The global locale of new instances.
|
|
80
|
+
|
|
81
|
+
// Time zones.
|
|
82
|
+
yuga.tz("2024-05-15 10:20", "Asia/Tokyo"); // 10:20 in Tokyo
|
|
83
|
+
yuga().tz("America/New_York").hour();
|
|
84
|
+
yuga().utc().format(); // "2024-05-15T10:20:00Z"
|
|
85
|
+
|
|
86
|
+
// Temporal.
|
|
87
|
+
yuga().toTemporal(); // Temporal.ZonedDateTime
|
|
88
|
+
yuga().toInstant();
|
|
89
|
+
yuga().toPlainDate();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
In Node.js, `console.log(yuga())` prints `Yuga<2024-05-15T10:20:30.040-04:00 America/New_York>`.
|
|
93
|
+
|
|
94
|
+
## Size
|
|
95
|
+
|
|
96
|
+
6.15 kB minified and brotlied, everything included (parsing formats, time zones, Intl locales). Day.js core is 2.8 kB, and 6.4 kB with the 9 plugins that add the same features, before locale files.
|
|
97
|
+
|
|
98
|
+
## Performance
|
|
99
|
+
|
|
100
|
+
Yuga keeps `Temporal` off the hot paths: the system's time zone and UTC are read off `Date`, other time zones use offsets cached between transitions, and unit fields are computed lazily on the first read. Operations per second on Node.js 26 (`pnpm bench`, higher is better):
|
|
101
|
+
|
|
102
|
+
| Operation | yuga | Day.js | Moment.js | Luxon |
|
|
103
|
+
| ------------------------------------- | ----: | -----: | --------: | ----: |
|
|
104
|
+
| `yuga(date)` | 30.7M | 15.2M | 6.2M | 4.4M |
|
|
105
|
+
| `yuga(isoString)` | 7.4M | 5.8M | 306K | 742K |
|
|
106
|
+
| `yuga(value, "YYYY-MM-DD hh:mm A")` | 2.5M | 785K | 449K | 208K |
|
|
107
|
+
| `.year()`, `.month()`, `.date()` | 36.6M | 25.5M | 17.8M | 34.9M |
|
|
108
|
+
| `.month(0)` | 6.8M | 555K | 4.8M | 3.0M |
|
|
109
|
+
| `.add(1, "M")` | 7.2M | 567K | 2.4M | 786K |
|
|
110
|
+
| `.startOf("month")` | 5.3M | 3.3M | 3.4M | 2.1M |
|
|
111
|
+
| `.isBefore(other)` | 37.5M | 3.1M | 23.2M | 11.5M |
|
|
112
|
+
| `.isSame(other, "month")` | 4.9M | 810K | 1.9M | 272K |
|
|
113
|
+
| `.diff(other, "M")` | 3.2M | 229K | 665K | 307K |
|
|
114
|
+
| `.format("dddd, MMMM D YYYY h:mm A")` | 9.6M | 652K | 2.8M | 1.4M |
|
|
115
|
+
| `.tz("Europe/Berlin")` | 32.2M | 40K | — | 392K |
|
|
116
|
+
| `.add(1, "d")` in `Europe/Berlin` | 16.9M | 1.9M | — | 166K |
|
|
117
|
+
|
|
118
|
+
Day.js runs with its `customParseFormat`, `utc`, `timezone`, `advancedFormat`, `weekOfYear`, `isoWeek`, `quarterOfYear`, `isSameOrBefore` and `isSameOrAfter` plugins (Moment.js without `moment-timezone`). Every function and getter is benchmarked in `benchmarks/`, by area, in several scenarios (time zones, units, inputs, locales, invalid instances, cold caches). The [reports page](https://vitest.igan.dev/yuga/) has all of them, from the latest run on `main`.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import "yugajs";
|
|
2
|
+
|
|
3
|
+
declare module "yugajs" {
|
|
4
|
+
export namespace yuga {
|
|
5
|
+
export namespace Fields {
|
|
6
|
+
/**
|
|
7
|
+
* The marker of the class, made to check if a type provided is an instance of Yuga.
|
|
8
|
+
*
|
|
9
|
+
* @constant true
|
|
10
|
+
*/
|
|
11
|
+
export type Marker = "$isYugaClass";
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* A boolean that marks the Yuga class and valid (not "Invalid Date").
|
|
15
|
+
*/
|
|
16
|
+
export type Valid = "$valid";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Milliseconds since the Unix Epoch, `NaN` when invalid (`yuga(null)`, "Invalid Date").
|
|
20
|
+
*/
|
|
21
|
+
export type Epoch = "$epoch";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The IANA time zone, `undefined` is the system's (followed the same as `Date` does).
|
|
25
|
+
*/
|
|
26
|
+
export type Zone = "$zone";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The name of the locale (`"en"`), the global locale when the instance was created.
|
|
30
|
+
*/
|
|
31
|
+
export type Locale = "$locale";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The zoned date time of `$epoch` in `$zone`, `null` when invalid. Built on first access and
|
|
35
|
+
* cached.
|
|
36
|
+
*/
|
|
37
|
+
export type Temporal = "$Temporal";
|
|
38
|
+
|
|
39
|
+
export type Values = Record<Unit.Short, number>;
|
|
40
|
+
|
|
41
|
+
export type Mutators = Record<Unit.Long, (value: number) => Yuga>;
|
|
42
|
+
|
|
43
|
+
export type Accessors = Record<Unit.Long, () => number>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// NOTE: `D` (date) references the month date, while `d` (day) references the weekday (0 is
|
|
47
|
+
// Sunday). `M` is 0-indexed. All of them are `NaN` when invalid.
|
|
48
|
+
export interface Fields extends Readonly<
|
|
49
|
+
Record<Fields.Marker, true> &
|
|
50
|
+
Record<Fields.Epoch, number> &
|
|
51
|
+
Record<Fields.Zone, string | undefined> &
|
|
52
|
+
Record<Fields.Locale, string> &
|
|
53
|
+
Record<Fields.Temporal, globalThis.Temporal.ZonedDateTime | null> &
|
|
54
|
+
Record<Fields.Valid, boolean> &
|
|
55
|
+
Fields.Values &
|
|
56
|
+
Fields.Mutators &
|
|
57
|
+
Fields.Accessors
|
|
58
|
+
> {}
|
|
59
|
+
|
|
60
|
+
export interface Yuga extends Fields {}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import "yugajs";
|
|
2
|
+
|
|
3
|
+
declare module "yugajs" {
|
|
4
|
+
export namespace yuga {
|
|
5
|
+
/**
|
|
6
|
+
* A locale, the same shape as Day.js's locale objects (the parts `format` uses).
|
|
7
|
+
*
|
|
8
|
+
* Locales are built out of `Intl` for any BCP 47 tag, `en` is built in. Optional names fall
|
|
9
|
+
* back to slicing the long ones, the same as Day.js (`"Jan"`, `"Sun"`, `"Su"`).
|
|
10
|
+
*/
|
|
11
|
+
export interface Locale {
|
|
12
|
+
/**
|
|
13
|
+
* The lowercase name the locale is registered under (`"en"`, `"en-gb"`).
|
|
14
|
+
*/
|
|
15
|
+
name: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Month names, January first (`MMMM`).
|
|
19
|
+
*/
|
|
20
|
+
months: readonly string[];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Short month names (`MMM`), the first 3 letters of `months` by default.
|
|
24
|
+
*/
|
|
25
|
+
monthsShort?: readonly string[];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Weekday names, Sunday first (`dddd`).
|
|
29
|
+
*/
|
|
30
|
+
weekdays: readonly string[];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Short weekday names (`ddd`), the first 3 letters of `weekdays` by default.
|
|
34
|
+
*/
|
|
35
|
+
weekdaysShort?: readonly string[];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Minimal weekday names (`dd`), the first 2 letters of `weekdays` by default.
|
|
39
|
+
*/
|
|
40
|
+
weekdaysMin?: readonly string[];
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The first day of the week, 0 is Sunday (the default) and 6 is Saturday.
|
|
44
|
+
*/
|
|
45
|
+
weekStart?: number;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The days of the new year its first week needs (`w`), 1 (the week of January 1st) by
|
|
49
|
+
* default. ISO weeks need 4 (the week of January 4th).
|
|
50
|
+
*
|
|
51
|
+
* NOTE: Day.js's `yearStart` (`weekOfYear` plugin), Moment.js's `7 + dow - doy`.
|
|
52
|
+
*/
|
|
53
|
+
yearStart?: number;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The meridiem of an hour (`A`, and `a` when `isLowercase`), `"AM"` / `"PM"` by default.
|
|
57
|
+
*/
|
|
58
|
+
meridiem?: (hour: number, minute: number, isLowercase: boolean) => string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The ordinal of a number (`Do`), `"1st"` in English.
|
|
62
|
+
*
|
|
63
|
+
* NOTE: Day.js's `en` wraps it in brackets (`"[1st]"`), those are removed when formatting.
|
|
64
|
+
*/
|
|
65
|
+
ordinal: (number: number, token?: string) => string | number;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* What invalid instances format to, `"Invalid Date"` by default.
|
|
69
|
+
*/
|
|
70
|
+
invalidDate?: string;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import "yugajs";
|
|
2
|
+
|
|
3
|
+
declare module "yugajs" {
|
|
4
|
+
export namespace yuga {
|
|
5
|
+
export interface Factory {
|
|
6
|
+
$: Utils;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Utils {
|
|
10
|
+
/**
|
|
11
|
+
* Converts any unit into the short variation of it.
|
|
12
|
+
*/
|
|
13
|
+
readonly short: {
|
|
14
|
+
ms: "ms";
|
|
15
|
+
millisecond: "ms";
|
|
16
|
+
milliseconds: "ms";
|
|
17
|
+
s: "s";
|
|
18
|
+
second: "s";
|
|
19
|
+
seconds: "s";
|
|
20
|
+
m: "m";
|
|
21
|
+
minute: "m";
|
|
22
|
+
minutes: "m";
|
|
23
|
+
h: "h";
|
|
24
|
+
hour: "h";
|
|
25
|
+
hours: "h";
|
|
26
|
+
d: "d";
|
|
27
|
+
day: "d";
|
|
28
|
+
days: "d";
|
|
29
|
+
w: "w";
|
|
30
|
+
week: "w";
|
|
31
|
+
weeks: "w";
|
|
32
|
+
D: "D";
|
|
33
|
+
date: "D";
|
|
34
|
+
dates: "D";
|
|
35
|
+
M: "M";
|
|
36
|
+
month: "M";
|
|
37
|
+
months: "M";
|
|
38
|
+
q: "q";
|
|
39
|
+
quarter: "q";
|
|
40
|
+
quarters: "q";
|
|
41
|
+
y: "y";
|
|
42
|
+
year: "y";
|
|
43
|
+
years: "y";
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The index of each settable unit in the wall-clock values `[y, M, D, h, m, s, ms]`, the same
|
|
48
|
+
* order as the `Date` constructor's arguments (the month is 0-indexed).
|
|
49
|
+
*/
|
|
50
|
+
readonly wall: {
|
|
51
|
+
y: 0;
|
|
52
|
+
M: 1;
|
|
53
|
+
D: 2;
|
|
54
|
+
h: 3;
|
|
55
|
+
m: 4;
|
|
56
|
+
s: 5;
|
|
57
|
+
ms: 6;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The regex of each format parsing token, every token has a single capturing group. The order
|
|
62
|
+
* of the keys is the order of the alternation, longer tokens come first (`YYYY` before
|
|
63
|
+
* `YY`).
|
|
64
|
+
*
|
|
65
|
+
* NOTE: Formats are compiled once, changes only apply to formats not used yet.
|
|
66
|
+
*/
|
|
67
|
+
readonly tokensMap: Readonly<Record<string, string>>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The default display format, ISO 8601 with the local offset (the same as Day.js).
|
|
71
|
+
*
|
|
72
|
+
* @constant "YYYY-MM-DDTHH:mm:ssZ"
|
|
73
|
+
*/
|
|
74
|
+
readonly format: string;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The time zone an instance of the config is in, `undefined` is the system's.
|
|
78
|
+
*
|
|
79
|
+
* `config.zone` comes first (validated, a `RangeError` when unknown), then Yuga instances and
|
|
80
|
+
* `Temporal.ZonedDateTime`s keep theirs, and `config.utc` is `"UTC"`.
|
|
81
|
+
*/
|
|
82
|
+
zone(config: Config): string | undefined;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Extracts the epoch milliseconds out of the provided config, `NaN` when invalid. Wall-clock
|
|
86
|
+
* values are read in `timeZone` (see `zone`).
|
|
87
|
+
*
|
|
88
|
+
* - `undefined` is now, `null` is invalid, numbers are epoch milliseconds.
|
|
89
|
+
* - Yuga instances, `Temporal.ZonedDateTime` and `Temporal.Instant` keep their epoch.
|
|
90
|
+
* - `Temporal.PlainDateTime` and `Temporal.PlainDate` are wall-clock values.
|
|
91
|
+
* - Strings with a format use it (Day.js's `customParseFormat`, missing fields default to the
|
|
92
|
+
* current year, the 1st and midnight, mismatches are invalid). Strings without one use
|
|
93
|
+
* Day.js core's loose `YYYY-MM-DD HH:mm:ss.SSS` regex as wall-clock values (unless they end
|
|
94
|
+
* in `Z` or an offset). Years 0000-0099 stay as is.
|
|
95
|
+
* - Everything else (dates, other strings) is read the same as the `Date` constructor.
|
|
96
|
+
*/
|
|
97
|
+
parse(config: Config, timeZone: string | undefined): number;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Computes every cached unit field (`y M D d h m s ms`, then the UTC offset in minutes) from
|
|
101
|
+
* the instance's `$epoch` in its `$zone`, all of them are `NaN` when invalid. Instances call
|
|
102
|
+
* it on the first read of any of them, it returns the instance.
|
|
103
|
+
*
|
|
104
|
+
* NOTE: The system's time zone and UTC are read off a `Date`, other time zones add their
|
|
105
|
+
* offset (cached between transitions) without building `$Temporal`.
|
|
106
|
+
*/
|
|
107
|
+
cascade(yuga: Mutable): Yuga;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { yuga as yuga$1 } from "yugajs";
|
|
2
|
+
declare module "yugajs" {
|
|
3
|
+
namespace yuga {
|
|
4
|
+
/**
|
|
5
|
+
* An extension is used to extend the existing yuga structure.
|
|
6
|
+
*/
|
|
7
|
+
export type Extension<TConfig> = (extensionConfig: TConfig, yugaClass: yuga.YugaClass) => void;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare const defineExtension: <TConfig>(extension: yuga$1.Extension<TConfig>) => yuga$1.Extension<TConfig>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>e;export{e as defineExtension};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
declare const yuga: yuga.Factory;
|
|
2
|
+
export declare class Yuga implements yuga.Yuga {
|
|
3
|
+
readonly $isYugaClass: true;
|
|
4
|
+
get $valid(): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Milliseconds since the Unix Epoch, `NaN` when invalid. Along with `$zone` it is the whole state
|
|
7
|
+
* of the instance, the unit fields below are derived from both by `yuga.$.cascade`.
|
|
8
|
+
*/
|
|
9
|
+
readonly $epoch: number;
|
|
10
|
+
/**
|
|
11
|
+
* The IANA time zone of the instance, `undefined` is the system's (which `Date` follows).
|
|
12
|
+
*/
|
|
13
|
+
readonly $zone: string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* The name of the instance's locale (`"en"`), the global locale when it was created.
|
|
16
|
+
*/
|
|
17
|
+
readonly $locale: string;
|
|
18
|
+
/**
|
|
19
|
+
* The cache of `$Temporal`, `undefined` until it is first built.
|
|
20
|
+
*/
|
|
21
|
+
private readonly $zoned;
|
|
22
|
+
/**
|
|
23
|
+
* The zoned date time of the instance, `null` when invalid.
|
|
24
|
+
*
|
|
25
|
+
* NOTE: Built on first access and cached, the hot paths (parsing, getters, comparisons and most
|
|
26
|
+
* setters in the system's time zone or UTC) don't need it.
|
|
27
|
+
*/
|
|
28
|
+
get $Temporal(): Temporal.ZonedDateTime | null;
|
|
29
|
+
readonly ms: number;
|
|
30
|
+
readonly s: number;
|
|
31
|
+
readonly m: number;
|
|
32
|
+
readonly h: number;
|
|
33
|
+
readonly d: number;
|
|
34
|
+
readonly D: number;
|
|
35
|
+
readonly M: number;
|
|
36
|
+
readonly y: number;
|
|
37
|
+
readonly millisecond: yuga.Yuga["millisecond"];
|
|
38
|
+
readonly second: yuga.Yuga["second"];
|
|
39
|
+
readonly minute: yuga.Yuga["minute"];
|
|
40
|
+
readonly hour: yuga.Yuga["hour"];
|
|
41
|
+
readonly day: yuga.Yuga["day"];
|
|
42
|
+
readonly week: yuga.Yuga["week"];
|
|
43
|
+
readonly date: yuga.Yuga["date"];
|
|
44
|
+
readonly month: yuga.Yuga["month"];
|
|
45
|
+
readonly quarter: yuga.Yuga["quarter"];
|
|
46
|
+
readonly year: yuga.Yuga["year"];
|
|
47
|
+
/**
|
|
48
|
+
* NOTE: The quarter and week are computed on access since they are rarely used.
|
|
49
|
+
*/
|
|
50
|
+
get q(): number;
|
|
51
|
+
/**
|
|
52
|
+
* The ISO week of the year (weeks start on Monday, the first week contains January 4th).
|
|
53
|
+
*/
|
|
54
|
+
get w(): number;
|
|
55
|
+
constructor(config: yuga.Config);
|
|
56
|
+
/**
|
|
57
|
+
* Creates an instance straight from epoch milliseconds (`NaN` for invalid) in a time zone
|
|
58
|
+
* (`undefined` for the system's), skipping parsing. This is how every method that returns a new
|
|
59
|
+
* instance creates it.
|
|
60
|
+
*
|
|
61
|
+
* @param zoned - The zoned date time of the same epoch and time zone if one was already built, it
|
|
62
|
+
* becomes the `$Temporal` cache.
|
|
63
|
+
* @param localeName - The name of a registered locale, the global locale by default.
|
|
64
|
+
*/
|
|
65
|
+
static create(epoch: number, zone: string | undefined, zoned?: Temporal.ZonedDateTime | null, localeName?: string): yuga.Yuga;
|
|
66
|
+
clone(): yuga.Yuga;
|
|
67
|
+
get(): string;
|
|
68
|
+
get(unit: yuga.Unit): number;
|
|
69
|
+
set(uglyUnit: yuga.Unit, value: number): yuga.Yuga;
|
|
70
|
+
add(value: number, unit?: yuga.Unit): yuga.Yuga;
|
|
71
|
+
subtract(value: number, unit?: yuga.Unit): yuga.Yuga;
|
|
72
|
+
startOf(unit: yuga.Unit | "isoWeek"): yuga.Yuga;
|
|
73
|
+
endOf(unit: yuga.Unit | "isoWeek"): yuga.Yuga;
|
|
74
|
+
isBefore(that: yuga.Parsable, unit?: yuga.Unit | "isoWeek"): boolean;
|
|
75
|
+
isSameOrBefore(that: yuga.Parsable, unit?: yuga.Unit | "isoWeek"): boolean;
|
|
76
|
+
isSame(that: yuga.Parsable, unit?: yuga.Unit | "isoWeek"): boolean;
|
|
77
|
+
isSameOrAfter(that: yuga.Parsable, unit?: yuga.Unit | "isoWeek"): boolean;
|
|
78
|
+
isAfter(that: yuga.Parsable, unit?: yuga.Unit | "isoWeek"): boolean;
|
|
79
|
+
isValid(): boolean;
|
|
80
|
+
diff(that?: yuga.Parsable, unit?: yuga.Unit, options?: boolean | yuga.DiffOptions): number;
|
|
81
|
+
format(template?: string): string;
|
|
82
|
+
locale(): string;
|
|
83
|
+
locale(preset: string | yuga.Locale, object?: yuga.Locale): yuga.Yuga;
|
|
84
|
+
tz(): string;
|
|
85
|
+
tz(zone: string, keepLocalTime?: boolean): yuga.Yuga;
|
|
86
|
+
utc(keepLocalTime?: boolean): yuga.Yuga;
|
|
87
|
+
local(): yuga.Yuga;
|
|
88
|
+
daysInMonth(): number;
|
|
89
|
+
utcOffset(): number;
|
|
90
|
+
unix(): number;
|
|
91
|
+
toDate(): Date;
|
|
92
|
+
toTemporal(): Temporal.ZonedDateTime;
|
|
93
|
+
toInstant(): Temporal.Instant;
|
|
94
|
+
toPlainDate(): Temporal.PlainDate;
|
|
95
|
+
toJSON(): string | null;
|
|
96
|
+
toISOString(): string;
|
|
97
|
+
toString(): string;
|
|
98
|
+
toUTCString(): string;
|
|
99
|
+
valueOf(): number;
|
|
100
|
+
}
|
|
101
|
+
declare namespace yuga {
|
|
102
|
+
export interface Yuga {
|
|
103
|
+
/**
|
|
104
|
+
* Get the current millisecond.
|
|
105
|
+
*/
|
|
106
|
+
millisecond(): number;
|
|
107
|
+
millisecond(value: number): yuga.Yuga;
|
|
108
|
+
second(): number;
|
|
109
|
+
second(value: number): yuga.Yuga;
|
|
110
|
+
minute(): number;
|
|
111
|
+
minute(value: number): yuga.Yuga;
|
|
112
|
+
hour(): number;
|
|
113
|
+
hour(value: number): yuga.Yuga;
|
|
114
|
+
day(): number;
|
|
115
|
+
day(value: number): yuga.Yuga;
|
|
116
|
+
week(): number;
|
|
117
|
+
week(value: number): yuga.Yuga;
|
|
118
|
+
date(): number;
|
|
119
|
+
date(value: number): yuga.Yuga;
|
|
120
|
+
month(): number;
|
|
121
|
+
month(value: number): yuga.Yuga;
|
|
122
|
+
quarter(): number;
|
|
123
|
+
quarter(value: number): yuga.Yuga;
|
|
124
|
+
year(): number;
|
|
125
|
+
year(value: number): yuga.Yuga;
|
|
126
|
+
/**
|
|
127
|
+
* Creates a clone of the current `Yuga` class.
|
|
128
|
+
*
|
|
129
|
+
* NOTE: Yuga doesn't support mutability, this function is ported from `Moment.js` & `Day.js`.
|
|
130
|
+
* While `Day.js` does default to immutability, plugins such as `badMutable` allow it, `Yuga`
|
|
131
|
+
* does not provide such plugins.
|
|
132
|
+
*/
|
|
133
|
+
clone(): Yuga;
|
|
134
|
+
/**
|
|
135
|
+
* Get the string value of the class.
|
|
136
|
+
*
|
|
137
|
+
* Equivalent to yuga().toString()
|
|
138
|
+
*/
|
|
139
|
+
get(): string;
|
|
140
|
+
/**
|
|
141
|
+
* Get the numeric value of the provided unit.
|
|
142
|
+
*
|
|
143
|
+
* NOTE: Equivalent to yuga()[unit]().
|
|
144
|
+
*
|
|
145
|
+
* @param { Unit } unit - The unit of the desired value to get.
|
|
146
|
+
*/
|
|
147
|
+
get(unit: Unit): number;
|
|
148
|
+
/**
|
|
149
|
+
* Set the numeric value of the provided unit.
|
|
150
|
+
*
|
|
151
|
+
* NOTE: Equivalent to `yuga()[unit](value)`.
|
|
152
|
+
*
|
|
153
|
+
* @param { Unit } unit - The unit of the desired value to set.
|
|
154
|
+
* @param { Number } value - The new value.
|
|
155
|
+
*/
|
|
156
|
+
set(unit: Unit, value: number): Yuga;
|
|
157
|
+
/**
|
|
158
|
+
* Adds an amount of a unit (milliseconds by default), Day.js's `add`.
|
|
159
|
+
*
|
|
160
|
+
* - `y`, `Q` and `M` move the wall clock and clamp the date to the end of the month (Jan 31 + 1
|
|
161
|
+
* month is Feb 29 in 2024), fractions are truncated.
|
|
162
|
+
* - `w` and `d` (or `D`) move the wall clock by whole days (fractions are rounded), keeping the
|
|
163
|
+
* time of day across DST.
|
|
164
|
+
* - `h`, `m`, `s` and `ms` are absolute time (an hour across DST is 60 minutes later).
|
|
165
|
+
*
|
|
166
|
+
* NOTE: Units are case-insensitive. Lowercase `q` is a quarter (Day.js adds milliseconds).
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* yuga("2024-01-31").add(1, "month"); // 2024-02-29
|
|
170
|
+
*/
|
|
171
|
+
add(value: number, unit?: Unit): Yuga;
|
|
172
|
+
/**
|
|
173
|
+
* Subtracts an amount of a unit, `add` with the value negated.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* yuga("2024-03-31").subtract(1, "M"); // 2024-02-29
|
|
177
|
+
*/
|
|
178
|
+
subtract(value: number, unit?: Unit): Yuga;
|
|
179
|
+
/**
|
|
180
|
+
* The start of a unit in the instance's time zone (`startOf("month")` is the 1st at 00:00).
|
|
181
|
+
*
|
|
182
|
+
* - `"week"` starts on the `weekStart` of the instance's locale (Sunday for `en`, Monday for
|
|
183
|
+
* `en-gb`), `"isoWeek"` always starts on Monday.
|
|
184
|
+
* - Calendar units are wall-clock midnights, a midnight the time zone skips moves forward.
|
|
185
|
+
* - `h`, `m` and `s` keep the instance's offset (the repeated hour of a DST fall back starts at
|
|
186
|
+
* its own 01:00, the same as Moment.js).
|
|
187
|
+
*
|
|
188
|
+
* NOTE: Milliseconds, unknown and missing units return the same moment.
|
|
189
|
+
*/
|
|
190
|
+
startOf(unit: Unit | "isoWeek"): Yuga;
|
|
191
|
+
/**
|
|
192
|
+
* The end of a unit, the start of the next one minus 1 ms (`endOf("day")` is 23:59:59.999).
|
|
193
|
+
*
|
|
194
|
+
* @see startOf
|
|
195
|
+
*/
|
|
196
|
+
endOf(unit: Unit | "isoWeek"): Yuga;
|
|
197
|
+
/**
|
|
198
|
+
* Checks if the instance is before a parsable, with a unit: if the end of the instance's unit
|
|
199
|
+
* is before it (`isBefore(x, "day")` is true when `x` is on a later day).
|
|
200
|
+
*
|
|
201
|
+
* NOTE: The unit is in the instance's time zone and locale (see `startOf`).
|
|
202
|
+
*/
|
|
203
|
+
isBefore(that: Parsable, unit?: Unit | "isoWeek"): boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Checks if the instance is the same as or before a parsable (by unit, see `isBefore`).
|
|
206
|
+
*/
|
|
207
|
+
isSameOrBefore(that: Parsable, unit?: Unit | "isoWeek"): boolean;
|
|
208
|
+
/**
|
|
209
|
+
* Checks if the instance is the same moment as a parsable, with a unit: if the parsable is
|
|
210
|
+
* within the instance's unit (`isSame(x, "week")` is the same Sunday to Saturday week in
|
|
211
|
+
* `en`).
|
|
212
|
+
*
|
|
213
|
+
* NOTE: The unit is in the instance's time zone and locale (see `startOf`).
|
|
214
|
+
*/
|
|
215
|
+
isSame(that: Parsable, unit?: Unit | "isoWeek"): boolean;
|
|
216
|
+
/**
|
|
217
|
+
* Checks if the instance is the same as or after a parsable (by unit, see `isAfter`).
|
|
218
|
+
*/
|
|
219
|
+
isSameOrAfter(that: Parsable, unit?: Unit | "isoWeek"): boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Checks if the instance is after a parsable, with a unit: if the parsable is before the start
|
|
222
|
+
* of the instance's unit.
|
|
223
|
+
*
|
|
224
|
+
* NOTE: The unit is in the instance's time zone and locale (see `startOf`).
|
|
225
|
+
*/
|
|
226
|
+
isAfter(that: Parsable, unit?: Unit | "isoWeek"): boolean;
|
|
227
|
+
/**
|
|
228
|
+
* Get the validity of the class, opposite of "Invalid Date" in the JS `Date` object.
|
|
229
|
+
*
|
|
230
|
+
* NOTE: Equivalent to `!isNaN(yuga().$epoch)`.
|
|
231
|
+
*/
|
|
232
|
+
isValid(): boolean;
|
|
233
|
+
/**
|
|
234
|
+
* The difference from a parsable (now when omitted) to the instance in a unit, milliseconds by
|
|
235
|
+
* default. `NaN` when either is invalid (for every unit, Day.js gives `0` for months).
|
|
236
|
+
*
|
|
237
|
+
* By default it matches Day.js: months, quarters and years use Moment.js's month difference
|
|
238
|
+
* (`2024-05-15` to `2024-01-31` is 3.48 months), days and weeks discount the change of the UTC
|
|
239
|
+
* offset, and the result is truncated toward zero.
|
|
240
|
+
*
|
|
241
|
+
* NOTE: The parsable is compared in the instance's time zone, the same as Moment.js.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* yuga("2024-05-15").diff("2024-01-31", "month", true); // 3.48…
|
|
245
|
+
*
|
|
246
|
+
* @param options - `true` keeps the fraction (Day.js's `float`), see `DiffOptions`.
|
|
247
|
+
*/
|
|
248
|
+
diff(that?: Parsable, unit?: Unit, options?: boolean | DiffOptions): number;
|
|
249
|
+
/**
|
|
250
|
+
* Formats with a template, Day.js's `format` and its `advancedFormat` tokens, in the instance's
|
|
251
|
+
* locale. Text in brackets is escaped (`"[Today is] dddd"`).
|
|
252
|
+
*
|
|
253
|
+
* - `YY YYYY M MM MMM MMMM D DD Do d dd ddd dddd H HH h hh a A m mm s ss SSS Z ZZ Q`
|
|
254
|
+
* - `w ww` the week of the locale (its `weekStart` and `yearStart`), `W WW` the ISO week.
|
|
255
|
+
*
|
|
256
|
+
* NOTE: With no template it is ISO 8601 with the offset (`"2024-05-15T06:00:00-04:00"`, and
|
|
257
|
+
* `"…Z"` in UTC), invalid instances are the locale's `invalidDate` (`"Invalid Date"`).
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* yuga("2024-05-15").format("dddd, MMMM Do YYYY"); // "Wednesday, May 15th 2024"
|
|
261
|
+
*/
|
|
262
|
+
format(template?: string): string;
|
|
263
|
+
/**
|
|
264
|
+
* Get the name of the instance's locale (`"en"`).
|
|
265
|
+
*/
|
|
266
|
+
locale(): string;
|
|
267
|
+
/**
|
|
268
|
+
* A clone in another locale, Day.js's `locale`. Names are any BCP 47 tag (built out of `Intl`)
|
|
269
|
+
* or registered locales, an object registers it under the name. An unknown locale keeps the
|
|
270
|
+
* current one.
|
|
271
|
+
*
|
|
272
|
+
* NOTE: The global locale stays as is, see `yuga.locale`.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* yuga("2024-05-15").locale("fr").format("dddd D MMMM"); // "mercredi 15 mai"
|
|
276
|
+
*/
|
|
277
|
+
locale(preset: string | Locale, object?: Locale): Yuga;
|
|
278
|
+
/**
|
|
279
|
+
* Get the IANA time zone of the instance (the system's when it has none of its own).
|
|
280
|
+
*/
|
|
281
|
+
tz(): string;
|
|
282
|
+
/**
|
|
283
|
+
* The same moment in another time zone (an IANA name, or an offset such as `"+05:30"`).
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* yuga("2024-05-15T10:00:00Z").tz("Asia/Tokyo").hour(); // 19
|
|
287
|
+
*
|
|
288
|
+
* @param keepLocalTime - Keep the wall clock rather than the moment (`10:00` in New York
|
|
289
|
+
* becomes `10:00` in Tokyo).
|
|
290
|
+
* @throws { RangeError } When the time zone is unknown.
|
|
291
|
+
*/
|
|
292
|
+
tz(zone: string, keepLocalTime?: boolean): Yuga;
|
|
293
|
+
/**
|
|
294
|
+
* The same moment in UTC, `tz("UTC")`.
|
|
295
|
+
*/
|
|
296
|
+
utc(keepLocalTime?: boolean): Yuga;
|
|
297
|
+
/**
|
|
298
|
+
* The same moment in the system's time zone.
|
|
299
|
+
*/
|
|
300
|
+
local(): Yuga;
|
|
301
|
+
/**
|
|
302
|
+
* Get the number of days in the current month.
|
|
303
|
+
*
|
|
304
|
+
* NOTE: `NaN` when invalid.
|
|
305
|
+
*/
|
|
306
|
+
daysInMonth(): number;
|
|
307
|
+
/**
|
|
308
|
+
* Get the offset from UTC in minutes, rounded to 15 minutes like Day.js (`-240` for New York in
|
|
309
|
+
* summer).
|
|
310
|
+
*
|
|
311
|
+
* NOTE: `NaN` when invalid.
|
|
312
|
+
*/
|
|
313
|
+
utcOffset(): number;
|
|
314
|
+
/**
|
|
315
|
+
* Get the number of seconds since the Unix Epoch, floored.
|
|
316
|
+
*
|
|
317
|
+
* NOTE: Equivalent to `Math.floor(yuga().valueOf() / 1000)`.
|
|
318
|
+
*/
|
|
319
|
+
unix(): number;
|
|
320
|
+
/**
|
|
321
|
+
* Formats to a Date class equivalent value of the class.
|
|
322
|
+
*/
|
|
323
|
+
toDate(): Date;
|
|
324
|
+
/**
|
|
325
|
+
* The `Temporal.ZonedDateTime` of the instance (its time zone, the system's when it has none).
|
|
326
|
+
*
|
|
327
|
+
* NOTE: The same object as `$Temporal`, built once. Throws a `RangeError` when invalid.
|
|
328
|
+
*/
|
|
329
|
+
toTemporal(): Temporal.ZonedDateTime;
|
|
330
|
+
/**
|
|
331
|
+
* The `Temporal.Instant` of the instance, throws a `RangeError` when invalid.
|
|
332
|
+
*/
|
|
333
|
+
toInstant(): Temporal.Instant;
|
|
334
|
+
/**
|
|
335
|
+
* The `Temporal.PlainDate` of the instance's date in its time zone, throws a `RangeError` when
|
|
336
|
+
* invalid.
|
|
337
|
+
*/
|
|
338
|
+
toPlainDate(): Temporal.PlainDate;
|
|
339
|
+
/**
|
|
340
|
+
* Serializes to the ISO 8601 string, or `null` when invalid (used by `JSON.stringify`).
|
|
341
|
+
*/
|
|
342
|
+
toJSON(): string | null;
|
|
343
|
+
/**
|
|
344
|
+
* Formats to an ISO 8601 string in UTC with milliseconds (`"2024-05-15T10:00:00.000Z"`).
|
|
345
|
+
*
|
|
346
|
+
* NOTE: Equivalent to `yuga().toDate().toISOString()`, throws a `RangeError` when invalid.
|
|
347
|
+
*/
|
|
348
|
+
toISOString(): string;
|
|
349
|
+
/**
|
|
350
|
+
* Formats to a string in the instance's time zone (`"Wed May 15 2024 06:00:00 GMT-0400 (Eastern
|
|
351
|
+
* Daylight Time)"`), "Invalid Date" when invalid.
|
|
352
|
+
*
|
|
353
|
+
* NOTE: Equivalent to `yuga().toDate().toString()` in the system's time zone. Day.js returns
|
|
354
|
+
* the UTC string instead, see `toUTCString`.
|
|
355
|
+
*/
|
|
356
|
+
toString(): string;
|
|
357
|
+
/**
|
|
358
|
+
* Formats to a UTC string (`"Wed, 15 May 2024 10:00:00 GMT"`), "Invalid Date" when invalid.
|
|
359
|
+
*
|
|
360
|
+
* NOTE: Equivalent to `yuga().toDate().toUTCString()` and Day.js's `toString()`.
|
|
361
|
+
*/
|
|
362
|
+
toUTCString(): string;
|
|
363
|
+
/**
|
|
364
|
+
* Get the number of milliseconds since the Unix Epoch of the Yuga class.
|
|
365
|
+
*
|
|
366
|
+
* NOTE: Equivalent to `yuga().toDate().valueOf()`, `NaN` when invalid.
|
|
367
|
+
*/
|
|
368
|
+
valueOf(): number;
|
|
369
|
+
}
|
|
370
|
+
export type YugaClass = new (config: yuga.Config) => yuga.Yuga;
|
|
371
|
+
export interface Config {
|
|
372
|
+
parsable?: Parsable;
|
|
373
|
+
/**
|
|
374
|
+
* The format of a string parsable (`"DD/MM/YYYY HH:mm"`), see `yuga.$.parse`.
|
|
375
|
+
*/
|
|
376
|
+
format?: string;
|
|
377
|
+
/**
|
|
378
|
+
* Use the `"UTC"` time zone instead of the system's, parsables that have a time zone of their
|
|
379
|
+
* own (Yuga instances, `Temporal.ZonedDateTime`) keep it.
|
|
380
|
+
*/
|
|
381
|
+
utc?: boolean;
|
|
382
|
+
/**
|
|
383
|
+
* The time zone of the instance (an IANA name, or an offset such as `"+05:30"`), it takes
|
|
384
|
+
* precedence over `utc` and the time zone of the parsable. Wall-clock values are read in it.
|
|
385
|
+
*/
|
|
386
|
+
zone?: string;
|
|
387
|
+
/**
|
|
388
|
+
* The locale of the instance, a name or an object to register (see `yuga.locale`). The global
|
|
389
|
+
* locale by default, or the locale of a parsed instance.
|
|
390
|
+
*/
|
|
391
|
+
locale?: string | Locale;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* The options of `yuga(parsable, options)`, a config without the parsable.
|
|
395
|
+
*/
|
|
396
|
+
export type Options = Omit<Config, "parsable">;
|
|
397
|
+
/**
|
|
398
|
+
* The options of `diff`, a boolean is the same as `{ float }` (Day.js's third argument).
|
|
399
|
+
*/
|
|
400
|
+
export interface DiffOptions {
|
|
401
|
+
/**
|
|
402
|
+
* Keep the fraction instead of truncating toward zero.
|
|
403
|
+
*/
|
|
404
|
+
float?: boolean;
|
|
405
|
+
/**
|
|
406
|
+
* Use Temporal's calendar arithmetic (`ZonedDateTime#until` totaled in the unit) instead of
|
|
407
|
+
* Day.js's algorithm: Jan 31 to Feb 29 is exactly 1 month.
|
|
408
|
+
*/
|
|
409
|
+
exact?: boolean;
|
|
410
|
+
}
|
|
411
|
+
export interface Factory {
|
|
412
|
+
/**
|
|
413
|
+
* Create an instance, now by default (see `Parsable`). Strings without a format are ISO 8601
|
|
414
|
+
* and Day.js's loose `YYYY-MM-DD HH:mm:ss.SSS` in the system's time zone.
|
|
415
|
+
*
|
|
416
|
+
* @example
|
|
417
|
+
* yuga("2024-05-15 10:20");
|
|
418
|
+
*/
|
|
419
|
+
(parsable?: Parsable): Yuga;
|
|
420
|
+
/**
|
|
421
|
+
* Create an instance from a string in a format (Day.js's `customParseFormat`), and optionally
|
|
422
|
+
* in a locale (for `MMM` / `MMMM` names, and the instance's locale).
|
|
423
|
+
*
|
|
424
|
+
* Tokens: `YYYY YY M MM MMM MMMM D DD Do H HH h hh m mm s ss SSS A a Q Z ZZ X x`, text in
|
|
425
|
+
* brackets is literal. Missing fields default the same as Day.js (the current year, the 1st of
|
|
426
|
+
* the month, midnight), strings that don't match are invalid.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* yuga("15/05/2024", "DD/MM/YYYY");
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* yuga("15 mai 2024", "D MMMM YYYY", "fr");
|
|
433
|
+
*/
|
|
434
|
+
(parsable: Parsable, format: string, locale?: string | Locale): Yuga;
|
|
435
|
+
/**
|
|
436
|
+
* Create an instance with options, Day.js's `dayjs(date, { utc, locale })`.
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* yuga("2024-05-15 10:20", { utc: true });
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* yuga("2024-05-15", { locale: "es" });
|
|
443
|
+
*/
|
|
444
|
+
(parsable: Parsable, options: Options): Yuga;
|
|
445
|
+
/**
|
|
446
|
+
* Check if a value is an `Yuga` class.
|
|
447
|
+
*/
|
|
448
|
+
isYuga(that: unknown): that is Yuga;
|
|
449
|
+
/**
|
|
450
|
+
* Create a Yuga object from a Unix timestamp, 10 digits, seconds since the Unix Epoch.
|
|
451
|
+
*/
|
|
452
|
+
unix(value: number): Yuga;
|
|
453
|
+
/**
|
|
454
|
+
* Create an instance in a time zone (Day.js's `dayjs.tz`): wall-clock values (strings without
|
|
455
|
+
* an offset, plain Temporal values) are read in it, moments (numbers, dates, instants, strings
|
|
456
|
+
* with an offset) keep their moment.
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* yuga.tz("2024-05-15 10:20", "Asia/Tokyo").toISOString(); // "2024-05-15T01:20:00.000Z"
|
|
460
|
+
*
|
|
461
|
+
* @throws { RangeError } When the time zone is unknown.
|
|
462
|
+
*/
|
|
463
|
+
tz(parsable: Parsable, zone: string): Yuga;
|
|
464
|
+
tz(parsable: Parsable, format: string, zone: string): Yuga;
|
|
465
|
+
/**
|
|
466
|
+
* Get the global locale's name (`"en"`).
|
|
467
|
+
*/
|
|
468
|
+
locale(): string;
|
|
469
|
+
/**
|
|
470
|
+
* Set the global locale (the one new instances use) and return its name. Names are any BCP 47
|
|
471
|
+
* tag (built out of `Intl`, lowercased: `"en-GB"` is `"en-gb"`) or registered locales, a name
|
|
472
|
+
* with an object registers the object (overriding `Intl`), and a locale object registers it
|
|
473
|
+
* under its own name. Unknown locales keep the global one.
|
|
474
|
+
*/
|
|
475
|
+
locale(preset: string | Locale, object?: Locale): string;
|
|
476
|
+
/**
|
|
477
|
+
* Resolve (and register) a locale without setting the global one, its name or `undefined` when
|
|
478
|
+
* unknown.
|
|
479
|
+
*/
|
|
480
|
+
locale(preset: string | Locale, object: Locale | undefined, isLocal: true): string | undefined;
|
|
481
|
+
/**
|
|
482
|
+
* Every registered locale, keyed by name (built-in `en`, and every locale resolved so far).
|
|
483
|
+
*/
|
|
484
|
+
locales: Record<string, Locale>;
|
|
485
|
+
/**
|
|
486
|
+
* Day.js's name of `locales`.
|
|
487
|
+
*/
|
|
488
|
+
Ls: Record<string, Locale>;
|
|
489
|
+
/**
|
|
490
|
+
* The built-in English locale (Day.js's `en`).
|
|
491
|
+
*/
|
|
492
|
+
en: Locale;
|
|
493
|
+
/**
|
|
494
|
+
* Extend an extension into the yuga class.
|
|
495
|
+
*/
|
|
496
|
+
extend(extension: yuga.Extension<undefined>): void;
|
|
497
|
+
extend<T>(extension: yuga.Extension<T>, config: T): void;
|
|
498
|
+
extend<T = never>(extension: yuga.Extension<T>, config?: T): void;
|
|
499
|
+
/**
|
|
500
|
+
* Plug a plugin into the yuga factory.
|
|
501
|
+
*/
|
|
502
|
+
plug<T = never>(extension: yuga.Plugin<T>, ...args: [T] extends [never] ? [] : [T] extends [undefined] ? [] : [config: T]): void;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Every value `yuga()` can parse, see `yuga.$.parse` for how each is read.
|
|
506
|
+
*/
|
|
507
|
+
export interface ExpandableParsable {
|
|
508
|
+
/**
|
|
509
|
+
* Returns yuga representation of `now` equivalent to `yuga(new Date())`.
|
|
510
|
+
*/
|
|
511
|
+
undefined: undefined;
|
|
512
|
+
/**
|
|
513
|
+
* Returns yuga representation of an `Invalid Date` equivalent to `yuga(new Date(NaN))`.
|
|
514
|
+
*
|
|
515
|
+
* NOTE: Backport from moment, moment sees it as parsing an invalid moment object.
|
|
516
|
+
*
|
|
517
|
+
* TODO: Should this even be allowed? should it be `Invalid`?
|
|
518
|
+
*/
|
|
519
|
+
null: null;
|
|
520
|
+
/**
|
|
521
|
+
* Can be used to parse custom strings.
|
|
522
|
+
*/
|
|
523
|
+
dateString: string;
|
|
524
|
+
timestamp: number;
|
|
525
|
+
dateClass: Date;
|
|
526
|
+
itself: Yuga;
|
|
527
|
+
zonedDateTime: Temporal.ZonedDateTime;
|
|
528
|
+
instant: Temporal.Instant;
|
|
529
|
+
plainDateTime: Temporal.PlainDateTime;
|
|
530
|
+
plainDate: Temporal.PlainDate;
|
|
531
|
+
}
|
|
532
|
+
export type Parsable = ExpandableParsable[keyof ExpandableParsable];
|
|
533
|
+
/**
|
|
534
|
+
* Units are case-insensitive (`"Year"`, `"MONTHS"`), except the short `D` (date) and `M` (month).
|
|
535
|
+
*/
|
|
536
|
+
export type Unit = Unit.Short | Unit.Long | Unit.Plural | Unit.Loose;
|
|
537
|
+
export namespace Unit {
|
|
538
|
+
export interface Map {
|
|
539
|
+
ms: "millisecond";
|
|
540
|
+
s: "second";
|
|
541
|
+
m: "minute";
|
|
542
|
+
h: "hour";
|
|
543
|
+
d: "day";
|
|
544
|
+
D: "date";
|
|
545
|
+
w: "week";
|
|
546
|
+
M: "month";
|
|
547
|
+
q: "quarter";
|
|
548
|
+
y: "year";
|
|
549
|
+
}
|
|
550
|
+
export type Short = keyof Map;
|
|
551
|
+
export type Long = Map[keyof Map];
|
|
552
|
+
export type Plural = `${Long}s`;
|
|
553
|
+
export type Loose = Capitalize<Long | Plural> | Uppercase<Long | Plural> | "Q";
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
export { yuga };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>e===void 0,t=e=>!!e&&typeof e==`object`&&`$isYugaClass`in e,n=Object.entries({ms:`millisecond`,s:`second`,m:`minute`,h:`hour`,d:`day`,w:`week`,D:`date`,M:`month`,q:`quarter`,y:`year`}),r=Object.create(null);for(let[e,t]of n)r[e]=r[t]=r[`${t}s`]=e;const i=e=>r[e]??r[String(e).toLowerCase()],a={ms:1,s:1e3,m:6e4,h:36e5,d:864e5,w:6048e5},o=/* @__PURE__ */ new Map,s=t=>{let n=o.get(t);return e(n)&&o.set(t,n=new Temporal.ZonedDateTime(0n,t).timeZoneId),n},c=e=>Math.abs(e)<=864e13?Math.trunc(e)+0:NaN,l=(e,t)=>e===e?new Temporal.ZonedDateTime(BigInt(e)*1000000n,t??Temporal.Now.timeZoneId()):null,u=126227808e5,d=/* @__PURE__ */ new Map,f=(e,t)=>{let n=d.get(t);n||d.set(t,n=/* @__PURE__ */ new Map);let r=Math.floor(e/2**31),i=n.get(r),a=i?.find(([t,n])=>e>=t&&e<n);if(!a){let o=new Temporal.ZonedDateTime(BigInt(e)*1000000n+BigInt(e<864e13),t);i||(n.size>4095&&n.clear(),n.set(r,i=[])),i.push(a=[o.getTimeZoneTransition(`previous`)?.epochMilliseconds??-1/0,o.getTimeZoneTransition(`next`)?.epochMilliseconds??1/0,o.offsetNanoseconds/1e6])}return a},p=(t,n)=>{let[r,i,a,o,s,l,d]=t,p=r>-1&&r<100;if(e(n)){if(!p)return new Date(r,i,a,o,s,l,d).getTime();let e=/* @__PURE__ */ new Date(0);return e.setFullYear(r,i,a),e.setHours(o,s,l,d)}let m=p?Date.UTC(Math.trunc(r)+400,i,a,o,s,l,d)-u:Date.UTC(r,i,a,o,s,l,d);if(n===`UTC`||m!==m)return m;let[h,g,_]=f(m,n);if(m-1728e5>=h&&m+1728e5<g)return c(m-_);try{let e=new Date(m);return new Temporal.PlainDateTime(e.getUTCFullYear(),e.getUTCMonth()+1,e.getUTCDate(),e.getUTCHours(),e.getUTCMinutes(),e.getUTCSeconds(),e.getUTCMilliseconds()).toZonedDateTime(n).epochMilliseconds}catch{return NaN}},m=(e,t)=>{let n=t-Math.floor(t/12)*12,r=e+(t-n)/12;return n===1?r%4||!(r%100)&&r%400?28:29:31-n%7%2},h=e=>m(e,1)+337,g=({y:e,M:t,D:n,d:r},i,a)=>{let o=Math.floor((367*t+5)/12)+(t>1?h(e)-367:0)+n-(r-i+7)%7+6-a;return o<0?o+=h(e-1):o>=h(e)&&(o-=h(e)),Math.floor(o/7)+1},_=e=>e|0||(e===e?0:NaN),v=(t,n)=>{if(e(n)){let e=new Date(t);return[e.getFullYear(),e.getMonth(),e.getDate(),e.getDay(),e.getHours(),e.getMinutes(),e.getSeconds(),e.getMilliseconds()]}let r=t===t?n===`UTC`?0:f(t,n)[2]:NaN,i=t+r,a=i>864e13?-1:+(i<-864e13),o=new Date(i+a*u);return[o.getUTCFullYear()-a*400,o.getUTCMonth(),o.getUTCDate(),o.getUTCDay(),o.getUTCHours(),o.getUTCMinutes(),o.getUTCSeconds(),o.getUTCMilliseconds(),_(r/1e3)]},y=({y:e,M:t,D:n,h:r,m:i,s:a,ms:o})=>[e,t,n,r,i,a,o],ee=[`y`,`M`,`D`,`d`,`h`,`m`,`s`,`ms`],b=(e,t,n)=>{let r=Number(t),o=i(n);if(o!==`y`&&o!==`q`&&o!==`M`&&o!==`w`&&o!==`d`&&o!==`D`)return c(e.$epoch+r*(a[o]??1));let s=y(e);return o===`w`||o===`d`||o===`D`?s[2]+=Math.round(r*(o===`w`?7:1)):(o===`y`?s[0]=Math.trunc(s[0]+r):s[1]=Math.trunc(s[1]+r*(o===`q`?3:1)),s[2]=Math.min(s[2],m(s[0],s[1]))),p(s,e.$zone)},te=(t,n,r,o)=>{let s=i(n),c=t.$epoch,l=a[s];if(l<864e5){let{h:e,m:n,s:r,ms:i}=t,a=c-(((e*60+n)*60+r)*1e3+i)%l;return[a,a+l-1]}if(!s){if(!/^isoWeeks?$/i.test(n))return[c,c];s=`w`,r=1}let u=y(t),d=2,f=1;s===`y`||s===`q`||s===`M`?(s===`y`?d=u[1]=0:(d=1,s===`q`&&(u[1]-=u[1]%(f=3))),u[2]=1):s===`w`&&(u[2]-=(t.d-r+7)%(f=7)),u.fill(0,3);let g=o===2?NaN:p(u,t.$zone);if(o===1)return[g,NaN];if(o===3&&e(t.$zone)){let[e,t,n]=u,r=d===2?f:d===1?m(e,t)+(f===3?m(e,t+1)+m(e,t+2):0):h(e),i=new Date(g+r*864e5),a=d===2?n+f-(n+f>m(e,t)?m(e,t):0):1;if(!(i.getHours()||i.getMinutes()||i.getSeconds())&&i.getDate()===a)return[g,i.getTime()-1]}return u[d]+=f,[g,p(u,t.$zone)-1]},x={d:`day`,w:`week`,M:`month`,q:`month`,y:`year`},S=(e,t)=>{if(e.D<t.D)return-S(t,e);let n=(t.y-e.y)*12+(t.M-e.M),r=t=>b(e,t,`M`),i=r(n),a=t.$epoch<i,o=r(n+(a?-1:1));return-(n+(t.$epoch-i)/(a?i-o:o-i))+0},ne=(e,t,n,r)=>{let{float:o,exact:s}=typeof r==`object`?r:{float:r},c=n&&i(n),l;if(!e.$valid||!t.$valid)return NaN;if(s&&c&&x[c]){let n=x[c],r=t.$Temporal,i=e.$Temporal;l=(o?r.until(i).total({unit:n,relativeTo:r}):r.until(i,{largestUnit:n,smallestUnit:n,roundingMode:`trunc`})[`${n}s`])/(c===`q`?3:1)}else l=s?(e.$epoch-t.$epoch)/(c&&a[c]||1):c===`M`||c===`q`||c===`y`?S(e,t)/(c===`M`?1:c===`q`?3:12):(e.$epoch-t.$epoch-(c===`d`||c===`w`?(t.utcOffset()-e.utcOffset())*6e4:0))/(c&&a[c]||1);return(o?l:Math.trunc(l))+0},re=Array.from({length:100},(e,t)=>(t<10?`0`:``)+t),C=(e,t=2)=>t<3&&re[e]||String(e).padStart(t,`0`),w=(e,t)=>{let n=Math.abs(e);return(e<0?`-`:`+`)+C(Math.floor(n/60))+t+C(Math.floor(n%60))},T=(e,t)=>w(e.utcOffset(),t),E=(e,t,n,r)=>e?.[n]||t[n]?.slice(0,r)||``,D=(e,t,n)=>{if(t.meridiem)return t.meridiem(e.h,e.m,n);let r=e.h<12?`AM`:`PM`;return n?r.toLowerCase():r},O={YY:({y:e})=>String(e).slice(-2),YYYY:({y:e})=>C(e,4),M:({M:e})=>e+1,MM:({M:e})=>C(e+1),MMM:({M:e},{monthsShort:t,months:n})=>E(t,n,e,3),MMMM:({M:e},{months:t})=>t[e]??``,Do:({D:e},t)=>String(t.ordinal(e)).replace(/^\[|]$/g,``),d:({d:e})=>e,dd:({d:e},{weekdaysMin:t,weekdays:n})=>E(t,n,e,2),ddd:({d:e},{weekdaysShort:t,weekdays:n})=>E(t,n,e,3),dddd:({d:e},{weekdays:t})=>t[e]??``,h:({h:e})=>e%12||12,hh:({h:e})=>C(e%12||12),a:(e,t)=>D(e,t,!0),A:(e,t)=>D(e,t,!1),SSS:({ms:e})=>C(e,3),Z:e=>T(e,`:`),ZZ:e=>T(e,``),Q:({q:e})=>e};for(let[e,t]of[[`D`,({D:e})=>e],[`H`,({h:e})=>e],[`m`,({m:e})=>e],[`s`,({s:e})=>e],[`w`,(e,{weekStart:t,yearStart:n})=>g(e,t??0,n??1)],[`W`,({w:e})=>e]])O[e]=t,O[e+e]=(e,n)=>C(t(e,n));const ie=/D[oD]?(\[[^[\]]*]|\s)+MMMM?/,ae={MMM:({M:e},t)=>t.$monthsShort?.[e]??E(t.monthsShort,t.months,e,3),MMMM:({M:e},t)=>(t.$months??t.months)[e]??``},oe=RegExp(`(\\[[^\\]]+]|${Object.keys(O).toSorted((e,t)=>t.length-e.length).join(`|`)})`),se=Object.create(null),ce=e=>{let t=ie.test(e);return e.split(oe).map((e,n)=>n%2?t&&ae[e]||O[e]||e.slice(1,-1):e)},k=(e,t,n)=>{if(!e.$valid)return n.invalidDate||`Invalid Date`;if(t===`YYYY-MM-DDTHH:mm:ssZ`||t===`YYYY-MM-DDTHH:mm:ss[Z]`){let{y:n,M:r,D:i,h:a,m:o,s}=e;return`${C(n,4)}-${C(r+1)}-${C(i)}T${C(a)}:${C(o)}:${C(s)}${t[19]===`Z`?T(e,`:`):`Z`}`}let r=se[t]??=ce(t),i=``;for(let t of r)i+=typeof t==`string`?t:t(e,n);return i},A=e=>{let t=[`th`,`st`,`nd`,`rd`],n=e%100;return e+(t[(n-20)%10]||t[n]||`th`)},j={name:`en`,weekdays:`Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday`.split(`_`),months:`January_February_March_April_May_June_July_August_September_October_November_December`.split(`_`),ordinal:A},M=Object.assign(Object.create(null),{en:j});let N=`en`;const le=e=>{try{if(!Intl.DateTimeFormat.supportedLocalesOf(e).length)return}catch{return}let t=new Intl.Locale(e),n=t=>{let n=t<2||t>4,r=n?`month`:`weekday`,i=new Intl.DateTimeFormat(e,{[r]:t%2?`short`:t===4?`narrow`:`long`,day:t>4?`numeric`:void 0});return Array.from({length:n?12:7},(e,t)=>i.formatToParts(n?new Date(2e3,t):new Date(2e3,0,t+2)).find(e=>e.type===r).value)},r,i={name:e,weekStart:t.getWeekInfo().firstDay%7,yearStart:`AD AN AT AX BE BG CH CZ DE DK EE ES FI FJ FO FR GB GF GG GI GP GR HU IE IM IS IT JE LI LT LU MC MQ NL NO PL PT RE RU SE SJ SK SM VA`.includes(t.maximize().region)?4:1,meridiem:(t,n,i)=>{let a=(r??=(e=>[0,12].map(t=>e.formatToParts(new Date(2e3,0,1,t)).find(({type:e})=>e===`dayPeriod`).value))(new Intl.DateTimeFormat(e,{hour:`numeric`,hour12:!0})))[t<12?0:1];return i?a.toLowerCase():a.toUpperCase()},ordinal:t.language===`en`?A:String};return[`months`,`monthsShort`,`weekdays`,`weekdaysShort`,`weekdaysMin`,`$monthsShort`,`$months`].forEach((e,t)=>Object.defineProperty(i,e,{configurable:!0,enumerable:t<5,get(){let r=n(t);return t===4&&new Set(r).size<7&&(r=this.weekdaysShort.map(e=>e.slice(0,2))),t>4&&(/\d/.test(r.join())||r.join()===this[t>5?`months`:`monthsShort`].join())&&(r=void 0),Object.defineProperty(this,e,{value:r,writable:!0}),r}})),i},P=(e,t,n)=>{if(!e)return N;let r;if(typeof e==`string`){let n=e.toLowerCase(),i=t??M[n]??le(n);i&&(M[r=n]=i)}else M[r=e.name.toLowerCase()]=e;return n?r:N=r??N},ue=Object.create(null),de=(t,n)=>{let r=[],i=t.replace(RegExp(`\\[([^\\]]*)]|${Object.keys(n).join(`|`)}|[^]`,`g`),(t,i)=>{let a=e(i)?n[t]:void 0;return e(a)?(i??t).replace(/[.*+?^${}()|[\]\\]/g,`\\$&`):(r.push(t),a)});return[RegExp(`^${i}`),r]},F=/* @__PURE__ */ new WeakMap,I=(e,t,n=t,r)=>{if(!t)return-1;let i=F.get(n);return i||F.set(n,i=t.map(e=>e.slice(0,r).toLowerCase())),i.indexOf(e)},L=`([^\\d\\s\\-_:/,()]+)`,R=`([^\\d\\s\\-_:/,()]+(?:\\s[^\\d\\s\\-_:/,()]+)*)`,z=/* @__PURE__ */ new WeakMap,fe=(e,t)=>{let n=z.get(t);if(!n){z.set(t,n=/* @__PURE__ */ new Map);for(let e=24;e--;)n.set((t.meridiem?.(e,0,!1)??(e<12?`am`:`pm`)).toLowerCase(),e>11)}return n.get(e.toLowerCase())},B=(e,t,n)=>{let{months:r,monthsShort:i}=n,a=e.toLowerCase(),o=()=>i?I(a,i):I(a,r,n,3),s=t===`MMM`?o():I(a,r);return s<0&&(s=t===`MMM`?I(a,r):o()),s<0&&(s=I(a,n.$months)),s<0&&(s=I(a,n.$monthsShort)),s},pe=([,t],n,r,i)=>{let a=[],o,s;for(let r=0;r<t.length;r+=1){let l=t[r],u=n[r+1]??``,d=parseInt(u,10);if(l===`X`||l===`x`)return c(u*(l===`X`?1e3:1));if(l===`A`||l===`a`){if(o=fe(u,i()),e(o))return NaN}else if(l===`Z`||l===`ZZ`)s=u===`Z`?0:(u[0]===`-`?-1:1)*(u.slice(1,3)*60+ +u.slice(-2));else if(l===`MMM`||l===`MMMM`){let e=B(u,l,i());if(e<0)return NaN;a[1]=e}else if(l===`YY`)a[0]=d+(d>68?1900:2e3);else if(l===`Q`)a[1]=(d-1)*3;else{let t=me[l];e(t)||(a[t]=t===1?d-1:d)}}let[l,u,d]=a,f=e(l)?v(Date.now(),r):[],m=[l??f[0],u??f[1]??0,d??(e(u)?f[2]:1)??1,a[3]??0,a[4]??0,a[5]??0,a[6]??0];return!e(o)&&m[3]<13&&(m[3]=m[3]%12+(o?12:0)),e(s)?p(m,r):p(m,`UTC`)-s*6e4},me={YYYY:0,MM:1,M:1,Do:2,DD:2,D:2,HH:3,H:3,hh:3,h:3,mm:4,m:4,ss:5,s:5,SSS:6},he=/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,ge={short:r,wall:{y:0,M:1,D:2,h:3,m:4,s:5,ms:6},tokensMap:{YYYY:`(\\d{4})`,YY:`(\\d{2})`,MMMM:L,MMM:L,MM:`(\\d{2})`,M:`(\\d{1,2})`,Do:`(\\d{1,2})[^\\d\\s]*`,DD:`(\\d{2})`,D:`(\\d{1,2})`,Q:`([1-4])`,d:`([0-6])`,HH:`(\\d{2})`,H:`(\\d{1,2})`,hh:`(\\d{2})`,h:`(\\d{1,2})`,mm:`(\\d{2})`,m:`(\\d{1,2})`,ss:`(\\d{2})`,s:`(\\d{1,2})`,SSS:`(\\d{3})`,A:R,a:R,ZZ:`([+-]\\d\\d:?\\d\\d|Z)`,Z:`([+-]\\d\\d:?\\d\\d|Z)`,X:`(-?\\d+(?:\\.\\d+)?)`,x:`(-?\\d+)`},format:`YYYY-MM-DDTHH:mm:ssZ`,cascade(e){return e.$cache=v(e.$epoch,e.$zone),e},zone({parsable:n,utc:r,zone:i}){return e(i)?typeof n!=`object`||!n||n instanceof Date?r?`UTC`:void 0:t(n)?n.$zone:n instanceof Temporal.ZonedDateTime?n.timeZoneId:r?`UTC`:void 0:s(i)},parse(n,r){let{parsable:i,format:a}=n;if(i===null)return NaN;if(e(i))return Date.now();if(typeof i==`number`)return c(i);if(typeof i==`string`){if(a){let e=ue[a]??=de(a,this.tokensMap),t=e[0].exec(i);return t?pe(e,t,r,()=>M[V(n)]??j):NaN}let e=/z$/i.test(i)?void 0:/[+-]\d\d:?\d\d$/.exec(i),t=e?i.search(/[t\s]/i):-1;if(e!==void 0&&(t<0||t>e.index)){let e=he.exec(i);if(e)return p([+e[1],(e[2]??1)-1||0,+(e[3]||1),+(e[4]??0),+(e[5]??0),+(e[6]??0),+(e[7]??`0`).slice(0,3)],r)}return Date.parse(i)}if(i instanceof Date)return i.getTime();if(t(i))return i.$epoch;if(i instanceof Temporal.ZonedDateTime||i instanceof Temporal.Instant)return i.epochMilliseconds;if(i instanceof Temporal.PlainDate||i instanceof Temporal.PlainDateTime){let{year:e,month:t,day:n,hour:a=0,minute:o=0,second:s=0,millisecond:c=0}=i.calendarId===`iso8601`?i:i.withCalendar(`iso8601`);return p([e,t-1,n,a,o,s,c],r)}return new Date(i).getTime()}},V=({locale:e,parsable:n})=>e&&P(e,void 0,!0)||(t(n)?n.$locale:N),H=((e,t,n)=>new X(typeof t==`object`?{...t,parsable:e}:{parsable:e,format:t,locale:n}));H.$=ge,H.isYuga=t;const U=({$locale:e})=>M[e]??j,_e=Object.create(null);let W=NaN,G=``;const K=e=>e.$cache??H.$.cascade(e).$cache,q=({$zone:e,$locale:t},n)=>X.create(n,e,void 0,t),J=(t,n,r)=>e(n)?[t.$epoch,t.$epoch]:te(t,n,U(t).weekStart??0,r),Y=e=>t(e)?e.$epoch:H.$.parse({parsable:e},void 0);var X=class n{get $valid(){return this.$epoch===this.$epoch}get $Temporal(){return this.$zoned??=l(this.$epoch,this.$zone)}get q(){return Math.floor(this.M/3)+1}get w(){return g(this,1,4)}constructor(e){let n=H.$.zone(e),r=this,{parsable:i}=e;r.$epoch=H.$.parse(e,n),r.$zone=n,r.$locale=V(e),r.$zoned=t(i)?i.$zone===n?i.$zoned:void 0:i instanceof Temporal.ZonedDateTime&&i.timeZoneId===n&&i.calendarId===`iso8601`&&!i.microsecond&&!i.nanosecond?i:void 0,r.$cache=void 0}static create(e,t,r,i){let a=Object.create(n.prototype);return a.$epoch=e,a.$zone=t,a.$locale=i??N,a.$zoned=r,a.$cache=void 0,a}clone(){let{$epoch:e,$zone:t,$locale:r,$zoned:i,$cache:a}=this,o=n.create(e,t,i,r);return o.$cache=a,o}get(t){return e(t)?this.toString():this[i(t)]}set(e,t){if(!this.$valid)return this;let n=i(e);if(n===`w`||n===`M`||n===`y`||n===`q`)return this.add(t-this[n],n);let r=y(this);if(n===`d`)r[2]+=t-this.d;else if(n in H.$.wall)r[H.$.wall[n]]=t;else return this;return q(this,p(r,this.$zone))}add(e,t){return q(this,b(this,e,t))}subtract(e,t){return q(this,b(this,-e,t))}startOf(e){return q(this,J(this,e,1)[0])}endOf(e){return q(this,J(this,e,2)[1])}isBefore(e,t){return J(this,t,2)[1]<Y(e)}isSameOrBefore(e,t){return J(this,t,1)[0]<=Y(e)}isSame(e,t){let n=Y(e),[r,i]=J(this,t,3);return r<=n&&n<=i}isSameOrAfter(e,t){return Y(e)<=J(this,t,2)[1]}isAfter(e,t){return Y(e)<J(this,t,1)[0]}isValid(){return this.$valid}diff(e,n,r){let i=t(e)?e:H(e);return ne(this,i.$zone===this.$zone?i:q(this,i.$epoch),n,r)}format(e){return k(this,e||(this.$zone===`UTC`?`YYYY-MM-DDTHH:mm:ss[Z]`:H.$.format),U(this))}locale(e,t){if(!e)return this.$locale;let n=this.clone();return n.$locale=P(e,t,!0)??this.$locale,n}tz(t,r){if(e(t))return this.$zone??Temporal.Now.timeZoneId();let i=s(t);return n.create(r&&this.$valid?p(y(this),i):this.$epoch,i,void 0,this.$locale)}utc(e){return this.tz(`UTC`,e)}local(){return n.create(this.$epoch,void 0,void 0,this.$locale)}daysInMonth(){return m(this.y,this.M)}utcOffset(){let e=K(this);return-Math.round(-(e[8]??=_(-new Date(this.$epoch).getTimezoneOffset()*60))/900)*15+0}unix(){return Math.floor(this.valueOf()/1e3)}toDate(){return new Date(this.valueOf())}toTemporal(){let e=this.$Temporal;if(!e)throw RangeError(`Invalid Date`);return e}toInstant(){return Temporal.Instant.fromEpochMilliseconds(this.$epoch)}toPlainDate(){return new Temporal.PlainDate(this.y,this.M+1,this.D)}toJSON(){return this.$valid?this.toISOString():null}toISOString(){let e=this.$epoch,t=Math.floor(e/864e5),n=e-t*864e5;if(t!==W){let n=new Date(e),r=n.getUTCFullYear();if(!(r>=0&&r<1e4))return n.toISOString();W=t,G=`${C(r,4)}-${C(n.getUTCMonth()+1)}-${C(n.getUTCDate())}`}return`${G}T${C(Math.floor(n/36e5))}:${C(Math.floor(n/6e4)%60)}:${C(Math.floor(n/1e3)%60)}.${C(n%1e3,3)}Z`}toString(){let t=this.$zone;if(e(t)||!this.$valid)return this.toDate().toString();let n=K(this)[8]/60,[r,i,a,o,s]=new Date(this.$epoch+n*6e4).toUTCString().split(` `),c=(_e[t]??=new Intl.DateTimeFormat(`en-US`,{timeZone:t,timeZoneName:`long`})).formatToParts(this.$epoch).find(({type:e})=>e===`timeZoneName`).value;return`${r.slice(0,3)} ${a} ${i} ${o} ${s} GMT${w(n,``)} (${c})`}toUTCString(){return this.toDate().toUTCString()}valueOf(){return this.$epoch}};const Z=X.prototype;Z.$isYugaClass=!0,Object.defineProperty(Z,Symbol.for(`nodejs.util.inspect.custom`),{value(){return`Yuga<${this.$valid?k(this,`YYYY-MM-DDTHH:mm:ss.SSSZ`,j):`Invalid Date`} ${this.tz()}>`}}),ee.forEach((e,t)=>Object.defineProperty(Z,e,{get(){return K(this)[t]}}));for(let[t,r]of n)Z[r]=function(n){return e(n)?this[t]:this.set(t,n)};const Q=Symbol(),$=(e,t)=>{let n=e;n[Q]||(n[Q]=!0,t())};H.extend=(e,t)=>$(e,()=>e(t,X)),H.plug=(e,t)=>$(e,()=>e(t,H)),H.unix=e=>new X({parsable:e*1e3}),H.tz=(e,t,n)=>new X({parsable:e,format:n&&t,zone:n??t}),H.locale=P,H.locales=H.Ls=M,H.en=j;export{X as Yuga,H as yuga};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { yuga as yuga$1 } from "yugajs";
|
|
2
|
+
declare module "yugajs" {
|
|
3
|
+
namespace yuga {
|
|
4
|
+
/**
|
|
5
|
+
* A plugin is used to create separate structures to the yuga.
|
|
6
|
+
*/
|
|
7
|
+
export type Plugin<TConfig> = (pluginConfig: TConfig, yugaFactory: yuga.Factory) => void;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare const definePlugin: <TConfig>(plugin: yuga$1.Plugin<TConfig>) => yuga$1.Plugin<TConfig>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>e;export{e as definePlugin};
|
package/package.json
CHANGED
|
@@ -1,6 +1,117 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yugajs",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Yuga is an immutable date / time / locale library built on Temporal and Intl, a modern ESM alternative to Day.js (and Moment.js) with the same API",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ISO-8601",
|
|
7
|
+
"date",
|
|
8
|
+
"day",
|
|
9
|
+
"dayjs",
|
|
10
|
+
"immutable",
|
|
11
|
+
"locale",
|
|
12
|
+
"moment",
|
|
13
|
+
"momentjs",
|
|
14
|
+
"time",
|
|
15
|
+
"yuga",
|
|
16
|
+
"yugajs"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://vitest.igan.dev/yuga/",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/igan-project/yuga/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "michaelhazan",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/igan-project/yuga.git"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"type": "module",
|
|
32
|
+
"main": "./dist/index.js",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"./plugin": {
|
|
43
|
+
"types": "./dist/plugin/index.d.ts",
|
|
44
|
+
"import": "./dist/plugin/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./extension": {
|
|
47
|
+
"types": "./dist/extension/index.d.ts",
|
|
48
|
+
"import": "./dist/extension/index.js"
|
|
49
|
+
},
|
|
50
|
+
"./plugin/*": {
|
|
51
|
+
"types": "./dist/plugin/*/index.d.ts",
|
|
52
|
+
"import": "./dist/plugin/*/index.js"
|
|
53
|
+
},
|
|
54
|
+
"./extension/*": {
|
|
55
|
+
"types": "./dist/extension/*/index.d.ts",
|
|
56
|
+
"import": "./dist/extension/*/index.js"
|
|
57
|
+
},
|
|
58
|
+
"./declarations/*": {
|
|
59
|
+
"types": "./dist/declarations/*.d.ts"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"size": "size-limit",
|
|
64
|
+
"bench": "vitest bench --project=\"bundle (bench)\" --reporter=verbose",
|
|
65
|
+
"test": "vitest run --project=unit --project=bundle",
|
|
66
|
+
"test:bundle": "vitest run --project=bundle",
|
|
67
|
+
"test:raw": "vitest run --project=unit",
|
|
68
|
+
"test:browser": "vitest run --project=browser",
|
|
69
|
+
"test:watch": "vitest watch --project=unit --project=bundle",
|
|
70
|
+
"test:ui": "vitest --ui --project=unit --project=bundle",
|
|
71
|
+
"spell": "cspell .",
|
|
72
|
+
"lint": "oxlint",
|
|
73
|
+
"lint:fix": "oxlint --fix",
|
|
74
|
+
"knip": "knip",
|
|
75
|
+
"format": "oxfmt .",
|
|
76
|
+
"format:check": "oxfmt --check .",
|
|
77
|
+
"typescript": "tsc",
|
|
78
|
+
"build": "tsdown",
|
|
79
|
+
"check": "turbo run test spell lint knip format:check typescript build",
|
|
80
|
+
"release": "pnpm build && changeset publish"
|
|
81
|
+
},
|
|
82
|
+
"devDependencies": {
|
|
83
|
+
"@changesets/cli": "3.1.0",
|
|
84
|
+
"@igan/vitest-report": "0.1.0",
|
|
85
|
+
"@size-limit/preset-small-lib": "14.3.0",
|
|
86
|
+
"@types/luxon": "3.7.6",
|
|
87
|
+
"@types/node": "26.6.11",
|
|
88
|
+
"@vitest/browser-playwright": "5.1.2",
|
|
89
|
+
"@vitest/ui": "5.1.2",
|
|
90
|
+
"cspell": "10.6.0",
|
|
91
|
+
"dayjs": "1.11.23",
|
|
92
|
+
"knip": "6.49.0",
|
|
93
|
+
"luxon": "3.7.2",
|
|
94
|
+
"moment": "2.31.0",
|
|
95
|
+
"oxfmt": "0.81.0",
|
|
96
|
+
"oxlint": "1.96.0",
|
|
97
|
+
"pkg-pr-new": "0.0.95",
|
|
98
|
+
"playwright": "1.65.1",
|
|
99
|
+
"size-limit": "14.3.0",
|
|
100
|
+
"temporal-polyfill": "1.0.9",
|
|
101
|
+
"tsdown": "0.23.7",
|
|
102
|
+
"turbo": "2.11.13",
|
|
103
|
+
"typescript": "7.0.2",
|
|
104
|
+
"vitest": "5.1.2"
|
|
105
|
+
},
|
|
106
|
+
"size-limit": [
|
|
107
|
+
{
|
|
108
|
+
"path": "./dist/index.js"
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
"packageManager": "pnpm@12.25.1+sha512.a6c677d7034d3d79af136f33db33cd6d4133d73fa565a06bd64a8f096d2537dd87a08d68e18ff377771ff51c18e2ec31be03b44670eb19e241890eae3aed016a",
|
|
112
|
+
"knip": {
|
|
113
|
+
"ignoreDependencies": [
|
|
114
|
+
"@changesets/cli"
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}
|