feelin 4.3.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 (114) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +2 -0
  4. data/CHANGELOG.md +10 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +43 -0
  8. data/feelin.gemspec +22 -0
  9. data/lib/feelin/js/node_modules/.package-lock.json +67 -0
  10. data/lib/feelin/js/node_modules/@lezer/common/LICENSE +21 -0
  11. data/lib/feelin/js/node_modules/@lezer/common/README.md +14 -0
  12. data/lib/feelin/js/node_modules/@lezer/common/dist/index.cjs +2181 -0
  13. data/lib/feelin/js/node_modules/@lezer/common/dist/index.d.cts +1137 -0
  14. data/lib/feelin/js/node_modules/@lezer/common/dist/index.d.ts +1137 -0
  15. data/lib/feelin/js/node_modules/@lezer/common/dist/index.js +2168 -0
  16. data/lib/feelin/js/node_modules/@lezer/common/package.json +32 -0
  17. data/lib/feelin/js/node_modules/@lezer/highlight/LICENSE +21 -0
  18. data/lib/feelin/js/node_modules/@lezer/highlight/README.md +14 -0
  19. data/lib/feelin/js/node_modules/@lezer/highlight/dist/index.cjs +915 -0
  20. data/lib/feelin/js/node_modules/@lezer/highlight/dist/index.d.cts +621 -0
  21. data/lib/feelin/js/node_modules/@lezer/highlight/dist/index.d.ts +623 -0
  22. data/lib/feelin/js/node_modules/@lezer/highlight/dist/index.js +904 -0
  23. data/lib/feelin/js/node_modules/@lezer/highlight/package.json +31 -0
  24. data/lib/feelin/js/node_modules/@lezer/lr/LICENSE +21 -0
  25. data/lib/feelin/js/node_modules/@lezer/lr/README.md +25 -0
  26. data/lib/feelin/js/node_modules/@lezer/lr/dist/constants.d.ts +45 -0
  27. data/lib/feelin/js/node_modules/@lezer/lr/dist/constants.js +5 -0
  28. data/lib/feelin/js/node_modules/@lezer/lr/dist/index.cjs +1890 -0
  29. data/lib/feelin/js/node_modules/@lezer/lr/dist/index.d.cts +303 -0
  30. data/lib/feelin/js/node_modules/@lezer/lr/dist/index.d.ts +303 -0
  31. data/lib/feelin/js/node_modules/@lezer/lr/dist/index.js +1883 -0
  32. data/lib/feelin/js/node_modules/@lezer/lr/package.json +32 -0
  33. data/lib/feelin/js/node_modules/feelin/LICENSE +21 -0
  34. data/lib/feelin/js/node_modules/feelin/README.md +65 -0
  35. data/lib/feelin/js/node_modules/feelin/dist/builtins.d.ts +355 -0
  36. data/lib/feelin/js/node_modules/feelin/dist/index.cjs +2072 -0
  37. data/lib/feelin/js/node_modules/feelin/dist/index.cjs.map +1 -0
  38. data/lib/feelin/js/node_modules/feelin/dist/index.d.ts +3 -0
  39. data/lib/feelin/js/node_modules/feelin/dist/index.esm.js +2063 -0
  40. data/lib/feelin/js/node_modules/feelin/dist/index.esm.js.map +1 -0
  41. data/lib/feelin/js/node_modules/feelin/dist/interpreter.d.ts +26 -0
  42. data/lib/feelin/js/node_modules/feelin/dist/parser.d.ts +4 -0
  43. data/lib/feelin/js/node_modules/feelin/dist/temporal.d.ts +6 -0
  44. data/lib/feelin/js/node_modules/feelin/dist/types.d.ts +35 -0
  45. data/lib/feelin/js/node_modules/feelin/dist/utils.d.ts +12 -0
  46. data/lib/feelin/js/node_modules/feelin/package.json +63 -0
  47. data/lib/feelin/js/node_modules/lezer-feel/LICENSE +21 -0
  48. data/lib/feelin/js/node_modules/lezer-feel/README.md +94 -0
  49. data/lib/feelin/js/node_modules/lezer-feel/dist/index.cjs +1328 -0
  50. data/lib/feelin/js/node_modules/lezer-feel/dist/index.cjs.map +1 -0
  51. data/lib/feelin/js/node_modules/lezer-feel/dist/index.d.ts +32 -0
  52. data/lib/feelin/js/node_modules/lezer-feel/dist/index.js +1323 -0
  53. data/lib/feelin/js/node_modules/lezer-feel/dist/index.js.map +1 -0
  54. data/lib/feelin/js/node_modules/lezer-feel/package.json +61 -0
  55. data/lib/feelin/js/node_modules/luxon/LICENSE.md +7 -0
  56. data/lib/feelin/js/node_modules/luxon/README.md +55 -0
  57. data/lib/feelin/js/node_modules/luxon/build/amd/luxon.js +8623 -0
  58. data/lib/feelin/js/node_modules/luxon/build/amd/luxon.js.map +1 -0
  59. data/lib/feelin/js/node_modules/luxon/build/cjs-browser/luxon.js +8621 -0
  60. data/lib/feelin/js/node_modules/luxon/build/cjs-browser/luxon.js.map +1 -0
  61. data/lib/feelin/js/node_modules/luxon/build/es6/luxon.js +8011 -0
  62. data/lib/feelin/js/node_modules/luxon/build/es6/luxon.js.map +1 -0
  63. data/lib/feelin/js/node_modules/luxon/build/global/luxon.js +8626 -0
  64. data/lib/feelin/js/node_modules/luxon/build/global/luxon.js.map +1 -0
  65. data/lib/feelin/js/node_modules/luxon/build/global/luxon.min.js +1 -0
  66. data/lib/feelin/js/node_modules/luxon/build/global/luxon.min.js.map +1 -0
  67. data/lib/feelin/js/node_modules/luxon/build/node/luxon.js +7679 -0
  68. data/lib/feelin/js/node_modules/luxon/build/node/luxon.js.map +1 -0
  69. data/lib/feelin/js/node_modules/luxon/package.json +87 -0
  70. data/lib/feelin/js/node_modules/luxon/src/datetime.js +2566 -0
  71. data/lib/feelin/js/node_modules/luxon/src/duration.js +990 -0
  72. data/lib/feelin/js/node_modules/luxon/src/errors.js +61 -0
  73. data/lib/feelin/js/node_modules/luxon/src/impl/conversions.js +206 -0
  74. data/lib/feelin/js/node_modules/luxon/src/impl/diff.js +95 -0
  75. data/lib/feelin/js/node_modules/luxon/src/impl/digits.js +90 -0
  76. data/lib/feelin/js/node_modules/luxon/src/impl/english.js +233 -0
  77. data/lib/feelin/js/node_modules/luxon/src/impl/formats.js +176 -0
  78. data/lib/feelin/js/node_modules/luxon/src/impl/formatter.js +409 -0
  79. data/lib/feelin/js/node_modules/luxon/src/impl/invalid.js +14 -0
  80. data/lib/feelin/js/node_modules/luxon/src/impl/locale.js +554 -0
  81. data/lib/feelin/js/node_modules/luxon/src/impl/regexParser.js +335 -0
  82. data/lib/feelin/js/node_modules/luxon/src/impl/tokenParser.js +505 -0
  83. data/lib/feelin/js/node_modules/luxon/src/impl/util.js +316 -0
  84. data/lib/feelin/js/node_modules/luxon/src/impl/zoneUtil.js +34 -0
  85. data/lib/feelin/js/node_modules/luxon/src/info.js +205 -0
  86. data/lib/feelin/js/node_modules/luxon/src/interval.js +665 -0
  87. data/lib/feelin/js/node_modules/luxon/src/luxon.js +26 -0
  88. data/lib/feelin/js/node_modules/luxon/src/package.json +4 -0
  89. data/lib/feelin/js/node_modules/luxon/src/settings.js +180 -0
  90. data/lib/feelin/js/node_modules/luxon/src/zone.js +97 -0
  91. data/lib/feelin/js/node_modules/luxon/src/zones/IANAZone.js +231 -0
  92. data/lib/feelin/js/node_modules/luxon/src/zones/fixedOffsetZone.js +150 -0
  93. data/lib/feelin/js/node_modules/luxon/src/zones/invalidZone.js +53 -0
  94. data/lib/feelin/js/node_modules/luxon/src/zones/systemZone.js +61 -0
  95. data/lib/feelin/js/node_modules/min-dash/LICENSE +21 -0
  96. data/lib/feelin/js/node_modules/min-dash/README.md +38 -0
  97. data/lib/feelin/js/node_modules/min-dash/dist/array.d.ts +12 -0
  98. data/lib/feelin/js/node_modules/min-dash/dist/collection.d.ts +174 -0
  99. data/lib/feelin/js/node_modules/min-dash/dist/fn.d.ts +37 -0
  100. data/lib/feelin/js/node_modules/min-dash/dist/index.cjs +910 -0
  101. data/lib/feelin/js/node_modules/min-dash/dist/index.d.ts +5 -0
  102. data/lib/feelin/js/node_modules/min-dash/dist/index.esm.js +872 -0
  103. data/lib/feelin/js/node_modules/min-dash/dist/lang.d.ts +29 -0
  104. data/lib/feelin/js/node_modules/min-dash/dist/min-dash.js +916 -0
  105. data/lib/feelin/js/node_modules/min-dash/dist/min-dash.min.js +1 -0
  106. data/lib/feelin/js/node_modules/min-dash/dist/object.d.ts +112 -0
  107. data/lib/feelin/js/node_modules/min-dash/package.json +72 -0
  108. data/lib/feelin/js/package-lock.json +72 -0
  109. data/lib/feelin/js/package.json +5 -0
  110. data/lib/feelin/version.rb +3 -0
  111. data/lib/feelin.rb +63 -0
  112. data/spec/feelin/feelin_spec.rb +38 -0
  113. data/spec/spec_helper.rb +2 -0
  114. metadata +198 -0
