atomic-fns 0.3.5 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/{core → dist}/collections/BTree.d.ts +6 -6
- package/{core → dist}/collections/BTree.js +17 -17
- package/{core → dist}/collections/Heap.d.ts +3 -3
- package/{core → dist}/collections/Heap.js +0 -0
- package/dist/collections/LRUCache.d.ts +80 -0
- package/dist/collections/LRUCache.js +126 -0
- package/{core → dist}/collections/SortedMap.d.ts +2 -2
- package/{core → dist}/collections/SortedMap.js +0 -0
- package/{core → dist}/collections/SortedSet.d.ts +3 -3
- package/{core → dist}/collections/SortedSet.js +1 -1
- package/{core → dist}/collections/SortedTree.d.ts +3 -3
- package/{core → dist}/collections/SortedTree.js +1 -1
- package/{core → dist}/collections/SplayTree.d.ts +5 -5
- package/{core → dist}/collections/SplayTree.js +0 -0
- package/{core → dist}/collections/abc.d.ts +0 -0
- package/{core → dist}/collections/abc.js +0 -0
- package/{core → dist}/collections/deque.d.ts +6 -6
- package/{core → dist}/collections/deque.js +0 -0
- package/{core → dist}/collections/frozenset.d.ts +0 -0
- package/{core → dist}/collections/frozenset.js +3 -6
- package/{core → dist}/collections/index.d.ts +4 -4
- package/{core → dist}/collections/index.js +0 -0
- package/{core → dist}/functools/index.d.ts +27 -13
- package/{core → dist}/functools/index.js +34 -12
- package/{core → dist}/globals/index.d.ts +7 -10
- package/{core → dist}/globals/index.js +7 -14
- package/dist/i18n/date/index.d.ts +204 -0
- package/dist/i18n/date/index.js +530 -0
- package/dist/i18n/date/utils.d.ts +65 -0
- package/dist/i18n/date/utils.js +312 -0
- package/dist/i18n/duration.d.ts +112 -0
- package/dist/i18n/duration.js +392 -0
- package/dist/i18n/format.d.ts +26 -0
- package/dist/i18n/format.js +495 -0
- package/{core → dist}/i18n/index.d.ts +1 -1
- package/{core → dist}/i18n/index.js +1 -1
- package/{core → dist}/index.d.ts +0 -0
- package/{core → dist}/index.js +0 -0
- package/{core → dist}/itertools/index.d.ts +27 -27
- package/{core → dist}/itertools/index.js +24 -24
- package/{core → dist}/math/index.d.ts +3 -3
- package/{core → dist}/math/index.js +0 -0
- package/{core → dist}/operators/index.d.ts +2 -2
- package/{core → dist}/operators/index.js +2 -2
- package/{core → dist}/random/index.d.ts +1 -1
- package/{core → dist}/random/index.js +3 -3
- package/package.json +28 -42
- package/.editorconfig +0 -12
- package/.esdoc.json +0 -27
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -35
- package/.firebaserc +0 -5
- package/.prettierrc +0 -19
- package/core/collections/BTree.ts +0 -1340
- package/core/collections/Heap.ts +0 -189
- package/core/collections/LRUCache.d.ts +0 -152
- package/core/collections/LRUCache.js +0 -409
- package/core/collections/LRUCache.ts +0 -486
- package/core/collections/SortedMap.ts +0 -146
- package/core/collections/SortedSet.ts +0 -110
- package/core/collections/SortedTree.ts +0 -160
- package/core/collections/SplayTree.ts +0 -354
- package/core/collections/abc.ts +0 -176
- package/core/collections/deque.ts +0 -338
- package/core/collections/frozenset.ts +0 -37
- package/core/collections/index.ts +0 -1024
- package/core/collections/test.html +0 -10
- package/core/collections/typedArrays.d.ts +0 -31
- package/core/collections/typedArrays.js +0 -143
- package/core/collections/typedArrays.ts +0 -163
- package/core/functools/index.ts +0 -260
- package/core/globals/index.ts +0 -481
- package/core/i18n/date/format.d.ts +0 -16
- package/core/i18n/date/format.js +0 -307
- package/core/i18n/date/format.ts +0 -343
- package/core/i18n/date/index.d.ts +0 -224
- package/core/i18n/date/index.js +0 -387
- package/core/i18n/date/index.ts +0 -471
- package/core/i18n/date/test.html +0 -67
- package/core/i18n/index.ts +0 -8
- package/core/index.ts +0 -8
- package/core/itertools/index.ts +0 -494
- package/core/math/index.ts +0 -152
- package/core/operators/index.ts +0 -310
- package/core/random/index.ts +0 -66
- package/firebase.json +0 -10
- package/jasmine.json +0 -14
- package/tests/BTree.spec.ts +0 -143
- package/tests/Cache.spec.ts +0 -161
- package/tests/Heap.spec.ts +0 -66
- package/tests/SortedMap.spec.ts +0 -144
- package/tests/SortedSet.spec.ts +0 -84
- package/tests/SortedTree.spec.ts +0 -164
- package/tests/SplayTree.spec.ts +0 -121
- package/tests/collections.spec.ts +0 -288
- package/tests/deque.spec.ts +0 -107
- package/tests/globals.spec.ts +0 -270
- package/tests/itertools.spec.ts +0 -266
- package/tests/math.spec.ts +0 -25
- package/tests/operators.spec.ts +0 -40
- package/tests/random.spec.ts +0 -46
- package/tsconfig.json +0 -126
- package/tsconfig.node.json +0 -18
- package/tsconfig.xo.json +0 -9
|
@@ -0,0 +1,392 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
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 {number | 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
|
+
*/
|
|
26
|
+
export declare function strftime(fmt: string, date: number | Date, locale?: any): string;
|