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,387 @@
1
+ import { isObject } from '../../globals/index.js';
2
+ import { format, getDate } from './format.js';
3
+ let globalOptions;
4
+ const systemOptions = () => {
5
+ if (!globalOptions)
6
+ globalOptions = Intl.DateTimeFormat().resolvedOptions();
7
+ return globalOptions;
8
+ };
9
+ const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ';
10
+ const INVALID_DATE_STRING = 'Invalid Date';
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 class IntlDate {
17
+ self;
18
+ year;
19
+ M;
20
+ day;
21
+ weekDay;
22
+ hour;
23
+ minute;
24
+ second;
25
+ millisecond;
26
+ locale;
27
+ /** Returns the current UTC date and time. */
28
+ static UTC(obj = {}) {
29
+ return new IntlDate(obj, { utc: true });
30
+ }
31
+ /** Returns the current local date and time.*/
32
+ static now() {
33
+ return new IntlDate();
34
+ }
35
+ /**
36
+ * Creates a new `IntlDate` tied to the specified locale (default is system locale).
37
+ * **Note:** The allowed values for `month` start at 1, which is different from legacy `Date`
38
+ * @param {DateLike | IntlDate} obj The date value (default is now)
39
+ * @param {{locale?: string, utc?: boolean}} opts
40
+ * @returns
41
+ */
42
+ constructor(obj, { locale, utc } = {}) {
43
+ // Set the default DateTime locale
44
+ this.locale = locale || systemOptions().locale;
45
+ if (obj instanceof IntlDate)
46
+ obj = obj.self;
47
+ if (typeof obj === 'string' || typeof obj === 'number' || obj instanceof Date) {
48
+ this.self = getDate(obj, utc);
49
+ }
50
+ else if (isObject(obj)) {
51
+ let { year, month, day, hour, minute, second, millisecond } = obj;
52
+ // NOTE month is from 1-12, not from 0-11
53
+ if (typeof month === 'number')
54
+ month -= 1;
55
+ if (utc) {
56
+ // from UTC to current
57
+ this.self = new Date(Date.UTC(year, month, day, hour, minute, second, millisecond));
58
+ }
59
+ else {
60
+ this.self = new Date(year, month, day, hour, minute, second, millisecond);
61
+ }
62
+ }
63
+ else {
64
+ this.self = new Date();
65
+ }
66
+ this.year = this.self.getFullYear();
67
+ this.M = this.self.getMonth();
68
+ this.weekDay = this.self.getDay();
69
+ this.day = this.self.getDate();
70
+ this.hour = this.self.getHours();
71
+ this.minute = this.self.getMinutes();
72
+ this.second = this.self.getSeconds();
73
+ this.millisecond = this.self.getMilliseconds();
74
+ return this;
75
+ }
76
+ isValid() {
77
+ return this.self.toString() !== INVALID_DATE_STRING;
78
+ }
79
+ /** Get the this date's month as a number from 1-12. */
80
+ get month() {
81
+ return this.M + 1;
82
+ }
83
+ /** Get the number of days in this date's month. */
84
+ daysInMonth() {
85
+ return daysInMonth(this.year, this.M);
86
+ }
87
+ /** Get the number of days in this date's year. */
88
+ daysInYear() {
89
+ return daysInYear(this.year);
90
+ }
91
+ /** Returns `true` if this date's year is a leap year, otherwise `false`. */
92
+ isLeapYear() {
93
+ return isLeapYear(this.year);
94
+ }
95
+ /** Gets the number of weeks according to locale in the current moment's year. */
96
+ weeksInYear() {
97
+ return weeksInYear(this.year);
98
+ }
99
+ /**
100
+ * Returns a localized string representation of this date.
101
+ * @param {Intl.DateTimeFormatOptions} opts The options to use for the format
102
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat Intl.DateTimeFormat()}
103
+ */
104
+ toString(opts) {
105
+ return this.self.toLocaleDateString(this.locale, opts);
106
+ }
107
+ /**
108
+ * 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().
109
+ * @param {string} str The format string to use
110
+ * @see {@link https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes Format Codes}
111
+ * @example
112
+ ```js
113
+ new IntlDate().format('%m/%d/%y') // '10/31/2022'
114
+ ```
115
+ */
116
+ format(str) {
117
+ return format(str, this.self, this.locale);
118
+ }
119
+ /**
120
+ * Returns a new copy of the native `Date` object used by this instance.
121
+ * @returns {Date} A new `Date` object
122
+ */
123
+ toDate() {
124
+ return new Date(this.isValid() ? this.self : NaN);
125
+ }
126
+ /**
127
+ * Returns the timestamp representing the UTC equivalent of this date.
128
+ * @returns {number} The UTC timestamp of this date.
129
+ */
130
+ toUTC() {
131
+ return dateToUTC(this.self);
132
+ }
133
+ /**
134
+ * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
135
+ * @see {@link IntlDate.timestamp}
136
+ * @see {@link IntlDate.unix}
137
+ * @example
138
+ ```js
139
+ new IntlDate().toSeconds() // 1318874398
140
+ ```
141
+ */
142
+ toSeconds() {
143
+ return Math.trunc(this.valueOf() / 1000);
144
+ }
145
+ /**
146
+ * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
147
+ * @see {@link IntlDate.toSeconds}
148
+ * @see {@link IntlDate.unix}
149
+ * @example
150
+ ```js
151
+ new IntlDate().timestamp() // 1318874398
152
+ ```
153
+ */
154
+ timestamp() {
155
+ return this.toSeconds();
156
+ }
157
+ /**
158
+ * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
159
+ * @see {@link IntlDate.toSeconds}
160
+ * @see {@link IntlDate.timestamp}
161
+ * @example
162
+ ```js
163
+ new IntlDate().unix() // 1318874398
164
+ ```
165
+ */
166
+ unix() {
167
+ return this.toSeconds();
168
+ }
169
+ /** Formats a string to the ISO8601 standard.
170
+ * @example
171
+ ```js
172
+ new IntlDate().toISOString() // '2022-10-31T22:44:30.652Z'
173
+ ```
174
+ */
175
+ toISOString() {
176
+ return this.self.toISOString();
177
+ }
178
+ /** Returns the date part formatted as ISO8601.
179
+ * @example
180
+ ```js
181
+ new IntlDate().toISODate() // '2022-10-31'
182
+ ```
183
+ */
184
+ toISODate() {
185
+ return this.self.toISOString().slice(0, 10);
186
+ }
187
+ /** Returns the time part formatted as ISO8601.
188
+ * @example
189
+ ```js
190
+ new IntlDate().toISOTime() // 'T22:44:30.652Z'
191
+ ```
192
+ */
193
+ toISOTime() {
194
+ return this.self.toISOString().slice(11);
195
+ }
196
+ /**
197
+ * Returns an object containing year, month, day-of-month, hours, minutes, seconds, milliseconds.
198
+ * @returns {Object} An object like `{year, month, date, hours, minutes, seconds, ms}`
199
+ */
200
+ toObject() {
201
+ return {
202
+ year: this.year,
203
+ month: this.month,
204
+ day: this.day,
205
+ hour: this.hour,
206
+ minute: this.M,
207
+ second: this.second,
208
+ millisecond: this.millisecond
209
+ };
210
+ }
211
+ /** Returns the number of milliseconds since the Unix Epoch (January 1, 1970 UTC) */
212
+ valueOf() {
213
+ return this.isValid() ? this.self.getTime() : NaN;
214
+ }
215
+ /** Returns the timezone string name */
216
+ zoneName() {
217
+ return this.format('%Z');
218
+ }
219
+ /** Returns the timezone GMT offset name */
220
+ offset() {
221
+ return this.format('%z');
222
+ }
223
+ /**
224
+ * Check if this date is before another date. The other value will be parsed as an `IntlDate` if not already so.
225
+ * @param {string | number | Object | Date | IntlDate} other Another date or date like object
226
+ * @returns {boolean} Returns `true` if this date is before the given value
227
+ */
228
+ isBefore(other) {
229
+ if (!this.isValid())
230
+ return false;
231
+ if (other instanceof IntlDate) {
232
+ return this.valueOf() < other.valueOf();
233
+ }
234
+ return this.valueOf() < new IntlDate(other).valueOf();
235
+ }
236
+ /**
237
+ * 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.
238
+ * @param {string | number | Object | Date | IntlDate} start The smaller date
239
+ * @param {string | number | Object | Date | IntlDate} stop The larger date
240
+ * @returns {boolean} Returns `true` if this date is greater than `start` and less than `stop`.
241
+ */
242
+ isBetween(start, stop) {
243
+ return this.isAfter(start) && this.isBefore(stop);
244
+ }
245
+ /**
246
+ * Check if this date is after another date. The other value will be parsed as an `IntlDate` if not already so.
247
+ * @param {string | number | DateParts | Date | IntlDate} other Another date or date like object
248
+ * @returns {boolean} Returns `true` if this date is after the given value
249
+ */
250
+ isAfter(other) {
251
+ if (!this.isValid())
252
+ return false;
253
+ if (other instanceof IntlDate) {
254
+ return this.valueOf() > other.valueOf();
255
+ }
256
+ return this.valueOf() > new IntlDate(other).valueOf();
257
+ }
258
+ /**
259
+ * Check if this date is the same as `other`.
260
+ * @param other Another date object
261
+ * @returns {boolean} Returns `true` if this date is the same as other
262
+ */
263
+ isSame(other) {
264
+ if (other instanceof IntlDate) {
265
+ return (this.isValid() &&
266
+ other.isValid() &&
267
+ this.valueOf() === other.valueOf() &&
268
+ this.locale === other.locale);
269
+ }
270
+ return this.valueOf() === other.valueOf();
271
+ }
272
+ /**
273
+ * Returns the minimum (most distant past) of the given date values.
274
+ * @param {Array<Date | IntlDate>} dates
275
+ * @returns {Date | IntlDate} The smallest date of `dates`
276
+ * @see {@link minDate}
277
+ */
278
+ static min(...dates) {
279
+ return minDate(...dates);
280
+ }
281
+ /**
282
+ * Returns the maximum (most distant future) of the given date values.
283
+ * @param {Array<Date | IntlDate>} dates
284
+ * @returns {Date | IntlDate} The largest date of `dates`
285
+ * @see {@link maxDate}
286
+ */
287
+ static max(...dates) {
288
+ return maxDate(...dates);
289
+ }
290
+ lt(other) {
291
+ return this.isBefore(other);
292
+ }
293
+ eq(other) {
294
+ return this.isSame(other);
295
+ }
296
+ compare(other) {
297
+ if (this.isBefore(other))
298
+ return -1;
299
+ if (this.isAfter(other))
300
+ return 1;
301
+ return 0;
302
+ }
303
+ }
304
+ /**
305
+ * Converts the given date to a UTC timestamp.
306
+ * @param {Date} date
307
+ * @returns {number} The milliseconds between the specified date and the UTC Epoch date.
308
+ */
309
+ export function dateToUTC(date) {
310
+ return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
311
+ }
312
+ /**
313
+ * Returns the minimum (most distant past) of the given date values.
314
+ * @param {Array<Date | IntlDate>} dates
315
+ * @returns {Date | IntlDate} The smallest date of `dates`
316
+ */
317
+ export function minDate(...dates) {
318
+ let result = dates[0];
319
+ for (let i = 1; i < dates.length; i++) {
320
+ if (dates[i].valueOf() < result.valueOf()) {
321
+ result = dates[i];
322
+ }
323
+ }
324
+ return result;
325
+ }
326
+ /**
327
+ * Returns the maximum (most distant future) of the given date values.
328
+ * @param {Array<Date | IntlDate>} dates
329
+ * @returns {Date | IntlDate} The largest date of `dates`
330
+ */
331
+ export function maxDate(...dates) {
332
+ let result = dates[0];
333
+ for (let i = 1; i < dates.length; i++) {
334
+ if (dates[i].valueOf() > result.valueOf()) {
335
+ result = dates[i];
336
+ }
337
+ }
338
+ return result;
339
+ }
340
+ /**
341
+ * Returns `true` if the year is a leap year, otherwise `false`.
342
+ * @param year The date year
343
+ * @returns {boolean} `true` if is a leap year
344
+ */
345
+ export function isLeapYear(year) {
346
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
347
+ }
348
+ /**
349
+ * Get the number of days in the given year.
350
+ * @param year The date year
351
+ * @returns {number} Number of days in year
352
+ */
353
+ export function daysInYear(year) {
354
+ return isLeapYear(year) ? 366 : 365;
355
+ }
356
+ /**
357
+ * Get the number of days in the given month and year.
358
+ * @param year The date year
359
+ * @param month The date month
360
+ * @returns {number} Number of days in month
361
+ */
362
+ export function daysInMonth(year, month) {
363
+ const modMonth = floorMod(month - 1, 12) + 1, modYear = year + (month - modMonth) / 12;
364
+ if (modMonth === 2) {
365
+ return isLeapYear(modYear) ? 29 : 28;
366
+ }
367
+ else {
368
+ return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][modMonth - 1];
369
+ }
370
+ }
371
+ /**
372
+ * Gets the number of weeks according to locale in the current moment's year.
373
+ * @param {number} weekYear The the date year
374
+ * @returns {number}
375
+ */
376
+ export function weeksInYear(weekYear) {
377
+ const p1 = (weekYear +
378
+ Math.floor(weekYear / 4) -
379
+ Math.floor(weekYear / 100) +
380
+ Math.floor(weekYear / 400)) %
381
+ 7, last = weekYear - 1, p2 = (last + Math.floor(last / 4) - Math.floor(last / 100) + Math.floor(last / 400)) % 7;
382
+ return p1 === 4 || p2 === 3 ? 53 : 52;
383
+ }
384
+ // x % n but takes the sign of n instead of x
385
+ function floorMod(x, n) {
386
+ return x - n * Math.floor(x / n);
387
+ }