@@ -0,0 +1,990 @@
1
+ import { InvalidArgumentError, InvalidDurationError, InvalidUnitError } from "./errors.js";
2
+ import Formatter from "./impl/formatter.js";
3
+ import Invalid from "./impl/invalid.js";
4
+ import Locale from "./impl/locale.js";
5
+ import { parseISODuration, parseISOTimeOnly } from "./impl/regexParser.js";
6
+ import {
7
+ asNumber,
8
+ hasOwnProperty,
9
+ isNumber,
10
+ isUndefined,
11
+ normalizeObject,
12
+ roundTo,
13
+ } from "./impl/util.js";
14
+ import Settings from "./settings.js";
15
+ import DateTime from "./datetime.js";
16
+
17
+ const INVALID = "Invalid Duration";
18
+
19
+ // unit conversion constants
20
+ export const lowOrderMatrix = {
21
+ weeks: {
22
+ days: 7,
23
+ hours: 7 * 24,
24
+ minutes: 7 * 24 * 60,
25
+ seconds: 7 * 24 * 60 * 60,
26
+ milliseconds: 7 * 24 * 60 * 60 * 1000,
27
+ },
28
+ days: {
29
+ hours: 24,
30
+ minutes: 24 * 60,
31
+ seconds: 24 * 60 * 60,
32
+ milliseconds: 24 * 60 * 60 * 1000,
33
+ },
34
+ hours: { minutes: 60, seconds: 60 * 60, milliseconds: 60 * 60 * 1000 },
35
+ minutes: { seconds: 60, milliseconds: 60 * 1000 },
36
+ seconds: { milliseconds: 1000 },
37
+ },
38
+ casualMatrix = {
39
+ years: {
40
+ quarters: 4,
41
+ months: 12,
42
+ weeks: 52,
43
+ days: 365,
44
+ hours: 365 * 24,
45
+ minutes: 365 * 24 * 60,
46
+ seconds: 365 * 24 * 60 * 60,
47
+ milliseconds: 365 * 24 * 60 * 60 * 1000,
48
+ },
49
+ quarters: {
50
+ months: 3,
51
+ weeks: 13,
52
+ days: 91,
53
+ hours: 91 * 24,
54
+ minutes: 91 * 24 * 60,
55
+ seconds: 91 * 24 * 60 * 60,
56
+ milliseconds: 91 * 24 * 60 * 60 * 1000,
57
+ },
58
+ months: {
59
+ weeks: 4,
60
+ days: 30,
61
+ hours: 30 * 24,
62
+ minutes: 30 * 24 * 60,
63
+ seconds: 30 * 24 * 60 * 60,
64
+ milliseconds: 30 * 24 * 60 * 60 * 1000,
65
+ },
66
+
67
+ ...lowOrderMatrix,
68
+ },
69
+ daysInYearAccurate = 146097.0 / 400,
70
+ daysInMonthAccurate = 146097.0 / 4800,
71
+ accurateMatrix = {
72
+ years: {
73
+ quarters: 4,
74
+ months: 12,
75
+ weeks: daysInYearAccurate / 7,
76
+ days: daysInYearAccurate,
77
+ hours: daysInYearAccurate * 24,
78
+ minutes: daysInYearAccurate * 24 * 60,
79
+ seconds: daysInYearAccurate * 24 * 60 * 60,
80
+ milliseconds: daysInYearAccurate * 24 * 60 * 60 * 1000,
81
+ },
82
+ quarters: {
83
+ months: 3,
84
+ weeks: daysInYearAccurate / 28,
85
+ days: daysInYearAccurate / 4,
86
+ hours: (daysInYearAccurate * 24) / 4,
87
+ minutes: (daysInYearAccurate * 24 * 60) / 4,
88
+ seconds: (daysInYearAccurate * 24 * 60 * 60) / 4,
89
+ milliseconds: (daysInYearAccurate * 24 * 60 * 60 * 1000) / 4,
90
+ },
91
+ months: {
92
+ weeks: daysInMonthAccurate / 7,
93
+ days: daysInMonthAccurate,
94
+ hours: daysInMonthAccurate * 24,
95
+ minutes: daysInMonthAccurate * 24 * 60,
96
+ seconds: daysInMonthAccurate * 24 * 60 * 60,
97
+ milliseconds: daysInMonthAccurate * 24 * 60 * 60 * 1000,
98
+ },
99
+ ...lowOrderMatrix,
100
+ };
101
+
102
+ // units ordered by size
103
+ const orderedUnits = [
104
+ "years",
105
+ "quarters",
106
+ "months",
107
+ "weeks",
108
+ "days",
109
+ "hours",
110
+ "minutes",
111
+ "seconds",
112
+ "milliseconds",
113
+ ];
114
+
115
+ const reverseUnits = orderedUnits.slice(0).reverse();
116
+
117
+ // clone really means "create another instance just like this one, but with these changes"
118
+ function clone(dur, alts, clear = false) {
119
+ // deep merge for vals
120
+ const conf = {
121
+ values: clear ? alts.values : { ...dur.values, ...(alts.values || {}) },
122
+ loc: dur.loc.clone(alts.loc),
123
+ conversionAccuracy: alts.conversionAccuracy || dur.conversionAccuracy,
124
+ matrix: alts.matrix || dur.matrix,
125
+ };
126
+ return new Duration(conf);
127
+ }
128
+
129
+ function durationToMillis(matrix, vals) {
130
+ let sum = vals.milliseconds ?? 0;
131
+ for (const unit of reverseUnits.slice(1)) {
132
+ if (vals[unit]) {
133
+ sum += vals[unit] * matrix[unit]["milliseconds"];
134
+ }
135
+ }
136
+ return sum;
137
+ }
138
+
139
+ // NB: mutates parameters
140
+ function normalizeValues(matrix, vals) {
141
+ // the logic below assumes the overall value of the duration is positive
142
+ // if this is not the case, factor is used to make it so
143
+ const factor = durationToMillis(matrix, vals) < 0 ? -1 : 1;
144
+
145
+ orderedUnits.reduceRight((previous, current) => {
146
+ if (!isUndefined(vals[current])) {
147
+ if (previous) {
148
+ const previousVal = vals[previous] * factor;
149
+ const conv = matrix[current][previous];
150
+
151
+ // if (previousVal < 0):
152
+ // lower order unit is negative (e.g. { years: 2, days: -2 })
153
+ // normalize this by reducing the higher order unit by the appropriate amount
154
+ // and increasing the lower order unit
155
+ // this can never make the higher order unit negative, because this function only operates
156
+ // on positive durations, so the amount of time represented by the lower order unit cannot
157
+ // be larger than the higher order unit
158
+ // else:
159
+ // lower order unit is positive (e.g. { years: 2, days: 450 } or { years: -2, days: 450 })
160
+ // in this case we attempt to convert as much as possible from the lower order unit into
161
+ // the higher order one
162
+ //
163
+ // Math.floor takes care of both of these cases, rounding away from 0
164
+ // if previousVal < 0 it makes the absolute value larger
165
+ // if previousVal >= it makes the absolute value smaller
166
+ const rollUp = Math.floor(previousVal / conv);
167
+ vals[current] += rollUp * factor;
168
+ vals[previous] -= rollUp * conv * factor;
169
+ }
170
+ return current;
171
+ } else {
172
+ return previous;
173
+ }
174
+ }, null);
175
+
176
+ // try to convert any decimals into smaller units if possible
177
+ // for example for { years: 2.5, days: 0, seconds: 0 } we want to get { years: 2, days: 182, hours: 12 }
178
+ orderedUnits.reduce((previous, current) => {
179
+ if (!isUndefined(vals[current])) {
180
+ if (previous) {
181
+ const fraction = vals[previous] % 1;
182
+ vals[previous] -= fraction;
183
+ vals[current] += fraction * matrix[previous][current];
184
+ }
185
+ return current;
186
+ } else {
187
+ return previous;
188
+ }
189
+ }, null);
190
+ }
191
+
192
+ // Remove all properties with a value of 0 from an object
193
+ function removeZeroes(vals) {
194
+ const newVals = {};
195
+ for (const [key, value] of Object.entries(vals)) {
196
+ if (value !== 0) {
197
+ newVals[key] = value;
198
+ }
199
+ }
200
+ return newVals;
201
+ }
202
+
203
+ /**
204
+ * A Duration object represents a period of time, like "2 months" or "1 day, 1 hour". Conceptually, it's just a map of units to their quantities, accompanied by some additional configuration and methods for creating, parsing, interrogating, transforming, and formatting them. They can be used on their own or in conjunction with other Luxon types; for example, you can use {@link DateTime#plus} to add a Duration object to a DateTime, producing another DateTime.
205
+ *
206
+ * Here is a brief overview of commonly used methods and getters in Duration:
207
+ *
208
+ * * **Creation** To create a Duration, use {@link Duration.fromMillis}, {@link Duration.fromObject}, or {@link Duration.fromISO}.
209
+ * * **Unit values** See the {@link Duration#years}, {@link Duration#months}, {@link Duration#weeks}, {@link Duration#days}, {@link Duration#hours}, {@link Duration#minutes}, {@link Duration#seconds}, {@link Duration#milliseconds} accessors.
210
+ * * **Configuration** See {@link Duration#locale} and {@link Duration#numberingSystem} accessors.
211
+ * * **Transformation** To create new Durations out of old ones use {@link Duration#plus}, {@link Duration#minus}, {@link Duration#normalize}, {@link Duration#set}, {@link Duration#reconfigure}, {@link Duration#shiftTo}, and {@link Duration#negate}.
212
+ * * **Output** To convert the Duration into other representations, see {@link Duration#as}, {@link Duration#toISO}, {@link Duration#toFormat}, and {@link Duration#toJSON}
213
+ *
214
+ * There's are more methods documented below. In addition, for more information on subtler topics like internationalization and validity, see the external documentation.
215
+ */
216
+ export default class Duration {
217
+ /**
218
+ * @private
219
+ */
220
+ constructor(config) {
221
+ const accurate = config.conversionAccuracy === "longterm" || false;
222
+ let matrix = accurate ? accurateMatrix : casualMatrix;
223
+
224
+ if (config.matrix) {
225
+ matrix = config.matrix;
226
+ }
227
+
228
+ /**
229
+ * @access private
230
+ */
231
+ this.values = config.values;
232
+ /**
233
+ * @access private
234
+ */
235
+ this.loc = config.loc || Locale.create();
236
+ /**
237
+ * @access private
238
+ */
239
+ this.conversionAccuracy = accurate ? "longterm" : "casual";
240
+ /**
241
+ * @access private
242
+ */
243
+ this.invalid = config.invalid || null;
244
+ /**
245
+ * @access private
246
+ */
247
+ this.matrix = matrix;
248
+ /**
249
+ * @access private
250
+ */
251
+ this.isLuxonDuration = true;
252
+ }
253
+
254
+ /**
255
+ * Create Duration from a number of milliseconds.
256
+ * @param {number} count of milliseconds
257
+ * @param {Object} opts - options for parsing
258
+ * @param {string} [opts.locale='en-US'] - the locale to use
259
+ * @param {string} opts.numberingSystem - the numbering system to use
260
+ * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use
261
+ * @return {Duration}
262
+ */
263
+ static fromMillis(count, opts) {
264
+ return Duration.fromObject({ milliseconds: count }, opts);
265
+ }
266
+
267
+ /**
268
+ * Create a Duration from a JavaScript object with keys like 'years' and 'hours'.
269
+ * If this object is empty then a zero milliseconds duration is returned.
270
+ * @param {Object} obj - the object to create the DateTime from
271
+ * @param {number} obj.years
272
+ * @param {number} obj.quarters
273
+ * @param {number} obj.months
274
+ * @param {number} obj.weeks
275
+ * @param {number} obj.days
276
+ * @param {number} obj.hours
277
+ * @param {number} obj.minutes
278
+ * @param {number} obj.seconds
279
+ * @param {number} obj.milliseconds
280
+ * @param {Object} [opts=[]] - options for creating this Duration
281
+ * @param {string} [opts.locale='en-US'] - the locale to use
282
+ * @param {string} opts.numberingSystem - the numbering system to use
283
+ * @param {string} [opts.conversionAccuracy='casual'] - the preset conversion system to use
284
+ * @param {string} [opts.matrix=Object] - the custom conversion system to use
285
+ * @return {Duration}
286
+ */
287
+ static fromObject(obj, opts = {}) {
288
+ if (obj == null || typeof obj !== "object") {
289
+ throw new InvalidArgumentError(
290
+ `Duration.fromObject: argument expected to be an object, got ${
291
+ obj === null ? "null" : typeof obj
292
+ }`
293
+ );
294
+ }
295
+
296
+ return new Duration({
297
+ values: normalizeObject(obj, Duration.normalizeUnit),
298
+ loc: Locale.fromObject(opts),
299
+ conversionAccuracy: opts.conversionAccuracy,
300
+ matrix: opts.matrix,
301
+ });
302
+ }
303
+
304
+ /**
305
+ * Create a Duration from DurationLike.
306
+ *
307
+ * @param {Object | number | Duration} durationLike
308
+ * One of:
309
+ * - object with keys like 'years' and 'hours'.
310
+ * - number representing milliseconds
311
+ * - Duration instance
312
+ * @return {Duration}
313
+ */
314
+ static fromDurationLike(durationLike) {
315
+ if (isNumber(durationLike)) {
316
+ return Duration.fromMillis(durationLike);
317
+ } else if (Duration.isDuration(durationLike)) {
318
+ return durationLike;
319
+ } else if (typeof durationLike === "object") {
320
+ return Duration.fromObject(durationLike);
321
+ } else {
322
+ throw new InvalidArgumentError(
323
+ `Unknown duration argument ${durationLike} of type ${typeof durationLike}`
324
+ );
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Create a Duration from an ISO 8601 duration string.
330
+ * @param {string} text - text to parse
331
+ * @param {Object} opts - options for parsing
332
+ * @param {string} [opts.locale='en-US'] - the locale to use
333
+ * @param {string} opts.numberingSystem - the numbering system to use
334
+ * @param {string} [opts.conversionAccuracy='casual'] - the preset conversion system to use
335
+ * @param {string} [opts.matrix=Object] - the preset conversion system to use
336
+ * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
337
+ * @example Duration.fromISO('P3Y6M1W4DT12H30M5S').toObject() //=> { years: 3, months: 6, weeks: 1, days: 4, hours: 12, minutes: 30, seconds: 5 }
338
+ * @example Duration.fromISO('PT23H').toObject() //=> { hours: 23 }
339
+ * @example Duration.fromISO('P5Y3M').toObject() //=> { years: 5, months: 3 }
340
+ * @return {Duration}
341
+ */
342
+ static fromISO(text, opts) {
343
+ const [parsed] = parseISODuration(text);
344
+ if (parsed) {
345
+ return Duration.fromObject(parsed, opts);
346
+ } else {
347
+ return Duration.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`);
348
+ }
349
+ }
350
+
351
+ /**
352
+ * Create a Duration from an ISO 8601 time string.
353
+ * @param {string} text - text to parse
354
+ * @param {Object} opts - options for parsing
355
+ * @param {string} [opts.locale='en-US'] - the locale to use
356
+ * @param {string} opts.numberingSystem - the numbering system to use
357
+ * @param {string} [opts.conversionAccuracy='casual'] - the preset conversion system to use
358
+ * @param {string} [opts.matrix=Object] - the conversion system to use
359
+ * @see https://en.wikipedia.org/wiki/ISO_8601#Times
360
+ * @example Duration.fromISOTime('11:22:33.444').toObject() //=> { hours: 11, minutes: 22, seconds: 33, milliseconds: 444 }
361
+ * @example Duration.fromISOTime('11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
362
+ * @example Duration.fromISOTime('T11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
363
+ * @example Duration.fromISOTime('1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
364
+ * @example Duration.fromISOTime('T1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
365
+ * @return {Duration}
366
+ */
367
+ static fromISOTime(text, opts) {
368
+ const [parsed] = parseISOTimeOnly(text);
369
+ if (parsed) {
370
+ return Duration.fromObject(parsed, opts);
371
+ } else {
372
+ return Duration.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`);
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Create an invalid Duration.
378
+ * @param {string} reason - simple string of why this datetime is invalid. Should not contain parameters or anything else data-dependent
379
+ * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information
380
+ * @return {Duration}
381
+ */
382
+ static invalid(reason, explanation = null) {
383
+ if (!reason) {
384
+ throw new InvalidArgumentError("need to specify a reason the Duration is invalid");
385
+ }
386
+
387
+ const invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation);
388
+
389
+ if (Settings.throwOnInvalid) {
390
+ throw new InvalidDurationError(invalid);
391
+ } else {
392
+ return new Duration({ invalid });
393
+ }
394
+ }
395
+
396
+ /**
397
+ * @private
398
+ */
399
+ static normalizeUnit(unit) {
400
+ const normalized = {
401
+ year: "years",
402
+ years: "years",
403
+ quarter: "quarters",
404
+ quarters: "quarters",
405
+ month: "months",
406
+ months: "months",
407
+ week: "weeks",
408
+ weeks: "weeks",
409
+ day: "days",
410
+ days: "days",
411
+ hour: "hours",
412
+ hours: "hours",
413
+ minute: "minutes",
414
+ minutes: "minutes",
415
+ second: "seconds",
416
+ seconds: "seconds",
417
+ millisecond: "milliseconds",
418
+ milliseconds: "milliseconds",
419
+ }[unit ? unit.toLowerCase() : unit];
420
+
421
+ if (!normalized) throw new InvalidUnitError(unit);
422
+
423
+ return normalized;
424
+ }
425
+
426
+ /**
427
+ * Check if an object is a Duration. Works across context boundaries
428
+ * @param {object} o
429
+ * @return {boolean}
430
+ */
431
+ static isDuration(o) {
432
+ return (o && o.isLuxonDuration) || false;
433
+ }
434
+
435
+ /**
436
+ * Get the locale of a Duration, such 'en-GB'
437
+ * @type {string}
438
+ */
439
+ get locale() {
440
+ return this.isValid ? this.loc.locale : null;
441
+ }
442
+
443
+ /**
444
+ * Get the numbering system of a Duration, such 'beng'. The numbering system is used when formatting the Duration
445
+ *
446
+ * @type {string}
447
+ */
448
+ get numberingSystem() {
449
+ return this.isValid ? this.loc.numberingSystem : null;
450
+ }
451
+
452
+ /**
453
+ * Returns a string representation of this Duration formatted according to the specified format string. You may use these tokens:
454
+ * * `S` for milliseconds
455
+ * * `s` for seconds
456
+ * * `m` for minutes
457
+ * * `h` for hours
458
+ * * `d` for days
459
+ * * `w` for weeks
460
+ * * `M` for months
461
+ * * `y` for years
462
+ * Notes:
463
+ * * Add padding by repeating the token, e.g. "yy" pads the years to two digits, "hhhh" pads the hours out to four digits
464
+ * * Tokens can be escaped by wrapping with single quotes.
465
+ * * The duration will be converted to the set of units in the format string using {@link Duration#shiftTo} and the Durations's conversion accuracy setting.
466
+ * @param {string} fmt - the format string
467
+ * @param {Object} opts - options
468
+ * @param {boolean} [opts.floor=true] - floor numerical values
469
+ * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("y d s") //=> "1 6 2"
470
+ * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("yy dd sss") //=> "01 06 002"
471
+ * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("M S") //=> "12 518402000"
472
+ * @return {string}
473
+ */
474
+ toFormat(fmt, opts = {}) {
475
+ // reverse-compat since 1.2; we always round down now, never up, and we do it by default
476
+ const fmtOpts = {
477
+ ...opts,
478
+ floor: opts.round !== false && opts.floor !== false,
479
+ };
480
+ return this.isValid
481
+ ? Formatter.create(this.loc, fmtOpts).formatDurationFromString(this, fmt)
482
+ : INVALID;
483
+ }
484
+
485
+ /**
486
+ * Returns a string representation of a Duration with all units included.
487
+ * To modify its behavior, use `listStyle` and any Intl.NumberFormat option, though `unitDisplay` is especially relevant.
488
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options
489
+ * @param {Object} opts - Formatting options. Accepts the same keys as the options parameter of the native `Intl.NumberFormat` constructor, as well as `listStyle`.
490
+ * @param {string} [opts.listStyle='narrow'] - How to format the merged list. Corresponds to the `style` property of the options parameter of the native `Intl.ListFormat` constructor.
491
+ * @example
492
+ * ```js
493
+ * var dur = Duration.fromObject({ days: 1, hours: 5, minutes: 6 })
494
+ * dur.toHuman() //=> '1 day, 5 hours, 6 minutes'
495
+ * dur.toHuman({ listStyle: "long" }) //=> '1 day, 5 hours, and 6 minutes'
496
+ * dur.toHuman({ unitDisplay: "short" }) //=> '1 day, 5 hr, 6 min'
497
+ * ```
498
+ */
499
+ toHuman(opts = {}) {
500
+ if (!this.isValid) return INVALID;
501
+
502
+ const l = orderedUnits
503
+ .map((unit) => {
504
+ const val = this.values[unit];
505
+ if (isUndefined(val)) {
506
+ return null;
507
+ }
508
+ return this.loc
509
+ .numberFormatter({ style: "unit", unitDisplay: "long", ...opts, unit: unit.slice(0, -1) })
510
+ .format(val);
511
+ })
512
+ .filter((n) => n);
513
+
514
+ return this.loc
515
+ .listFormatter({ type: "conjunction", style: opts.listStyle || "narrow", ...opts })
516
+ .format(l);
517
+ }
518
+
519
+ /**
520
+ * Returns a JavaScript object with this Duration's values.
521
+ * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject() //=> { years: 1, days: 6, seconds: 2 }
522
+ * @return {Object}
523
+ */
524
+ toObject() {
525
+ if (!this.isValid) return {};
526
+ return { ...this.values };
527
+ }
528
+
529
+ /**
530
+ * Returns an ISO 8601-compliant string representation of this Duration.
531
+ * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
532
+ * @example Duration.fromObject({ years: 3, seconds: 45 }).toISO() //=> 'P3YT45S'
533
+ * @example Duration.fromObject({ months: 4, seconds: 45 }).toISO() //=> 'P4MT45S'
534
+ * @example Duration.fromObject({ months: 5 }).toISO() //=> 'P5M'
535
+ * @example Duration.fromObject({ minutes: 5 }).toISO() //=> 'PT5M'
536
+ * @example Duration.fromObject({ milliseconds: 6 }).toISO() //=> 'PT0.006S'
537
+ * @return {string}
538
+ */
539
+ toISO() {
540
+ // we could use the formatter, but this is an easier way to get the minimum string
541
+ if (!this.isValid) return null;
542
+
543
+ let s = "P";
544
+ if (this.years !== 0) s += this.years + "Y";
545
+ if (this.months !== 0 || this.quarters !== 0) s += this.months + this.quarters * 3 + "M";
546
+ if (this.weeks !== 0) s += this.weeks + "W";
547
+ if (this.days !== 0) s += this.days + "D";
548
+ if (this.hours !== 0 || this.minutes !== 0 || this.seconds !== 0 || this.milliseconds !== 0)
549
+ s += "T";
550
+ if (this.hours !== 0) s += this.hours + "H";
551
+ if (this.minutes !== 0) s += this.minutes + "M";
552
+ if (this.seconds !== 0 || this.milliseconds !== 0)
553
+ // this will handle "floating point madness" by removing extra decimal places
554
+ // https://stackoverflow.com/questions/588004/is-floating-point-math-broken
555
+ s += roundTo(this.seconds + this.milliseconds / 1000, 3) + "S";
556
+ if (s === "P") s += "T0S";
557
+ return s;
558
+ }
559
+
560
+ /**
561
+ * Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day.
562
+ * Note that this will return null if the duration is invalid, negative, or equal to or greater than 24 hours.
563
+ * @see https://en.wikipedia.org/wiki/ISO_8601#Times
564
+ * @param {Object} opts - options
565
+ * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0
566
+ * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0
567
+ * @param {boolean} [opts.includePrefix=false] - include the `T` prefix
568
+ * @param {string} [opts.format='extended'] - choose between the basic and extended format
569
+ * @example Duration.fromObject({ hours: 11 }).toISOTime() //=> '11:00:00.000'
570
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }) //=> '11:00:00'
571
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }) //=> '11:00'
572
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }) //=> 'T11:00:00.000'
573
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ format: 'basic' }) //=> '110000.000'
574
+ * @return {string}
575
+ */
576
+ toISOTime(opts = {}) {
577
+ if (!this.isValid) return null;
578
+
579
+ const millis = this.toMillis();
580
+ if (millis < 0 || millis >= 86400000) return null;
581
+
582
+ opts = {
583
+ suppressMilliseconds: false,
584
+ suppressSeconds: false,
585
+ includePrefix: false,
586
+ format: "extended",
587
+ ...opts,
588
+ includeOffset: false,
589
+ };
590
+
591
+ const dateTime = DateTime.fromMillis(millis, { zone: "UTC" });
592
+ return dateTime.toISOTime(opts);
593
+ }
594
+
595
+ /**
596
+ * Returns an ISO 8601 representation of this Duration appropriate for use in JSON.
597
+ * @return {string}
598
+ */
599
+ toJSON() {
600
+ return this.toISO();
601
+ }
602
+
603
+ /**
604
+ * Returns an ISO 8601 representation of this Duration appropriate for use in debugging.
605
+ * @return {string}
606
+ */
607
+ toString() {
608
+ return this.toISO();
609
+ }
610
+
611
+ /**
612
+ * Returns a string representation of this Duration appropriate for the REPL.
613
+ * @return {string}
614
+ */
615
+ [Symbol.for("nodejs.util.inspect.custom")]() {
616
+ if (this.isValid) {
617
+ return `Duration { values: ${JSON.stringify(this.values)} }`;
618
+ } else {
619
+ return `Duration { Invalid, reason: ${this.invalidReason} }`;
620
+ }
621
+ }
622
+
623
+ /**
624
+ * Returns an milliseconds value of this Duration.
625
+ * @return {number}
626
+ */
627
+ toMillis() {
628
+ if (!this.isValid) return NaN;
629
+
630
+ return durationToMillis(this.matrix, this.values);
631
+ }
632
+
633
+ /**
634
+ * Returns an milliseconds value of this Duration. Alias of {@link toMillis}
635
+ * @return {number}
636
+ */
637
+ valueOf() {
638
+ return this.toMillis();
639
+ }
640
+
641
+ /**
642
+ * Make this Duration longer by the specified amount. Return a newly-constructed Duration.
643
+ * @param {Duration|Object|number} duration - The amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()
644
+ * @return {Duration}
645
+ */
646
+ plus(duration) {
647
+ if (!this.isValid) return this;
648
+
649
+ const dur = Duration.fromDurationLike(duration),
650
+ result = {};
651
+
652
+ for (const k of orderedUnits) {
653
+ if (hasOwnProperty(dur.values, k) || hasOwnProperty(this.values, k)) {
654
+ result[k] = dur.get(k) + this.get(k);
655
+ }
656
+ }
657
+
658
+ return clone(this, { values: result }, true);
659
+ }
660
+
661
+ /**
662
+ * Make this Duration shorter by the specified amount. Return a newly-constructed Duration.
663
+ * @param {Duration|Object|number} duration - The amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()
664
+ * @return {Duration}
665
+ */
666
+ minus(duration) {
667
+ if (!this.isValid) return this;
668
+
669
+ const dur = Duration.fromDurationLike(duration);
670
+ return this.plus(dur.negate());
671
+ }
672
+
673
+ /**
674
+ * Scale this Duration by the specified amount. Return a newly-constructed Duration.
675
+ * @param {function} fn - The function to apply to each unit. Arity is 1 or 2: the value of the unit and, optionally, the unit name. Must return a number.
676
+ * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits(x => x * 2) //=> { hours: 2, minutes: 60 }
677
+ * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits((x, u) => u === "hours" ? x * 2 : x) //=> { hours: 2, minutes: 30 }
678
+ * @return {Duration}
679
+ */
680
+ mapUnits(fn) {
681
+ if (!this.isValid) return this;
682
+ const result = {};
683
+ for (const k of Object.keys(this.values)) {
684
+ result[k] = asNumber(fn(this.values[k], k));
685
+ }
686
+ return clone(this, { values: result }, true);
687
+ }
688
+
689
+ /**
690
+ * Get the value of unit.
691
+ * @param {string} unit - a unit such as 'minute' or 'day'
692
+ * @example Duration.fromObject({years: 2, days: 3}).get('years') //=> 2
693
+ * @example Duration.fromObject({years: 2, days: 3}).get('months') //=> 0
694
+ * @example Duration.fromObject({years: 2, days: 3}).get('days') //=> 3
695
+ * @return {number}
696
+ */
697
+ get(unit) {
698
+ return this[Duration.normalizeUnit(unit)];
699
+ }
700
+
701
+ /**
702
+ * "Set" the values of specified units. Return a newly-constructed Duration.
703
+ * @param {Object} values - a mapping of units to numbers
704
+ * @example dur.set({ years: 2017 })
705
+ * @example dur.set({ hours: 8, minutes: 30 })
706
+ * @return {Duration}
707
+ */
708
+ set(values) {
709
+ if (!this.isValid) return this;
710
+
711
+ const mixed = { ...this.values, ...normalizeObject(values, Duration.normalizeUnit) };
712
+ return clone(this, { values: mixed });
713
+ }
714
+
715
+ /**
716
+ * "Set" the locale and/or numberingSystem. Returns a newly-constructed Duration.
717
+ * @example dur.reconfigure({ locale: 'en-GB' })
718
+ * @return {Duration}
719
+ */
720
+ reconfigure({ locale, numberingSystem, conversionAccuracy, matrix } = {}) {
721
+ const loc = this.loc.clone({ locale, numberingSystem });
722
+ const opts = { loc, matrix, conversionAccuracy };
723
+ return clone(this, opts);
724
+ }
725
+
726
+ /**
727
+ * Return the length of the duration in the specified unit.
728
+ * @param {string} unit - a unit such as 'minutes' or 'days'
729
+ * @example Duration.fromObject({years: 1}).as('days') //=> 365
730
+ * @example Duration.fromObject({years: 1}).as('months') //=> 12
731
+ * @example Duration.fromObject({hours: 60}).as('days') //=> 2.5
732
+ * @return {number}
733
+ */
734
+ as(unit) {
735
+ return this.isValid ? this.shiftTo(unit).get(unit) : NaN;
736
+ }
737
+
738
+ /**
739
+ * Reduce this Duration to its canonical representation in its current units.
740
+ * Assuming the overall value of the Duration is positive, this means:
741
+ * - excessive values for lower-order units are converted to higher-order units (if possible, see first and second example)
742
+ * - negative lower-order units are converted to higher order units (there must be such a higher order unit, otherwise
743
+ * the overall value would be negative, see third example)
744
+ * - fractional values for higher-order units are converted to lower-order units (if possible, see fourth example)
745
+ *
746
+ * If the overall value is negative, the result of this method is equivalent to `this.negate().normalize().negate()`.
747
+ * @example Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject() //=> { years: 15, days: 255 }
748
+ * @example Duration.fromObject({ days: 5000 }).normalize().toObject() //=> { days: 5000 }
749
+ * @example Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject() //=> { hours: 11, minutes: 15 }
750
+ * @example Duration.fromObject({ years: 2.5, days: 0, hours: 0 }).normalize().toObject() //=> { years: 2, days: 182, hours: 12 }
751
+ * @return {Duration}
752
+ */
753
+ normalize() {
754
+ if (!this.isValid) return this;
755
+ const vals = this.toObject();
756
+ normalizeValues(this.matrix, vals);
757
+ return clone(this, { values: vals }, true);
758
+ }
759
+
760
+ /**
761
+ * Rescale units to its largest representation
762
+ * @example Duration.fromObject({ milliseconds: 90000 }).rescale().toObject() //=> { minutes: 1, seconds: 30 }
763
+ * @return {Duration}
764
+ */
765
+ rescale() {
766
+ if (!this.isValid) return this;
767
+ const vals = removeZeroes(this.normalize().shiftToAll().toObject());
768
+ return clone(this, { values: vals }, true);
769
+ }
770
+
771
+ /**
772
+ * Convert this Duration into its representation in a different set of units.
773
+ * @example Duration.fromObject({ hours: 1, seconds: 30 }).shiftTo('minutes', 'milliseconds').toObject() //=> { minutes: 60, milliseconds: 30000 }
774
+ * @return {Duration}
775
+ */
776
+ shiftTo(...units) {
777
+ if (!this.isValid) return this;
778
+
779
+ if (units.length === 0) {
780
+ return this;
781
+ }
782
+
783
+ units = units.map((u) => Duration.normalizeUnit(u));
784
+
785
+ const built = {},
786
+ accumulated = {},
787
+ vals = this.toObject();
788
+ let lastUnit;
789
+
790
+ for (const k of orderedUnits) {
791
+ if (units.indexOf(k) >= 0) {
792
+ lastUnit = k;
793
+
794
+ let own = 0;
795
+
796
+ // anything we haven't boiled down yet should get boiled to this unit
797
+ for (const ak in accumulated) {
798
+ own += this.matrix[ak][k] * accumulated[ak];
799
+ accumulated[ak] = 0;
800
+ }
801
+
802
+ // plus anything that's already in this unit
803
+ if (isNumber(vals[k])) {
804
+ own += vals[k];
805
+ }
806
+
807
+ // only keep the integer part for now in the hopes of putting any decimal part
808
+ // into a smaller unit later
809
+ const i = Math.trunc(own);
810
+ built[k] = i;
811
+ accumulated[k] = (own * 1000 - i * 1000) / 1000;
812
+
813
+ // otherwise, keep it in the wings to boil it later
814
+ } else if (isNumber(vals[k])) {
815
+ accumulated[k] = vals[k];
816
+ }
817
+ }
818
+
819
+ // anything leftover becomes the decimal for the last unit
820
+ // lastUnit must be defined since units is not empty
821
+ for (const key in accumulated) {
822
+ if (accumulated[key] !== 0) {
823
+ built[lastUnit] +=
824
+ key === lastUnit ? accumulated[key] : accumulated[key] / this.matrix[lastUnit][key];
825
+ }
826
+ }
827
+
828
+ normalizeValues(this.matrix, built);
829
+ return clone(this, { values: built }, true);
830
+ }
831
+
832
+ /**
833
+ * Shift this Duration to all available units.
834
+ * Same as shiftTo("years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds")
835
+ * @return {Duration}
836
+ */
837
+ shiftToAll() {
838
+ if (!this.isValid) return this;
839
+ return this.shiftTo(
840
+ "years",
841
+ "months",
842
+ "weeks",
843
+ "days",
844
+ "hours",
845
+ "minutes",
846
+ "seconds",
847
+ "milliseconds"
848
+ );
849
+ }
850
+
851
+ /**
852
+ * Return the negative of this Duration.
853
+ * @example Duration.fromObject({ hours: 1, seconds: 30 }).negate().toObject() //=> { hours: -1, seconds: -30 }
854
+ * @return {Duration}
855
+ */
856
+ negate() {
857
+ if (!this.isValid) return this;
858
+ const negated = {};
859
+ for (const k of Object.keys(this.values)) {
860
+ negated[k] = this.values[k] === 0 ? 0 : -this.values[k];
861
+ }
862
+ return clone(this, { values: negated }, true);
863
+ }
864
+
865
+ /**
866
+ * Get the years.
867
+ * @type {number}
868
+ */
869
+ get years() {
870
+ return this.isValid ? this.values.years || 0 : NaN;
871
+ }
872
+
873
+ /**
874
+ * Get the quarters.
875
+ * @type {number}
876
+ */
877
+ get quarters() {
878
+ return this.isValid ? this.values.quarters || 0 : NaN;
879
+ }
880
+
881
+ /**
882
+ * Get the months.
883
+ * @type {number}
884
+ */
885
+ get months() {
886
+ return this.isValid ? this.values.months || 0 : NaN;
887
+ }
888
+
889
+ /**
890
+ * Get the weeks
891
+ * @type {number}
892
+ */
893
+ get weeks() {
894
+ return this.isValid ? this.values.weeks || 0 : NaN;
895
+ }
896
+
897
+ /**
898
+ * Get the days.
899
+ * @type {number}
900
+ */
901
+ get days() {
902
+ return this.isValid ? this.values.days || 0 : NaN;
903
+ }
904
+
905
+ /**
906
+ * Get the hours.
907
+ * @type {number}
908
+ */
909
+ get hours() {
910
+ return this.isValid ? this.values.hours || 0 : NaN;
911
+ }
912
+
913
+ /**
914
+ * Get the minutes.
915
+ * @type {number}
916
+ */
917
+ get minutes() {
918
+ return this.isValid ? this.values.minutes || 0 : NaN;
919
+ }
920
+
921
+ /**
922
+ * Get the seconds.
923
+ * @return {number}
924
+ */
925
+ get seconds() {
926
+ return this.isValid ? this.values.seconds || 0 : NaN;
927
+ }
928
+
929
+ /**
930
+ * Get the milliseconds.
931
+ * @return {number}
932
+ */
933
+ get milliseconds() {
934
+ return this.isValid ? this.values.milliseconds || 0 : NaN;
935
+ }
936
+
937
+ /**
938
+ * Returns whether the Duration is invalid. Invalid durations are returned by diff operations
939
+ * on invalid DateTimes or Intervals.
940
+ * @return {boolean}
941
+ */
942
+ get isValid() {
943
+ return this.invalid === null;
944
+ }
945
+
946
+ /**
947
+ * Returns an error code if this Duration became invalid, or null if the Duration is valid
948
+ * @return {string}
949
+ */
950
+ get invalidReason() {
951
+ return this.invalid ? this.invalid.reason : null;
952
+ }
953
+
954
+ /**
955
+ * Returns an explanation of why this Duration became invalid, or null if the Duration is valid
956
+ * @type {string}
957
+ */
958
+ get invalidExplanation() {
959
+ return this.invalid ? this.invalid.explanation : null;
960
+ }
961
+
962
+ /**
963
+ * Equality check
964
+ * Two Durations are equal iff they have the same units and the same values for each unit.
965
+ * @param {Duration} other
966
+ * @return {boolean}
967
+ */
968
+ equals(other) {
969
+ if (!this.isValid || !other.isValid) {
970
+ return false;
971
+ }
972
+
973
+ if (!this.loc.equals(other.loc)) {
974
+ return false;
975
+ }
976
+
977
+ function eq(v1, v2) {
978
+ // Consider 0 and undefined as equal
979
+ if (v1 === undefined || v1 === 0) return v2 === undefined || v2 === 0;
980
+ return v1 === v2;
981
+ }
982
+
983
+ for (const u of orderedUnits) {
984
+ if (!eq(this.values[u], other.values[u])) {
985
+ return false;
986
+ }
987
+ }
988
+ return true;
989
+ }
990
+ }