From 9b0e86a8e74768c4fe848fb5ce8d754292db4e3e Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Tue, 23 Apr 2024 00:37:58 -0400 Subject: New upstream version 8.3.0. --- npm_assets/node_modules/luxon/src/interval.js | 127 ++++++++++++++++---------- 1 file changed, 81 insertions(+), 46 deletions(-) (limited to 'npm_assets/node_modules/luxon/src/interval.js') diff --git a/npm_assets/node_modules/luxon/src/interval.js b/npm_assets/node_modules/luxon/src/interval.js index 71273fd..33caf6d 100644 --- a/npm_assets/node_modules/luxon/src/interval.js +++ b/npm_assets/node_modules/luxon/src/interval.js @@ -1,8 +1,10 @@ import DateTime, { friendlyDateTime } from "./datetime.js"; -import Duration, { friendlyDuration } from "./duration.js"; +import Duration from "./duration.js"; import Settings from "./settings.js"; import { InvalidArgumentError, InvalidIntervalError } from "./errors.js"; import Invalid from "./impl/invalid.js"; +import Formatter from "./impl/formatter.js"; +import * as Formats from "./impl/formats.js"; const INVALID = "Invalid Interval"; @@ -27,12 +29,12 @@ function validateStartEnd(start, end) { * * Here is a brief overview of the most commonly used methods and getters in Interval: * - * * **Creation** To create an Interval, use {@link fromDateTimes}, {@link after}, {@link before}, or {@link fromISO}. - * * **Accessors** Use {@link start} and {@link end} to get the start and end. - * * **Interrogation** To analyze the Interval, use {@link count}, {@link length}, {@link hasSame}, {@link contains}, {@link isAfter}, or {@link isBefore}. - * * **Transformation** To create other Intervals out of this one, use {@link set}, {@link splitAt}, {@link splitBy}, {@link divideEqually}, {@link merge}, {@link xor}, {@link union}, {@link intersection}, or {@link difference}. - * * **Comparison** To compare this Interval to another one, use {@link equals}, {@link overlaps}, {@link abutsStart}, {@link abutsEnd}, {@link engulfs}. - * * **Output** To convert the Interval into other representations, see {@link toString}, {@link toISO}, {@link toISODate}, {@link toISOTime}, {@link toFormat}, and {@link toDuration}. + * * **Creation** To create an Interval, use {@link Interval.fromDateTimes}, {@link Interval.after}, {@link Interval.before}, or {@link Interval.fromISO}. + * * **Accessors** Use {@link Interval#start} and {@link Interval#end} to get the start and end. + * * **Interrogation** To analyze the Interval, use {@link Interval#count}, {@link Interval#length}, {@link Interval#hasSame}, {@link Interval#contains}, {@link Interval#isAfter}, or {@link Interval#isBefore}. + * * **Transformation** To create other Intervals out of this one, use {@link Interval#set}, {@link Interval#splitAt}, {@link Interval#splitBy}, {@link Interval#divideEqually}, {@link Interval.merge}, {@link Interval.xor}, {@link Interval#union}, {@link Interval#intersection}, or {@link Interval#difference}. + * * **Comparison** To compare this Interval to another one, use {@link Interval#equals}, {@link Interval#overlaps}, {@link Interval#abutsStart}, {@link Interval#abutsEnd}, {@link Interval#engulfs} + * * **Output** To convert the Interval into other representations, see {@link Interval#toString}, {@link Interval#toLocaleString}, {@link Interval#toISO}, {@link Interval#toISODate}, {@link Interval#toISOTime}, {@link Interval#toFormat}, and {@link Interval#toDuration}. */ export default class Interval { /** @@ -92,7 +94,7 @@ export default class Interval { if (validateError == null) { return new Interval({ start: builtStart, - end: builtEnd + end: builtEnd, }); } else { return validateError; @@ -106,7 +108,7 @@ export default class Interval { * @return {Interval} */ static after(start, duration) { - const dur = friendlyDuration(duration), + const dur = Duration.fromDurationLike(duration), dt = friendlyDateTime(start); return Interval.fromDateTimes(dt, dt.plus(dur)); } @@ -118,7 +120,7 @@ export default class Interval { * @return {Interval} */ static before(end, duration) { - const dur = friendlyDuration(duration), + const dur = Duration.fromDurationLike(duration), dt = friendlyDateTime(end); return Interval.fromDateTimes(dt.minus(dur), dt); } @@ -127,7 +129,7 @@ export default class Interval { * Create an Interval from an ISO 8601 string. * Accepts `/`, `/`, and `/` formats. * @param {string} text - the ISO string to parse - * @param {Object} [opts] - options to pass {@link DateTime.fromISO} and optionally {@link Duration.fromISO} + * @param {Object} [opts] - options to pass {@link DateTime#fromISO} and optionally {@link Duration#fromISO} * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals * @return {Interval} */ @@ -229,7 +231,7 @@ export default class Interval { /** * Returns the count of minutes, hours, days, months, or years included in the Interval, even in part. - * Unlike {@link length} this counts sections of the calendar, not periods of time, e.g. specifying 'day' + * Unlike {@link Interval#length} this counts sections of the calendar, not periods of time, e.g. specifying 'day' * asks 'what dates are included in this interval?', not 'how many days long is this interval?' * @param {string} [unit='milliseconds'] - the unit of time to count. * @return {number} @@ -238,7 +240,7 @@ export default class Interval { if (!this.isValid) return NaN; const start = this.start.startOf(unit), end = this.end.startOf(unit); - return Math.floor(end.diff(start, unit).get(unit)) + 1; + return Math.floor(end.diff(start, unit).get(unit)) + (end.valueOf() !== this.end.valueOf()); } /** @@ -302,14 +304,14 @@ export default class Interval { /** * Split this Interval at each of the specified DateTimes - * @param {...[DateTime]} dateTimes - the unit of time to count. - * @return {[Interval]} + * @param {...DateTime} dateTimes - the unit of time to count. + * @return {Array} */ splitAt(...dateTimes) { if (!this.isValid) return []; const sorted = dateTimes .map(friendlyDateTime) - .filter(d => this.contains(d)) + .filter((d) => this.contains(d)) .sort(), results = []; let { s } = this, @@ -330,10 +332,10 @@ export default class Interval { * Split this Interval into smaller Intervals, each of the specified length. * Left over time is grouped into a smaller interval * @param {Duration|Object|number} duration - The length of each resulting interval. - * @return {[Interval]} + * @return {Array} */ splitBy(duration) { - const dur = friendlyDuration(duration); + const dur = Duration.fromDurationLike(duration); if (!this.isValid || !dur.isValid || dur.as("milliseconds") === 0) { return []; @@ -345,7 +347,7 @@ export default class Interval { const results = []; while (s < this.e) { - const added = this.start.plus(dur.mapUnits(x => x * idx)); + const added = this.start.plus(dur.mapUnits((x) => x * idx)); next = +added > +this.e ? this.e : added; results.push(Interval.fromDateTimes(s, next)); s = next; @@ -358,7 +360,7 @@ export default class Interval { /** * Split this Interval into the specified number of smaller intervals. * @param {number} numberOfParts - The number of Intervals to divide the Interval into. - * @return {[Interval]} + * @return {Array} */ divideEqually(numberOfParts) { if (!this.isValid) return []; @@ -452,22 +454,24 @@ export default class Interval { /** * Merge an array of Intervals into a equivalent minimal set of Intervals. * Combines overlapping and adjacent Intervals. - * @param {[Interval]} intervals - * @return {[Interval]} + * @param {Array} intervals + * @return {Array} */ static merge(intervals) { - const [found, final] = intervals.sort((a, b) => a.s - b.s).reduce( - ([sofar, current], item) => { - if (!current) { - return [sofar, item]; - } else if (current.overlaps(item) || current.abutsStart(item)) { - return [sofar, current.union(item)]; - } else { - return [sofar.concat([current]), item]; - } - }, - [[], null] - ); + const [found, final] = intervals + .sort((a, b) => a.s - b.s) + .reduce( + ([sofar, current], item) => { + if (!current) { + return [sofar, item]; + } else if (current.overlaps(item) || current.abutsStart(item)) { + return [sofar, current.union(item)]; + } else { + return [sofar.concat([current]), item]; + } + }, + [[], null] + ); if (final) { found.push(final); } @@ -476,14 +480,17 @@ export default class Interval { /** * Return an array of Intervals representing the spans of time that only appear in one of the specified Intervals. - * @param {[Interval]} intervals - * @return {[Interval]} + * @param {Array} intervals + * @return {Array} */ static xor(intervals) { let start = null, currentCount = 0; const results = [], - ends = intervals.map(i => [{ time: i.s, type: "s" }, { time: i.e, type: "e" }]), + ends = intervals.map((i) => [ + { time: i.s, type: "s" }, + { time: i.e, type: "e" }, + ]), flattened = Array.prototype.concat(...ends), arr = flattened.sort((a, b) => a.time - b.time); @@ -507,12 +514,12 @@ export default class Interval { /** * Return an Interval representing the span of time in this Interval that doesn't overlap with any of the specified Intervals. * @param {...Interval} intervals - * @return {[Interval]} + * @return {Array} */ difference(...intervals) { return Interval.xor([this].concat(intervals)) - .map(i => this.intersection(i)) - .filter(i => i && !i.isEmpty()); + .map((i) => this.intersection(i)) + .filter((i) => i && !i.isEmpty()); } /** @@ -524,10 +531,34 @@ export default class Interval { return `[${this.s.toISO()} – ${this.e.toISO()})`; } + /** + * Returns a localized string representing this Interval. Accepts the same options as the + * Intl.DateTimeFormat constructor and any presets defined by Luxon, such as + * {@link DateTime.DATE_FULL} or {@link DateTime.TIME_SIMPLE}. The exact behavior of this method + * is browser-specific, but in general it will return an appropriate representation of the + * Interval in the assigned locale. Defaults to the system's locale if no locale has been + * specified. + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat + * @param {Object} [formatOpts=DateTime.DATE_SHORT] - Either a DateTime preset or + * Intl.DateTimeFormat constructor options. + * @param {Object} opts - Options to override the configuration of the start DateTime. + * @example Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(); //=> 11/7/2022 – 11/8/2022 + * @example Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(DateTime.DATE_FULL); //=> November 7 – 8, 2022 + * @example Interval.fromISO('2022-11-07T09:00Z/2022-11-08T09:00Z').toLocaleString(DateTime.DATE_FULL, { locale: 'fr-FR' }); //=> 7–8 novembre 2022 + * @example Interval.fromISO('2022-11-07T17:00Z/2022-11-07T19:00Z').toLocaleString(DateTime.TIME_SIMPLE); //=> 6:00 – 8:00 PM + * @example Interval.fromISO('2022-11-07T17:00Z/2022-11-07T19:00Z').toLocaleString({ weekday: 'short', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' }); //=> Mon, Nov 07, 6:00 – 8:00 p + * @return {string} + */ + toLocaleString(formatOpts = Formats.DATE_SHORT, opts = {}) { + return this.isValid + ? Formatter.create(this.s.loc.clone(opts), formatOpts).formatInterval(this) + : INVALID; + } + /** * Returns an ISO 8601-compliant string representation of this Interval. * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals - * @param {Object} opts - The same options as {@link DateTime.toISO} + * @param {Object} opts - The same options as {@link DateTime#toISO} * @return {string} */ toISO(opts) { @@ -550,7 +581,7 @@ export default class Interval { * Returns an ISO 8601-compliant string representation of time of this Interval. * The date components are ignored. * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals - * @param {Object} opts - The same options as {@link DateTime.toISO} + * @param {Object} opts - The same options as {@link DateTime#toISO} * @return {string} */ toISOTime(opts) { @@ -559,10 +590,14 @@ export default class Interval { } /** - * Returns a string representation of this Interval formatted according to the specified format string. - * @param {string} dateFormat - the format string. This string formats the start and end time. See {@link DateTime.toFormat} for details. - * @param {Object} opts - options - * @param {string} [opts.separator = ' – '] - a separator to place between the start and end representations + * Returns a string representation of this Interval formatted according to the specified format + * string. **You may not want this.** See {@link Interval#toLocaleString} for a more flexible + * formatting tool. + * @param {string} dateFormat - The format string. This string formats the start and end time. + * See {@link DateTime#toFormat} for details. + * @param {Object} opts - Options. + * @param {string} [opts.separator = ' – '] - A separator to place between the start and end + * representations. * @return {string} */ toFormat(dateFormat, { separator = " – " } = {}) { -- cgit v1.2.3