atomic-fns 1.1.6 → 1.2.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 (51) hide show
  1. package/README.md +51 -12
  2. package/dist/collections/BTree.d.ts +11 -10
  3. package/dist/collections/BTree.js +58 -80
  4. package/dist/collections/Heap.d.ts +2 -2
  5. package/dist/collections/Heap.js +1 -1
  6. package/dist/collections/LRUCache.d.ts +8 -5
  7. package/dist/collections/LRUCache.js +24 -11
  8. package/dist/collections/SortedMap.d.ts +4 -4
  9. package/dist/collections/SortedMap.js +4 -3
  10. package/dist/collections/SortedSet.d.ts +2 -2
  11. package/dist/collections/SortedTree.d.ts +9 -9
  12. package/dist/collections/SortedTree.js +7 -6
  13. package/dist/collections/SplayTree.d.ts +17 -15
  14. package/dist/collections/SplayTree.js +42 -29
  15. package/dist/collections/Trie.d.ts +2 -2
  16. package/dist/collections/Trie.js +26 -26
  17. package/dist/collections/deque.d.ts +11 -11
  18. package/dist/collections/deque.js +17 -10
  19. package/dist/collections/frozenset.js +4 -1
  20. package/dist/collections/index.d.ts +30 -48
  21. package/dist/collections/index.js +210 -82
  22. package/dist/decimal/index.d.ts +7 -5
  23. package/dist/decimal/index.js +63 -28
  24. package/dist/functools/index.d.ts +10 -34
  25. package/dist/functools/index.js +25 -57
  26. package/dist/globals/index.d.ts +5 -22
  27. package/dist/globals/index.js +24 -40
  28. package/dist/i18n/currency.d.ts +10 -7
  29. package/dist/i18n/currency.js +22 -7
  30. package/dist/i18n/date/index.d.ts +22 -16
  31. package/dist/i18n/date/index.js +89 -67
  32. package/dist/i18n/date/utils.d.ts +6 -7
  33. package/dist/i18n/date/utils.js +15 -113
  34. package/dist/i18n/duration.d.ts +13 -12
  35. package/dist/i18n/duration.js +38 -32
  36. package/dist/i18n/format.d.ts +10 -2
  37. package/dist/i18n/format.js +100 -217
  38. package/dist/i18n/index.d.ts +2 -1
  39. package/dist/itertools/index.d.ts +21 -25
  40. package/dist/itertools/index.js +41 -44
  41. package/dist/math/index.d.ts +12 -29
  42. package/dist/math/index.js +16 -33
  43. package/dist/mod.d.ts +1 -0
  44. package/dist/mod.js +1 -0
  45. package/dist/operators/index.d.ts +3 -3
  46. package/dist/operators/index.js +62 -38
  47. package/dist/random/index.js +9 -6
  48. package/dist/string/index.d.ts +1 -1
  49. package/dist/string/index.js +2 -4
  50. package/dist/types.d.ts +3 -3
  51. package/package.json +16 -21
