@bodil/core 0.1.3 → 0.2.1

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/dist/assert.d.ts CHANGED
@@ -40,7 +40,7 @@ export declare function assertNever<T extends never>(_value: T): T;
40
40
  * Returns the given value unchanged if it's neither null nor undefined.
41
41
  * Throws an exception otherwise.
42
42
  */
43
- export declare function present<A>(value: A): NonNullable<A>;
43
+ export declare function present<A>(value: A, message?: string): NonNullable<A>;
44
44
  /**
45
45
  * Tests whether a value is null, undefined, false, an empty array or an empty
46
46
  * string.
package/dist/assert.js CHANGED
@@ -55,8 +55,8 @@ export function assertNever(_value) {
55
55
  * Returns the given value unchanged if it's neither null nor undefined.
56
56
  * Throws an exception otherwise.
57
57
  */
58
- export function present(value) {
59
- assertAlways(value !== null && value !== undefined);
58
+ export function present(value, message) {
59
+ assertAlways(value !== null && value !== undefined, message);
60
60
  return value;
61
61
  }
62
62
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,cAAe,SAAQ,KAAK;CAAG;AAE5C;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAClB,SAAkB,EAClB,UAAkB,0BAA0B;IAE5C,IACK,MAAM,CAAC,IAAoC,CAAC,GAAG,KAAK,SAAS;QAC7D,MAAM,CAAC,IAA6C,CAAC,GAAG,CAAC,GAAG,EAC/D,CAAC;QACC,IAAI,SAAS,KAAK,KAAK,EAAE,CAAC;YACtB,MAAM,IAAI,cAAc,CAAC,OAAO,CAAC,CAAC;QACtC,CAAC;IACL,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,SAAkB,EAAE,OAAgB;IAC7D,IAAI,SAAS,KAAK,KAAK,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,OAAO,IAAI,0BAA0B,CAAC,CAAC;IAC3D,CAAC;AACL,CAAC;A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C;QACjD,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAC/C,CAAC;AACN,CAAC;AAED;;GAEG;AACH,mEAAmE;AACnE,MAAM,UAAU,SAAS,CAAC,KAAc;IACpC,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AACjD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,KAAc;IACrC,OAAO,CACH,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAQ,KAA2B,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,UAAU,CACtE,CAAC;AACN,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC1C,OAAO,CACH,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAQ,KAAgC,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,UAAU,CAChF,CAAC;AACN,CAAC"}
package/dist/async.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Return a promise that will resolve after the specified number of
3
- * milliseconds.
3
+ * milliseconds or {@link Temporal.Duration}.
4
4
  */
5
- export declare function sleep(ms: number): Promise<void>;
5
+ export declare function sleep(duration: number | Temporal.Duration): Promise<void>;
6
6
  export interface AbortableJob<A> extends Disposable {
7
7
  result: Promise<A>;
8
8
  abort: (message?: string) => void;
package/dist/async.js CHANGED
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * Return a promise that will resolve after the specified number of
3
- * milliseconds.
3
+ * milliseconds or {@link Temporal.Duration}.
4
4
  */
5
- export function sleep(ms) {
5
+ export function sleep(duration) {
6
6
  return new Promise((resolve) => {
7
- setTimeout(resolve, ms);
7
+ setTimeout(resolve, typeof duration === "number" ? duration : duration.total("milliseconds"));
8
8
  });
9
9
  }
10
10
  /**
package/dist/async.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"async.js","sourceRoot":"","sources":["../src/async.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAC,EAAU;IAC5B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAC5B,CAAC,CAAC,CAAC;AACP,CAAC;AAQD;;GAEG;AACH,MAAM,UAAU,SAAS,CACrB,EAAsC,EACtC,kBAAmC,IAAI,eAAe,EAAE;IAExD,OAAO;QACH,MAAM,EAAE,eAAe,CAAC,MAAM;QAC9B,MAAM,EAAE,EAAE,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,IAAI,CACnC,CAAC,MAAM,EAAE,EAAE;YACP,eAAe,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;YACxC,OAAO,MAAM,CAAC;QAClB,CAAC,EACD,CAAC,KAAK,EAAE,EAAE;YACN,MAAM,OAAO,GAAY,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC;YACxD,OAAO,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;QAC3D,CAAC,CACJ;QACD,KAAK,EAAE,CAAC,OAAO,GAAG,qBAAqB,EAAQ,EAAE,CAC7C,eAAe,CAAC,KAAK,CAAC,IAAI,YAAY,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QAClE,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CACnB,eAAe,CAAC,KAAK,CAAC,IAAI,YAAY,CAAC,sBAAsB,EAAE,YAAY,CAAC,CAAC;KACpF,CAAC;AACN,CAAC"}
1
+ {"version":3,"file":"async.js","sourceRoot":"","sources":["../src/async.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAC,QAAoC;IACtD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,UAAU,CACN,OAAO,EACP,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAC3E,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC;AAQD;;GAEG;AACH,MAAM,UAAU,SAAS,CACrB,EAAsC,EACtC,kBAAmC,IAAI,eAAe,EAAE;IAExD,OAAO;QACH,MAAM,EAAE,eAAe,CAAC,MAAM;QAC9B,MAAM,EAAE,EAAE,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,IAAI,CACnC,CAAC,MAAM,EAAE,EAAE;YACP,eAAe,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;YACxC,OAAO,MAAM,CAAC;QAClB,CAAC,EACD,CAAC,KAAK,EAAE,EAAE;YACN,MAAM,OAAO,GAAY,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC;YACxD,OAAO,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;QAC3D,CAAC,CACJ;QACD,KAAK,EAAE,CAAC,OAAO,GAAG,qBAAqB,EAAQ,EAAE,CAC7C,eAAe,CAAC,KAAK,CAAC,IAAI,YAAY,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QAClE,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CACnB,eAAe,CAAC,KAAK,CAAC,IAAI,YAAY,CAAC,sBAAsB,EAAE,YAAY,CAAC,CAAC;KACpF,CAAC;AACN,CAAC"}
package/dist/fun.d.ts CHANGED
@@ -2,3 +2,7 @@
2
2
  * Create a function that always returns `value`.
3
3
  */
4
4
  export declare function always<A>(value: A): () => A;
5
+ /**
6
+ * The identity function: it just returns its argument unchanged.
7
+ */
8
+ export declare function id<A>(value: A): A;
package/dist/fun.js CHANGED
@@ -4,4 +4,10 @@
4
4
  export function always(value) {
5
5
  return () => value;
6
6
  }
7
+ /**
8
+ * The identity function: it just returns its argument unchanged.
9
+ */
10
+ export function id(value) {
11
+ return value;
12
+ }
7
13
  //# sourceMappingURL=fun.js.map
package/dist/fun.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"fun.js","sourceRoot":"","sources":["../src/fun.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,MAAM,CAAI,KAAQ;IAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACvB,CAAC"}
1
+ {"version":3,"file":"fun.js","sourceRoot":"","sources":["../src/fun.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,UAAU,MAAM,CAAI,KAAQ;IAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC1B,OAAO,KAAK,CAAC;AACjB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import * as Assert from "./assert";
2
- import * as Async from "./async";
3
- import * as Date from "./date";
4
- import * as Event from "./event";
5
- import * as Fun from "./fun";
6
- import * as Order from "./order";
7
- import * as Types from "./types";
8
- export { Assert, Async, Date, Event, Fun, Order, Types };
1
+ import * as assert from "./assert";
2
+ import * as async from "./async";
3
+ import * as event from "./event";
4
+ import * as fun from "./fun";
5
+ import * as order from "./order";
6
+ import * as time from "./time";
7
+ import * as types from "./types";
8
+ export { assert, async, event, fun, order, time, types };
9
9
  import { None, Some, Option, Ok, Err, Result } from "@bodil/opt";
10
10
  export { None, Some, Option, Ok, Err, Result };
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
- import * as Assert from "./assert";
2
- import * as Async from "./async";
3
- import * as Date from "./date";
4
- import * as Event from "./event";
5
- import * as Fun from "./fun";
6
- import * as Order from "./order";
7
- import * as Types from "./types";
8
- export { Assert, Async, Date, Event, Fun, Order, Types };
1
+ import * as assert from "./assert";
2
+ import * as async from "./async";
3
+ import * as event from "./event";
4
+ import * as fun from "./fun";
5
+ import * as order from "./order";
6
+ import * as time from "./time";
7
+ import * as types from "./types";
8
+ export { assert, async, event, fun, order, time, types };
9
9
  import { None, Some, Option, Ok, Err, Result } from "@bodil/opt";
10
10
  export { None, Some, Option, Ok, Err, Result };
11
11
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAEzD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAEzD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC"}
package/dist/time.d.ts ADDED
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Test if the given {@link Temporal.Instant} comes before a certain moment
3
+ * in time.
4
+ *
5
+ * If `other` is unspecified, compare with the current time.
6
+ *
7
+ * If `other` is a {@link Temporal.Duration}, compare with that duration
8
+ * subtracted from the current time.
9
+ *
10
+ * If `other` is a {@link Temporal.Instant}, compare with that instant.
11
+ *
12
+ * @example
13
+ * time.isBefore(instant) // is `instant` in the past?
14
+ * time.isBefore(instant, otherInstant) // is `instant` before `otherInstant`?
15
+ * time.isBefore(instant, duration) // is `instant` more than `duration` ago?
16
+ */
17
+ export declare function isBefore(instant: Temporal.Instant, other?: Temporal.Instant | Temporal.Duration): boolean;
18
+ /**
19
+ * Test if the given {@link Temporal.Instant} comes after a certain moment in
20
+ * time.
21
+ *
22
+ * If `other` is unspecified, compare with the current time.
23
+ *
24
+ * If `other` is a {@link Temporal.Duration}, compare with that duration added
25
+ * to the current time.
26
+ *
27
+ * If `other` is a {@link Temporal.Instant}, compare with that instant.
28
+ *
29
+ * @example
30
+ * time.isAfter(instant) // is `instant` in the future?
31
+ * time.isAfter(instant, otherInstant) // is `instant` after `otherInstant`?
32
+ * time.isAfter(instant, duration) // is `instant` more than `duration` into the future?
33
+ */
34
+ export declare function isAfter(instant: Temporal.Instant, other?: Temporal.Instant | Temporal.Duration): boolean;
35
+ /**
36
+ * Find the shortest duration or earliest time given a series of
37
+ * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
38
+ */
39
+ export declare function min<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T;
40
+ /**
41
+ * Find the longest duration or latest time given a series of
42
+ * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
43
+ */
44
+ export declare function max<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T;
45
+ /**
46
+ * The current time as a {@link Temporal.Instant}.
47
+ *
48
+ * A shorthand for `Temporal.Now.instant()`.
49
+ */
50
+ export declare function now(): Temporal.Instant;
51
+ /**
52
+ * The current time as a {@link Temporal.ZonedDateTime}.
53
+ *
54
+ * A shorthand for `Temporal.Now.zonedDateTimeISO()`.
55
+ */
56
+ export declare function nowZ(): Temporal.ZonedDateTime;
57
+ /**
58
+ * Construct a duration of the given number of days.
59
+ */
60
+ export declare function days(days: number): Temporal.Duration;
61
+ /**
62
+ * Construct a duration of the given number of hours.
63
+ */
64
+ export declare function hours(hours: number): Temporal.Duration;
65
+ /**
66
+ * Construct a duration of the given number of minutes.
67
+ */
68
+ export declare function minutes(minutes: number): Temporal.Duration;
69
+ /**
70
+ * Construct a duration of the given number of seconds.
71
+ */
72
+ export declare function seconds(seconds: number): Temporal.Duration;
73
+ /**
74
+ * Construct a duration of the given number of milliseconds.
75
+ */
76
+ export declare function millis(milliseconds: number): Temporal.Duration;
package/dist/time.js ADDED
@@ -0,0 +1,123 @@
1
+ import { present } from "./assert";
2
+ import { Cmp } from "./order";
3
+ /**
4
+ * Test if the given {@link Temporal.Instant} comes before a certain moment
5
+ * in time.
6
+ *
7
+ * If `other` is unspecified, compare with the current time.
8
+ *
9
+ * If `other` is a {@link Temporal.Duration}, compare with that duration
10
+ * subtracted from the current time.
11
+ *
12
+ * If `other` is a {@link Temporal.Instant}, compare with that instant.
13
+ *
14
+ * @example
15
+ * time.isBefore(instant) // is `instant` in the past?
16
+ * time.isBefore(instant, otherInstant) // is `instant` before `otherInstant`?
17
+ * time.isBefore(instant, duration) // is `instant` more than `duration` ago?
18
+ */
19
+ export function isBefore(instant, other) {
20
+ return (Temporal.Instant.compare(instant, other instanceof Temporal.Duration
21
+ ? Temporal.Now.instant().subtract(other)
22
+ : other ?? Temporal.Now.instant()) === Cmp.LT);
23
+ }
24
+ /**
25
+ * Test if the given {@link Temporal.Instant} comes after a certain moment in
26
+ * time.
27
+ *
28
+ * If `other` is unspecified, compare with the current time.
29
+ *
30
+ * If `other` is a {@link Temporal.Duration}, compare with that duration added
31
+ * to the current time.
32
+ *
33
+ * If `other` is a {@link Temporal.Instant}, compare with that instant.
34
+ *
35
+ * @example
36
+ * time.isAfter(instant) // is `instant` in the future?
37
+ * time.isAfter(instant, otherInstant) // is `instant` after `otherInstant`?
38
+ * time.isAfter(instant, duration) // is `instant` more than `duration` into the future?
39
+ */
40
+ export function isAfter(instant, other) {
41
+ return (Temporal.Instant.compare(instant, other instanceof Temporal.Duration
42
+ ? Temporal.Now.instant().add(other)
43
+ : other ?? Temporal.Now.instant()) === Cmp.GT);
44
+ }
45
+ /**
46
+ * Find the shortest duration or earliest time given a series of
47
+ * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
48
+ */
49
+ export function min(...rest) {
50
+ let first = present(rest.shift(), "time.min() called with no arguments");
51
+ const compare = first instanceof Temporal.Duration
52
+ ? Temporal.Duration.compare
53
+ : Temporal.Instant.compare;
54
+ for (const next of rest) {
55
+ if (compare(next, first) === Cmp.LT) {
56
+ first = next;
57
+ }
58
+ }
59
+ return first;
60
+ }
61
+ /**
62
+ * Find the longest duration or latest time given a series of
63
+ * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
64
+ */
65
+ export function max(...rest) {
66
+ let first = present(rest.shift(), "time.max() called with no arguments");
67
+ const compare = first instanceof Temporal.Duration
68
+ ? Temporal.Duration.compare
69
+ : Temporal.Instant.compare;
70
+ for (const next of rest) {
71
+ if (compare(next, first) === Cmp.GT) {
72
+ first = next;
73
+ }
74
+ }
75
+ return first;
76
+ }
77
+ /**
78
+ * The current time as a {@link Temporal.Instant}.
79
+ *
80
+ * A shorthand for `Temporal.Now.instant()`.
81
+ */
82
+ export function now() {
83
+ return Temporal.Now.instant();
84
+ }
85
+ /**
86
+ * The current time as a {@link Temporal.ZonedDateTime}.
87
+ *
88
+ * A shorthand for `Temporal.Now.zonedDateTimeISO()`.
89
+ */
90
+ export function nowZ() {
91
+ return Temporal.Now.zonedDateTimeISO();
92
+ }
93
+ /**
94
+ * Construct a duration of the given number of days.
95
+ */
96
+ export function days(days) {
97
+ return Temporal.Duration.from({ hours: days * 24 });
98
+ }
99
+ /**
100
+ * Construct a duration of the given number of hours.
101
+ */
102
+ export function hours(hours) {
103
+ return Temporal.Duration.from({ hours });
104
+ }
105
+ /**
106
+ * Construct a duration of the given number of minutes.
107
+ */
108
+ export function minutes(minutes) {
109
+ return Temporal.Duration.from({ minutes });
110
+ }
111
+ /**
112
+ * Construct a duration of the given number of seconds.
113
+ */
114
+ export function seconds(seconds) {
115
+ return Temporal.Duration.from({ seconds });
116
+ }
117
+ /**
118
+ * Construct a duration of the given number of milliseconds.
119
+ */
120
+ export function millis(milliseconds) {
121
+ return Temporal.Duration.from({ milliseconds });
122
+ }
123
+ //# sourceMappingURL=time.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time.js","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,GAAG,EAAgB,MAAM,SAAS,CAAC;AAE5C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,QAAQ,CACpB,OAAyB,EACzB,KAA4C;IAE5C,OAAO,CACH,QAAQ,CAAC,OAAO,CAAC,OAAO,CACpB,OAAO,EACP,KAAK,YAAY,QAAQ,CAAC,QAAQ;QAC9B,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC;QACxC,CAAC,CAAC,KAAK,IAAI,QAAQ,CAAC,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0,0 +1 @@
1
+ import "temporal-polyfill/global";
@@ -0,0 +1,38 @@
1
+ import "temporal-polyfill/global";
2
+ import { expect, test } from "vitest";
3
+ import { time } from ".";
4
+ test("time.min", () => {
5
+ const a = time.seconds(1);
6
+ const b = time.seconds(5);
7
+ const c = time.seconds(10);
8
+ expect(time.min(a, b, c)).toEqual(a);
9
+ });
10
+ test("time.max", () => {
11
+ const a = time.seconds(1);
12
+ const b = time.seconds(5);
13
+ const c = time.seconds(10);
14
+ expect(time.max(a, b, c)).toEqual(c);
15
+ });
16
+ test("time.isBefore", () => {
17
+ const a = time.now().subtract(time.seconds(10));
18
+ const b = a.subtract(time.seconds(10));
19
+ const c = time.now().add(time.minutes(10));
20
+ expect(time.isBefore(c)).toBeFalsy();
21
+ expect(time.isBefore(a)).toBeTruthy();
22
+ expect(time.isBefore(a, b)).toBeFalsy();
23
+ expect(time.isBefore(b, a)).toBeTruthy();
24
+ expect(time.isBefore(a, time.seconds(5))).toBeTruthy();
25
+ expect(time.isBefore(a, time.seconds(60))).toBeFalsy();
26
+ });
27
+ test("time.isAfter", () => {
28
+ const a = time.now().add(time.seconds(10));
29
+ const b = a.add(time.seconds(10));
30
+ const c = time.now().subtract(time.minutes(10));
31
+ expect(time.isAfter(c)).toBeFalsy();
32
+ expect(time.isAfter(a)).toBeTruthy();
33
+ expect(time.isAfter(a, b)).toBeFalsy();
34
+ expect(time.isAfter(b, a)).toBeTruthy();
35
+ expect(time.isAfter(a, time.seconds(5))).toBeTruthy();
36
+ expect(time.isAfter(a, time.seconds(60))).toBeFalsy();
37
+ });
38
+ //# sourceMappingURL=time.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time.test.js","sourceRoot":"","sources":["../src/time.test.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,CAAC;AAElC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC;AAEzB,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,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IACxC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IACtD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AAC1D,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bodil/core",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "description": "My TypeScript standard library",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,12 +32,6 @@
32
32
  "import": "./dist/async.js"
33
33
  }
34
34
  },
35
- "./date": {
36
- "import": {
37
- "types": "./dist/date.d.ts",
38
- "import": "./dist/date.js"
39
- }
40
- },
41
35
  "./event": {
42
36
  "import": {
43
37
  "types": "./dist/event.d.ts",
@@ -56,6 +50,12 @@
56
50
  "import": "./dist/order.js"
57
51
  }
58
52
  },
53
+ "./time": {
54
+ "import": {
55
+ "types": "./dist/time.d.ts",
56
+ "import": "./dist/time.js"
57
+ }
58
+ },
59
59
  "./types": {
60
60
  "import": {
61
61
  "types": "./dist/types.d.ts",
@@ -67,7 +67,8 @@
67
67
  "access": "public"
68
68
  },
69
69
  "dependencies": {
70
- "@bodil/opt": "^0.2.6"
70
+ "@bodil/opt": "^0.2.6",
71
+ "temporal-polyfill": "^0.3.0"
71
72
  },
72
73
  "devDependencies": {
73
74
  "@bodil/opt-vitest": "^1.0.0",
@@ -80,7 +81,7 @@
80
81
  "eslint-plugin-jsdoc": "^50.7.1",
81
82
  "globals": "^15.15.0",
82
83
  "typescript": "^5.8.3",
83
- "vitest": "^3.2.2"
84
+ "vitest": "^3.2.3"
84
85
  },
85
86
  "scripts": {
86
87
  "build": "tsc",
package/src/assert.ts CHANGED
@@ -66,8 +66,8 @@ export function assertNever<T extends never>(_value: T): T {
66
66
  * Returns the given value unchanged if it's neither null nor undefined.
67
67
  * Throws an exception otherwise.
68
68
  */
69
- export function present<A>(value: A): NonNullable<A> {
70
- assertAlways(value !== null && value !== undefined);
69
+ export function present<A>(value: A, message?: string): NonNullable<A> {
70
+ assertAlways(value !== null && value !== undefined, message);
71
71
  return value;
72
72
  }
73
73
 
package/src/async.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  /**
2
2
  * Return a promise that will resolve after the specified number of
3
- * milliseconds.
3
+ * milliseconds or {@link Temporal.Duration}.
4
4
  */
5
- export function sleep(ms: number): Promise<void> {
5
+ export function sleep(duration: number | Temporal.Duration): Promise<void> {
6
6
  return new Promise((resolve) => {
7
- setTimeout(resolve, ms);
7
+ setTimeout(
8
+ resolve,
9
+ typeof duration === "number" ? duration : duration.total("milliseconds")
10
+ );
8
11
  });
9
12
  }
10
13
 
package/src/fun.ts CHANGED
@@ -4,3 +4,10 @@
4
4
  export function always<A>(value: A): () => A {
5
5
  return () => value;
6
6
  }
7
+
8
+ /**
9
+ * The identity function: it just returns its argument unchanged.
10
+ */
11
+ export function id<A>(value: A): A {
12
+ return value;
13
+ }
package/src/index.ts CHANGED
@@ -1,11 +1,11 @@
1
- import * as Assert from "./assert";
2
- import * as Async from "./async";
3
- import * as Date from "./date";
4
- import * as Event from "./event";
5
- import * as Fun from "./fun";
6
- import * as Order from "./order";
7
- import * as Types from "./types";
8
- export { Assert, Async, Date, Event, Fun, Order, Types };
1
+ import * as assert from "./assert";
2
+ import * as async from "./async";
3
+ import * as event from "./event";
4
+ import * as fun from "./fun";
5
+ import * as order from "./order";
6
+ import * as time from "./time";
7
+ import * as types from "./types";
8
+ export { assert, async, event, fun, order, time, types };
9
9
 
10
10
  import { None, Some, Option, Ok, Err, Result } from "@bodil/opt";
11
11
  export { None, Some, Option, Ok, Err, Result };
@@ -0,0 +1,46 @@
1
+ import "temporal-polyfill/global";
2
+
3
+ import { expect, test } from "vitest";
4
+ import { time } from ".";
5
+
6
+ test("time.min", () => {
7
+ const a = time.seconds(1);
8
+ const b = time.seconds(5);
9
+ const c = time.seconds(10);
10
+
11
+ expect(time.min(a, b, c)).toEqual(a);
12
+ });
13
+
14
+ test("time.max", () => {
15
+ const a = time.seconds(1);
16
+ const b = time.seconds(5);
17
+ const c = time.seconds(10);
18
+
19
+ expect(time.max(a, b, c)).toEqual(c);
20
+ });
21
+
22
+ test("time.isBefore", () => {
23
+ const a = time.now().subtract(time.seconds(10));
24
+ const b = a.subtract(time.seconds(10));
25
+ const c = time.now().add(time.minutes(10));
26
+
27
+ expect(time.isBefore(c)).toBeFalsy();
28
+ expect(time.isBefore(a)).toBeTruthy();
29
+ expect(time.isBefore(a, b)).toBeFalsy();
30
+ expect(time.isBefore(b, a)).toBeTruthy();
31
+ expect(time.isBefore(a, time.seconds(5))).toBeTruthy();
32
+ expect(time.isBefore(a, time.seconds(60))).toBeFalsy();
33
+ });
34
+
35
+ test("time.isAfter", () => {
36
+ const a = time.now().add(time.seconds(10));
37
+ const b = a.add(time.seconds(10));
38
+ const c = time.now().subtract(time.minutes(10));
39
+
40
+ expect(time.isAfter(c)).toBeFalsy();
41
+ expect(time.isAfter(a)).toBeTruthy();
42
+ expect(time.isAfter(a, b)).toBeFalsy();
43
+ expect(time.isAfter(b, a)).toBeTruthy();
44
+ expect(time.isAfter(a, time.seconds(5))).toBeTruthy();
45
+ expect(time.isAfter(a, time.seconds(60))).toBeFalsy();
46
+ });
package/src/time.ts ADDED
@@ -0,0 +1,151 @@
1
+ import { present } from "./assert";
2
+ import { Cmp, type OrderFn } from "./order";
3
+
4
+ /**
5
+ * Test if the given {@link Temporal.Instant} comes before a certain moment
6
+ * in time.
7
+ *
8
+ * If `other` is unspecified, compare with the current time.
9
+ *
10
+ * If `other` is a {@link Temporal.Duration}, compare with that duration
11
+ * subtracted from the current time.
12
+ *
13
+ * If `other` is a {@link Temporal.Instant}, compare with that instant.
14
+ *
15
+ * @example
16
+ * time.isBefore(instant) // is `instant` in the past?
17
+ * time.isBefore(instant, otherInstant) // is `instant` before `otherInstant`?
18
+ * time.isBefore(instant, duration) // is `instant` more than `duration` ago?
19
+ */
20
+ export function isBefore(
21
+ instant: Temporal.Instant,
22
+ other?: Temporal.Instant | Temporal.Duration
23
+ ): boolean {
24
+ return (
25
+ Temporal.Instant.compare(
26
+ instant,
27
+ other instanceof Temporal.Duration
28
+ ? Temporal.Now.instant().subtract(other)
29
+ : other ?? Temporal.Now.instant()
30
+ ) === Cmp.LT
31
+ );
32
+ }
33
+
34
+ /**
35
+ * Test if the given {@link Temporal.Instant} comes after a certain moment in
36
+ * time.
37
+ *
38
+ * If `other` is unspecified, compare with the current time.
39
+ *
40
+ * If `other` is a {@link Temporal.Duration}, compare with that duration added
41
+ * to the current time.
42
+ *
43
+ * If `other` is a {@link Temporal.Instant}, compare with that instant.
44
+ *
45
+ * @example
46
+ * time.isAfter(instant) // is `instant` in the future?
47
+ * time.isAfter(instant, otherInstant) // is `instant` after `otherInstant`?
48
+ * time.isAfter(instant, duration) // is `instant` more than `duration` into the future?
49
+ */
50
+ export function isAfter(
51
+ instant: Temporal.Instant,
52
+ other?: Temporal.Instant | Temporal.Duration
53
+ ): boolean {
54
+ return (
55
+ Temporal.Instant.compare(
56
+ instant,
57
+ other instanceof Temporal.Duration
58
+ ? Temporal.Now.instant().add(other)
59
+ : other ?? Temporal.Now.instant()
60
+ ) === Cmp.GT
61
+ );
62
+ }
63
+
64
+ /**
65
+ * Find the shortest duration or earliest time given a series of
66
+ * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
67
+ */
68
+ export function min<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T {
69
+ let first: T = present(rest.shift(), "time.min() called with no arguments");
70
+ const compare =
71
+ first instanceof Temporal.Duration
72
+ ? (Temporal.Duration.compare as OrderFn<T>)
73
+ : (Temporal.Instant.compare as OrderFn<T>);
74
+ for (const next of rest) {
75
+ if (compare(next, first) === Cmp.LT) {
76
+ first = next;
77
+ }
78
+ }
79
+ return first;
80
+ }
81
+
82
+ /**
83
+ * Find the longest duration or latest time given a series of
84
+ * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
85
+ */
86
+ export function max<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T {
87
+ let first: T = present(rest.shift(), "time.max() called with no arguments");
88
+ const compare =
89
+ first instanceof Temporal.Duration
90
+ ? (Temporal.Duration.compare as OrderFn<T>)
91
+ : (Temporal.Instant.compare as OrderFn<T>);
92
+ for (const next of rest) {
93
+ if (compare(next, first) === Cmp.GT) {
94
+ first = next;
95
+ }
96
+ }
97
+ return first;
98
+ }
99
+
100
+ /**
101
+ * The current time as a {@link Temporal.Instant}.
102
+ *
103
+ * A shorthand for `Temporal.Now.instant()`.
104
+ */
105
+ export function now(): Temporal.Instant {
106
+ return Temporal.Now.instant();
107
+ }
108
+
109
+ /**
110
+ * The current time as a {@link Temporal.ZonedDateTime}.
111
+ *
112
+ * A shorthand for `Temporal.Now.zonedDateTimeISO()`.
113
+ */
114
+ export function nowZ(): Temporal.ZonedDateTime {
115
+ return Temporal.Now.zonedDateTimeISO();
116
+ }
117
+
118
+ /**
119
+ * Construct a duration of the given number of days.
120
+ */
121
+ export function days(days: number): Temporal.Duration {
122
+ return Temporal.Duration.from({ hours: days * 24 });
123
+ }
124
+
125
+ /**
126
+ * Construct a duration of the given number of hours.
127
+ */
128
+ export function hours(hours: number): Temporal.Duration {
129
+ return Temporal.Duration.from({ hours });
130
+ }
131
+
132
+ /**
133
+ * Construct a duration of the given number of minutes.
134
+ */
135
+ export function minutes(minutes: number): Temporal.Duration {
136
+ return Temporal.Duration.from({ minutes });
137
+ }
138
+
139
+ /**
140
+ * Construct a duration of the given number of seconds.
141
+ */
142
+ export function seconds(seconds: number): Temporal.Duration {
143
+ return Temporal.Duration.from({ seconds });
144
+ }
145
+
146
+ /**
147
+ * Construct a duration of the given number of milliseconds.
148
+ */
149
+ export function millis(milliseconds: number): Temporal.Duration {
150
+ return Temporal.Duration.from({ milliseconds });
151
+ }
package/src/date.ts DELETED
@@ -1,185 +0,0 @@
1
- export const MILLIS_IN_DAY = 86400000 as Milliseconds;
2
- export const MILLIS_IN_HOUR = 3600000 as Milliseconds;
3
- export const MILLIS_IN_MINUTE = 60000 as Milliseconds;
4
- export const MILLIS_IN_SECOND = 1000 as Milliseconds;
5
-
6
- export type Milliseconds = number;
7
-
8
- /**
9
- * Find the earliest {@link Date} in a list of {@link Date}s.
10
- */
11
- export function min(...dates: Array<Date>): Date {
12
- return dates.reduce((a, b) => (a < b ? a : b));
13
- }
14
-
15
- /**
16
- * Find the latest {@link Date} in a list of {@link Date}s.
17
- */
18
- export function max(...dates: Array<Date>): Date {
19
- return dates.reduce((a, b) => (a > b ? a : b));
20
- }
21
-
22
- /**
23
- * Test if two {@link Date}s lie in the same calendar year.
24
- */
25
- export function isSameYear(a: Date, b: Date): boolean {
26
- return a.getFullYear() === b.getFullYear();
27
- }
28
-
29
- /**
30
- * Create a new {@link Date} that's the given number of milliseconds after the
31
- * given {@link Date}.
32
- */
33
- export function addMillis(date: Date, millis: Milliseconds): Date {
34
- const time = +date;
35
- return new Date(time + millis);
36
- }
37
-
38
- /**
39
- * Create a new {@link Date} that's the given number of milliseconds before the
40
- * given {@link Date}.
41
- */
42
- export function subMillis(date: Date, millis: Milliseconds): Date {
43
- return addMillis(date, -millis);
44
- }
45
-
46
- /**
47
- * Create a new {@link Date} that's the given number of seconds after the
48
- * given {@link Date}.
49
- */
50
- export function addSeconds(date: Date, seconds: number): Date {
51
- return addMillis(date, seconds * MILLIS_IN_SECOND);
52
- }
53
-
54
- /**
55
- * Create a new {@link Date} that's the given number of seconds before the
56
- * given {@link Date}.
57
- */
58
- export function subSeconds(date: Date, seconds: number): Date {
59
- return subMillis(date, seconds * MILLIS_IN_SECOND);
60
- }
61
-
62
- /**
63
- * Create a new {@link Date} that's the given number of minutes after the
64
- * given {@link Date}.
65
- */
66
- export function addMinutes(date: Date, minutes: number): Date {
67
- return addMillis(date, minutes * MILLIS_IN_MINUTE);
68
- }
69
-
70
- /**
71
- * Create a new {@link Date} that's the given number of minutes before the
72
- * given {@link Date}.
73
- */
74
- export function subMinutes(date: Date, minutes: number): Date {
75
- return subMillis(date, minutes * MILLIS_IN_MINUTE);
76
- }
77
-
78
- /**
79
- * Create a new {@link Date} that's the given number of hours after the
80
- * given {@link Date}.
81
- */
82
- export function addHours(date: Date, hours: number): Date {
83
- return addMillis(date, hours * MILLIS_IN_HOUR);
84
- }
85
-
86
- /**
87
- * Create a new {@link Date} that's the given number of hours before the
88
- * given {@link Date}.
89
- */
90
- export function subHours(date: Date, hours: number): Date {
91
- return subMillis(date, hours * MILLIS_IN_HOUR);
92
- }
93
-
94
- /**
95
- * Create a new {@link Date} that's the given number of days after the
96
- * given {@link Date}.
97
- */
98
- export function addDays(date: Date, days: number): Date {
99
- const newDate = new Date(+date);
100
- newDate.setDate(date.getDate() + days);
101
- return newDate;
102
- }
103
-
104
- /**
105
- * Create a new {@link Date} that's the given number of days before the
106
- * given {@link Date}.
107
- */
108
- export function subDays(date: Date, days: number): Date {
109
- return addDays(date, -days);
110
- }
111
-
112
- /**
113
- * Calculate the number of days between two {@link Date}s.
114
- */
115
- export function daysBetween(a: Date, b: Date): number {
116
- return Math.round((+a - +b) / MILLIS_IN_DAY);
117
- }
118
-
119
- /**
120
- * Calculate the number of hours between two {@link Date}s.
121
- */
122
- export function hoursBetween(a: Date, b: Date): number {
123
- return Math.round((+a - +b) / MILLIS_IN_HOUR);
124
- }
125
-
126
- /**
127
- * Calculate the number of minutes between two {@link Date}s.
128
- */
129
- export function minutesBetween(a: Date, b: Date): number {
130
- return Math.round((+a - +b) / MILLIS_IN_MINUTE);
131
- }
132
-
133
- /**
134
- * Calculate the number of seconds between two {@link Date}s.
135
- */
136
- export function secondsBetween(a: Date, b: Date): number {
137
- return Math.round((+a - +b) / MILLIS_IN_SECOND);
138
- }
139
-
140
- /**
141
- * Calculate the number of milliseconds between two {@link Date}s.
142
- */
143
- export function millisBetween(a: Date, b: Date): Milliseconds {
144
- return +a - +b;
145
- }
146
-
147
- /**
148
- * Test if a {@link Date} is more than the given number of milliseconds before
149
- * the present.
150
- */
151
- export function isOlderThan(date: Date, millis: Milliseconds): boolean {
152
- return date <= subMillis(new Date(), millis);
153
- }
154
-
155
- /**
156
- * Test if a {@link Date} is more than the given number of seconds before the
157
- * present.
158
- */
159
- export function isOlderThanSeconds(date: Date, seconds: number): boolean {
160
- return date <= subMillis(new Date(), seconds * MILLIS_IN_SECOND);
161
- }
162
-
163
- /**
164
- * Test if a {@link Date} is more than the given number of minutes before the
165
- * present.
166
- */
167
- export function isOlderThanMinutes(date: Date, minutes: number): boolean {
168
- return date <= subMillis(new Date(), minutes * MILLIS_IN_MINUTE);
169
- }
170
-
171
- /**
172
- * Test if a {@link Date} is more than the given number of hours before the
173
- * present.
174
- */
175
- export function isOlderThanHours(date: Date, hours: number): boolean {
176
- return date <= subMillis(new Date(), hours * MILLIS_IN_HOUR);
177
- }
178
-
179
- /**
180
- * Test if a {@link Date} is more than the given number of days before the
181
- * present.
182
- */
183
- export function isOlderThanDays(date: Date, days: number): boolean {
184
- return date <= subMillis(new Date(), days * MILLIS_IN_DAY);
185
- }