atomic-fns 1.1.7 → 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 +405 -60
  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 -249
  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 -642
  27. package/dist/collections/index.js +14 -716
  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 -145
  55. package/dist/decimal/index.js +10 -374
  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 -50
  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 -226
  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 +101 -58
  171. package/dist/collections/BTree.d.ts +0 -229
  172. package/dist/collections/BTree.js +0 -1165
  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 -80
  176. package/dist/collections/LRUCache.js +0 -126
  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 -141
  183. package/dist/collections/SplayTree.d.ts +0 -105
  184. package/dist/collections/SplayTree.js +0 -338
  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 -30
  191. package/dist/functools/index.d.ts +0 -130
  192. package/dist/functools/index.js +0 -235
  193. package/dist/globals/index.d.ts +0 -285
  194. package/dist/globals/index.js +0 -463
  195. package/dist/i18n/currency.d.ts +0 -24
  196. package/dist/i18n/currency.js +0 -85
  197. package/dist/i18n/date/index.d.ts +0 -203
  198. package/dist/i18n/date/index.js +0 -531
  199. package/dist/i18n/date/utils.d.ts +0 -65
  200. package/dist/i18n/date/utils.js +0 -290
  201. package/dist/i18n/duration.d.ts +0 -112
  202. package/dist/i18n/duration.js +0 -392
  203. package/dist/i18n/format.d.ts +0 -31
  204. package/dist/i18n/format.js +0 -506
  205. package/dist/i18n/index.d.ts +0 -8
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -255
  208. package/dist/itertools/index.js +0 -437
  209. package/dist/math/index.d.ts +0 -96
  210. package/dist/math/index.js +0 -98
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -152
  214. package/dist/operators/index.js +0 -305
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,392 +0,0 @@
1
- import { pick } from '../collections/index.js';
2
- import { isNumber, isObject, ValueError } from '../globals/index.js';
3
- import { round } from '../math/index.js';
4
- import { parseISODuration } from './date/utils.js';
5
- const lowOrderMatrix = {
6
- weeks: {
7
- days: 7,
8
- hours: 7 * 24,
9
- minutes: 7 * 24 * 60,
10
- seconds: 7 * 24 * 60 * 60,
11
- milliseconds: 7 * 24 * 60 * 60 * 1000
12
- },
13
- days: {
14
- hours: 24,
15
- minutes: 24 * 60,
16
- seconds: 24 * 60 * 60,
17
- milliseconds: 24 * 60 * 60 * 1000
18
- },
19
- hours: { minutes: 60, seconds: 60 * 60, milliseconds: 60 * 60 * 1000 },
20
- minutes: { seconds: 60, milliseconds: 60 * 1000 },
21
- seconds: { milliseconds: 1000 }
22
- };
23
- const casualMatrix = {
24
- years: {
25
- quarters: 4,
26
- months: 12,
27
- weeks: 52,
28
- days: 365,
29
- hours: 365 * 24,
30
- minutes: 365 * 24 * 60,
31
- seconds: 365 * 24 * 60 * 60,
32
- milliseconds: 365 * 24 * 60 * 60 * 1000
33
- },
34
- quarters: {
35
- months: 3,
36
- weeks: 13,
37
- days: 91,
38
- hours: 91 * 24,
39
- minutes: 91 * 24 * 60,
40
- seconds: 91 * 24 * 60 * 60,
41
- milliseconds: 91 * 24 * 60 * 60 * 1000
42
- },
43
- months: {
44
- weeks: 4,
45
- days: 30,
46
- hours: 30 * 24,
47
- minutes: 30 * 24 * 60,
48
- seconds: 30 * 24 * 60 * 60,
49
- milliseconds: 30 * 24 * 60 * 60 * 1000
50
- },
51
- ...lowOrderMatrix
52
- };
53
- const exactDaysInYear = 146097.0 / 400;
54
- const exactDaysInMonth = 146097.0 / 4800;
55
- const exactMatrix = {
56
- years: {
57
- quarters: 4,
58
- months: 12,
59
- weeks: exactDaysInYear / 7,
60
- days: exactDaysInYear,
61
- hours: exactDaysInYear * 24,
62
- minutes: exactDaysInYear * 24 * 60,
63
- seconds: exactDaysInYear * 24 * 60 * 60,
64
- milliseconds: exactDaysInYear * 24 * 60 * 60 * 1000
65
- },
66
- quarters: {
67
- months: 3,
68
- weeks: exactDaysInYear / 28,
69
- days: exactDaysInYear / 4,
70
- hours: (exactDaysInYear * 24) / 4,
71
- minutes: (exactDaysInYear * 24 * 60) / 4,
72
- seconds: (exactDaysInYear * 24 * 60 * 60) / 4,
73
- milliseconds: (exactDaysInYear * 24 * 60 * 60 * 1000) / 4
74
- },
75
- months: {
76
- weeks: exactDaysInMonth / 7,
77
- days: exactDaysInMonth,
78
- hours: exactDaysInMonth * 24,
79
- minutes: exactDaysInMonth * 24 * 60,
80
- seconds: exactDaysInMonth * 24 * 60 * 60,
81
- milliseconds: exactDaysInMonth * 24 * 60 * 60 * 1000
82
- },
83
- ...lowOrderMatrix
84
- };
85
- export const UNITS_PLURAL = {
86
- year: 'years',
87
- years: 'years',
88
- quarter: 'quarters',
89
- quarters: 'quarters',
90
- month: 'months',
91
- months: 'months',
92
- week: 'weeks',
93
- weeks: 'weeks',
94
- day: 'days',
95
- days: 'days',
96
- hour: 'hours',
97
- hours: 'hours',
98
- minute: 'minutes',
99
- minutes: 'minutes',
100
- second: 'seconds',
101
- seconds: 'seconds',
102
- millisecond: 'milliseconds',
103
- milliseconds: 'milliseconds'
104
- };
105
- const orderedUnits = [
106
- 'years',
107
- 'quarters',
108
- 'months',
109
- 'weeks',
110
- 'days',
111
- 'hours',
112
- 'minutes',
113
- 'seconds',
114
- 'milliseconds'
115
- ];
116
- const reverseUnits = orderedUnits.slice(0).reverse();
117
- export class Duration {
118
- values;
119
- matrix = casualMatrix;
120
- constructor(dur = {}, exact = false) {
121
- if (dur instanceof Duration) {
122
- this.values = { ...dur.values };
123
- }
124
- else if (isNumber(dur)) {
125
- this.values = { milliseconds: dur };
126
- }
127
- else if (isObject(dur)) {
128
- this.values = normalizeUnits(dur);
129
- }
130
- if (exact) {
131
- // Set the conversion matrix to use
132
- this.matrix = exactMatrix;
133
- }
134
- }
135
- /** Get the years. */
136
- get years() {
137
- return this.values.years || 0;
138
- }
139
- /** Get the months. */
140
- get months() {
141
- return this.values.months || 0;
142
- }
143
- /** Get the weeks. */
144
- get weeks() {
145
- return this.values.weeks || 0;
146
- }
147
- /** Get the days. */
148
- get days() {
149
- return this.values.days || 0;
150
- }
151
- /** Get the hours. */
152
- get hours() {
153
- return this.values.hours || 0;
154
- }
155
- /** et the minutes. */
156
- get minutes() {
157
- return this.values.minutes || 0;
158
- }
159
- /** Get the seconds. */
160
- get seconds() {
161
- return this.values.seconds || 0;
162
- }
163
- /** Get the milliseconds. */
164
- get milliseconds() {
165
- return this.values.milliseconds || 0;
166
- }
167
- /**
168
- * Get the value of unit.
169
- * @param {string} unit - a unit such as 'minute' or 'day'
170
- * @example Duration.fromObject({years: 2, days: 3}).get('years') // 2
171
- * @example Duration.fromObject({years: 2, days: 3}).get('months') // 0
172
- * @example Duration.fromObject({years: 2, days: 3}).get('days') // 3
173
- * @return {number}
174
- */
175
- get(unit) {
176
- return this[UNITS_PLURAL[unit.toLowerCase()]];
177
- }
178
- add(duration) {
179
- const dur = new Duration(duration);
180
- const result = {};
181
- for (const k of orderedUnits) {
182
- if (dur.values[k] || this.values[k]) {
183
- result[k] = dur.get(k) + this.get(k);
184
- }
185
- }
186
- return new Duration(result);
187
- }
188
- /**
189
- * Make this Duration shorter by the specified amount. Return a newly-constructed Duration.
190
- * @param {Duration|Object|number} duration - The amount to subtract.
191
- * @return {Duration}
192
- */
193
- subtract(duration) {
194
- const dur = new Duration(duration);
195
- return this.add(dur.negated());
196
- }
197
- /**
198
- * Return the negative of this Duration.
199
- * @example
200
- ```js
201
- new Duration({ hours: 1, seconds: 30 }).negated().toObject()
202
- // { hours: -1, seconds: -30 }
203
- ```
204
- * @returns {Duration}
205
- */
206
- negated() {
207
- const negated = {};
208
- for (const k of Object.keys(this.values)) {
209
- negated[k] = this.values[k] === 0 ? 0 : -this.values[k];
210
- }
211
- return new Duration(negated);
212
- }
213
- /**
214
- * Return the absolute values of this Duration.
215
- * @example
216
- ```js
217
- new Duration({ hours: 1, seconds: -30 }).abs().toObject()
218
- // { hours: 1, seconds: 30 }
219
- ```
220
- * @returns {Duration}
221
- */
222
- abs() {
223
- const parts = {};
224
- for (const k of Object.keys(this.values)) {
225
- parts[k] = Math.abs(this.values[k]);
226
- }
227
- return new Duration(parts);
228
- }
229
- shiftToAll() {
230
- return this.shiftTo('years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds');
231
- }
232
- shiftTo(...units) {
233
- if (units.length === 0) {
234
- return this;
235
- }
236
- units = units.map((u) => UNITS_PLURAL[u.toLowerCase()]);
237
- const built = {};
238
- const accumulated = {};
239
- const vals = this.toObject();
240
- let lastUnit;
241
- for (const toUnit of orderedUnits) {
242
- if (units.includes(toUnit)) {
243
- lastUnit = toUnit;
244
- let own = 0;
245
- // anything we haven't boiled down yet should get boiled to this unit
246
- for (const fromUnit in accumulated) {
247
- own += accumulated[fromUnit] * this.matrix[fromUnit][toUnit];
248
- accumulated[fromUnit] = 0;
249
- }
250
- // plus anything that's already in this unit
251
- if (typeof vals[toUnit] === 'number') {
252
- own += vals[toUnit];
253
- }
254
- const i = Math.trunc(own);
255
- built[toUnit] = i;
256
- accumulated[toUnit] = (own * 1000 - i * 1000) / 1000;
257
- // plus anything further down the chain that should be rolled up in to this
258
- for (const fromUnit in vals) {
259
- if (orderedUnits.indexOf(fromUnit) > orderedUnits.indexOf(toUnit)) {
260
- convert(this.matrix, vals, fromUnit, built, toUnit);
261
- }
262
- }
263
- }
264
- else if (isNumber(vals[toUnit])) {
265
- accumulated[toUnit] = vals[toUnit];
266
- }
267
- }
268
- // anything leftover becomes the decimal for the last unit
269
- // lastUnit must be defined since units is not empty
270
- for (const key in accumulated) {
271
- if (accumulated[key] !== 0) {
272
- built[lastUnit] +=
273
- key === lastUnit ? accumulated[key] : accumulated[key] / this.matrix[lastUnit][key];
274
- }
275
- }
276
- return new Duration(built);
277
- }
278
- total(unit) {
279
- return this.shiftTo(unit).get(unit);
280
- }
281
- exact(unit) {
282
- this.matrix = exactMatrix;
283
- if (!unit)
284
- return this;
285
- return this.total(unit);
286
- }
287
- toObject() {
288
- return { ...this.values };
289
- }
290
- normalize() {
291
- normalizeValues(this.matrix, this.values);
292
- return new Duration(this.values);
293
- }
294
- rescale() {
295
- const dur = this.normalize().shiftToAll();
296
- const vals = removeZeroes(dur.values);
297
- return new Duration(vals);
298
- }
299
- /**
300
- * Returns an ISO 8601-compliant string representation of this Duration.
301
- * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
302
- * @example Duration.fromObject({ years: 3, seconds: 45 }).toString() // 'P3YT45S'
303
- * @example Duration.fromObject({ months: 4, seconds: 45 }).toString() // 'P4MT45S'
304
- * @example Duration.fromObject({ months: 5 }).toString() // 'P5M'
305
- * @example Duration.fromObject({ minutes: 5 }).toString() // 'PT5M'
306
- * @example Duration.fromObject({ milliseconds: 6 }).toString() // 'PT0.006S'
307
- * @return {string}
308
- */
309
- toISOString() {
310
- let s = 'P';
311
- if (this.years !== 0)
312
- s += this.years + 'Y';
313
- if (this.months !== 0)
314
- s += this.months + 'M';
315
- if (this.weeks !== 0)
316
- s += this.weeks + 'W';
317
- if (this.days !== 0)
318
- s += this.days + 'D';
319
- if (this.hours !== 0 || this.minutes !== 0 || this.seconds !== 0 || this.milliseconds !== 0)
320
- s += 'T';
321
- if (this.hours !== 0)
322
- s += this.hours + 'H';
323
- if (this.minutes !== 0)
324
- s += this.minutes + 'M';
325
- if (this.seconds !== 0 || this.milliseconds !== 0)
326
- // this will handle "floating point madness" by removing extra decimal places
327
- // https://stackoverflow.com/questions/588004/is-floating-point-math-broken
328
- s += round(this.seconds + this.milliseconds / 1000, 3) + 'S';
329
- if (s === 'P')
330
- s += 'T0S';
331
- return s;
332
- }
333
- toString() {
334
- return this.toISOString();
335
- }
336
- /**
337
- * Create a Duration from an ISO 8601 duration string.
338
- *
339
- * @example
340
- ```js
341
- Duration.fromISO('P3Y6M1W4DT12H30M5S').toObject()
342
- // { years: 3, months: 6, weeks: 1, days: 4, hours: 12, minutes: 30, seconds: 5 }
343
- Duration.fromISO('PT23H').toObject()
344
- // { hours: 23 }
345
- Duration.fromISO('P5Y3M').toObject()
346
- // { years: 5, months: 3 }
347
- ```
348
- * @return {Duration}
349
- */
350
- static fromISO(text) {
351
- const [parsed] = parseISODuration(text);
352
- if (parsed)
353
- return new Duration(parsed);
354
- throw new ValueError(`The input "${text}" can't be parsed as ISO 8601`);
355
- }
356
- }
357
- function normalizeUnits(obj) {
358
- const vals = {};
359
- for (const unit of Object.keys(obj)) {
360
- vals[UNITS_PLURAL[unit]] = obj[unit];
361
- }
362
- return vals;
363
- }
364
- function antiTrunc(n) {
365
- return n < 0 ? Math.floor(n) : Math.ceil(n);
366
- }
367
- // NB: mutates parameters
368
- function convert(matrix, sources, fromUnit, result, toUnit) {
369
- const conv = matrix[toUnit][fromUnit];
370
- const raw = sources[fromUnit] / conv;
371
- const sameSign = Math.sign(raw) === Math.sign(result[toUnit]);
372
- // ok, so this is wild, but see the matrix in the tests
373
- const added = !sameSign && result[toUnit] !== 0 && Math.abs(raw) <= 1 ? antiTrunc(raw) : Math.trunc(raw);
374
- result[toUnit] += added;
375
- sources[fromUnit] -= added * conv;
376
- }
377
- // NB: mutates parameters
378
- function normalizeValues(matrix, vals) {
379
- reverseUnits.reduce((previous, current) => {
380
- if (vals[current]) {
381
- if (previous) {
382
- convert(matrix, vals, previous, vals, current);
383
- }
384
- return current;
385
- }
386
- return previous;
387
- }, null);
388
- }
389
- // Remove all properties with a value of 0 from an object
390
- function removeZeroes(vals) {
391
- return pick(vals, (value) => value !== 0);
392
- }
@@ -1,31 +0,0 @@
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 asDate(date: string | number | Date, utc?: boolean): Date;
8
- /**
9
- * 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}.
10
- * @param {string} str The format string to use
11
- * @see {@link https://momentjs.com/docs/#/displaying/format/ List of formats}
12
- * @example
13
- ```js
14
- formatDate(new Date(), 'MM/DD/YYYY') // '10/31/2022'
15
- ```
16
- */
17
- export declare function formatDate(formatStr: string, date: Date, locale?: string): string;
18
- /**
19
- * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
20
- * @param {string} fmt The format string to apply
21
- * @param {Date} date The date value
22
- * @param {?string} locale The locale to use when formatting (default is system locale).
23
- * @returns {string} The string representation of date
24
- * @see {@link https://strftime.org/ strftime format}
25
- * @example
26
- ```js
27
- strftime('%A, %d/%m/%y', new Date())
28
- // 'Saturday, 01/02/2020'
29
- ```
30
- */
31
- export declare function strftime(fmt: string, date: Date, locale?: string): string;