atomic-fns 0.4.2 → 1.1.1
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 +6 -17
- package/dist/collections/BTree.d.ts +1 -1
- package/dist/collections/Trie.d.ts +42 -0
- package/dist/collections/Trie.js +104 -0
- package/dist/collections/deque.d.ts +2 -2
- package/dist/collections/deque.js +6 -8
- package/dist/collections/index.d.ts +208 -106
- package/dist/collections/index.js +179 -436
- package/dist/decimal/index.d.ts +147 -0
- package/dist/decimal/index.js +376 -0
- package/dist/functools/index.d.ts +26 -18
- package/dist/functools/index.js +36 -50
- package/dist/globals/decimal.d.ts +142 -0
- package/dist/globals/decimal.js +371 -0
- package/dist/globals/index.d.ts +19 -31
- package/dist/globals/index.js +9 -24
- package/dist/i18n/currency.d.ts +24 -0
- package/dist/i18n/currency.js +85 -0
- package/dist/i18n/date/format.d.ts +16 -0
- package/dist/i18n/date/format.js +313 -0
- package/dist/i18n/date/index.d.ts +9 -9
- package/dist/i18n/date/test.html +72 -0
- package/dist/i18n/date/utils.d.ts +3 -3
- package/dist/i18n/date/utils.js +6 -28
- package/dist/i18n/format.d.ts +2 -2
- package/dist/i18n/format.js +1 -1
- package/dist/i18n/index.d.ts +1 -0
- package/dist/i18n/index.js +1 -0
- package/dist/itertools/index.d.ts +52 -24
- package/dist/itertools/index.js +4 -32
- package/dist/math/index.d.ts +7 -8
- package/dist/math/index.js +3 -1
- package/dist/operators/index.d.ts +2 -2
- package/dist/operators/index.js +0 -6
- package/dist/random/index.d.ts +1 -1
- package/dist/random/index.js +2 -2
- package/dist/types.d.ts +19 -0
- package/dist/types.js +1 -0
- package/package.json +1 -1
package/dist/globals/index.d.ts
CHANGED
|
@@ -3,27 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module Globals
|
|
5
5
|
*/
|
|
6
|
-
/** This is just a noop function. */
|
|
7
|
-
export declare const noop: () => void;
|
|
8
6
|
/**
|
|
9
7
|
* A generic function type with arbitrary arguments and return.
|
|
10
8
|
*/
|
|
11
|
-
export declare type Function<
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
* @see {@link Collections.filter}
|
|
15
|
-
* @see {@link Collections.find}
|
|
16
|
-
* @see {@link Collections.forEach}
|
|
17
|
-
* @see {@link Collections.index}
|
|
18
|
-
* @see {@link Collections.map}
|
|
19
|
-
* @see {@link Collections.pick}
|
|
20
|
-
* @see {@link Collections.omit}
|
|
21
|
-
* @see {@link Collections.uniq}
|
|
22
|
-
* @see {@link Collections.sortedUniq}
|
|
23
|
-
* @see {@link Collections.groupBy}
|
|
24
|
-
*/
|
|
25
|
-
export declare type Iteratee<T = any, K = any> = (value: T, key?: K, arr?: any) => any;
|
|
26
|
-
export declare type Predicate<T = any> = (value: T) => any;
|
|
9
|
+
export declare type Function<TReturn = any> = (...args: any) => TReturn;
|
|
10
|
+
/** This is just a noop function. */
|
|
11
|
+
export declare const noop: () => void;
|
|
27
12
|
/** A function that always returns `true`. */
|
|
28
13
|
export declare const True: () => boolean;
|
|
29
14
|
/** A function that always returns `false`. */
|
|
@@ -79,12 +64,12 @@ export declare class ValueError extends CustomError {
|
|
|
79
64
|
export declare class ZeroDivisionError extends CustomError {
|
|
80
65
|
}
|
|
81
66
|
/**
|
|
82
|
-
* Returns the true type of any value with correct detection for null
|
|
83
|
-
* Object
|
|
67
|
+
* Returns the true type of any value with correct detection for `null`, `Array`,
|
|
68
|
+
* `Object`, `Promise`, `Symbol`, and `NaN`. The correct values are inferred from {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag | Symbol.toStringTag} and the value's prototype.
|
|
84
69
|
* @param value
|
|
85
70
|
* @returns The value type.
|
|
86
71
|
*/
|
|
87
|
-
export declare function type(value: any):
|
|
72
|
+
export declare function type(value: any): string;
|
|
88
73
|
/**
|
|
89
74
|
* Returns a string representing the object.
|
|
90
75
|
* @param obj
|
|
@@ -153,7 +138,7 @@ export declare const isWeakMap: <K extends object, V>(x: any) => x is WeakMap<K,
|
|
|
153
138
|
* @param value
|
|
154
139
|
* @returns The number of elements in the collection or `undefined`.
|
|
155
140
|
*/
|
|
156
|
-
export declare function len(value: any):
|
|
141
|
+
export declare function len(value: any): number;
|
|
157
142
|
/**
|
|
158
143
|
* Generates a unique ID using random numbers. If a prefix is given, the ID is appended to it and returned as a string.
|
|
159
144
|
* @param {string} [pre=''] The id prefix.
|
|
@@ -167,7 +152,7 @@ uniqueId('user_')
|
|
|
167
152
|
// 'user_1033763188'
|
|
168
153
|
```
|
|
169
154
|
*/
|
|
170
|
-
export declare const uniqueId: (pre?: string) =>
|
|
155
|
+
export declare const uniqueId: (pre?: string) => string;
|
|
171
156
|
/**
|
|
172
157
|
* Generates a monotonically increasing, pseudo-random based uuid-v4 string. These are sortable, url-friendly and are 100% unique if the IDs are generated more than 1 millisecond apart. If two IDs are generated at the same millisecond, the chance of collision is still pretty low (1 in 10^15).
|
|
173
158
|
* @returns Returns a GUID formatted string.
|
|
@@ -196,7 +181,7 @@ export declare const ord: (x: string) => number;
|
|
|
196
181
|
* @returns The array of object keys.
|
|
197
182
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Object.keys()}
|
|
198
183
|
*/
|
|
199
|
-
export declare const keys: (object:
|
|
184
|
+
export declare const keys: <T>(object: T) => (keyof T)[];
|
|
200
185
|
/**
|
|
201
186
|
* Returns an array of the own enumerable property values of `object`.
|
|
202
187
|
* @param object The given object.
|
|
@@ -218,14 +203,15 @@ export declare const has: (obj: any, attr: PropertyKey) => boolean;
|
|
|
218
203
|
* @param {*} [value=undefined]
|
|
219
204
|
* @return The property value or default value.
|
|
220
205
|
*/
|
|
221
|
-
export declare function get(key: PropertyKey
|
|
206
|
+
export declare function get<T>(key: PropertyKey, obj: T, value?: any): any;
|
|
207
|
+
export declare function get<T>(key: PropertyKey[], obj: T, value?: any): any;
|
|
222
208
|
/**
|
|
223
209
|
* Deletes a given property key from an object.
|
|
224
210
|
* @param attr The property name.
|
|
225
211
|
* @param x The given object.
|
|
226
212
|
* @returns The modified object.
|
|
227
213
|
*/
|
|
228
|
-
export declare function del(attr: PropertyKey, x:
|
|
214
|
+
export declare function del<T>(attr: PropertyKey, x: T): T;
|
|
229
215
|
/**
|
|
230
216
|
* Adds a property to an object, or modifies the existing value.
|
|
231
217
|
* @param attr The name or Symbol of the property to be defined or modified.
|
|
@@ -242,7 +228,8 @@ export declare const HASH_KEY: unique symbol;
|
|
|
242
228
|
* @param obj The given object.
|
|
243
229
|
* @returns The hash value.
|
|
244
230
|
*/
|
|
245
|
-
export declare function hash(obj:
|
|
231
|
+
export declare function hash(obj: string): number;
|
|
232
|
+
export declare function hash<T>(obj: T): T;
|
|
246
233
|
/**
|
|
247
234
|
* A hash value generator for strings.
|
|
248
235
|
* @param {string} str The given string.
|
|
@@ -274,24 +261,25 @@ export declare const oct: (n: number) => string;
|
|
|
274
261
|
* @returns A floating point number parsed from the given value or `NaN`.
|
|
275
262
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat parseFloat}
|
|
276
263
|
*/
|
|
277
|
-
export declare const float: (x:
|
|
264
|
+
export declare const float: (x: string) => number;
|
|
278
265
|
/**
|
|
279
266
|
* Returns an integer number constructed from a number or string `x`.
|
|
280
267
|
* @param x The number or string.
|
|
281
268
|
* @returns An integer number parsed from the given value or `NaN`.
|
|
282
269
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt parseInt}
|
|
283
270
|
*/
|
|
284
|
-
export declare const int: (x:
|
|
271
|
+
export declare const int: (x: string, base?: number) => number;
|
|
285
272
|
/**
|
|
286
273
|
* Converts the value to an array using `Array.from()`.
|
|
287
274
|
* @param value
|
|
288
275
|
* @returns A new array value.
|
|
289
276
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Array.from()}
|
|
290
277
|
*/
|
|
291
|
-
export declare const list: (value?: any) =>
|
|
278
|
+
export declare const list: <T>(value?: any) => T[];
|
|
292
279
|
/**
|
|
293
280
|
* Retrieve the next item from the iterator by calling its `next()` method.
|
|
294
281
|
* @param iter The iterator.
|
|
295
282
|
* @returns The next value from the iterator.
|
|
296
283
|
*/
|
|
297
|
-
export declare
|
|
284
|
+
export declare function next<T>(iter: Iterator<T>): T | undefined;
|
|
285
|
+
export declare function next<T>(iter: Generator<any, T>): T | undefined;
|
package/dist/globals/index.js
CHANGED
|
@@ -69,8 +69,8 @@ export class ValueError extends CustomError {
|
|
|
69
69
|
export class ZeroDivisionError extends CustomError {
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
|
-
* Returns the true type of any value with correct detection for null
|
|
73
|
-
* Object
|
|
72
|
+
* Returns the true type of any value with correct detection for `null`, `Array`,
|
|
73
|
+
* `Object`, `Promise`, `Symbol`, and `NaN`. The correct values are inferred from {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag | Symbol.toStringTag} and the value's prototype.
|
|
74
74
|
* @param value
|
|
75
75
|
* @returns The value type.
|
|
76
76
|
*/
|
|
@@ -198,12 +198,12 @@ export function len(value) {
|
|
|
198
198
|
if (isNumber(value.length)) {
|
|
199
199
|
return value.length;
|
|
200
200
|
}
|
|
201
|
-
if (isFunction(value.size)) {
|
|
202
|
-
return value.size();
|
|
203
|
-
}
|
|
204
201
|
if (isNumber(value.size)) {
|
|
205
202
|
return value.size;
|
|
206
203
|
}
|
|
204
|
+
if (isFunction(value.size)) {
|
|
205
|
+
return value.size();
|
|
206
|
+
}
|
|
207
207
|
if (isObject(value)) {
|
|
208
208
|
return Object.keys(value).length;
|
|
209
209
|
}
|
|
@@ -274,14 +274,7 @@ export const values = (object) => Object.values(object);
|
|
|
274
274
|
* @returns `true` if the object has the property name.
|
|
275
275
|
*/
|
|
276
276
|
export const has = (obj, attr) => obj && attr in obj;
|
|
277
|
-
|
|
278
|
-
* Check if the attribute is present in the object or return a default value instead.
|
|
279
|
-
* @param {PropertyKey} key
|
|
280
|
-
* @param {*} obj
|
|
281
|
-
* @param {*} [value=undefined]
|
|
282
|
-
* @return The property value or default value.
|
|
283
|
-
*/
|
|
284
|
-
export function get(key, obj, value = undefined) {
|
|
277
|
+
export function get(key, obj, value) {
|
|
285
278
|
if (obj == null)
|
|
286
279
|
return value;
|
|
287
280
|
let paths = Array.isArray(key) ? key : [key];
|
|
@@ -327,11 +320,6 @@ export function set(attr, obj, value, writable = false, enumerable = false) {
|
|
|
327
320
|
});
|
|
328
321
|
}
|
|
329
322
|
export const HASH_KEY = Symbol.for('HASH_KEY');
|
|
330
|
-
/**
|
|
331
|
-
* Return a hash value for a given object. The hash will be the same for subsequent invocations.
|
|
332
|
-
* @param obj The given object.
|
|
333
|
-
* @returns The hash value.
|
|
334
|
-
*/
|
|
335
323
|
export function hash(obj) {
|
|
336
324
|
if (typeof obj === 'string') {
|
|
337
325
|
return hashCode(obj);
|
|
@@ -399,9 +387,6 @@ export const int = (x, base = 10) => parseInt(x, base);
|
|
|
399
387
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Array.from()}
|
|
400
388
|
*/
|
|
401
389
|
export const list = (value) => (value ? Array.from(value) : []);
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
* @returns The next value from the iterator.
|
|
406
|
-
*/
|
|
407
|
-
export const next = (iter) => iter.next().value;
|
|
390
|
+
export function next(iter) {
|
|
391
|
+
return iter.next().value;
|
|
392
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Decimal } from '../decimal/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Currency represents a local currency amount that can be formatted using the `Intl` apis.
|
|
4
|
+
*/
|
|
5
|
+
export declare class Currency extends Decimal {
|
|
6
|
+
private readonly locale;
|
|
7
|
+
private readonly currency;
|
|
8
|
+
private readonly numberFormat;
|
|
9
|
+
private readonly options;
|
|
10
|
+
constructor(amount: any, currency: string, locale?: string, opts?: Intl.NumberFormatOptions);
|
|
11
|
+
clone(n: any): Currency;
|
|
12
|
+
negated(): Currency;
|
|
13
|
+
add(x: any): Currency;
|
|
14
|
+
sub(x: any): Currency;
|
|
15
|
+
div(x: any): Currency;
|
|
16
|
+
mul(x: any): Currency;
|
|
17
|
+
toDecimal(): any;
|
|
18
|
+
toString(): string;
|
|
19
|
+
format(opts?: Intl.NumberFormatOptions, locale?: string): string;
|
|
20
|
+
displayName(): string;
|
|
21
|
+
accounting(): string;
|
|
22
|
+
precision(digits?: number): string;
|
|
23
|
+
eq(x: any): any;
|
|
24
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { defaults } from '../collections/index.js';
|
|
2
|
+
import { isObject } from '../globals/index.js';
|
|
3
|
+
import { Decimal } from '../decimal/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Currency represents a local currency amount that can be formatted using the `Intl` apis.
|
|
6
|
+
*/
|
|
7
|
+
export class Currency extends Decimal {
|
|
8
|
+
locale;
|
|
9
|
+
currency;
|
|
10
|
+
numberFormat;
|
|
11
|
+
options = {
|
|
12
|
+
style: 'currency',
|
|
13
|
+
currencyDisplay: 'symbol',
|
|
14
|
+
maximumFractionDigits: 20
|
|
15
|
+
};
|
|
16
|
+
constructor(amount, currency, locale, opts = {}) {
|
|
17
|
+
super(amount);
|
|
18
|
+
if (isObject(locale)) {
|
|
19
|
+
opts = locale;
|
|
20
|
+
locale = undefined;
|
|
21
|
+
}
|
|
22
|
+
const systemLocale = new Intl.NumberFormat(locale, defaults(opts, { currency }, this.options));
|
|
23
|
+
const resolved = systemLocale.resolvedOptions();
|
|
24
|
+
this.numberFormat = systemLocale;
|
|
25
|
+
this.options.currency = currency;
|
|
26
|
+
this.currency = resolved.currency;
|
|
27
|
+
this.locale = resolved.locale;
|
|
28
|
+
}
|
|
29
|
+
// @ts-expect-error overrides
|
|
30
|
+
clone(n) {
|
|
31
|
+
return new Currency(n, this.currency, this.locale, this.options);
|
|
32
|
+
}
|
|
33
|
+
// @ts-expect-error overrides
|
|
34
|
+
negated() {
|
|
35
|
+
const n = super.negated.call(this);
|
|
36
|
+
return this.clone(n);
|
|
37
|
+
}
|
|
38
|
+
// @ts-expect-error overrides
|
|
39
|
+
add(x) {
|
|
40
|
+
const n = super.add.call(this.toDecimal(), x);
|
|
41
|
+
return this.clone(n);
|
|
42
|
+
}
|
|
43
|
+
// @ts-expect-error overrides
|
|
44
|
+
sub(x) {
|
|
45
|
+
const n = super.sub.call(this.toDecimal(), x);
|
|
46
|
+
return this.clone(n);
|
|
47
|
+
}
|
|
48
|
+
// @ts-expect-error overrides
|
|
49
|
+
div(x) {
|
|
50
|
+
const n = super.div.call(this.toDecimal(), x);
|
|
51
|
+
return this.clone(n);
|
|
52
|
+
}
|
|
53
|
+
// @ts-expect-error overrides
|
|
54
|
+
mul(x) {
|
|
55
|
+
const n = super.mul.call(this.toDecimal(), x);
|
|
56
|
+
return this.clone(n);
|
|
57
|
+
}
|
|
58
|
+
toDecimal() {
|
|
59
|
+
return super.clone.call(this);
|
|
60
|
+
}
|
|
61
|
+
toString() {
|
|
62
|
+
const number = this.toNumber();
|
|
63
|
+
return this.numberFormat.format(number);
|
|
64
|
+
}
|
|
65
|
+
format(opts = {}, locale) {
|
|
66
|
+
const number = this.toNumber();
|
|
67
|
+
opts = defaults(opts, this.options);
|
|
68
|
+
return new Intl.NumberFormat(locale ?? this.locale, opts).format(number);
|
|
69
|
+
}
|
|
70
|
+
displayName() {
|
|
71
|
+
return this.format({ currencyDisplay: 'name' });
|
|
72
|
+
}
|
|
73
|
+
accounting() {
|
|
74
|
+
return this.format({ currencySign: 'accounting' });
|
|
75
|
+
}
|
|
76
|
+
precision(digits) {
|
|
77
|
+
return this.format({ maximumFractionDigits: digits });
|
|
78
|
+
}
|
|
79
|
+
eq(x) {
|
|
80
|
+
if (x?.currency && x.currency !== this.currency) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return super.eq.call(this, x);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new Date from the given arguments.
|
|
3
|
+
* @param {string | number | Date} date The date value
|
|
4
|
+
* @param {?boolean} [utc=false] Interprets the given value as a UTC date
|
|
5
|
+
* @returns {Date} The new date object
|
|
6
|
+
*/
|
|
7
|
+
export declare function getDate(date: string | number | Date, utc?: boolean): Date;
|
|
8
|
+
/**
|
|
9
|
+
* Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
|
|
10
|
+
* @param {string} fmt The format string to apply
|
|
11
|
+
* @param {number | Date} date The date value
|
|
12
|
+
* @param {?string} locale The locale to use when formatting (default is system locale).
|
|
13
|
+
* @returns {string} The string representation of date
|
|
14
|
+
* @see {@link https://strftime.org/ strftime format}
|
|
15
|
+
*/
|
|
16
|
+
export declare function format(fmt: string, date: number | Date, locale?: any): string;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
const NATIVE_DATE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/;
|
|
2
|
+
/**
|
|
3
|
+
* Returns a new Date from the given arguments.
|
|
4
|
+
* @param {string | number | Date} date The date value
|
|
5
|
+
* @param {?boolean} [utc=false] Interprets the given value as a UTC date
|
|
6
|
+
* @returns {Date} The new date object
|
|
7
|
+
*/
|
|
8
|
+
export function getDate(date, utc = false) {
|
|
9
|
+
if (date === null)
|
|
10
|
+
return new Date(NaN);
|
|
11
|
+
if (!date)
|
|
12
|
+
return new Date();
|
|
13
|
+
if (date instanceof Date)
|
|
14
|
+
return new Date(date);
|
|
15
|
+
if (typeof date === 'string' && !/Z$/i.test(date)) {
|
|
16
|
+
const d = date.match(NATIVE_DATE);
|
|
17
|
+
if (d) {
|
|
18
|
+
const m = d[2] - 1 || 0;
|
|
19
|
+
const ms = (d[7] || '0').slice(0, 3);
|
|
20
|
+
if (utc) {
|
|
21
|
+
return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
|
|
22
|
+
}
|
|
23
|
+
return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return new Date(date);
|
|
27
|
+
}
|
|
28
|
+
const n = 'numeric';
|
|
29
|
+
const s = 'short';
|
|
30
|
+
const l = 'long';
|
|
31
|
+
const dd = '2-digit';
|
|
32
|
+
const TOKEN_MAP = {
|
|
33
|
+
// weekday
|
|
34
|
+
'%A': { weekday: l },
|
|
35
|
+
'%a': { weekday: s },
|
|
36
|
+
'%aa': { weekday: n },
|
|
37
|
+
// day
|
|
38
|
+
'%d': { day: dd },
|
|
39
|
+
'%-d': { day: n },
|
|
40
|
+
// month
|
|
41
|
+
'%m': { month: dd },
|
|
42
|
+
'%-m': { month: n },
|
|
43
|
+
'%B': { month: l },
|
|
44
|
+
'%b': { month: s },
|
|
45
|
+
'%bb': { month: n },
|
|
46
|
+
// year
|
|
47
|
+
'%y': { year: n },
|
|
48
|
+
'%Y': { year: dd },
|
|
49
|
+
// hour
|
|
50
|
+
'%K': { hourCycle: 'h11', hour: dd },
|
|
51
|
+
'%I': { hourCycle: 'h12', hour: dd },
|
|
52
|
+
'%-I': { hourCycle: 'h12', hour: n },
|
|
53
|
+
'%H': { hourCycle: 'h23', hour: dd },
|
|
54
|
+
'%-H': { hourCycle: 'h23', hour: n },
|
|
55
|
+
'%k': { hourCycle: 'h24', hour: n },
|
|
56
|
+
// minutes
|
|
57
|
+
'%M': { minute: dd },
|
|
58
|
+
'%-M': { minute: n },
|
|
59
|
+
// seconds
|
|
60
|
+
'%S': { second: dd },
|
|
61
|
+
'%-S': { second: n },
|
|
62
|
+
// day period
|
|
63
|
+
'%P': { dayPeriod: l },
|
|
64
|
+
'%p': { dayPeriod: s },
|
|
65
|
+
'%pp': { dayPeriod: 'narrow' },
|
|
66
|
+
// era
|
|
67
|
+
'%N': { era: l, year: dd },
|
|
68
|
+
'%n': { era: s, year: dd },
|
|
69
|
+
'%nn': { era: n, year: 'narrow' },
|
|
70
|
+
// timeZoneName
|
|
71
|
+
'%Z': { timeZoneName: 'longGeneric', year: dd },
|
|
72
|
+
'%-Z': { timeZoneName: 'shortGeneric', year: dd },
|
|
73
|
+
'%z': { timeZoneName: 'longOffset', year: dd },
|
|
74
|
+
'%-z': { timeZoneName: 'shortOffset', year: dd },
|
|
75
|
+
'%ZZ': { timeZoneName: l, year: dd },
|
|
76
|
+
'%-ZZ': { timeZoneName: s, year: dd }
|
|
77
|
+
};
|
|
78
|
+
const HOUR_PARTS = { '%I': 1, '%-I': 1, '%K': 1, '%k': 1 };
|
|
79
|
+
const ERA_PARTS = { '%n': 1, '%N': 1, '%nn': 1 };
|
|
80
|
+
const TZ_PARTS = { '%ZZ': 1, '%-ZZ': 1, '%-z': 1, '%z': 1, '%-Z': 1, '%Z': 1 };
|
|
81
|
+
const formatter = (locale, opts) => new Intl.DateTimeFormat(locale, opts);
|
|
82
|
+
/**
|
|
83
|
+
* Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
|
|
84
|
+
* @param {string} fmt The format string to apply
|
|
85
|
+
* @param {number | Date} date The date value
|
|
86
|
+
* @param {?string} locale The locale to use when formatting (default is system locale).
|
|
87
|
+
* @returns {string} The string representation of date
|
|
88
|
+
* @see {@link https://strftime.org/ strftime format}
|
|
89
|
+
*/
|
|
90
|
+
export function format(fmt, date, locale) {
|
|
91
|
+
const results = [];
|
|
92
|
+
let dayPeriod = '';
|
|
93
|
+
let dayPeriodIndex = -1;
|
|
94
|
+
for (let i = 0; i < fmt.length; i++) {
|
|
95
|
+
let part = fmt[i];
|
|
96
|
+
if (part === '%') {
|
|
97
|
+
// check for escaped '%'
|
|
98
|
+
if (fmt[++i] === '%') {
|
|
99
|
+
results.push('%');
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (fmt[i] === '-') {
|
|
103
|
+
part += fmt[i++];
|
|
104
|
+
}
|
|
105
|
+
part += fmt[i];
|
|
106
|
+
// add any repeated characters that come after
|
|
107
|
+
while (i < fmt.length - 1 && fmt[i + 1] === fmt[i])
|
|
108
|
+
part += fmt[++i];
|
|
109
|
+
if (HOUR_PARTS[part]) {
|
|
110
|
+
const res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ');
|
|
111
|
+
// save the AM/PM for the correct spot
|
|
112
|
+
results.push(res[0]);
|
|
113
|
+
dayPeriod = res[1];
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (part === '%P' || part === '%p') {
|
|
117
|
+
// leave this flag in place for later
|
|
118
|
+
dayPeriodIndex = results.length;
|
|
119
|
+
results.push(part);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (ERA_PARTS[part] || TZ_PARTS[part]) {
|
|
123
|
+
// the formatting includes the date/year so we take what comes after
|
|
124
|
+
const res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ');
|
|
125
|
+
results.push(res.slice(1).join(' '));
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
// format any other parts
|
|
129
|
+
let res = formatter(locale, TOKEN_MAP[part]).format(date);
|
|
130
|
+
if (part === '%S' && res.length === 1)
|
|
131
|
+
res = '0' + res;
|
|
132
|
+
results.push(res);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
// output as literal
|
|
136
|
+
results.push(part);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// insert the AM/PM at the correct spot
|
|
140
|
+
if (dayPeriodIndex >= 0)
|
|
141
|
+
results[dayPeriodIndex] = dayPeriod || '';
|
|
142
|
+
return results.join('');
|
|
143
|
+
}
|
|
144
|
+
const DATETIME_DEFAULT = {};
|
|
145
|
+
/** 10/14/1983 */
|
|
146
|
+
const DATE_SHORT = {
|
|
147
|
+
year: n,
|
|
148
|
+
month: n,
|
|
149
|
+
day: n
|
|
150
|
+
};
|
|
151
|
+
/** Oct 14, 1983 */
|
|
152
|
+
const DATE_MED = {
|
|
153
|
+
year: n,
|
|
154
|
+
month: s,
|
|
155
|
+
day: n
|
|
156
|
+
};
|
|
157
|
+
/** Fri, Oct 14, 1983 */
|
|
158
|
+
const DATE_MED_WITH_WEEKDAY = {
|
|
159
|
+
year: n,
|
|
160
|
+
month: s,
|
|
161
|
+
day: n,
|
|
162
|
+
weekday: s
|
|
163
|
+
};
|
|
164
|
+
/** October 14, 1983 */
|
|
165
|
+
const DATE_FULL = {
|
|
166
|
+
year: n,
|
|
167
|
+
month: l,
|
|
168
|
+
day: n
|
|
169
|
+
};
|
|
170
|
+
/** Tuesday, October 14, 1983 */
|
|
171
|
+
const DATE_HUGE = {
|
|
172
|
+
year: n,
|
|
173
|
+
month: l,
|
|
174
|
+
day: n,
|
|
175
|
+
weekday: l
|
|
176
|
+
};
|
|
177
|
+
/** 09:30 AM */
|
|
178
|
+
const TIME_SIMPLE = {
|
|
179
|
+
hour: n,
|
|
180
|
+
minute: n
|
|
181
|
+
};
|
|
182
|
+
/** 09:30:23 AM */
|
|
183
|
+
const TIME_WITH_SECONDS = {
|
|
184
|
+
hour: n,
|
|
185
|
+
minute: n,
|
|
186
|
+
second: n
|
|
187
|
+
};
|
|
188
|
+
/** 09:30:23 AM EDT */
|
|
189
|
+
const TIME_WITH_SHORT_OFFSET = {
|
|
190
|
+
hour: n,
|
|
191
|
+
minute: n,
|
|
192
|
+
second: n,
|
|
193
|
+
timeZoneName: s
|
|
194
|
+
};
|
|
195
|
+
/** 09:30:23 AM Eastern Daylight Time */
|
|
196
|
+
const TIME_WITH_LONG_OFFSET = {
|
|
197
|
+
hour: n,
|
|
198
|
+
minute: n,
|
|
199
|
+
second: n,
|
|
200
|
+
timeZoneName: l
|
|
201
|
+
};
|
|
202
|
+
/** 09:30 */
|
|
203
|
+
const TIME_24_SIMPLE = {
|
|
204
|
+
hour: n,
|
|
205
|
+
minute: n,
|
|
206
|
+
hourCycle: 'h23'
|
|
207
|
+
};
|
|
208
|
+
/** 09:30:23 */
|
|
209
|
+
const TIME_24_WITH_SECONDS = {
|
|
210
|
+
hour: n,
|
|
211
|
+
minute: n,
|
|
212
|
+
second: n,
|
|
213
|
+
hourCycle: 'h23'
|
|
214
|
+
};
|
|
215
|
+
/** 09:30:23 EDT */
|
|
216
|
+
const TIME_24_WITH_SHORT_OFFSET = {
|
|
217
|
+
hour: n,
|
|
218
|
+
minute: n,
|
|
219
|
+
second: n,
|
|
220
|
+
hourCycle: 'h23',
|
|
221
|
+
timeZoneName: s
|
|
222
|
+
};
|
|
223
|
+
/** 09:30:23 Eastern Daylight Time */
|
|
224
|
+
const TIME_24_WITH_LONG_OFFSET = {
|
|
225
|
+
hour: n,
|
|
226
|
+
minute: n,
|
|
227
|
+
second: n,
|
|
228
|
+
hourCycle: 'h23',
|
|
229
|
+
timeZoneName: l
|
|
230
|
+
};
|
|
231
|
+
/** 10/14/1983, 9:30 AM */
|
|
232
|
+
const DATETIME_SHORT = {
|
|
233
|
+
year: n,
|
|
234
|
+
month: n,
|
|
235
|
+
day: n,
|
|
236
|
+
hour: n,
|
|
237
|
+
minute: n
|
|
238
|
+
};
|
|
239
|
+
/** 10/14/1983, 9:30:33 AM */
|
|
240
|
+
const DATETIME_SHORT_WITH_SECONDS = {
|
|
241
|
+
year: n,
|
|
242
|
+
month: n,
|
|
243
|
+
day: n,
|
|
244
|
+
hour: n,
|
|
245
|
+
minute: n,
|
|
246
|
+
second: n
|
|
247
|
+
};
|
|
248
|
+
/** Oct 14, 1983, 9:30 AM */
|
|
249
|
+
const DATETIME_MED = {
|
|
250
|
+
year: n,
|
|
251
|
+
month: s,
|
|
252
|
+
day: n,
|
|
253
|
+
hour: n,
|
|
254
|
+
minute: n
|
|
255
|
+
};
|
|
256
|
+
/** Oct 14, 1983, 9:30:33 AM */
|
|
257
|
+
const DATETIME_MED_WITH_SECONDS = {
|
|
258
|
+
year: n,
|
|
259
|
+
month: s,
|
|
260
|
+
day: n,
|
|
261
|
+
hour: n,
|
|
262
|
+
minute: n,
|
|
263
|
+
second: n
|
|
264
|
+
};
|
|
265
|
+
/** Fri, 14 Oct 1983, 9:30 AM */
|
|
266
|
+
const DATETIME_MED_WITH_WEEKDAY = {
|
|
267
|
+
year: n,
|
|
268
|
+
month: s,
|
|
269
|
+
day: n,
|
|
270
|
+
weekday: s,
|
|
271
|
+
hour: n,
|
|
272
|
+
minute: n
|
|
273
|
+
};
|
|
274
|
+
/** October 14, 1983, 9:30 AM EDT */
|
|
275
|
+
const DATETIME_FULL = {
|
|
276
|
+
year: n,
|
|
277
|
+
month: l,
|
|
278
|
+
day: n,
|
|
279
|
+
hour: n,
|
|
280
|
+
minute: n,
|
|
281
|
+
timeZoneName: s
|
|
282
|
+
};
|
|
283
|
+
/** October 14, 1983, 9:30:33 AM EDT */
|
|
284
|
+
const DATETIME_FULL_WITH_SECONDS = {
|
|
285
|
+
year: n,
|
|
286
|
+
month: l,
|
|
287
|
+
day: n,
|
|
288
|
+
hour: n,
|
|
289
|
+
minute: n,
|
|
290
|
+
second: n,
|
|
291
|
+
timeZoneName: s
|
|
292
|
+
};
|
|
293
|
+
/** Friday, October 14, 1983, 9:30 AM Eastern Daylight Time */
|
|
294
|
+
const DATETIME_HUGE = {
|
|
295
|
+
year: n,
|
|
296
|
+
month: l,
|
|
297
|
+
day: n,
|
|
298
|
+
weekday: l,
|
|
299
|
+
hour: n,
|
|
300
|
+
minute: n,
|
|
301
|
+
timeZoneName: l
|
|
302
|
+
};
|
|
303
|
+
/** Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time */
|
|
304
|
+
const DATETIME_HUGE_WITH_SECONDS = {
|
|
305
|
+
year: n,
|
|
306
|
+
month: l,
|
|
307
|
+
day: n,
|
|
308
|
+
weekday: l,
|
|
309
|
+
hour: n,
|
|
310
|
+
minute: n,
|
|
311
|
+
second: n,
|
|
312
|
+
timeZoneName: l
|
|
313
|
+
};
|