@@ -1,4 +1,4 @@
1
- export declare type DateObject = {
1
+ export interface DateObject {
2
2
  year?: number;
3
3
  month?: number;
4
4
  day?: number;
@@ -7,7 +7,7 @@ export declare type DateObject = {
7
7
  minute?: number;
8
8
  second?: number;
9
9
  millisecond?: number;
10
- };
10
+ }
11
11
  /**
12
12
  * Returns the minimum (most distant past) of the given date values.
13
13
  * @template T
@@ -51,11 +51,10 @@ export declare function parseISODuration(s: any): any;
51
51
  export declare function weeksInYear(year: number, firstDayOfWeek?: number, firstDayOfYear?: number): number;
52
52
  /**
53
53
  * Returns the week of the year for the specified date
54
- * @param year
55
- * @param dayOfYear
56
- * @param firstDayOfWeek
57
- * @param firstDayOfYear
58
- * @returns
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
59
58
  */
60
59
  export declare function weekOfYear(date: string | number | Date, firstDayOfWeek?: number, firstDayOfYear?: number): {
61
60
  week: any;
@@ -1,3 +1,4 @@
1
+ import { asDate } from '../format.js';
1
2
  /**
2
3
  * Returns x % n but takes the sign of n instead of x
3
4
  * @param x
@@ -7,28 +8,13 @@
7
8
  function floorMod(x, n) {
8
9
  return x - n * Math.floor(x / n);
9
10
  }
10
- function padStart(input, n = 2) {
11
- const isNeg = input < 0;
12
- let padded;
13
- if (isNeg) {
14
- padded = '-' + ('' + -input).padStart(n, '0');
15
- }
16
- else {
17
- padded = ('' + input).padStart(n, '0');
18
- }
19
- return padded;
20
- }
21
- function parseInteger(str) {
22
- if (str != null && str !== '')
23
- return parseInt(str, 10);
24
- }
25
11
  function parseFloating(str) {
26
12
  if (str != null && str !== '')
27
13
  return parseFloat(str);
28
14
  }
29
15
  function parseMilliseconds(str) {
30
16
  if (str != null && str !== '') {
31
- const f = parseFloat('0.' + str) * 1000;
17
+ const f = parseFloat(`0.${str}`) * 1000;
32
18
  return Math.floor(f);
33
19
  }
34
20
  }
@@ -39,7 +25,7 @@ export function minDate(...dates) {
39
25
  ans = ans[0];
40
26
  }
41
27
  for (let i = 1; i < dates.length; i++) {
42
- if (dates[i].valueOf() < ans.valueOf()) {
28
+ if (dates[i].valueOf() < ans.valueOf() || Number.isNaN(ans.valueOf())) {
43
29
  ans = dates[i];
44
30
  }
45
31
  }
@@ -52,7 +38,7 @@ export function maxDate(...dates) {
52
38
  ans = ans[0];
53
39
  }
54
40
  for (let i = 1; i < dates.length; i++) {
55
- if (dates[i].valueOf() > ans.valueOf()) {
41
+ if (dates[i].valueOf() > ans.valueOf() || Number.isNaN(ans.valueOf())) {
56
42
  ans = dates[i];
57
43
  }
58
44
  }
@@ -80,7 +66,7 @@ export function daysInYear(year) {
80
66
  * @returns {number} Number of days in month
81
67
  */
82
68
  export function daysInMonth(date) {
83
- const d = new Date(date), year = d.getFullYear(), month = d.getMonth() + 1;
69
+ const d = asDate(date), year = d.getFullYear(), month = d.getMonth() + 1;
84
70
  const modMonth = floorMod(month - 1, 12) + 1, modYear = year + (month - modMonth) / 12;
85
71
  if (modMonth === 2) {
86
72
  return isLeapYear(modYear) ? 29 : 28;
@@ -89,51 +75,6 @@ export function daysInMonth(date) {
89
75
  return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][modMonth - 1];
90
76
  }
91
77
  }
92
- // covert a calendar object to a local timestamp (epoch, but with the offset baked in)
93
- function objToLocalTS(obj) {
94
- let d = Date.UTC(obj.year, obj.month - 1, obj.day, obj.hour, obj.minute, obj.second, obj.millisecond);
95
- // for legacy reasons, years between 0 and 99 are interpreted as 19XX; revert that
96
- if (obj.year < 100 && obj.year >= 0) {
97
- let dt = new Date(d);
98
- dt.setUTCFullYear(dt.getUTCFullYear() - 1900);
99
- d = dt.getTime();
100
- }
101
- return +d;
102
- }
103
- function untruncateYear(year) {
104
- if (year > 99) {
105
- return year;
106
- }
107
- else
108
- return year > 60 ? 1900 + year : 2000 + year;
109
- }
110
- // PARSING
111
- function parseZoneInfo(ts, offsetFormat, locale, timeZone) {
112
- const date = new Date(ts), intlOpts = {
113
- hourCycle: 'h23',
114
- year: 'numeric',
115
- month: '2-digit',
116
- day: '2-digit',
117
- hour: '2-digit',
118
- minute: '2-digit',
119
- timeZone,
120
- timeZoneName: offsetFormat
121
- };
122
- const parsed = new Intl.DateTimeFormat(locale, intlOpts)
123
- .formatToParts(date)
124
- .find((m) => m.type === 'timeZoneName');
125
- return parsed?.value;
126
- }
127
- // signedOffset('-5', '30') -> -330
128
- function signedOffset(offHourStr, offMinuteStr) {
129
- let offHour = parseInt(offHourStr, 10);
130
- // don't || this because we want to preserve -0
131
- if (Number.isNaN(offHour)) {
132
- offHour = 0;
133
- }
134
- const offMin = parseInt(offMinuteStr, 10) || 0, offMinSigned = offHour < 0 || Object.is(offHour, -0) ? -offMin : offMin;
135
- return offHour * 60 + offMinSigned;
136
- }
137
78
  // COERCION
138
79
  export function asNumber(value) {
139
80
  const numericValue = Number(value);
@@ -141,24 +82,6 @@ export function asNumber(value) {
141
82
  throw new Error(`Invalid unit value ${value}`);
142
83
  return numericValue;
143
84
  }
144
- function formatOffset(offset, format) {
145
- const hours = Math.trunc(Math.abs(offset / 60)), minutes = Math.trunc(Math.abs(offset % 60)), sign = offset >= 0 ? '+' : '-';
146
- switch (format) {
147
- case 'short':
148
- return `${sign}${padStart(hours, 2)}:${padStart(minutes, 2)}`;
149
- case 'narrow':
150
- return `${sign}${hours}${minutes > 0 ? `:${minutes}` : ''}`;
151
- case 'techie':
152
- return `${sign}${padStart(hours, 2)}${padStart(minutes, 2)}`;
153
- default:
154
- throw new RangeError(`Value format ${format} is out of range for property format`);
155
- }
156
- }
157
- function timeObject(obj) {
158
- const { hour, minute, second, millisecond } = obj;
159
- return { hour, minute, second, millisecond };
160
- }
161
- const ianaRegex = /[A-Za-z_+-]{1,256}(?::?\/[A-Za-z0-9_+-]{1,256}(?:\/[A-Za-z0-9_+-]{1,256})?)?/;
162
85
  const isoDuration = /^-?P(?:(?:(-?\d{1,20}(?:\.\d{1,20})?)Y)?(?:(-?\d{1,20}(?:\.\d{1,20})?)M)?(?:(-?\d{1,20}(?:\.\d{1,20})?)W)?(?:(-?\d{1,20}(?:\.\d{1,20})?)D)?(?:T(?:(-?\d{1,20}(?:\.\d{1,20})?)H)?(?:(-?\d{1,20}(?:\.\d{1,20})?)M)?(?:(-?\d{1,20})(?:[.,](-?\d{1,20}))?S)?)?)$/;
163
86
  function extractISODuration(match) {
164
87
  const [s, yearStr, monthStr, weekStr, dayStr, hourStr, minuteStr, secondStr, millisecondsStr] = match;
@@ -197,16 +120,15 @@ function createUTCDate(...args) {
197
120
  let date, y = args[0];
198
121
  // the Date.UTC function remaps years 0-99 to 1900-1999
199
122
  if (y < 100 && y >= 0) {
200
- args = Array.from(arguments);
201
123
  // preserve leap years using a full 400 year cycle, then reset
202
124
  args[0] = y + 400;
203
125
  date = new Date(Date.UTC.apply(null, args));
204
- if (isFinite(date.getUTCFullYear())) {
126
+ if (Number.isFinite(date.getUTCFullYear())) {
205
127
  date.setUTCFullYear(y);
206
128
  }
207
129
  }
208
130
  else {
209
- date = new Date(Date.UTC.apply(null, arguments));
131
+ date = new Date(Date.UTC.apply(null, args));
210
132
  }
211
133
  return date;
212
134
  }
@@ -217,26 +139,6 @@ function firstWeekOffset(year, dow, doy) {
217
139
  fwdlw = (7 + createUTCDate(year, 0, fwd).getUTCDay() - dow) % 7;
218
140
  return -fwdlw + fwd - 1;
219
141
  }
220
- // https://en.wikipedia.org/wiki/ISO_week_date#Calculating_a_date_given_the_year.2C_week_number_and_weekday
221
- function dayOfYearFromWeeks(year, week, weekday, dow, doy) {
222
- var localWeekday = (7 + weekday - dow) % 7, weekOffset = firstWeekOffset(year, dow, doy), dayOfYear = 1 + 7 * (week - 1) + localWeekday + weekOffset, resYear, resDayOfYear;
223
- if (dayOfYear <= 0) {
224
- resYear = year - 1;
225
- resDayOfYear = daysInYear(resYear) + dayOfYear;
226
- }
227
- else if (dayOfYear > daysInYear(year)) {
228
- resYear = year + 1;
229
- resDayOfYear = dayOfYear - daysInYear(year);
230
- }
231
- else {
232
- resYear = year;
233
- resDayOfYear = dayOfYear;
234
- }
235
- return {
236
- year: resYear,
237
- dayOfYear: resDayOfYear
238
- };
239
- }
240
142
  /**
241
143
  * Returns the number of weeks in the specified year.
242
144
  * @param year
@@ -249,14 +151,13 @@ export function weeksInYear(year, firstDayOfWeek = 1, firstDayOfYear = 1) {
249
151
  }
250
152
  /**
251
153
  * Returns the week of the year for the specified date
252
- * @param year
253
- * @param dayOfYear
254
- * @param firstDayOfWeek
255
- * @param firstDayOfYear
256
- * @returns
154
+ * @param date The date value
155
+ * @param firstDayOfWeek The weekday a week starts on, where Sunday is 0
156
+ * @param firstDayOfYear Picks week 1: the week holding January `7 + firstDayOfWeek - firstDayOfYear`, so `1, 4` gives ISO weeks
157
+ * @returns The week and the year that week belongs to
257
158
  */
258
159
  export function weekOfYear(date, firstDayOfWeek = 1, firstDayOfYear = 1) {
259
- const year = new Date(date).getFullYear();
160
+ const year = asDate(date).getFullYear();
260
161
  const doy = dayOfYear(date);
261
162
  let weekOffset = firstWeekOffset(year, firstDayOfWeek, firstDayOfYear), week = Math.floor((doy - weekOffset - 1) / 7) + 1, resWeek, resYear;
262
163
  if (week < 1) {
@@ -280,11 +181,12 @@ export function isoWeeksInYear(year) {
280
181
  return weeksInYear(year, 1, 4);
281
182
  }
282
183
  export function dayOfYear(input) {
283
- input = new Date(input);
184
+ input = asDate(input);
284
185
  input.setHours(0);
285
186
  input.setMinutes(0);
286
187
  input.setSeconds(0);
287
188
  input.setMilliseconds(0);
288
- let start = new Date(input.getFullYear(), 0, 1, 0, 0, 0, 0);
189
+ const start = new Date(input);
190
+ start.setMonth(0, 1);
289
191
  return Math.round((+input - start.getTime()) / 864e5) + 1;
290
192
  }
@@ -1,4 +1,4 @@
1
- export declare type DurationUnit = 'year' | 'years' | 'quarter' | 'quarters' | 'month' | 'months' | 'week' | 'weeks' | 'day' | 'days' | 'hour' | 'hours' | 'minute' | 'minutes' | 'second' | 'seconds' | 'millisecond' | 'milliseconds';
1
+ export type DurationUnit = 'year' | 'years' | 'quarter' | 'quarters' | 'month' | 'months' | 'week' | 'weeks' | 'day' | 'days' | 'hour' | 'hours' | 'minute' | 'minutes' | 'second' | 'seconds' | 'millisecond' | 'milliseconds';
2
2
  export interface TDuration {
3
3
  years?: number;
4
4
  months?: number;
@@ -14,6 +14,7 @@ export declare class Duration {
14
14
  values: TDuration;
15
15
  private matrix;
16
16
  constructor(dur?: number | TDuration | Duration, exact?: boolean);
17
+ private withValues;
17
18
  /** Get the years. */
18
19
  get years(): number;
19
20
  /** Get the months. */
@@ -24,7 +25,7 @@ export declare class Duration {
24
25
  get days(): number;
25
26
  /** Get the hours. */
26
27
  get hours(): number;
27
- /** et the minutes. */
28
+ /** Get the minutes. */
28
29
  get minutes(): number;
29
30
  /** Get the seconds. */
30
31
  get seconds(): number;
@@ -33,19 +34,19 @@ export declare class Duration {
33
34
  /**
34
35
  * Get the value of unit.
35
36
  * @param {string} unit - a unit such as 'minute' or 'day'
36
- * @example Duration.fromObject({years: 2, days: 3}).get('years') // 2
37
- * @example Duration.fromObject({years: 2, days: 3}).get('months') // 0
38
- * @example Duration.fromObject({years: 2, days: 3}).get('days') // 3
37
+ * @example new Duration({years: 2, days: 3}).get('years') // 2
38
+ * @example new Duration({years: 2, days: 3}).get('months') // 0
39
+ * @example new Duration({years: 2, days: 3}).get('days') // 3
39
40
  * @return {number}
40
41
  */
41
- get(unit: any): any;
42
+ get(unit: string): number;
42
43
  add(duration: TDuration): Duration;
43
44
  /**
44
45
  * Make this Duration shorter by the specified amount. Return a newly-constructed Duration.
45
46
  * @param {Duration|Object|number} duration - The amount to subtract.
46
47
  * @return {Duration}
47
48
  */
48
- subtract(duration: any): Duration;
49
+ subtract(duration: Duration | TDuration | number): Duration;
49
50
  /**
50
51
  * Return the negative of this Duration.
51
52
  * @example
@@ -85,11 +86,11 @@ export declare class Duration {
85
86
  /**
86
87
  * Returns an ISO 8601-compliant string representation of this Duration.
87
88
  * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
88
- * @example Duration.fromObject({ years: 3, seconds: 45 }).toString() // 'P3YT45S'
89
- * @example Duration.fromObject({ months: 4, seconds: 45 }).toString() // 'P4MT45S'
90
- * @example Duration.fromObject({ months: 5 }).toString() // 'P5M'
91
- * @example Duration.fromObject({ minutes: 5 }).toString() // 'PT5M'
92
- * @example Duration.fromObject({ milliseconds: 6 }).toString() // 'PT0.006S'
89
+ * @example new Duration({ years: 3, seconds: 45 }).toString() // 'P3YT45S'
90
+ * @example new Duration({ months: 4, seconds: 45 }).toString() // 'P4MT45S'
91
+ * @example new Duration({ months: 5 }).toString() // 'P5M'
92
+ * @example new Duration({ minutes: 5 }).toString() // 'PT5M'
93
+ * @example new Duration({ milliseconds: 6 }).toString() // 'PT0.006S'
93
94
  * @return {string}
94
95
  */
95
96
  toISOString(): string;
@@ -115,23 +115,26 @@ const orderedUnits = [
115
115
  ];
116
116
  const reverseUnits = orderedUnits.slice(0).reverse();
117
117
  export class Duration {
118
- values;
118
+ values = {};
119
119
  matrix = casualMatrix;
120
120
  constructor(dur = {}, exact = false) {
121
121
  if (dur instanceof Duration) {
122
122
  this.values = { ...dur.values };
123
123
  }
124
- else if (isNumber(dur)) {
124
+ else if (typeof dur === 'number') {
125
125
  this.values = { milliseconds: dur };
126
126
  }
127
127
  else if (isObject(dur)) {
128
128
  this.values = normalizeUnits(dur);
129
129
  }
130
- if (exact) {
130
+ if (exact || (dur instanceof Duration && dur.matrix === exactMatrix)) {
131
131
  // Set the conversion matrix to use
132
132
  this.matrix = exactMatrix;
133
133
  }
134
134
  }
135
+ withValues(values) {
136
+ return new Duration(values, this.matrix === exactMatrix);
137
+ }
135
138
  /** Get the years. */
136
139
  get years() {
137
140
  return this.values.years || 0;
@@ -152,7 +155,7 @@ export class Duration {
152
155
  get hours() {
153
156
  return this.values.hours || 0;
154
157
  }
155
- /** et the minutes. */
158
+ /** Get the minutes. */
156
159
  get minutes() {
157
160
  return this.values.minutes || 0;
158
161
  }
@@ -167,13 +170,13 @@ export class Duration {
167
170
  /**
168
171
  * Get the value of unit.
169
172
  * @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
+ * @example new Duration({years: 2, days: 3}).get('years') // 2
174
+ * @example new Duration({years: 2, days: 3}).get('months') // 0
175
+ * @example new Duration({years: 2, days: 3}).get('days') // 3
173
176
  * @return {number}
174
177
  */
175
178
  get(unit) {
176
- return this[UNITS_PLURAL[unit.toLowerCase()]];
179
+ return this.values[UNITS_PLURAL[unit.toLowerCase()]] ?? 0;
177
180
  }
178
181
  add(duration) {
179
182
  const dur = new Duration(duration);
@@ -183,7 +186,7 @@ export class Duration {
183
186
  result[k] = dur.get(k) + this.get(k);
184
187
  }
185
188
  }
186
- return new Duration(result);
189
+ return this.withValues(result);
187
190
  }
188
191
  /**
189
192
  * Make this Duration shorter by the specified amount. Return a newly-constructed Duration.
@@ -208,7 +211,7 @@ export class Duration {
208
211
  for (const k of Object.keys(this.values)) {
209
212
  negated[k] = this.values[k] === 0 ? 0 : -this.values[k];
210
213
  }
211
- return new Duration(negated);
214
+ return this.withValues(negated);
212
215
  }
213
216
  /**
214
217
  * Return the absolute values of this Duration.
@@ -224,7 +227,7 @@ export class Duration {
224
227
  for (const k of Object.keys(this.values)) {
225
228
  parts[k] = Math.abs(this.values[k]);
226
229
  }
227
- return new Duration(parts);
230
+ return this.withValues(parts);
228
231
  }
229
232
  shiftToAll() {
230
233
  return this.shiftTo('years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds', 'milliseconds');
@@ -273,59 +276,61 @@ export class Duration {
273
276
  key === lastUnit ? accumulated[key] : accumulated[key] / this.matrix[lastUnit][key];
274
277
  }
275
278
  }
276
- return new Duration(built);
279
+ return this.withValues(built);
277
280
  }
278
281
  total(unit) {
279
282
  return this.shiftTo(unit).get(unit);
280
283
  }
281
284
  exact(unit) {
285
+ if (unit)
286
+ return new Duration(this, true).total(unit);
282
287
  this.matrix = exactMatrix;
283
- if (!unit)
284
- return this;
285
- return this.total(unit);
288
+ return this;
286
289
  }
287
290
  toObject() {
288
291
  return { ...this.values };
289
292
  }
290
293
  normalize() {
291
- normalizeValues(this.matrix, this.values);
292
- return new Duration(this.values);
294
+ const values = this.toObject();
295
+ normalizeValues(this.matrix, values);
296
+ return this.withValues(values);
293
297
  }
294
298
  rescale() {
295
299
  const dur = this.normalize().shiftToAll();
296
300
  const vals = removeZeroes(dur.values);
297
- return new Duration(vals);
301
+ return this.withValues(vals);
298
302
  }
299
303
  /**
300
304
  * Returns an ISO 8601-compliant string representation of this Duration.
301
305
  * @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'
306
+ * @example new Duration({ years: 3, seconds: 45 }).toString() // 'P3YT45S'
307
+ * @example new Duration({ months: 4, seconds: 45 }).toString() // 'P4MT45S'
308
+ * @example new Duration({ months: 5 }).toString() // 'P5M'
309
+ * @example new Duration({ minutes: 5 }).toString() // 'PT5M'
310
+ * @example new Duration({ milliseconds: 6 }).toString() // 'PT0.006S'
307
311
  * @return {string}
308
312
  */
309
313
  toISOString() {
310
314
  let s = 'P';
315
+ const months = this.get('quarters') * 3 + this.months;
311
316
  if (this.years !== 0)
312
- s += this.years + 'Y';
313
- if (this.months !== 0)
314
- s += this.months + 'M';
317
+ s += `${this.years}Y`;
318
+ if (months !== 0)
319
+ s += `${months}M`;
315
320
  if (this.weeks !== 0)
316
- s += this.weeks + 'W';
321
+ s += `${this.weeks}W`;
317
322
  if (this.days !== 0)
318
- s += this.days + 'D';
323
+ s += `${this.days}D`;
319
324
  if (this.hours !== 0 || this.minutes !== 0 || this.seconds !== 0 || this.milliseconds !== 0)
320
325
  s += 'T';
321
326
  if (this.hours !== 0)
322
- s += this.hours + 'H';
327
+ s += `${this.hours}H`;
323
328
  if (this.minutes !== 0)
324
- s += this.minutes + 'M';
329
+ s += `${this.minutes}M`;
325
330
  if (this.seconds !== 0 || this.milliseconds !== 0)
326
331
  // this will handle "floating point madness" by removing extra decimal places
327
332
  // https://stackoverflow.com/questions/588004/is-floating-point-math-broken
328
- s += round(this.seconds + this.milliseconds / 1000, 3) + 'S';
333
+ s += `${round(this.seconds + this.milliseconds / 1000, 3)}S`;
329
334
  if (s === 'P')
330
335
  s += 'T0S';
331
336
  return s;
@@ -357,7 +362,8 @@ export class Duration {
357
362
  function normalizeUnits(obj) {
358
363
  const vals = {};
359
364
  for (const unit of Object.keys(obj)) {
360
- vals[UNITS_PLURAL[unit]] = obj[unit];
365
+ if (obj[unit] != null)
366
+ vals[UNITS_PLURAL[unit]] = obj[unit];
361
367
  }
362
368
  return vals;
363
369
  }
@@ -1,3 +1,4 @@
1
+ export declare function fromFields(utc: boolean, year: number, month: any, day: any, hour: any, minute: any, second: any, ms: any): Date;
1
2
  /**
2
3
  * Returns a new Date from the given arguments.
3
4
  * @param {string | number | Date} date The date value
@@ -7,11 +8,13 @@
7
8
  export declare function asDate(date: string | number | Date, utc?: boolean): Date;
8
9
  /**
9
10
  * 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
+ * @param {string} formatStr The format string to use
12
+ * @param {Date} date The date value
13
+ * @param {?string} locale The locale to use when formatting (default is system locale).
11
14
  * @see {@link https://momentjs.com/docs/#/displaying/format/ List of formats}
12
15
  * @example
13
16
  ```js
14
- formatDate(new Date(), 'MM/DD/YYYY') // '10/31/2022'
17
+ formatDate('MM/DD/YYYY', new Date()) // '10/31/2022'
15
18
  ```
16
19
  */
17
20
  export declare function formatDate(formatStr: string, date: Date, locale?: string): string;
@@ -22,5 +25,10 @@ export declare function formatDate(formatStr: string, date: Date, locale?: strin
22
25
  * @param {?string} locale The locale to use when formatting (default is system locale).
23
26
  * @returns {string} The string representation of date
24
27
  * @see {@link https://strftime.org/ strftime format}
28
+ * @example
29
+ ```js
30
+ strftime('%A, %d/%m/%y', new Date())
31
+ // 'Saturday, 01/02/2020'
32
+ ```
25
33
  */
26
34
  export declare function strftime(fmt: string, date: Date, locale?: string): string;