pond-ts 0.1.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.
- package/LICENSE +21 -0
- package/README.md +348 -0
- package/dist/BoundedSequence.d.ts +28 -0
- package/dist/BoundedSequence.d.ts.map +1 -0
- package/dist/BoundedSequence.js +70 -0
- package/dist/BoundedSequence.js.map +1 -0
- package/dist/Event.d.ts +84 -0
- package/dist/Event.d.ts.map +1 -0
- package/dist/Event.js +162 -0
- package/dist/Event.js.map +1 -0
- package/dist/Interval.d.ts +51 -0
- package/dist/Interval.d.ts.map +1 -0
- package/dist/Interval.js +130 -0
- package/dist/Interval.js.map +1 -0
- package/dist/Sequence.d.ts +80 -0
- package/dist/Sequence.d.ts.map +1 -0
- package/dist/Sequence.js +197 -0
- package/dist/Sequence.js.map +1 -0
- package/dist/Time.d.ts +43 -0
- package/dist/Time.d.ts.map +1 -0
- package/dist/Time.js +78 -0
- package/dist/Time.js.map +1 -0
- package/dist/TimeRange.d.ts +45 -0
- package/dist/TimeRange.d.ts.map +1 -0
- package/dist/TimeRange.js +144 -0
- package/dist/TimeRange.js.map +1 -0
- package/dist/TimeSeries.d.ts +337 -0
- package/dist/TimeSeries.d.ts.map +1 -0
- package/dist/TimeSeries.js +1217 -0
- package/dist/TimeSeries.js.map +1 -0
- package/dist/calendar.d.ts +24 -0
- package/dist/calendar.d.ts.map +1 -0
- package/dist/calendar.js +96 -0
- package/dist/calendar.js.map +1 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +7 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/temporal.d.ts +37 -0
- package/dist/temporal.d.ts.map +1 -0
- package/dist/temporal.js +29 -0
- package/dist/temporal.js.map +1 -0
- package/dist/types.d.ts +175 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +3 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +145 -0
- package/dist/validate.js.map +1 -0
- package/package.json +44 -0
package/dist/Time.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type TimeZoneOptions } from './calendar.js';
|
|
2
|
+
import { TimeRange } from './TimeRange.js';
|
|
3
|
+
import type { EventKey, TemporalLike, TimestampInput } from './temporal.js';
|
|
4
|
+
/** A point-in-time event key represented as a single millisecond timestamp. Example: `new Time(Date.now())`. */
|
|
5
|
+
export declare class Time implements EventKey {
|
|
6
|
+
readonly kind = "time";
|
|
7
|
+
readonly timestamp: number;
|
|
8
|
+
/** Example: `Time.parse("2025-01-01T09:00", { timeZone: "Europe/Madrid" })`. Parses a strict ISO-like timestamp string into an absolute `Time`. */
|
|
9
|
+
static parse(value: string, options?: TimeZoneOptions): Time;
|
|
10
|
+
/** Example: `new Time(new Date())`. Creates a point-in-time key from a `Date` or millisecond timestamp. */
|
|
11
|
+
constructor(value: TimestampInput);
|
|
12
|
+
/** Example: `time.type() // "time"`. Returns the key kind. */
|
|
13
|
+
type(): 'time';
|
|
14
|
+
/** Example: `time.begin()`. Returns the inclusive start of the key in milliseconds since epoch. */
|
|
15
|
+
begin(): number;
|
|
16
|
+
/** Example: `time.end()`. Returns the inclusive end of the key in milliseconds since epoch. */
|
|
17
|
+
end(): number;
|
|
18
|
+
/** Example: `time.timestampMs()`. Returns the key timestamp in milliseconds since epoch. */
|
|
19
|
+
timestampMs(): number;
|
|
20
|
+
/** Example: `Number(time)`. Returns the primitive millisecond timestamp for numeric coercion. */
|
|
21
|
+
valueOf(): number;
|
|
22
|
+
/** Example: `time.timeRange()`. Returns this point as a zero-width `TimeRange`. */
|
|
23
|
+
timeRange(): TimeRange;
|
|
24
|
+
/** Example: `time.duration() // 0`. Returns the temporal duration of the key in milliseconds. */
|
|
25
|
+
duration(): number;
|
|
26
|
+
/** Example: `time.overlaps(range)`. Returns `true` when this point overlaps the supplied temporal value. */
|
|
27
|
+
overlaps(other: TemporalLike): boolean;
|
|
28
|
+
/** Example: `time.contains(Date.now())`. Returns `true` when this point fully contains the supplied temporal value. */
|
|
29
|
+
contains(other: TemporalLike): boolean;
|
|
30
|
+
/** Example: `time.isBefore(otherTime)`. Returns `true` when this point ends strictly before the supplied temporal value begins. */
|
|
31
|
+
isBefore(other: TemporalLike): boolean;
|
|
32
|
+
/** Example: `time.isAfter(otherTime)`. Returns `true` when this point begins strictly after the supplied temporal value ends. */
|
|
33
|
+
isAfter(other: TemporalLike): boolean;
|
|
34
|
+
/** Example: `time.intersection(range)`. Returns the temporal intersection with the supplied value, if any. */
|
|
35
|
+
intersection(other: TemporalLike): TimeRange | undefined;
|
|
36
|
+
/** Example: `time.trim(range)`. Returns this key when it falls within the supplied temporal value, otherwise `undefined`. */
|
|
37
|
+
trim(other: TemporalLike): Time | undefined;
|
|
38
|
+
/** Example: `time.equals(otherTime)`. Returns `true` when the supplied key is the same `Time`. */
|
|
39
|
+
equals(other: EventKey): boolean;
|
|
40
|
+
/** Example: `time.compare(otherTime)`. Compares this key to another key for ordering. */
|
|
41
|
+
compare(other: EventKey): number;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=Time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Time.d.ts","sourceRoot":"","sources":["../src/Time.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG5E,gHAAgH;AAChH,qBAAa,IAAK,YAAW,QAAQ;IACnC,QAAQ,CAAC,IAAI,UAAU;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,mJAAmJ;IACnJ,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,IAAI;IAIhE,2GAA2G;gBAC/F,KAAK,EAAE,cAAc;IAKjC,8DAA8D;IAC9D,IAAI,IAAI,MAAM;IAId,mGAAmG;IACnG,KAAK,IAAI,MAAM;IAIf,+FAA+F;IAC/F,GAAG,IAAI,MAAM;IAIb,4FAA4F;IAC5F,WAAW,IAAI,MAAM;IAIrB,iGAAiG;IACjG,OAAO,IAAI,MAAM;IAIjB,mFAAmF;IACnF,SAAS,IAAI,SAAS;IAItB,iGAAiG;IACjG,QAAQ,IAAI,MAAM;IAIlB,4GAA4G;IAC5G,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAItC,uHAAuH;IACvH,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAItC,mIAAmI;IACnI,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAItC,iIAAiI;IACjI,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAIrC,8GAA8G;IAC9G,YAAY,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,GAAG,SAAS;IAIxD,6HAA6H;IAC7H,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,GAAG,SAAS;IAI3C,kGAAkG;IAClG,MAAM,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO;IAIhC,yFAAyF;IACzF,OAAO,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM;CAGjC"}
|
package/dist/Time.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { parseTimestampString } from './calendar.js';
|
|
2
|
+
import { TimeRange } from './TimeRange.js';
|
|
3
|
+
import { compareEventKeys, normalizeTimestamp } from './temporal.js';
|
|
4
|
+
/** A point-in-time event key represented as a single millisecond timestamp. Example: `new Time(Date.now())`. */
|
|
5
|
+
export class Time {
|
|
6
|
+
kind = 'time';
|
|
7
|
+
timestamp;
|
|
8
|
+
/** Example: `Time.parse("2025-01-01T09:00", { timeZone: "Europe/Madrid" })`. Parses a strict ISO-like timestamp string into an absolute `Time`. */
|
|
9
|
+
static parse(value, options = {}) {
|
|
10
|
+
return new Time(parseTimestampString(value, options));
|
|
11
|
+
}
|
|
12
|
+
/** Example: `new Time(new Date())`. Creates a point-in-time key from a `Date` or millisecond timestamp. */
|
|
13
|
+
constructor(value) {
|
|
14
|
+
this.timestamp = normalizeTimestamp(value, 'time');
|
|
15
|
+
Object.freeze(this);
|
|
16
|
+
}
|
|
17
|
+
/** Example: `time.type() // "time"`. Returns the key kind. */
|
|
18
|
+
type() {
|
|
19
|
+
return this.kind;
|
|
20
|
+
}
|
|
21
|
+
/** Example: `time.begin()`. Returns the inclusive start of the key in milliseconds since epoch. */
|
|
22
|
+
begin() {
|
|
23
|
+
return this.timestamp;
|
|
24
|
+
}
|
|
25
|
+
/** Example: `time.end()`. Returns the inclusive end of the key in milliseconds since epoch. */
|
|
26
|
+
end() {
|
|
27
|
+
return this.timestamp;
|
|
28
|
+
}
|
|
29
|
+
/** Example: `time.timestampMs()`. Returns the key timestamp in milliseconds since epoch. */
|
|
30
|
+
timestampMs() {
|
|
31
|
+
return this.timestamp;
|
|
32
|
+
}
|
|
33
|
+
/** Example: `Number(time)`. Returns the primitive millisecond timestamp for numeric coercion. */
|
|
34
|
+
valueOf() {
|
|
35
|
+
return this.timestamp;
|
|
36
|
+
}
|
|
37
|
+
/** Example: `time.timeRange()`. Returns this point as a zero-width `TimeRange`. */
|
|
38
|
+
timeRange() {
|
|
39
|
+
return new TimeRange({ start: this.timestamp, end: this.timestamp });
|
|
40
|
+
}
|
|
41
|
+
/** Example: `time.duration() // 0`. Returns the temporal duration of the key in milliseconds. */
|
|
42
|
+
duration() {
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
/** Example: `time.overlaps(range)`. Returns `true` when this point overlaps the supplied temporal value. */
|
|
46
|
+
overlaps(other) {
|
|
47
|
+
return this.timeRange().overlaps(other);
|
|
48
|
+
}
|
|
49
|
+
/** Example: `time.contains(Date.now())`. Returns `true` when this point fully contains the supplied temporal value. */
|
|
50
|
+
contains(other) {
|
|
51
|
+
return this.timeRange().contains(other);
|
|
52
|
+
}
|
|
53
|
+
/** Example: `time.isBefore(otherTime)`. Returns `true` when this point ends strictly before the supplied temporal value begins. */
|
|
54
|
+
isBefore(other) {
|
|
55
|
+
return this.timeRange().isBefore(other);
|
|
56
|
+
}
|
|
57
|
+
/** Example: `time.isAfter(otherTime)`. Returns `true` when this point begins strictly after the supplied temporal value ends. */
|
|
58
|
+
isAfter(other) {
|
|
59
|
+
return this.timeRange().isAfter(other);
|
|
60
|
+
}
|
|
61
|
+
/** Example: `time.intersection(range)`. Returns the temporal intersection with the supplied value, if any. */
|
|
62
|
+
intersection(other) {
|
|
63
|
+
return this.timeRange().intersection(other);
|
|
64
|
+
}
|
|
65
|
+
/** Example: `time.trim(range)`. Returns this key when it falls within the supplied temporal value, otherwise `undefined`. */
|
|
66
|
+
trim(other) {
|
|
67
|
+
return this.overlaps(other) ? this : undefined;
|
|
68
|
+
}
|
|
69
|
+
/** Example: `time.equals(otherTime)`. Returns `true` when the supplied key is the same `Time`. */
|
|
70
|
+
equals(other) {
|
|
71
|
+
return other instanceof Time && this.timestamp === other.timestamp;
|
|
72
|
+
}
|
|
73
|
+
/** Example: `time.compare(otherTime)`. Compares this key to another key for ordering. */
|
|
74
|
+
compare(other) {
|
|
75
|
+
return compareEventKeys(this, other);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=Time.js.map
|
package/dist/Time.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Time.js","sourceRoot":"","sources":["../src/Time.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAwB,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE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QAC1B,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,mIAAmI;IACnI,QAAQ,CAAC,KAAmB;QAC1B,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,iIAAiI;IACjI,OAAO,CAAC,KAAmB;QACzB,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACzC,CAAC;IAED,8GAA8G;IAC9G,YAAY,CAAC,KAAmB;QAC9B,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IAED,6HAA6H;IAC7H,IAAI,CAAC,KAAmB;QACtB,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IACjD,CAAC;IAED,kGAAkG;IAClG,MAAM,CAAC,KAAe;QACpB,OAAO,KAAK,YAAY,IAAI,IAAI,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,CAAC;IACrE,CAAC;IAED,yFAAyF;IACzF,OAAO,CAAC,KAAe;QACrB,OAAO,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type CalendarOptions, type CalendarUnit, type TimeZoneOptions } from './calendar.js';
|
|
2
|
+
import type { TemporalLike, TimeRangeInput } from './temporal.js';
|
|
3
|
+
import type { EventKey } from './temporal.js';
|
|
4
|
+
export declare function toTimeRange(value: TemporalLike): TimeRange;
|
|
5
|
+
/** A time interval event key with inclusive start and end boundaries. Example: `new TimeRange({ start, end })`. */
|
|
6
|
+
export declare class TimeRange implements EventKey {
|
|
7
|
+
readonly kind = "timeRange";
|
|
8
|
+
readonly start: number;
|
|
9
|
+
readonly endMs: number;
|
|
10
|
+
/** Example: `TimeRange.fromDate("2025-01-01", { timeZone: "Europe/Madrid" })`. Creates the local calendar-day range for the supplied ISO date. */
|
|
11
|
+
static fromDate(reference: string, options?: TimeZoneOptions): TimeRange;
|
|
12
|
+
/** Example: `TimeRange.fromCalendar("week", "2025-01-01", { timeZone: "UTC", weekStartsOn: 1 })`. Creates the containing calendar range for the supplied reference. */
|
|
13
|
+
static fromCalendar(unit: CalendarUnit, reference: string, options?: CalendarOptions): TimeRange;
|
|
14
|
+
/** Example: `new TimeRange([start, end])`. Creates an interval key from a `{ start, end }` object or tuple. */
|
|
15
|
+
constructor(input: TimeRangeInput);
|
|
16
|
+
/** Example: `range.type() // "timeRange"`. Returns the key kind. */
|
|
17
|
+
type(): 'timeRange';
|
|
18
|
+
/** Example: `range.begin()`. Returns the inclusive start of the range in milliseconds since epoch. */
|
|
19
|
+
begin(): number;
|
|
20
|
+
/** Example: `range.end()`. Returns the inclusive end of the range in milliseconds since epoch. */
|
|
21
|
+
end(): number;
|
|
22
|
+
/** Example: `range.timeRange()`. Returns this key as a `TimeRange`. */
|
|
23
|
+
timeRange(): TimeRange;
|
|
24
|
+
/** Example: `range.duration()`. Returns the temporal duration of the range in milliseconds. */
|
|
25
|
+
duration(): number;
|
|
26
|
+
/** Example: `range.midpoint()`. Returns the midpoint of the range in milliseconds since epoch. */
|
|
27
|
+
midpoint(): number;
|
|
28
|
+
/** Example: `range.contains(otherRange)`. Returns `true` when this range fully contains the supplied temporal value. */
|
|
29
|
+
contains(other: TemporalLike): boolean;
|
|
30
|
+
/** Example: `range.overlaps(otherRange)`. Returns `true` when this range overlaps the supplied temporal value. */
|
|
31
|
+
overlaps(other: TemporalLike): boolean;
|
|
32
|
+
/** Example: `range.isBefore(otherRange)`. Returns `true` when this range ends strictly before the supplied temporal value begins. */
|
|
33
|
+
isBefore(other: TemporalLike): boolean;
|
|
34
|
+
/** Example: `range.isAfter(otherRange)`. Returns `true` when this range begins strictly after the supplied temporal value ends. */
|
|
35
|
+
isAfter(other: TemporalLike): boolean;
|
|
36
|
+
/** Example: `range.intersection(otherRange)`. Returns the overlapping portion of this range and the supplied temporal value, if any. */
|
|
37
|
+
intersection(other: TemporalLike): TimeRange | undefined;
|
|
38
|
+
/** Example: `range.trim(otherRange)`. Returns this range clipped to the supplied temporal value, if the two overlap. */
|
|
39
|
+
trim(other: TemporalLike): TimeRange | undefined;
|
|
40
|
+
/** Example: `range.equals(otherRange)`. Returns `true` when the supplied key is the same `TimeRange`. */
|
|
41
|
+
equals(other: EventKey): boolean;
|
|
42
|
+
/** Example: `range.compare(otherRange)`. Compares this key to another key for ordering. */
|
|
43
|
+
compare(other: EventKey): number;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=TimeRange.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TimeRange.d.ts","sourceRoot":"","sources":["../src/TimeRange.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAEV,YAAY,EACZ,cAAc,EAEf,MAAM,eAAe,CAAC;AAEvB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAmB9C,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,CA8B1D;AAED,mHAAmH;AACnH,qBAAa,SAAU,YAAW,QAAQ;IACxC,QAAQ,CAAC,IAAI,eAAe;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,kJAAkJ;IAClJ,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,GAAG,SAAS;IAI5E,uKAAuK;IACvK,MAAM,CAAC,YAAY,CACjB,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,eAAoB,GAC5B,SAAS;IAIZ,+GAA+G;gBACnG,KAAK,EAAE,cAAc;IAqBjC,oEAAoE;IACpE,IAAI,IAAI,WAAW;IAInB,sGAAsG;IACtG,KAAK,IAAI,MAAM;IAIf,kGAAkG;IAClG,GAAG,IAAI,MAAM;IAIb,uEAAuE;IACvE,SAAS,IAAI,SAAS;IAItB,+FAA+F;IAC/F,QAAQ,IAAI,MAAM;IAIlB,kGAAkG;IAClG,QAAQ,IAAI,MAAM;IAIlB,wHAAwH;IACxH,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAKtC,kHAAkH;IAClH,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAKtC,qIAAqI;IACrI,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAKtC,mIAAmI;IACnI,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAKrC,wIAAwI;IACxI,YAAY,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,GAAG,SAAS;IAWxD,wHAAwH;IACxH,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,SAAS,GAAG,SAAS;IAIhD,yGAAyG;IACzG,MAAM,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO;IAQhC,2FAA2F;IAC3F,OAAO,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM;CAGjC"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { calendarRangeForReference, dayRangeForDate, } from './calendar.js';
|
|
2
|
+
import { compareEventKeys, normalizeTimestamp } from './temporal.js';
|
|
3
|
+
function isBoundedTemporal(value) {
|
|
4
|
+
return (typeof value === 'object' &&
|
|
5
|
+
value !== null &&
|
|
6
|
+
'begin' in value &&
|
|
7
|
+
'end' in value);
|
|
8
|
+
}
|
|
9
|
+
function isRangedTemporal(value) {
|
|
10
|
+
return typeof value === 'object' && value !== null && 'timeRange' in value;
|
|
11
|
+
}
|
|
12
|
+
export function toTimeRange(value) {
|
|
13
|
+
if (value instanceof TimeRange) {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
if (isRangedTemporal(value)) {
|
|
17
|
+
return value.timeRange();
|
|
18
|
+
}
|
|
19
|
+
if (isBoundedTemporal(value)) {
|
|
20
|
+
return new TimeRange({ start: value.begin(), end: value.end() });
|
|
21
|
+
}
|
|
22
|
+
if (value instanceof Date || typeof value === 'number') {
|
|
23
|
+
const timestamp = normalizeTimestamp(value, 'time');
|
|
24
|
+
return new TimeRange({ start: timestamp, end: timestamp });
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
if (value.length === 2) {
|
|
28
|
+
return new TimeRange(value);
|
|
29
|
+
}
|
|
30
|
+
const interval = value;
|
|
31
|
+
return new TimeRange({ start: interval[1], end: interval[2] });
|
|
32
|
+
}
|
|
33
|
+
if ('value' in value) {
|
|
34
|
+
const interval = value;
|
|
35
|
+
return new TimeRange({ start: interval.start, end: interval.end });
|
|
36
|
+
}
|
|
37
|
+
return new TimeRange(value);
|
|
38
|
+
}
|
|
39
|
+
/** A time interval event key with inclusive start and end boundaries. Example: `new TimeRange({ start, end })`. */
|
|
40
|
+
export class TimeRange {
|
|
41
|
+
kind = 'timeRange';
|
|
42
|
+
start;
|
|
43
|
+
endMs;
|
|
44
|
+
/** Example: `TimeRange.fromDate("2025-01-01", { timeZone: "Europe/Madrid" })`. Creates the local calendar-day range for the supplied ISO date. */
|
|
45
|
+
static fromDate(reference, options = {}) {
|
|
46
|
+
return new TimeRange(dayRangeForDate(reference, options));
|
|
47
|
+
}
|
|
48
|
+
/** Example: `TimeRange.fromCalendar("week", "2025-01-01", { timeZone: "UTC", weekStartsOn: 1 })`. Creates the containing calendar range for the supplied reference. */
|
|
49
|
+
static fromCalendar(unit, reference, options = {}) {
|
|
50
|
+
return new TimeRange(calendarRangeForReference(unit, reference, options));
|
|
51
|
+
}
|
|
52
|
+
/** Example: `new TimeRange([start, end])`. Creates an interval key from a `{ start, end }` object or tuple. */
|
|
53
|
+
constructor(input) {
|
|
54
|
+
let rawStart;
|
|
55
|
+
let rawEnd;
|
|
56
|
+
if (Array.isArray(input)) {
|
|
57
|
+
rawStart = input[0];
|
|
58
|
+
rawEnd = input[1];
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const objectInput = input;
|
|
62
|
+
rawStart = objectInput.start;
|
|
63
|
+
rawEnd = objectInput.end;
|
|
64
|
+
}
|
|
65
|
+
const start = normalizeTimestamp(rawStart, 'timeRange start');
|
|
66
|
+
const end = normalizeTimestamp(rawEnd, 'timeRange end');
|
|
67
|
+
if (start > end) {
|
|
68
|
+
throw new TypeError('timeRange start must be <= end');
|
|
69
|
+
}
|
|
70
|
+
this.start = start;
|
|
71
|
+
this.endMs = end;
|
|
72
|
+
Object.freeze(this);
|
|
73
|
+
}
|
|
74
|
+
/** Example: `range.type() // "timeRange"`. Returns the key kind. */
|
|
75
|
+
type() {
|
|
76
|
+
return this.kind;
|
|
77
|
+
}
|
|
78
|
+
/** Example: `range.begin()`. Returns the inclusive start of the range in milliseconds since epoch. */
|
|
79
|
+
begin() {
|
|
80
|
+
return this.start;
|
|
81
|
+
}
|
|
82
|
+
/** Example: `range.end()`. Returns the inclusive end of the range in milliseconds since epoch. */
|
|
83
|
+
end() {
|
|
84
|
+
return this.endMs;
|
|
85
|
+
}
|
|
86
|
+
/** Example: `range.timeRange()`. Returns this key as a `TimeRange`. */
|
|
87
|
+
timeRange() {
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
/** Example: `range.duration()`. Returns the temporal duration of the range in milliseconds. */
|
|
91
|
+
duration() {
|
|
92
|
+
return this.endMs - this.start;
|
|
93
|
+
}
|
|
94
|
+
/** Example: `range.midpoint()`. Returns the midpoint of the range in milliseconds since epoch. */
|
|
95
|
+
midpoint() {
|
|
96
|
+
return this.start + this.duration() / 2;
|
|
97
|
+
}
|
|
98
|
+
/** Example: `range.contains(otherRange)`. Returns `true` when this range fully contains the supplied temporal value. */
|
|
99
|
+
contains(other) {
|
|
100
|
+
const range = toTimeRange(other);
|
|
101
|
+
return range.begin() >= this.begin() && range.end() <= this.end();
|
|
102
|
+
}
|
|
103
|
+
/** Example: `range.overlaps(otherRange)`. Returns `true` when this range overlaps the supplied temporal value. */
|
|
104
|
+
overlaps(other) {
|
|
105
|
+
const range = toTimeRange(other);
|
|
106
|
+
return this.begin() <= range.end() && range.begin() <= this.end();
|
|
107
|
+
}
|
|
108
|
+
/** Example: `range.isBefore(otherRange)`. Returns `true` when this range ends strictly before the supplied temporal value begins. */
|
|
109
|
+
isBefore(other) {
|
|
110
|
+
const range = toTimeRange(other);
|
|
111
|
+
return this.end() < range.begin();
|
|
112
|
+
}
|
|
113
|
+
/** Example: `range.isAfter(otherRange)`. Returns `true` when this range begins strictly after the supplied temporal value ends. */
|
|
114
|
+
isAfter(other) {
|
|
115
|
+
const range = toTimeRange(other);
|
|
116
|
+
return this.begin() > range.end();
|
|
117
|
+
}
|
|
118
|
+
/** Example: `range.intersection(otherRange)`. Returns the overlapping portion of this range and the supplied temporal value, if any. */
|
|
119
|
+
intersection(other) {
|
|
120
|
+
const range = toTimeRange(other);
|
|
121
|
+
if (!this.overlaps(range)) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
return new TimeRange({
|
|
125
|
+
start: Math.max(this.begin(), range.begin()),
|
|
126
|
+
end: Math.min(this.end(), range.end()),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/** Example: `range.trim(otherRange)`. Returns this range clipped to the supplied temporal value, if the two overlap. */
|
|
130
|
+
trim(other) {
|
|
131
|
+
return this.intersection(other);
|
|
132
|
+
}
|
|
133
|
+
/** Example: `range.equals(otherRange)`. Returns `true` when the supplied key is the same `TimeRange`. */
|
|
134
|
+
equals(other) {
|
|
135
|
+
return (other instanceof TimeRange &&
|
|
136
|
+
this.start === other.start &&
|
|
137
|
+
this.endMs === other.endMs);
|
|
138
|
+
}
|
|
139
|
+
/** Example: `range.compare(otherRange)`. Compares this key to another key for ordering. */
|
|
140
|
+
compare(other) {
|
|
141
|
+
return compareEventKeys(this, other);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=TimeRange.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TimeRange.js","sourceRoot":"","sources":["../src/TimeRange.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,yBAAyB,EACzB,eAAe,GAIhB,MAAM,eAAe,CAAC;AAOvB,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAKrE,SAAS,iBAAiB,CACxB,KAAc;IAEd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAO,IAAI,KAAK;QAChB,KAAK,IAAI,KAAK,CACf,CAAC;AACJ,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAc;IACtC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,WAAW,IAAI,KAAK,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAmB;IAC7C,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;QAC/B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC,SAAS,EAAE,CAAC;IAC3B,CAAC;IACD,IAAI,iBAAiB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,IAAI,SAAS,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;IACD,IAAI,KAAK,YAAY,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACvD,MAAM,SAAS,GAAG,kBAAkB,CAAC,KAAK,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