atomic-fns 0.3.3 → 0.3.4

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.
Files changed (43) hide show
  1. package/README.md +17 -3
  2. package/core/collections/LRUCache.d.ts +22 -1
  3. package/core/collections/LRUCache.js +22 -1
  4. package/core/collections/LRUCache.ts +22 -1
  5. package/core/collections/SortedMap.d.ts +1 -1
  6. package/core/collections/SortedMap.js +1 -1
  7. package/core/collections/SortedMap.ts +1 -1
  8. package/core/collections/SortedSet.d.ts +6 -0
  9. package/core/collections/SortedSet.js +6 -0
  10. package/core/collections/SortedSet.ts +6 -0
  11. package/core/collections/SortedTree.d.ts +3 -3
  12. package/core/collections/SortedTree.js +3 -3
  13. package/core/collections/SortedTree.ts +3 -3
  14. package/core/collections/index.d.ts +2 -0
  15. package/core/collections/index.js +7 -5
  16. package/core/collections/index.ts +7 -5
  17. package/core/functools/index.d.ts +2 -2
  18. package/core/functools/index.ts +6 -3
  19. package/core/globals/index.d.ts +22 -3
  20. package/core/globals/index.js +28 -5
  21. package/core/globals/index.ts +39 -6
  22. package/core/i18n/date/format.d.ts +16 -0
  23. package/core/i18n/date/format.js +307 -0
  24. package/core/i18n/date/format.ts +343 -0
  25. package/core/i18n/date/index.d.ts +223 -0
  26. package/core/i18n/date/index.js +387 -0
  27. package/core/i18n/date/index.ts +471 -0
  28. package/core/i18n/date/test.html +67 -0
  29. package/core/i18n/index.d.ts +7 -0
  30. package/core/i18n/index.js +7 -0
  31. package/core/i18n/index.ts +8 -0
  32. package/core/index.d.ts +1 -0
  33. package/core/index.js +1 -0
  34. package/core/index.ts +1 -0
  35. package/core/operators/index.d.ts +1 -1
  36. package/core/operators/index.js +35 -5
  37. package/core/operators/index.ts +34 -6
  38. package/package.json +11 -11
  39. package/tests/globals.spec.ts +9 -9
  40. package/tsconfig.json +2 -1
  41. package/core/datetime/index.d.ts +0 -45
  42. package/core/datetime/index.js +0 -71
  43. package/core/datetime/index.ts +0 -128
