atomic-fns 1.2.0 → 2.0.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.
Files changed (216) hide show
  1. package/README.md +397 -91
  2. package/dist/array/index.d.ts +9 -0
  3. package/dist/array/index.js +9 -0
  4. package/dist/array/list.d.ts +99 -0
  5. package/dist/array/list.js +133 -0
  6. package/dist/array/sets.d.ts +130 -0
  7. package/dist/array/sets.js +213 -0
  8. package/dist/async/index.d.ts +6 -0
  9. package/dist/async/index.js +6 -0
  10. package/dist/async/retry.d.ts +38 -0
  11. package/dist/async/retry.js +56 -0
  12. package/dist/async/sleep.d.ts +42 -0
  13. package/dist/async/sleep.js +76 -0
  14. package/dist/collections/bisect.d.ts +103 -0
  15. package/dist/collections/bisect.js +157 -0
  16. package/dist/collections/deque.d.ts +67 -97
  17. package/dist/collections/deque.js +197 -256
  18. package/dist/collections/entries.d.ts +89 -0
  19. package/dist/collections/entries.js +131 -0
  20. package/dist/collections/frozen-set.d.ts +42 -0
  21. package/dist/collections/frozen-set.js +64 -0
  22. package/dist/collections/group.d.ts +141 -0
  23. package/dist/collections/group.js +241 -0
  24. package/dist/collections/heap.d.ts +64 -0
  25. package/dist/collections/heap.js +205 -0
  26. package/dist/collections/index.d.ts +15 -623
  27. package/dist/collections/index.js +14 -844
  28. package/dist/collections/internal/arrayKeys.d.ts +19 -0
  29. package/dist/collections/internal/arrayKeys.js +83 -0
  30. package/dist/collections/internal/invariants.d.ts +4 -0
  31. package/dist/collections/internal/invariants.js +12 -0
  32. package/dist/collections/internal/iterable.d.ts +25 -0
  33. package/dist/collections/internal/iterable.js +62 -0
  34. package/dist/collections/iterate.d.ts +162 -0
  35. package/dist/collections/iterate.js +188 -0
  36. package/dist/collections/lru-cache.d.ts +58 -0
  37. package/dist/collections/lru-cache.js +164 -0
  38. package/dist/collections/order.d.ts +19 -0
  39. package/dist/collections/order.js +38 -0
  40. package/dist/collections/shape.d.ts +32 -0
  41. package/dist/collections/shape.js +86 -0
  42. package/dist/collections/sorted-map.d.ts +171 -0
  43. package/dist/collections/sorted-map.js +845 -0
  44. package/dist/collections/sorted-set.d.ts +103 -0
  45. package/dist/collections/sorted-set.js +147 -0
  46. package/dist/collections/splay-tree.d.ts +82 -0
  47. package/dist/collections/splay-tree.js +354 -0
  48. package/dist/collections/trie.d.ts +58 -0
  49. package/dist/collections/trie.js +196 -0
  50. package/dist/decimal/currency.d.ts +211 -0
  51. package/dist/decimal/currency.js +321 -0
  52. package/dist/decimal/decimal.d.ts +299 -0
  53. package/dist/decimal/decimal.js +660 -0
  54. package/dist/decimal/index.d.ts +11 -148
  55. package/dist/decimal/index.js +10 -410
  56. package/dist/decimal/internal/bits.d.ts +2 -0
  57. package/dist/decimal/internal/bits.js +7 -0
  58. package/dist/decimal/internal/echo.d.ts +4 -0
  59. package/dist/decimal/internal/echo.js +6 -0
  60. package/dist/decimal/internal/isqrt.d.ts +9 -0
  61. package/dist/decimal/internal/isqrt.js +34 -0
  62. package/dist/decimal/internal/locale.d.ts +7 -0
  63. package/dist/decimal/internal/locale.js +24 -0
  64. package/dist/decimal/internal/minor-units.d.ts +2 -0
  65. package/dist/decimal/internal/minor-units.js +33 -0
  66. package/dist/decimal/internal/pow10.d.ts +24 -0
  67. package/dist/decimal/internal/pow10.js +96 -0
  68. package/dist/encoding/base64.d.ts +39 -0
  69. package/dist/encoding/base64.js +53 -0
  70. package/dist/encoding/hex.d.ts +23 -0
  71. package/dist/encoding/hex.js +34 -0
  72. package/dist/encoding/index.d.ts +6 -0
  73. package/dist/encoding/index.js +6 -0
  74. package/dist/function/debounce.d.ts +68 -0
  75. package/dist/function/debounce.js +203 -0
  76. package/dist/function/index.d.ts +6 -0
  77. package/dist/function/index.js +6 -0
  78. package/dist/function/shared.d.ts +2 -0
  79. package/dist/function/shared.js +5 -0
  80. package/dist/function/wrap.d.ts +130 -0
  81. package/dist/function/wrap.js +113 -0
  82. package/dist/hash/digest.d.ts +18 -0
  83. package/dist/hash/digest.js +26 -0
  84. package/dist/hash/hash.d.ts +22 -0
  85. package/dist/hash/hash.js +252 -0
  86. package/dist/hash/hashString.d.ts +13 -0
  87. package/dist/hash/hashString.js +53 -0
  88. package/dist/hash/index.d.ts +8 -0
  89. package/dist/hash/index.js +8 -0
  90. package/dist/hash/stableStringify.d.ts +19 -0
  91. package/dist/hash/stableStringify.js +102 -0
  92. package/dist/id/index.d.ts +7 -0
  93. package/dist/id/index.js +7 -0
  94. package/dist/id/nanoid.d.ts +16 -0
  95. package/dist/id/nanoid.js +65 -0
  96. package/dist/id/uniqueId.d.ts +14 -0
  97. package/dist/id/uniqueId.js +20 -0
  98. package/dist/id/uuid.d.ts +25 -0
  99. package/dist/id/uuid.js +64 -0
  100. package/dist/index.d.ts +15 -8
  101. package/dist/index.js +15 -8
  102. package/dist/internal/bytes.d.ts +11 -0
  103. package/dist/internal/bytes.js +33 -0
  104. package/dist/internal/codec.d.ts +11 -0
  105. package/dist/internal/codec.js +84 -0
  106. package/dist/internal/compare.d.ts +10 -0
  107. package/dist/internal/compare.js +26 -0
  108. package/dist/internal/crypto.d.ts +5 -0
  109. package/dist/internal/crypto.js +11 -0
  110. package/dist/internal/keys.d.ts +12 -0
  111. package/dist/internal/keys.js +23 -0
  112. package/dist/internal/kind.d.ts +44 -0
  113. package/dist/internal/kind.js +139 -0
  114. package/dist/internal/sparse.d.ts +51 -0
  115. package/dist/internal/sparse.js +104 -0
  116. package/dist/internal/text.d.ts +7 -0
  117. package/dist/internal/text.js +15 -0
  118. package/dist/internal/timer.d.ts +10 -0
  119. package/dist/internal/timer.js +17 -0
  120. package/dist/internal/truncate.d.ts +10 -0
  121. package/dist/internal/truncate.js +22 -0
  122. package/dist/internal/walk.d.ts +47 -0
  123. package/dist/internal/walk.js +100 -0
  124. package/dist/is/index.d.ts +8 -0
  125. package/dist/is/index.js +8 -0
  126. package/dist/is/isArrayLike.d.ts +18 -0
  127. package/dist/is/isArrayLike.js +25 -0
  128. package/dist/is/isError.d.ts +13 -0
  129. package/dist/is/isError.js +18 -0
  130. package/dist/is/isNil.d.ts +12 -0
  131. package/dist/is/isNil.js +14 -0
  132. package/dist/is/isPlainObject.d.ts +15 -0
  133. package/dist/is/isPlainObject.js +34 -0
  134. package/dist/number/index.d.ts +7 -0
  135. package/dist/number/index.js +7 -0
  136. package/dist/number/round.d.ts +86 -0
  137. package/dist/number/round.js +130 -0
  138. package/dist/number/stats.d.ts +52 -0
  139. package/dist/number/stats.js +115 -0
  140. package/dist/object/cloneDeep.d.ts +28 -0
  141. package/dist/object/cloneDeep.js +200 -0
  142. package/dist/object/index.d.ts +14 -0
  143. package/dist/object/index.js +13 -0
  144. package/dist/object/isEqual.d.ts +64 -0
  145. package/dist/object/isEqual.js +289 -0
  146. package/dist/object/matches.d.ts +40 -0
  147. package/dist/object/matches.js +284 -0
  148. package/dist/object/merge.d.ts +66 -0
  149. package/dist/object/merge.js +272 -0
  150. package/dist/object/parsePath.d.ts +22 -0
  151. package/dist/object/parsePath.js +111 -0
  152. package/dist/object/path.d.ts +85 -0
  153. package/dist/object/path.js +208 -0
  154. package/dist/object/pick.d.ts +91 -0
  155. package/dist/object/pick.js +180 -0
  156. package/dist/object/reshape.d.ts +73 -0
  157. package/dist/object/reshape.js +144 -0
  158. package/dist/random/index.d.ts +96 -32
  159. package/dist/random/index.js +215 -53
  160. package/dist/string/case.d.ts +71 -0
  161. package/dist/string/case.js +152 -0
  162. package/dist/string/fold.d.ts +35 -0
  163. package/dist/string/fold.js +100 -0
  164. package/dist/string/index.d.ts +6 -187
  165. package/dist/string/index.js +6 -224
  166. package/dist/string/truncate.d.ts +25 -0
  167. package/dist/string/truncate.js +44 -0
  168. package/dist/string/words.d.ts +27 -0
  169. package/dist/string/words.js +241 -0
  170. package/package.json +99 -51
  171. package/dist/collections/BTree.d.ts +0 -230
  172. package/dist/collections/BTree.js +0 -1143
  173. package/dist/collections/Heap.d.ts +0 -69
  174. package/dist/collections/Heap.js +0 -181
  175. package/dist/collections/LRUCache.d.ts +0 -83
  176. package/dist/collections/LRUCache.js +0 -139
  177. package/dist/collections/SortedMap.d.ts +0 -92
  178. package/dist/collections/SortedMap.js +0 -131
  179. package/dist/collections/SortedSet.d.ts +0 -69
  180. package/dist/collections/SortedSet.js +0 -99
  181. package/dist/collections/SortedTree.d.ts +0 -103
  182. package/dist/collections/SortedTree.js +0 -142
  183. package/dist/collections/SplayTree.d.ts +0 -107
  184. package/dist/collections/SplayTree.js +0 -351
  185. package/dist/collections/Trie.d.ts +0 -42
  186. package/dist/collections/Trie.js +0 -104
  187. package/dist/collections/abc.d.ts +0 -152
  188. package/dist/collections/abc.js +0 -39
  189. package/dist/collections/frozenset.d.ts +0 -19
  190. package/dist/collections/frozenset.js +0 -33
  191. package/dist/functools/index.d.ts +0 -106
  192. package/dist/functools/index.js +0 -203
  193. package/dist/globals/index.d.ts +0 -268
  194. package/dist/globals/index.js +0 -447
  195. package/dist/i18n/currency.d.ts +0 -27
  196. package/dist/i18n/currency.js +0 -100
  197. package/dist/i18n/date/index.d.ts +0 -210
  198. package/dist/i18n/date/index.js +0 -552
  199. package/dist/i18n/date/utils.d.ts +0 -64
  200. package/dist/i18n/date/utils.js +0 -192
  201. package/dist/i18n/duration.d.ts +0 -113
  202. package/dist/i18n/duration.js +0 -398
  203. package/dist/i18n/format.d.ts +0 -34
  204. package/dist/i18n/format.js +0 -378
  205. package/dist/i18n/index.d.ts +0 -9
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -251
  208. package/dist/itertools/index.js +0 -434
  209. package/dist/math/index.d.ts +0 -79
  210. package/dist/math/index.js +0 -81
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -148
  214. package/dist/operators/index.js +0 -325
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,552 +0,0 @@
1
- import { isObject, ValueError } from '../../globals/index.js';
2
- import { Duration, UNITS_PLURAL } from '../duration.js';
3
- import { asDate, formatDate, fromFields, strftime } from '../format.js';
4
- import { asNumber, dayOfYear, daysInMonth, daysInYear, isLeapYear, maxDate, minDate, weekOfYear, weeksInYear } from './utils.js';
5
- // Export date functions
6
- export { asDate, daysInMonth, daysInYear, formatDate, isLeapYear, maxDate, minDate, strftime, weeksInYear };
7
- const INVALID_DATE_STRING = 'Invalid Date';
8
- /**
9
- * 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.
10
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis}
11
- */
12
- export class IntlDate {
13
- _date;
14
- locale;
15
- intlRelativeFormat;
16
- /** Parses the provided value as UTC date or returns the current UTC date. */
17
- static UTC(value, locale) {
18
- return new IntlDate(value, { utc: true, locale });
19
- }
20
- /** Returns the current local date and time. */
21
- static now() {
22
- return new IntlDate();
23
- }
24
- /** Creates a new date from a Unix timestamp (seconds since the unix epoch) */
25
- static unix(seconds) {
26
- return new IntlDate(seconds * 1000);
27
- }
28
- /**
29
- * Creates a new `IntlDate` in local time and specified locale (default is system locale).
30
- * **Note:** The allowed values for `month` start at 1, which is different from legacy `Date`
31
- * @param {DateLike} obj The date value (default is current local time)
32
- * @param {Object} opts The options for this date
33
- * @param {boolean?} opts.utc Converts input or current time to UTC
34
- * @param {string?} opts.locale A locale string to use for this date
35
- * @returns
36
- */
37
- constructor(obj, { locale, utc } = {}) {
38
- this.locale = locale;
39
- if (obj instanceof IntlDate) {
40
- obj = obj._date;
41
- }
42
- if (typeof obj === 'string' || typeof obj === 'number' || obj instanceof Date) {
43
- this._date = asDate(obj, utc);
44
- }
45
- else if (isObject(obj)) {
46
- let { year, month, day, hour, minute, second, millisecond } = obj;
47
- // NOTE month is from 1-12, not from 0-11
48
- month = (month ?? 1) - 1;
49
- this._date = fromFields(utc, Number(year), month, day || 1, hour || 0, minute || 0, second || 0, millisecond || 0);
50
- }
51
- else {
52
- this._date = asDate(obj, utc);
53
- }
54
- return this;
55
- }
56
- /** Get the date's year. */
57
- get year() {
58
- return this._date.getFullYear();
59
- }
60
- /** Get the this date's month as a number from 1 to 12, inclusive. */
61
- get month() {
62
- return this._date.getMonth() + 1;
63
- }
64
- /** Returns the weekday as a number between 1 and 7, inclusive, where Monday is 1 and Sunday is 7. */
65
- get dayOfWeek() {
66
- if (this._date.getDay() === 0)
67
- return 7;
68
- return this._date.getDay();
69
- }
70
- /** Get the this date's current day of the month. */
71
- get day() {
72
- return this._date.getDate();
73
- }
74
- /** Get the this date's current hour. */
75
- get hour() {
76
- return this._date.getHours();
77
- }
78
- /** Get the this date's current minute. */
79
- get minute() {
80
- return this._date.getMinutes();
81
- }
82
- /** Get the this date's current second. */
83
- get second() {
84
- return this._date.getSeconds();
85
- }
86
- /** Get the this date's current millisecond. */
87
- get millisecond() {
88
- return this._date.getMilliseconds();
89
- }
90
- isValid() {
91
- return this._date.toString() !== INVALID_DATE_STRING;
92
- }
93
- /** Get the number of days in this date's month. */
94
- daysInMonth() {
95
- return daysInMonth(this._date);
96
- }
97
- /** Get the number of days in this date's year. */
98
- daysInYear() {
99
- return daysInYear(this.year);
100
- }
101
- dayOfYear() {
102
- return dayOfYear(this._date);
103
- }
104
- /** Returns `true` if this date's year is a leap year. */
105
- isLeapYear() {
106
- return isLeapYear(this.year);
107
- }
108
- /** Gets the number of Monday-first weeks in the current year, where week 1 is the first full week. */
109
- weeksInYear() {
110
- return weeksInYear(this.year);
111
- }
112
- /** Gets the number of weeks in the current year, according to ISO weeks. */
113
- isoWeeksInYear() {
114
- return weeksInYear(this.year, 1, 4);
115
- }
116
- /** Returns the current week of the year. */
117
- week() {
118
- return weekOfYear(this._date).week;
119
- }
120
- /** Returns the ISO week of the year. */
121
- isoWeek() {
122
- return weekOfYear(this._date, 1, 4).week;
123
- }
124
- /**
125
- * Returns a localized string representation of this date.
126
- * @param {Intl.DateTimeFormatOptions} opts The options to use for the format
127
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat Intl.DateTimeFormat()}
128
- */
129
- toString(opts) {
130
- return this._date.toLocaleString(this.locale, opts);
131
- }
132
- /**
133
- * Returns a localized string representation of this date, according to the given format string. Format codes use the same specification as {@link https://momentjs.com/docs/#/displaying/format/ moment}.
134
- * @param {string} str The format string to use
135
- * @see {@link https://momentjs.com/docs/#/displaying/format/ List of formats}
136
- * @example
137
- ```js
138
- new IntlDate().format('MM/DD/YYYY') // '10/31/2022'
139
- ```
140
- */
141
- format(str) {
142
- return formatDate(str, this._date, this.locale);
143
- }
144
- /**
145
- * Returns a new copy of the native `Date` object used by this instance.
146
- * @returns {Date} A new `Date` object
147
- */
148
- toDate() {
149
- return new Date(this.isValid() ? this._date : NaN);
150
- }
151
- /**
152
- * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
153
- * @see {@link IntlDate.timestamp}
154
- * @see {@link IntlDate.unix}
155
- * @example
156
- ```js
157
- new IntlDate().toSeconds() // 1318874398
158
- ```
159
- */
160
- toSeconds() {
161
- return Math.trunc(this.getTime() / 1000);
162
- }
163
- /**
164
- * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
165
- * @see {@link IntlDate.toSeconds}
166
- * @see {@link IntlDate.unix}
167
- * @example
168
- ```js
169
- new IntlDate().timestamp() // 1318874398
170
- ```
171
- */
172
- timestamp() {
173
- return this.toSeconds();
174
- }
175
- /**
176
- * Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
177
- * @see {@link IntlDate.toSeconds}
178
- * @see {@link IntlDate.timestamp}
179
- * @example
180
- ```js
181
- new IntlDate().unix() // 1318874398
182
- ```
183
- */
184
- unix() {
185
- return this.toSeconds();
186
- }
187
- /** Formats a string to the ISO8601 standard.
188
- * @example
189
- ```js
190
- new IntlDate().toISOString() // '2022-10-31T22:44:30.652Z'
191
- ```
192
- */
193
- toISOString() {
194
- return this._date.toISOString();
195
- }
196
- /** Returns the date part formatted as ISO8601.
197
- * @example
198
- ```js
199
- new IntlDate().toISODate() // '2022-10-31'
200
- ```
201
- */
202
- toISODate() {
203
- return this._date.toISOString().split('T')[0];
204
- }
205
- /** Returns the time part formatted as ISO8601.
206
- * @example
207
- ```js
208
- new IntlDate().toISOTime() // '22:44:30.652Z'
209
- ```
210
- */
211
- toISOTime() {
212
- return this._date.toISOString().split('T')[1];
213
- }
214
- /**
215
- * Returns an object containing year, month, day-of-month, hours, minutes, seconds, milliseconds.
216
- * @returns {Object} An object like `{year, month, day, hour, minute, second, millisecond}`
217
- */
218
- toObject() {
219
- return {
220
- year: this.year,
221
- month: this.month,
222
- day: this.day,
223
- hour: this.hour,
224
- minute: this.minute,
225
- second: this.second,
226
- millisecond: this.millisecond
227
- };
228
- }
229
- /** Returns the number of milliseconds since the Unix Epoch (January 1, 1970 UTC) */
230
- getTime() {
231
- return this.isValid() ? this._date.getTime() : NaN;
232
- }
233
- /** Alias of {@link IntlDate.getTime} */
234
- valueOf() {
235
- return this.getTime();
236
- }
237
- /** Returns the timezone string name */
238
- zoneName() {
239
- return this.format('zzz');
240
- }
241
- /** Returns the timezone GMT offset as a string */
242
- zone() {
243
- if (!this.isValid())
244
- return INVALID_DATE_STRING;
245
- const zoneFormat = new Intl.DateTimeFormat(this.locale, { timeZoneName: 'longOffset' });
246
- return zoneFormat.formatToParts(this._date).find((part) => part.type === 'timeZoneName').value;
247
- }
248
- /** Returns the difference in `minutes` between this date and UTC */
249
- utcOffset() {
250
- return this._date.getTimezoneOffset();
251
- }
252
- /**
253
- * Check if this date is before another date. The other value will be parsed as an `IntlDate` if not already so.
254
- * @param {DateLike} other Another date or date like object
255
- * @returns {boolean} Returns `true` if this date is before the given value
256
- */
257
- isBefore(other) {
258
- if (other instanceof IntlDate) {
259
- return this.getTime() < other.getTime();
260
- }
261
- return this.getTime() < new IntlDate(other).getTime();
262
- }
263
- /**
264
- * 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.
265
- * @param {DateLike} start The smaller date
266
- * @param {DateLike} stop The larger date
267
- * @returns {boolean} Returns `true` if this date is greater than `start` and less than `stop`.
268
- */
269
- isBetween(start, stop) {
270
- return this.isAfter(start) && this.isBefore(stop);
271
- }
272
- /**
273
- * Check if this date is after another date. The other value will be parsed as an `IntlDate` if not already so.
274
- * @param {DateLike} other Another date or date like object
275
- * @returns {boolean} Returns `true` if this date is after the given value
276
- */
277
- isAfter(other) {
278
- if (other instanceof IntlDate) {
279
- return this.getTime() > other.getTime();
280
- }
281
- return this.getTime() > new IntlDate(other).getTime();
282
- }
283
- /**
284
- * Check if this date is the same as `other`.
285
- * @param other Another date object
286
- * @returns {boolean} Returns `true` if this date is the same as other
287
- */
288
- isSame(other) {
289
- if (other instanceof IntlDate) {
290
- return (this.isValid() &&
291
- other.isValid() &&
292
- this.getTime() === other.getTime() &&
293
- this.locale === other.locale);
294
- }
295
- return this.getTime() === other.getTime();
296
- }
297
- lt(other) {
298
- return this.isBefore(other);
299
- }
300
- eq(other) {
301
- return this.isSame(other);
302
- }
303
- compare(other) {
304
- if (!this.isValid())
305
- return 1;
306
- const dt = new IntlDate(other);
307
- if (!dt.isValid())
308
- return -1;
309
- return this.getTime() - dt.getTime();
310
- }
311
- set(values) {
312
- if (!this.isValid())
313
- return this;
314
- const normalized = normalizeObject(values, normalizeUnit);
315
- const mixed = { ...this.toObject(), ...normalized };
316
- // if we didn't set the day but we ended up on an overflow date,
317
- // use the last day of the right month
318
- if (normalized.day === undefined) {
319
- const target = new Date(this._date);
320
- target.setFullYear(mixed.year, mixed.month - 1, 1);
321
- mixed.day = Math.min(daysInMonth(target), mixed.day);
322
- }
323
- const result = this.clone(mixed);
324
- return normalized.weekday === undefined
325
- ? result
326
- : result.add({ days: normalized.weekday - result.dayOfWeek });
327
- }
328
- get relativeFormat() {
329
- if (this.intlRelativeFormat)
330
- return this.intlRelativeFormat;
331
- this.intlRelativeFormat = new Intl.RelativeTimeFormat(this.locale);
332
- return new Intl.RelativeTimeFormat(this.locale);
333
- }
334
- relativeTime(n, unit = 'seconds') {
335
- return Number.isNaN(n) ? INVALID_DATE_STRING : this.relativeFormat.format(n, unit);
336
- }
337
- fromNow(unit = 'seconds') {
338
- const diff = this.diff(new Date(), unit);
339
- return this.relativeTime(diff, unit);
340
- }
341
- from(other, unit = 'seconds') {
342
- const diff = this.diff(other, unit);
343
- return this.relativeTime(diff, unit);
344
- }
345
- toNow(unit = 'seconds') {
346
- const diff = this.diff(new Date(), unit);
347
- return this.relativeTime(-diff, unit);
348
- }
349
- to(other, unit = 'seconds') {
350
- const diff = this.diff(other, unit);
351
- return this.relativeTime(-diff, unit);
352
- }
353
- diff(other, unit = 'milliseconds', exact = false) {
354
- const dt = new IntlDate(other);
355
- const normalizedUnit = UNITS_PLURAL[unit.toLowerCase()];
356
- const monthsInUnit = MONTHS_IN_UNIT[normalizedUnit];
357
- if (monthsInUnit && !exact) {
358
- // whole months are the ones `add` can take from `other` without passing this date
359
- let months = (this.year - dt.year) * 12 + this.month - dt.month;
360
- const sign = Math.sign(this.getTime() - dt.getTime());
361
- if (sign * (dt.add({ months }).getTime() - this.getTime()) > 0)
362
- months -= sign;
363
- return Math.trunc(months / monthsInUnit);
364
- }
365
- let elapsed = this.getTime() - dt.getTime();
366
- if (normalizedUnit === 'days' || normalizedUnit === 'weeks') {
367
- // a calendar day can last 23 or 25 hours, so measure it on the wall clock
368
- elapsed += (dt._date.getTimezoneOffset() - this._date.getTimezoneOffset()) * 6e4;
369
- }
370
- const dur = new Duration(elapsed, exact);
371
- let total = dur.total(unit);
372
- if (!exact)
373
- total = Math.trunc(total);
374
- return total;
375
- }
376
- add(duration, exact = false) {
377
- return this.clone(addDuration(this._date, duration, exact));
378
- }
379
- subtract(duration, exact = false) {
380
- return this.clone(subDuration(this._date, duration, exact));
381
- }
382
- startOf(unit) {
383
- if (!this.isValid())
384
- return this;
385
- const obj = {};
386
- const normalizedUnit = UNITS_PLURAL[unit.toLowerCase()];
387
- switch (normalizedUnit) {
388
- case 'years':
389
- obj.month = 1;
390
- // falls through
391
- case 'quarters':
392
- case 'months':
393
- obj.day = 1;
394
- // falls through
395
- case 'weeks':
396
- case 'days':
397
- obj.hour = 0;
398
- // falls through
399
- case 'hours':
400
- obj.minute = 0;
401
- // falls through
402
- case 'minutes':
403
- obj.second = 0;
404
- // falls through
405
- case 'seconds':
406
- obj.millisecond = 0;
407
- break;
408
- case 'milliseconds':
409
- break;
410
- // no default, an unknown unit changes nothing
411
- }
412
- if (normalizedUnit === 'weeks') {
413
- return this.subtract({ days: this.dayOfWeek - 1 }).startOf('days');
414
- }
415
- if (normalizedUnit === 'quarters') {
416
- const q = Math.ceil(this.month / 3);
417
- obj.month = (q - 1) * 3 + 1;
418
- }
419
- const start = this.set(obj);
420
- if (obj.hour !== undefined)
421
- return start;
422
- // Date reads a repeated wall clock as its first pass, this date may be on the second
423
- const offset = this._date.getTimezoneOffset();
424
- const kept = new Date(start.getTime() + (offset - start._date.getTimezoneOffset()) * 6e4);
425
- return kept.getTimezoneOffset() === offset ? this.clone(kept) : start;
426
- }
427
- endOf(unit) {
428
- return this.isValid()
429
- ? this.add({ [unit]: 1 })
430
- .startOf(unit)
431
- .subtract({ milliseconds: 1 })
432
- : this;
433
- }
434
- clone(date) {
435
- return new IntlDate(date ?? this, { locale: this.locale });
436
- }
437
- static max(...args) {
438
- return maxDate.apply(undefined, args);
439
- }
440
- static min(...args) {
441
- return minDate.apply(undefined, args);
442
- }
443
- inspect() {
444
- return this.isValid() ? `new IntlDate("${this.toISOString()}")` : 'new IntlDate(NaN)';
445
- }
446
- }
447
- /**
448
- * Add the specified years, months, weeks, days, hours, minutes and seconds to the given date
449
- * @param date
450
- * @param duration
451
- * @param exact Kept for callers that pass it, it no longer changes the result.
452
- * @returns
453
- */
454
- export function addDuration(date, duration, exact = false) {
455
- const newDate = asDate(date);
456
- const { values } = new Duration(duration, exact);
457
- const { years = 0, quarters = 0, months = 0, weeks = 0, days = 0 } = values;
458
- // largest unit first and all the months in one step, so a clamped day cannot carry over
459
- setDateUnit(newDate, 'months', Math.trunc(years * 12 + quarters * 3 + Number(months)));
460
- setDateUnit(newDate, 'days', Math.trunc(weeks * 7 + Number(days)));
461
- for (const unit of ['hours', 'minutes', 'seconds', 'milliseconds']) {
462
- setDateUnit(newDate, unit, Number(values[unit] ?? 0));
463
- }
464
- return newDate;
465
- }
466
- function addMonths(date, months) {
467
- const target = new Date(date);
468
- target.setFullYear(date.getFullYear(), date.getMonth() + months, 1);
469
- const day = Math.min(date.getDate(), daysInMonth(target));
470
- date.setFullYear(target.getFullYear(), target.getMonth(), day);
471
- }
472
- function setDateUnit(date, unit, value) {
473
- // writing a date field again would move a repeated hour to its first pass
474
- if (value === 0)
475
- return date;
476
- switch (unit) {
477
- case 'months':
478
- addMonths(date, value);
479
- break;
480
- case 'days':
481
- date.setDate(date.getDate() + value);
482
- break;
483
- case 'hours':
484
- date.setTime(date.getTime() + value * 36e5);
485
- break;
486
- case 'minutes':
487
- date.setTime(date.getTime() + value * 6e4);
488
- break;
489
- case 'seconds':
490
- date.setTime(date.getTime() + value * 1e3);
491
- break;
492
- case 'milliseconds':
493
- date.setTime(date.getTime() + value);
494
- break;
495
- default:
496
- break;
497
- }
498
- return date;
499
- }
500
- /**
501
- * Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
502
- * @param date
503
- * @param duration
504
- * @param exact Kept for callers that pass it, it no longer changes the result.
505
- * @returns
506
- */
507
- export function subDuration(date, duration, exact = false) {
508
- return addDuration(date, new Duration(duration, exact).negated(), exact);
509
- }
510
- const MONTHS_IN_UNIT = { years: 12, quarters: 3, months: 1 };
511
- const UNITS_SINGULAR = {
512
- year: 'year',
513
- years: 'year',
514
- month: 'month',
515
- months: 'month',
516
- day: 'day',
517
- days: 'day',
518
- hour: 'hour',
519
- hours: 'hour',
520
- minute: 'minute',
521
- minutes: 'minute',
522
- quarter: 'quarter',
523
- quarters: 'quarter',
524
- second: 'second',
525
- seconds: 'second',
526
- millisecond: 'millisecond',
527
- milliseconds: 'millisecond',
528
- weekday: 'weekday',
529
- weekdays: 'weekday',
530
- weeknumber: 'weekNumber',
531
- weeksnumber: 'weekNumber',
532
- weeknumbers: 'weekNumber',
533
- weekyear: 'weekYear',
534
- weekyears: 'weekYear',
535
- ordinal: 'ordinal'
536
- };
537
- function normalizeUnit(unit) {
538
- const normalized = UNITS_SINGULAR[unit.toLowerCase()];
539
- if (!normalized)
540
- throw new ValueError(`Invalid unit ${unit}`);
541
- return normalized;
542
- }
543
- function normalizeObject(obj, normalizer) {
544
- const normalized = {};
545
- for (const key of Object.keys(obj)) {
546
- const value = obj[key];
547
- if (value == null)
548
- continue;
549
- normalized[normalizer(key)] = asNumber(value);
550
- }
551
- return normalized;
552
- }
@@ -1,64 +0,0 @@
1
- export interface DateObject {
2
- year?: number;
3
- month?: number;
4
- day?: number;
5
- weekday?: number;
6
- hour?: number;
7
- minute?: number;
8
- second?: number;
9
- millisecond?: number;
10
- }
11
- /**
12
- * Returns the minimum (most distant past) of the given date values.
13
- * @template T
14
- * @param {Array<T>} dates
15
- * @returns {T} The smallest date of `dates`
16
- */
17
- export declare function minDate<T>(dates: Array<T>): T;
18
- /**
19
- * Returns the maximum (most distant future) of the given date values.
20
- * @template T
21
- * @param {Array<T>} dates
22
- * @returns {T} The largest date of `dates`
23
- */
24
- export declare function maxDate<T>(dates: Array<T>): T;
25
- /**
26
- * Returns `true` if the year is a leap year, otherwise `false`.
27
- * @param year The date year
28
- * @returns {boolean} `true` if is a leap year
29
- */
30
- export declare function isLeapYear(year: number): boolean;
31
- /**
32
- * Get the number of days in the given year.
33
- * @param year The date year
34
- * @returns {number} Number of days in year
35
- */
36
- export declare function daysInYear(year: number): number;
37
- /**
38
- * Get the number of days in the given date's month and year.
39
- * @param date The specified date
40
- * @returns {number} Number of days in month
41
- */
42
- export declare function daysInMonth(date: string | number | Date): number;
43
- export declare function asNumber(value: any): number;
44
- export declare function parseISODuration(s: any): any;
45
- /**
46
- * Returns the number of weeks in the specified year.
47
- * @param year
48
- * @param firstDayOfWeek
49
- * @param firstDayOfYear
50
- */
51
- export declare function weeksInYear(year: number, firstDayOfWeek?: number, firstDayOfYear?: number): number;
52
- /**
53
- * Returns the week of the year for the specified date
54
- * @param date The date value
55
- * @param firstDayOfWeek The weekday a week starts on, where Sunday is 0
56
- * @param firstDayOfYear Picks week 1: the week holding January `7 + firstDayOfWeek - firstDayOfYear`, so `1, 4` gives ISO weeks
57
- * @returns The week and the year that week belongs to
58
- */
59
- export declare function weekOfYear(date: string | number | Date, firstDayOfWeek?: number, firstDayOfYear?: number): {
60
- week: any;
61
- year: any;
62
- };
63
- export declare function isoWeeksInYear(year: number): number;
64
- export declare function dayOfYear(input: string | number | Date): number;