@@ -0,0 +1,343 @@
1
+ const NATIVE_DATE =
2
+ /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/
3
+
4
+ /**
5
+ * Returns a new Date from the given arguments.
6
+ * @param {string | number | Date} date The date value
7
+ * @param {?boolean} [utc=false] Interprets the given value as a UTC date
8
+ * @returns {Date} The new date object
9
+ */
10
+ export function getDate(date: string | number | Date, utc = false) {
11
+ if (date === null) return new Date(NaN)
12
+ if (!date) return new Date()
13
+ if (date instanceof Date) return new Date(date)
14
+ if (typeof date === 'string' && !/Z$/i.test(date)) {
15
+ const d = date.match(NATIVE_DATE) as any
16
+ if (d) {
17
+ const m = d[2] - 1 || 0
18
+ const ms = (d[7] || '0').slice(0, 3)
19
+ if (utc) {
20
+ return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
21
+ }
22
+ return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
23
+ }
24
+ }
25
+ return new Date(date)
26
+ }
27
+
28
+ const n = 'numeric',
29
+ s = 'short',
30
+ l = 'long',
31
+ dd = '2-digit',
32
+ 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
+
79
+ const HOUR_PARTS = { '%I': 1, '%-I': 1, '%K': 1, '%k': 1 }
80
+ const ERA_PARTS = { '%n': 1, '%N': 1, '%nn': 1 }
81
+ const TZ_PARTS = { '%ZZ': 1, '%-ZZ': 1, '%-z': 1, '%z': 1, '%-Z': 1, '%Z': 1 }
82
+
83
+ const formatter = (locale: string, opts) => new Intl.DateTimeFormat(locale, opts)
84
+
85
+ /**
86
+ * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
87
+ * @param {string} fmt The format string to apply
88
+ * @param {number | Date} date The date value
89
+ * @param {?string} locale The locale to use when formatting (default is system locale).
90
+ * @returns {string} The string representation of date
91
+ * @see {@link https://strftime.org/ strftime format}
92
+ */
93
+ export function format(fmt: string, date: number | Date, locale?) {
94
+ let results: string[] = [],
95
+ dayPeriod: string = '',
96
+ dayPeriodIndex = -1
97
+ for (let i = 0; i < fmt.length; i++) {
98
+ let part = fmt[i]
99
+ if (part === '%') {
100
+ // check for escaped '%'
101
+ if (fmt[++i] == '%') {
102
+ results.push('%')
103
+ continue
104
+ }
105
+
106
+ if (fmt[i] === '-') {
107
+ part += fmt[i++]
108
+ }
109
+
110
+ part += fmt[i]
111
+ // add any repeated characters that come after
112
+ while (i < fmt.length - 1 && fmt[i + 1] === fmt[i]) part += fmt[++i]
113
+
114
+ if (HOUR_PARTS[part]) {
115
+ let res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ')
116
+ // save the AM/PM for the correct spot
117
+ results.push(res[0])
118
+ dayPeriod = res[1]
119
+ continue
120
+ }
121
+
122
+ if (part === '%P' || part === '%p') {
123
+ // leave this flag in place for later
124
+ dayPeriodIndex = results.length
125
+ results.push(part)
126
+ continue
127
+ }
128
+
129
+ if (ERA_PARTS[part] || TZ_PARTS[part]) {
130
+ // the formatting includes the date/year so we take what comes after
131
+ let res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ')
132
+ results.push(res.slice(1).join(' '))
133
+ continue
134
+ }
135
+
136
+ // format any other parts
137
+ let res = formatter(locale, TOKEN_MAP[part]).format(date)
138
+ if (part === '%S' && res.length === 1) res = '0' + res
139
+ results.push(res)
140
+ } else {
141
+ // output as literal
142
+ results.push(part)
143
+ }
144
+ }
145
+
146
+ // insert the AM/PM at the correct spot
147
+ if (dayPeriodIndex >= 0) results[dayPeriodIndex] = dayPeriod || ''
148
+
149
+ return results.join('')
150
+ }
151
+
152
+ const DATETIME_DEFAULT = {}
153
+
154
+ /** 10/14/1983 */
155
+ const DATE_SHORT = {
156
+ year: n,
157
+ month: n,
158
+ day: n
159
+ }
160
+
161
+ /** Oct 14, 1983 */
162
+ const DATE_MED = {
163
+ year: n,
164
+ month: s,
165
+ day: n
166
+ }
167
+
168
+ /** Fri, Oct 14, 1983 */
169
+ const DATE_MED_WITH_WEEKDAY = {
170
+ year: n,
171
+ month: s,
172
+ day: n,
173
+ weekday: s
174
+ }
175
+
176
+ /** October 14, 1983 */
177
+ const DATE_FULL = {
178
+ year: n,
179
+ month: l,
180
+ day: n
181
+ }
182
+
183
+ /** Tuesday, October 14, 1983 */
184
+ const DATE_HUGE = {
185
+ year: n,
186
+ month: l,
187
+ day: n,
188
+ weekday: l
189
+ }
190
+
191
+ /** 09:30 AM */
192
+ const TIME_SIMPLE = {
193
+ hour: n,
194
+ minute: n
195
+ }
196
+
197
+ /** 09:30:23 AM */
198
+ const TIME_WITH_SECONDS = {
199
+ hour: n,
200
+ minute: n,
201
+ second: n
202
+ }
203
+
204
+ /** 09:30:23 AM EDT */
205
+ const TIME_WITH_SHORT_OFFSET = {
206
+ hour: n,
207
+ minute: n,
208
+ second: n,
209
+ timeZoneName: s
210
+ }
211
+
212
+ /** 09:30:23 AM Eastern Daylight Time */
213
+ const TIME_WITH_LONG_OFFSET = {
214
+ hour: n,
215
+ minute: n,
216
+ second: n,
217
+ timeZoneName: l
218
+ }
219
+
220
+ /** 09:30 */
221
+ const TIME_24_SIMPLE = {
222
+ hour: n,
223
+ minute: n,
224
+ hourCycle: 'h23'
225
+ }
226
+
227
+ /** 09:30:23 */
228
+ const TIME_24_WITH_SECONDS = {
229
+ hour: n,
230
+ minute: n,
231
+ second: n,
232
+ hourCycle: 'h23'
233
+ }
234
+
235
+ /** 09:30:23 EDT */
236
+ const TIME_24_WITH_SHORT_OFFSET = {
237
+ hour: n,
238
+ minute: n,
239
+ second: n,
240
+ hourCycle: 'h23',
241
+ timeZoneName: s
242
+ }
243
+
244
+ /** 09:30:23 Eastern Daylight Time */
245
+ const TIME_24_WITH_LONG_OFFSET = {
246
+ hour: n,
247
+ minute: n,
248
+ second: n,
249
+ hourCycle: 'h23',
250
+ timeZoneName: l
251
+ }
252
+
253
+ /** 10/14/1983, 9:30 AM */
254
+ const DATETIME_SHORT = {
255
+ year: n,
256
+ month: n,
257
+ day: n,
258
+ hour: n,
259
+ minute: n
260
+ }
261
+
262
+ /** 10/14/1983, 9:30:33 AM */
263
+ const DATETIME_SHORT_WITH_SECONDS = {
264
+ year: n,
265
+ month: n,
266
+ day: n,
267
+ hour: n,
268
+ minute: n,
269
+ second: n
270
+ }
271
+
272
+ /** Oct 14, 1983, 9:30 AM */
273
+ const DATETIME_MED = {
274
+ year: n,
275
+ month: s,
276
+ day: n,
277
+ hour: n,
278
+ minute: n
279
+ }
280
+
281
+ /** Oct 14, 1983, 9:30:33 AM */
282
+ const DATETIME_MED_WITH_SECONDS = {
283
+ year: n,
284
+ month: s,
285
+ day: n,
286
+ hour: n,
287
+ minute: n,
288
+ second: n
289
+ }
290
+
291
+ /** Fri, 14 Oct 1983, 9:30 AM */
292
+ const DATETIME_MED_WITH_WEEKDAY = {
293
+ year: n,
294
+ month: s,
295
+ day: n,
296
+ weekday: s,
297
+ hour: n,
298
+ minute: n
299
+ }
300
+
301
+ /** October 14, 1983, 9:30 AM EDT */
302
+ const DATETIME_FULL = {
303
+ year: n,
304
+ month: l,
305
+ day: n,
306
+ hour: n,
307
+ minute: n,
308
+ timeZoneName: s
309
+ }
310
+
311
+ /** October 14, 1983, 9:30:33 AM EDT */
312
+ const DATETIME_FULL_WITH_SECONDS = {
313
+ year: n,
314
+ month: l,
315
+ day: n,
316
+ hour: n,
317
+ minute: n,
318
+ second: n,
319
+ timeZoneName: s
320
+ }
321
+
322
+ /** Friday, October 14, 1983, 9:30 AM Eastern Daylight Time */
323
+ const DATETIME_HUGE = {
324
+ year: n,
325
+ month: l,
326
+ day: n,
327
+ weekday: l,
328
+ hour: n,
329
+ minute: n,
330
+ timeZoneName: l
331
+ }
332
+
333
+ /** Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time */
334
+ const DATETIME_HUGE_WITH_SECONDS = {
335
+ year: n,
336
+ month: l,
337
+ day: n,
338
+ weekday: l,
339
+ hour: n,
340
+ minute: n,
341
+ second: n,
342
+ timeZoneName: l
343
+ }
@@ -0,0 +1,223 @@
1
+ export declare type DateParts = {
2
+ year?: number;
3
+ month?: number;
4
+ day?: number;
5
+ hour?: number;
6
+ minute?: number;
7
+ second?: number;
8
+ millisecond?: number;
9
+ };
10
+ export declare type DateLike = number | string | Date | DateParts;
11
+ /**
12
+ * Creates a date tied to a given locale (default system locale) which can be formatted in that locale's language using the native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} directly or using a formatting string compatible with `strftime`.
13
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis}
14
+ * @see {@link https://strftime.org/ strftime format}
15
+ */
16
+ export declare class IntlDate {
17
+ private self;
18
+ readonly year: number;
19
+ readonly M: number;
20
+ readonly day: number;
21
+ readonly weekDay: number;
22
+ readonly hour: number;
23
+ readonly minute: number;
24
+ readonly second: number;
25
+ readonly millisecond: number;
26
+ readonly locale: string;
27
+ /** Returns the current UTC date and time. */
28
+ static UTC(obj?: DateLike): IntlDate;
29
+ /** Returns the current local date and time.*/
30
+ static now(): IntlDate;
31
+ /**
32
+ * Creates a new `IntlDate` tied to the specified locale (default is system locale).
33
+ * **Note:** The allowed values for `month` start at 1, which is different from legacy `Date`
34
+ * @param {DateLike | IntlDate} obj The date value (default is now)
35
+ * @param {{locale?: string, utc?: boolean}} opts
36
+ * @returns
37
+ */
38
+ constructor(obj?: DateLike | IntlDate, { locale, utc }?: any);
39
+ isValid(): boolean;
40
+ /** Get the this date's month as a number from 1-12. */
41
+ get month(): number;
42
+ /** Get the number of days in this date's month. */
43
+ daysInMonth(): number | null;
44
+ /** Get the number of days in this date's year. */
45
+ daysInYear(): 366 | 365;
46
+ /** Returns `true` if this date's year is a leap year, otherwise `false`. */
47
+ isLeapYear(): boolean;
48
+ /** Gets the number of weeks according to locale in the current moment's year. */
49
+ weeksInYear(): 53 | 52;
50
+ /**
51
+ * Returns a localized string representation of this date.
52
+ * @param {Intl.DateTimeFormatOptions} opts The options to use for the format
53
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat Intl.DateTimeFormat()}
54
+ */
55
+ toString(opts?: Intl.DateTimeFormatOptions): string;
56
+ /**
57
+ * Returns a localized string representation of this date, according to the given format string. Format codes use the same specification as C / Python's strftime().
58
+ * @param {string} str The format string to use
59
+ * @see {@link https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes Format Codes}
60
+ * @example
61
+ ```js
62
+ new IntlDate().format('%m/%d/%y') // '10/31/2022'
63
+ ```
64
+ */
65
+ format(str: string): string;
66
+ /**
67
+ * Returns a new copy of the native `Date` object used by this instance.
68
+ * @returns {Date} A new `Date` object
69
+ */
70
+ toDate(): Date;
71
+ /**
72
+ * Returns the timestamp representing the UTC equivalent of this date.
73
+ * @returns {number} The UTC timestamp of this date.
74
+ */
75
+ toUTC(): number;
76
+ /**
77
+ * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
78
+ * @see {@link IntlDate.timestamp}
79
+ * @see {@link IntlDate.unix}
80
+ * @example
81
+ ```js
82
+ new IntlDate().toSeconds() // 1318874398
83
+ ```
84
+ */
85
+ toSeconds(): number;
86
+ /**
87
+ * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
88
+ * @see {@link IntlDate.toSeconds}
89
+ * @see {@link IntlDate.unix}
90
+ * @example
91
+ ```js
92
+ new IntlDate().timestamp() // 1318874398
93
+ ```
94
+ */
95
+ timestamp(): number;
96
+ /**
97
+ * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
98
+ * @see {@link IntlDate.toSeconds}
99
+ * @see {@link IntlDate.timestamp}
100
+ * @example
101
+ ```js
102
+ new IntlDate().unix() // 1318874398
103
+ ```
104
+ */
105
+ unix(): number;
106
+ /** Formats a string to the ISO8601 standard.
107
+ * @example
108
+ ```js
109
+ new IntlDate().toISOString() // '2022-10-31T22:44:30.652Z'
110
+ ```
111
+ */
112
+ toISOString(): string;
113
+ /** Returns the date part formatted as ISO8601.
114
+ * @example
115
+ ```js
116
+ new IntlDate().toISODate() // '2022-10-31'
117
+ ```
118
+ */
119
+ toISODate(): string;
120
+ /** Returns the time part formatted as ISO8601.
121
+ * @example
122
+ ```js
123
+ new IntlDate().toISOTime() // 'T22:44:30.652Z'
124
+ ```
125
+ */
126
+ toISOTime(): string;
127
+ /**
128
+ * Returns an object containing year, month, day-of-month, hours, minutes, seconds, milliseconds.
129
+ * @returns {Object} An object like `{year, month, date, hours, minutes, seconds, ms}`
130
+ */
131
+ toObject(): DateParts;
132
+ /** Returns the number of milliseconds since the Unix Epoch (January 1, 1970 UTC) */
133
+ valueOf(): number;
134
+ /** Returns the timezone string name */
135
+ zoneName(): string;
136
+ /** Returns the timezone GMT offset name */
137
+ offset(): string;
138
+ /**
139
+ * Check if this date is before another date. The other value will be parsed as an `IntlDate` if not already so.
140
+ * @param {string | number | Object | Date | IntlDate} other Another date or date like object
141
+ * @returns {boolean} Returns `true` if this date is before the given value
142
+ */
143
+ isBefore(other: DateLike | IntlDate): boolean;
144
+ /**
145
+ * Check if this date is strictly after the given `start` and before `stop` dates. The values will be parsed as an `IntlDate` if not already so.
146
+ * @param {string | number | Object | Date | IntlDate} start The smaller date
147
+ * @param {string | number | Object | Date | IntlDate} stop The larger date
148
+ * @returns {boolean} Returns `true` if this date is greater than `start` and less than `stop`.
149
+ */
150
+ isBetween(start: DateLike | IntlDate, stop: DateLike | IntlDate): boolean;
151
+ /**
152
+ * Check if this date is after another date. The other value will be parsed as an `IntlDate` if not already so.
153
+ * @param {string | number | DateParts | Date | IntlDate} other Another date or date like object
154
+ * @returns {boolean} Returns `true` if this date is after the given value
155
+ */
156
+ isAfter(other: DateLike | IntlDate): boolean;
157
+ /**
158
+ * Check if this date is the same as `other`.
159
+ * @param other Another date object
160
+ * @returns {boolean} Returns `true` if this date is the same as other
161
+ */
162
+ isSame(other: Date | IntlDate): boolean;
163
+ /**
164
+ * Returns the minimum (most distant past) of the given date values.
165
+ * @param {Array<Date | IntlDate>} dates
166
+ * @returns {Date | IntlDate} The smallest date of `dates`
167
+ * @see {@link minDate}
168
+ */
169
+ static min(...dates: Array<Date | IntlDate>): Date | IntlDate;
170
+ /**
171
+ * Returns the maximum (most distant future) of the given date values.
172
+ * @param {Array<Date | IntlDate>} dates
173
+ * @returns {Date | IntlDate} The largest date of `dates`
174
+ * @see {@link maxDate}
175
+ */
176
+ static max(...dates: Array<Date | IntlDate>): Date | IntlDate;
177
+ lt(other: Date | IntlDate): boolean;
178
+ eq(other: Date | IntlDate): boolean;
179
+ compare(other: Date | IntlDate): 0 | 1 | -1;
180
+ }
181
+ /**
182
+ * Converts the given date to a UTC timestamp.
183
+ * @param {Date} date
184
+ * @returns {number} The milliseconds between the specified date and the UTC Epoch date.
185
+ */
186
+ export declare function dateToUTC(date: Date): number;
187
+ /**
188
+ * Returns the minimum (most distant past) of the given date values.
189
+ * @param {Array<Date | IntlDate>} dates
190
+ * @returns {Date | IntlDate} The smallest date of `dates`
191
+ */
192
+ export declare function minDate(...dates: Array<Date | IntlDate>): Date | IntlDate;
193
+ /**
194
+ * Returns the maximum (most distant future) of the given date values.
195
+ * @param {Array<Date | IntlDate>} dates
196
+ * @returns {Date | IntlDate} The largest date of `dates`
197
+ */
198
+ export declare function maxDate(...dates: Array<Date | IntlDate>): Date | IntlDate;
199
+ /**
200
+ * Returns `true` if the year is a leap year, otherwise `false`.
201
+ * @param year The date year
202
+ * @returns {boolean} `true` if is a leap year
203
+ */
204
+ export declare function isLeapYear(year: number): boolean;
205
+ /**
206
+ * Get the number of days in the given year.
207
+ * @param year The date year
208
+ * @returns {number} Number of days in year
209
+ */
210
+ export declare function daysInYear(year: number): 366 | 365;
211
+ /**
212
+ * Get the number of days in the given month and year.
213
+ * @param year The date year
214
+ * @param month The date month
215
+ * @returns {number} Number of days in month
216
+ */
217
+ export declare function daysInMonth(year: number, month: number): number | null;
218
+ /**
219
+ * Gets the number of weeks according to locale in the current moment's year.
220
+ * @param {number} weekYear The the date year
221
+ * @returns {number}
222
+ */
223
+ export declare function weeksInYear(weekYear: number): 53 | 52;