@bodil/core 0.5.5 → 0.5.7
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 +93 -0
- package/dist/assert.test.d.ts +1 -0
- package/dist/assert.test.js +2 -2
- package/dist/assert.test.js.map +1 -1
- package/dist/async/abortable-promise.d.ts +71 -0
- package/dist/async/abortable-promise.test.d.ts +1 -0
- package/dist/async/abortable-promise.test.js +8 -8
- package/dist/async/abortable-promise.test.js.map +1 -1
- package/dist/async/emitter.d.ts +10 -0
- package/dist/async/emitter.test.d.ts +1 -0
- package/dist/async/emitter.test.js.map +1 -1
- package/dist/async.d.ts +28 -0
- package/dist/async.js +1 -0
- package/dist/async.js.map +1 -1
- package/dist/collections/lfu-cache.d.ts +45 -0
- package/dist/collections/lfu-cache.test.d.ts +1 -0
- package/dist/collections/linked-list.d.ts +47 -0
- package/dist/collections/lru-cache.d.ts +45 -0
- package/dist/collections/lru-cache.test.d.ts +1 -0
- package/dist/collections.d.ts +7 -0
- package/dist/decorators.d.ts +12 -0
- package/dist/decorators.test.d.ts +1 -0
- package/dist/disposable.d.ts +122 -0
- package/dist/disposable.js +17 -0
- package/dist/disposable.js.map +1 -1
- package/dist/disposable.test.d.ts +1 -0
- package/dist/disposable.test.js +18 -3
- package/dist/disposable.test.js.map +1 -1
- package/dist/fun.d.ts +40 -0
- package/dist/index.d.ts +14 -0
- package/dist/order.d.ts +55 -0
- package/dist/order.test.d.ts +1 -0
- package/dist/time.d.ts +80 -0
- package/dist/time.test.d.ts +1 -0
- package/dist/types.d.ts +17 -0
- package/package.json +15 -15
- package/src/assert.test.ts +10 -10
- package/src/async/abortable-promise.test.ts +22 -22
- package/src/async/emitter.test.ts +1 -1
- package/src/async.ts +2 -1
- package/src/disposable.test.ts +21 -3
- package/src/disposable.ts +18 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tools for working with {@link Disposable}s and things that should have been
|
|
3
|
+
* {@link Disposable}.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { Constructor } from "type-fest";
|
|
8
|
+
import type { Unsubscribable } from "type-fest/globals";
|
|
9
|
+
/**
|
|
10
|
+
* Test whether something implements {@link Disposable}.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isDisposable(value: unknown): value is Disposable;
|
|
13
|
+
/**
|
|
14
|
+
* Type for something that can be turned into a {@link Disposable}.
|
|
15
|
+
*
|
|
16
|
+
* Includes things that already implement {@link Disposable}, objects with an
|
|
17
|
+
* `unsubscribe()` method (an {@link Unsubscribable}), and functions
|
|
18
|
+
* with no arguments or return values which are assumed to be unsubscribe
|
|
19
|
+
* functions, as popularised by React.
|
|
20
|
+
*/
|
|
21
|
+
export type Disposifiable = (() => void) | Unsubscribable | Disposable;
|
|
22
|
+
/**
|
|
23
|
+
* Construct a new {@link Disposable} out of any {@link Disposifiable}.
|
|
24
|
+
*
|
|
25
|
+
* This constructs a new object which implements {@link Disposable}, and nothing
|
|
26
|
+
* else. The new object's dispose method will dispose of the original
|
|
27
|
+
* {@link Disposifiable}, and ensure that this happens only once, regardless of
|
|
28
|
+
* how many times the dispose method is called.
|
|
29
|
+
*/
|
|
30
|
+
export declare function toDisposable(value: Disposifiable): Disposable;
|
|
31
|
+
/**
|
|
32
|
+
* Test whether something is {@link Disposifiable}.
|
|
33
|
+
*
|
|
34
|
+
* This is true if that something implements either of {@link Disposable} or
|
|
35
|
+
* {@link Unsubscribable}, or if it is a function.
|
|
36
|
+
*/
|
|
37
|
+
export declare function isDisposifiable(value: unknown): value is Disposifiable;
|
|
38
|
+
/**
|
|
39
|
+
* Call {@link setTimeout} with the provided callback and return a
|
|
40
|
+
* {@link Disposable} which will cancel the timeout when disposed.
|
|
41
|
+
*/
|
|
42
|
+
export declare function timeout(fn: () => void, timeout: number | Temporal.Duration): Disposable;
|
|
43
|
+
/**
|
|
44
|
+
* Call {@link setInterval} with the provided callback and return a
|
|
45
|
+
* {@link Disposable} which will cancel the interval when disposed.
|
|
46
|
+
*/
|
|
47
|
+
export declare function interval(fn: () => void, interval: number | Temporal.Duration): Disposable;
|
|
48
|
+
export declare function idleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): Disposable;
|
|
49
|
+
/**
|
|
50
|
+
* An implementation of {@link AbortController} which also implements
|
|
51
|
+
* {@link Disposable}.
|
|
52
|
+
*
|
|
53
|
+
* Disposing the controller automatically triggers the abort signal.
|
|
54
|
+
*/
|
|
55
|
+
export declare class DisposableAbortController implements AbortController, Disposable {
|
|
56
|
+
#private;
|
|
57
|
+
get signal(): AbortSignal;
|
|
58
|
+
abort(reason?: any): void;
|
|
59
|
+
[Symbol.dispose](): void;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create an {@link AbortController} which also implements {@link Disposable}.
|
|
63
|
+
*
|
|
64
|
+
* Disposing the controller automatically triggers the abort signal.
|
|
65
|
+
*
|
|
66
|
+
* @returns A {@link DisposableAbortController}.
|
|
67
|
+
*/
|
|
68
|
+
export declare function abortController(): DisposableAbortController;
|
|
69
|
+
export interface DisposableContext extends Disposable {
|
|
70
|
+
/**
|
|
71
|
+
* Disposes the resources currently held by this context.
|
|
72
|
+
*/
|
|
73
|
+
dispose(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Register a {@link Disposifiable} resource with this context, to be
|
|
76
|
+
* disposed along with it.
|
|
77
|
+
*
|
|
78
|
+
* @returns The {@link Disposable} that was registered with the context.
|
|
79
|
+
*/
|
|
80
|
+
use(resource: null): null;
|
|
81
|
+
use(resource: undefined): undefined;
|
|
82
|
+
use(resource: Disposifiable): Disposable;
|
|
83
|
+
use(resource: Disposifiable | undefined): Disposable | undefined;
|
|
84
|
+
use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
|
|
85
|
+
adopt<A>(value: A, onDispose: (value: A) => void): A;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A reentrant version of {@link DisposableStack}.
|
|
89
|
+
*
|
|
90
|
+
* This maintains a list of {@link Disposable}s, added with the
|
|
91
|
+
* {@link DisposableContext.use} method, which will all be disposed when you
|
|
92
|
+
* dispose of the {@link DisposableContext} itself.
|
|
93
|
+
*
|
|
94
|
+
* Where this differs from {@link DisposableStack} is that disposing it only
|
|
95
|
+
* resets it after disposing of its dependents, and you can reuse it as many
|
|
96
|
+
* times as you like. This is very handy for managing lifecycle resources, eg. a
|
|
97
|
+
* web component can allocate resources in its `connectedCallback` method and
|
|
98
|
+
* during its connected lifecycle phase which can all be disposed in
|
|
99
|
+
* `disconnectedCallback` with a single call to
|
|
100
|
+
* {@link DisposableContext.dispose}, and it's instantly ready to go again on
|
|
101
|
+
* reconnect.
|
|
102
|
+
*
|
|
103
|
+
* @class
|
|
104
|
+
*/
|
|
105
|
+
export declare const DisposableContext: Constructor<DisposableContext>;
|
|
106
|
+
/**
|
|
107
|
+
* A class mixin which provides a {@link Disposable} implementation.
|
|
108
|
+
*
|
|
109
|
+
* The `use(resource)` method can be used to register {@link Disposifiable}
|
|
110
|
+
* resources which will be disposed when the object's dispose method is called.
|
|
111
|
+
*/
|
|
112
|
+
export declare const AutoDisposable: <C extends Constructor<object>>(superclass: C) => {
|
|
113
|
+
new (...arguments_: any[]): {
|
|
114
|
+
__context?: DisposableContext;
|
|
115
|
+
use(resource: null): null;
|
|
116
|
+
use(resource: undefined): undefined;
|
|
117
|
+
use(resource: Disposifiable): Disposable;
|
|
118
|
+
use(resource: Disposifiable | undefined): Disposable | undefined;
|
|
119
|
+
use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
|
|
120
|
+
[Symbol.dispose](): void;
|
|
121
|
+
};
|
|
122
|
+
} & C;
|
package/dist/disposable.js
CHANGED
|
@@ -52,6 +52,23 @@ export function toDisposable(value) {
|
|
|
52
52
|
},
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
|
+
function isUnsubscribable(value) {
|
|
56
|
+
try {
|
|
57
|
+
return typeof value.unsubscribe === "function";
|
|
58
|
+
}
|
|
59
|
+
catch (_e) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Test whether something is {@link Disposifiable}.
|
|
65
|
+
*
|
|
66
|
+
* This is true if that something implements either of {@link Disposable} or
|
|
67
|
+
* {@link Unsubscribable}, or if it is a function.
|
|
68
|
+
*/
|
|
69
|
+
export function isDisposifiable(value) {
|
|
70
|
+
return isDisposable(value) || isUnsubscribable(value) || typeof value === "function";
|
|
71
|
+
}
|
|
55
72
|
/**
|
|
56
73
|
* Call {@link setTimeout} with the provided callback and return a
|
|
57
74
|
* {@link Disposable} which will cancel the timeout when disposed.
|
package/dist/disposable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"disposable.js","sourceRoot":"","sources":["../src/disposable.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAIlC,gDAAgD;AAChD,MAAM,CAAC,OAAO,KAAK,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAEhD;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACvC,IAAI,CAAC;QACD,OAAO,OAAQ,KAAoB,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,CAAC;IACvE,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACV,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAYD,SAAS,gBAAgB,CAAC,KAAoB;IAC1C,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5B,IAAI,OAAQ,KAAoB,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,EAAE,CAAC;YAC9D,OAAQ,KAAoB,CAAC,MAAM,CAAC,OAAO,CAAC,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K,CAAC,CAAC,CAAC;QACjC,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAmC,MAAM,CAAC,MAAM,CAC1E,UAAU,EACV,iBAAiB,CACpB;IACG,CAAC,CAAC,uBAAuB;IACzB,CAAC,CAAC,yBAAyB,CAAC;AAEhC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAgC,UAAa,EAAE,EAAE,CAC3E,KAAM,SAAQ,UAAU;IAAxB;;QACI,cAAS,GAAuB,IAAI,iBAAiB,EAAE,CAAC;IAkB5D,CAAC;IAhBG,CAAC,MAAM,CAAC,OAAO,CAAC;QACZ,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YAC/B,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;YACzB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC/B,CAAC;IACL,CAAC;IAOD,GAAG,CAAC,QAA0C;QAC1C,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,4CAA4C,CAAC,CAAC;QACrE,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxC,CAAC;CACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/disposable.test.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
1
2
|
import { test, expect } from "vitest";
|
|
2
|
-
import { DisposableContext } from "./disposable";
|
|
3
|
+
import { DisposableContext, isDisposifiable } from "./disposable";
|
|
3
4
|
test("disposes resources", () => {
|
|
4
5
|
const context = new DisposableContext();
|
|
5
6
|
let d1 = false;
|
|
@@ -28,14 +29,14 @@ test("disposer errors", () => {
|
|
|
28
29
|
context.use(() => {
|
|
29
30
|
throw new TypeError("welp");
|
|
30
31
|
});
|
|
31
|
-
expect(() => context.dispose()).
|
|
32
|
+
expect(() => context.dispose()).toThrow(TypeError);
|
|
32
33
|
context.use(() => {
|
|
33
34
|
throw new TypeError("welp");
|
|
34
35
|
});
|
|
35
36
|
context.use(() => {
|
|
36
37
|
throw new TypeError("wolp");
|
|
37
38
|
});
|
|
38
|
-
expect(() => context.dispose()).
|
|
39
|
+
expect(() => context.dispose()).toThrow(SuppressedError);
|
|
39
40
|
});
|
|
40
41
|
test("using", () => {
|
|
41
42
|
const called = [];
|
|
@@ -51,4 +52,18 @@ test("using", () => {
|
|
|
51
52
|
catch (_e) { }
|
|
52
53
|
expect(called).deep.equals(["welp", "wolp"]);
|
|
53
54
|
});
|
|
55
|
+
test("isDisposifiable", () => {
|
|
56
|
+
const disposable = {
|
|
57
|
+
[Symbol.dispose]() { },
|
|
58
|
+
};
|
|
59
|
+
const unsubscribable = { unsubscribe() { } };
|
|
60
|
+
expect(isDisposifiable(disposable)).toBeTruthy();
|
|
61
|
+
expect(isDisposifiable(unsubscribable)).toBeTruthy();
|
|
62
|
+
expect(isDisposifiable(null)).toBeFalsy();
|
|
63
|
+
expect(isDisposifiable("nope")).toBeFalsy();
|
|
64
|
+
expect(isDisposifiable(() => { })).toBeTruthy();
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
66
|
+
expect(isDisposifiable((a, b, c) => { })).toBeTruthy();
|
|
67
|
+
expect(isDisposifiable({})).toBeFalsy();
|
|
68
|
+
});
|
|
54
69
|
//# sourceMappingURL=disposable.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"disposable.test.js","sourceRoot":"","sources":["../src/disposable.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"disposable.test.js","sourceRoot":"","sources":["../src/disposable.test.ts"],"names":[],"mappings":"A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AAC;KACH,CAAC;IACvB,MAAM,cAAc,GAAG,EAAE,WAAW,KAAI,CAAC,EAA2B,CAAC;IAErE,MAAM,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IACjD,MAAM,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IACrD,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1C,MAAM,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IAC5C,MAAM,CAAC,eAAe,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IAC/C,6DAA6D;IAC7D,MAAM,CAAC,eAAe,CAAC,CAAC,CAAM,EAAE,CAAM,EAAE,CAAM,EAAE,EAAE,GAAE,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;IACrE,MAAM,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AAC5C,CAAC,CAAC,CAAC"}
|
package/dist/fun.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The functional programmer's toolkit.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Create a function that always returns `value`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts @import.meta.vitest
|
|
10
|
+
* const alwaysFive: () => number = always(5);
|
|
11
|
+
* expect(alwaysFive()).toEqual(5);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function always<A>(value: A): () => A;
|
|
15
|
+
/**
|
|
16
|
+
* The identity function: it just returns its argument unchanged.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts @import.meta.vitest
|
|
20
|
+
* expect(id(1337)).toEqual(1337);
|
|
21
|
+
* expect(id("Hello Joe!")).toEqual("Hello Joe!");
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function id<A>(value: A): A;
|
|
25
|
+
/**
|
|
26
|
+
* A function which does nothing and returns `void`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function nop(): void;
|
|
29
|
+
/**
|
|
30
|
+
* A function which applies the `+` operator to its arguments.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts @import.meta.vitest
|
|
34
|
+
* expect(sum(2, 3)).toEqual(5);
|
|
35
|
+
* expect(sum("foo", "bar")).toEqual("foobar");
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function sum(a: number, b: number): number;
|
|
39
|
+
export declare function sum(a: bigint, b: bigint): bigint;
|
|
40
|
+
export declare function sum(a: string, b: string): string;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* My personal JavaScript standard library.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import * as assert from "./assert";
|
|
6
|
+
import * as async from "./async";
|
|
7
|
+
import * as collections from "./collections";
|
|
8
|
+
import * as decorators from "./decorators";
|
|
9
|
+
import * as disposable from "./disposable";
|
|
10
|
+
import * as fun from "./fun";
|
|
11
|
+
import * as order from "./order";
|
|
12
|
+
import * as time from "./time";
|
|
13
|
+
import * as types from "./types";
|
|
14
|
+
export { assert, async, collections, decorators, disposable, fun, order, time, types };
|
package/dist/order.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorting and ordering.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import { type Option } from "@bodil/opt";
|
|
6
|
+
export declare enum Cmp {
|
|
7
|
+
/** Less than. */
|
|
8
|
+
LT = -1,
|
|
9
|
+
/** Equal to. */
|
|
10
|
+
EQ = 0,
|
|
11
|
+
/** Greater than. */
|
|
12
|
+
GT = 1
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* An order function is a function which is used to compare two items, returning
|
|
16
|
+
* a {@link Cmp} to describe the first item in relation to the second, eg.
|
|
17
|
+
* {@link Cmp.GT} says the first item is greater than the second item.
|
|
18
|
+
*/
|
|
19
|
+
export type OrderFn<A> = (left: A, right: A) => Cmp;
|
|
20
|
+
/**
|
|
21
|
+
* The set of types which can be compared using the `<` and `>` operators.
|
|
22
|
+
*/
|
|
23
|
+
export type OperatorComparable = number | string | bigint | Date;
|
|
24
|
+
/**
|
|
25
|
+
* An order function which sorts {@link OperatorComparable} items in ascending
|
|
26
|
+
* order.
|
|
27
|
+
*/
|
|
28
|
+
export declare function ascending<A extends OperatorComparable>(left: A, right: A): Cmp;
|
|
29
|
+
/**
|
|
30
|
+
* An order function which sorts {@link OperatorComparable} items in descending
|
|
31
|
+
* order.
|
|
32
|
+
*/
|
|
33
|
+
export declare function descending<A extends OperatorComparable>(left: A, right: A): Cmp;
|
|
34
|
+
/**
|
|
35
|
+
* Take an order function and invert its order.
|
|
36
|
+
*/
|
|
37
|
+
export declare function reverse<T>(fn: OrderFn<T>): OrderFn<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
40
|
+
* has been sorted, find the lowest index at which the value could be inserted
|
|
41
|
+
* while maintaining sort order.
|
|
42
|
+
*/
|
|
43
|
+
export declare function bisectLow<A>(cmp: OrderFn<A>, array: Array<A>, key: A): number;
|
|
44
|
+
/**
|
|
45
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
46
|
+
* has been sorted, find the highest index at which the value could be inserted
|
|
47
|
+
* while maintaining sort order.
|
|
48
|
+
*/
|
|
49
|
+
export declare function bisectHigh<A>(cmp: OrderFn<A>, array: Array<A>, key: A): number;
|
|
50
|
+
/**
|
|
51
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
52
|
+
* has been sorted, find the index of the specific value if it exists in the
|
|
53
|
+
* array.
|
|
54
|
+
*/
|
|
55
|
+
export declare function find<A>(cmp: OrderFn<A>, array: Array<A>, key: A): Option<number>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@bodil/opt-vitest";
|
package/dist/time.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for working with {@link Temporal}.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Test if the given {@link Temporal.Instant} comes before a certain moment
|
|
7
|
+
* in time.
|
|
8
|
+
*
|
|
9
|
+
* If `other` is unspecified, compare with the current time.
|
|
10
|
+
*
|
|
11
|
+
* If `other` is a {@link Temporal.Duration}, compare with that duration
|
|
12
|
+
* subtracted from the current time.
|
|
13
|
+
*
|
|
14
|
+
* If `other` is a {@link Temporal.Instant}, compare with that instant.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* time.isBefore(instant) // is `instant` in the past?
|
|
18
|
+
* time.isBefore(instant, otherInstant) // is `instant` before `otherInstant`?
|
|
19
|
+
* time.isBefore(instant, duration) // is `instant` more than `duration` ago?
|
|
20
|
+
*/
|
|
21
|
+
export declare function isBefore(instant: Temporal.Instant, other?: Temporal.Instant | Temporal.Duration): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Test if the given {@link Temporal.Instant} comes after a certain moment in
|
|
24
|
+
* time.
|
|
25
|
+
*
|
|
26
|
+
* If `other` is unspecified, compare with the current time.
|
|
27
|
+
*
|
|
28
|
+
* If `other` is a {@link Temporal.Duration}, compare with that duration added
|
|
29
|
+
* to the current time.
|
|
30
|
+
*
|
|
31
|
+
* If `other` is a {@link Temporal.Instant}, compare with that instant.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* time.isAfter(instant) // is `instant` in the future?
|
|
35
|
+
* time.isAfter(instant, otherInstant) // is `instant` after `otherInstant`?
|
|
36
|
+
* time.isAfter(instant, duration) // is `instant` more than `duration` into the future?
|
|
37
|
+
*/
|
|
38
|
+
export declare function isAfter(instant: Temporal.Instant, other?: Temporal.Instant | Temporal.Duration): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Find the shortest duration or earliest time given a series of
|
|
41
|
+
* {@link Temporal.Duration}s or {@link Temporal.Instant}s.
|
|
42
|
+
*/
|
|
43
|
+
export declare function min<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T;
|
|
44
|
+
/**
|
|
45
|
+
* Find the longest duration or latest time given a series of
|
|
46
|
+
* {@link Temporal.Duration}s or {@link Temporal.Instant}s.
|
|
47
|
+
*/
|
|
48
|
+
export declare function max<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T;
|
|
49
|
+
/**
|
|
50
|
+
* The current time as a {@link Temporal.Instant}.
|
|
51
|
+
*
|
|
52
|
+
* A shorthand for `Temporal.Now.instant()`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function now(): Temporal.Instant;
|
|
55
|
+
/**
|
|
56
|
+
* The current time as a {@link Temporal.ZonedDateTime}.
|
|
57
|
+
*
|
|
58
|
+
* A shorthand for `Temporal.Now.zonedDateTimeISO()`.
|
|
59
|
+
*/
|
|
60
|
+
export declare function nowZ(): Temporal.ZonedDateTime;
|
|
61
|
+
/**
|
|
62
|
+
* Construct a duration of the given number of days.
|
|
63
|
+
*/
|
|
64
|
+
export declare function days(days: number): Temporal.Duration;
|
|
65
|
+
/**
|
|
66
|
+
* Construct a duration of the given number of hours.
|
|
67
|
+
*/
|
|
68
|
+
export declare function hours(hours: number): Temporal.Duration;
|
|
69
|
+
/**
|
|
70
|
+
* Construct a duration of the given number of minutes.
|
|
71
|
+
*/
|
|
72
|
+
export declare function minutes(minutes: number): Temporal.Duration;
|
|
73
|
+
/**
|
|
74
|
+
* Construct a duration of the given number of seconds.
|
|
75
|
+
*/
|
|
76
|
+
export declare function seconds(seconds: number): Temporal.Duration;
|
|
77
|
+
/**
|
|
78
|
+
* Construct a duration of the given number of milliseconds.
|
|
79
|
+
*/
|
|
80
|
+
export declare function millis(milliseconds: number): Temporal.Duration;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "temporal-polyfill/global";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility types.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Constructs a type by excluding `undefined` from `T`.
|
|
7
|
+
*
|
|
8
|
+
* This is like {@link NonNullable} but it only removes `undefined`, not `null`.
|
|
9
|
+
*/
|
|
10
|
+
export type Present<T> = T extends undefined ? never : T;
|
|
11
|
+
/**
|
|
12
|
+
* Interface for classes which implement equality checking.
|
|
13
|
+
*/
|
|
14
|
+
export interface Equals {
|
|
15
|
+
/** Tests whether a given value is equal to this value. */
|
|
16
|
+
equals(other: unknown): boolean;
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bodil/core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"description": "My TypeScript standard library",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -80,29 +80,29 @@
|
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
82
|
"@bodil/opt": "^1.0.0",
|
|
83
|
-
"type-fest": "^5.
|
|
83
|
+
"type-fest": "^5.8.0"
|
|
84
84
|
},
|
|
85
85
|
"devDependencies": {
|
|
86
86
|
"@bodil/opt-vitest": "^1.1.0",
|
|
87
|
-
"@eslint/eslintrc": "^3.3.
|
|
88
|
-
"@eslint/js": "^
|
|
89
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
90
|
-
"@typescript-eslint/parser": "^8.
|
|
87
|
+
"@eslint/eslintrc": "^3.3.6",
|
|
88
|
+
"@eslint/js": "^10.0.1",
|
|
89
|
+
"@typescript-eslint/eslint-plugin": "^8.68.0",
|
|
90
|
+
"@typescript-eslint/parser": "^8.68.0",
|
|
91
91
|
"@typhonjs-typedoc/ts-lib-docs": "^2024.12.25",
|
|
92
|
-
"eslint": "^9.
|
|
92
|
+
"eslint": "^10.9.1",
|
|
93
93
|
"eslint-config-prettier": "^10.1.8",
|
|
94
|
-
"eslint-plugin-jsdoc": "^
|
|
95
|
-
"globals": "^
|
|
96
|
-
"npm-run-all2": "^
|
|
97
|
-
"prettier": "^3.
|
|
94
|
+
"eslint-plugin-jsdoc": "^63.3.3",
|
|
95
|
+
"globals": "^17.11.0",
|
|
96
|
+
"npm-run-all2": "^9.0.3",
|
|
97
|
+
"prettier": "^3.9.6",
|
|
98
98
|
"temporal-polyfill": "^0.3.2",
|
|
99
|
-
"typedoc": "^0.28.
|
|
99
|
+
"typedoc": "^0.28.20",
|
|
100
100
|
"typedoc-plugin-extras": "^4.0.1",
|
|
101
101
|
"typedoc-plugin-mdn-links": "^5.1.1",
|
|
102
102
|
"typedoc-theme-fresh": "^0.2.3",
|
|
103
|
-
"typescript": "^
|
|
104
|
-
"vite-plugin-doctest": "^
|
|
105
|
-
"vitest": "^
|
|
103
|
+
"typescript": "^6.0.3",
|
|
104
|
+
"vite-plugin-doctest": "^2.0.0",
|
|
105
|
+
"vitest": "^4.1.11"
|
|
106
106
|
},
|
|
107
107
|
"scripts": {
|
|
108
108
|
"build": "tsc",
|
package/src/assert.test.ts
CHANGED
|
@@ -29,8 +29,8 @@ test("deepEqual", () => {
|
|
|
29
29
|
new Map([
|
|
30
30
|
[1, 2],
|
|
31
31
|
[3, 4],
|
|
32
|
-
])
|
|
33
|
-
)
|
|
32
|
+
]),
|
|
33
|
+
),
|
|
34
34
|
).toEqual(true);
|
|
35
35
|
expect(
|
|
36
36
|
isDeepEqual(
|
|
@@ -41,8 +41,8 @@ test("deepEqual", () => {
|
|
|
41
41
|
new Map([
|
|
42
42
|
[1, 2],
|
|
43
43
|
[3, 4],
|
|
44
|
-
])
|
|
45
|
-
)
|
|
44
|
+
]),
|
|
45
|
+
),
|
|
46
46
|
).toEqual(false);
|
|
47
47
|
|
|
48
48
|
// Uint8Array
|
|
@@ -53,24 +53,24 @@ test("deepEqual", () => {
|
|
|
53
53
|
expect(
|
|
54
54
|
isDeepEqual(
|
|
55
55
|
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
56
|
-
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } }
|
|
57
|
-
)
|
|
56
|
+
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
57
|
+
),
|
|
58
58
|
).toEqual(true);
|
|
59
59
|
expect(
|
|
60
60
|
isDeepEqual(
|
|
61
61
|
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
62
|
-
{ foo: [1, new Set([1, 2, 3])], bar: { baz: time.days(1) } }
|
|
63
|
-
)
|
|
62
|
+
{ foo: [1, new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
63
|
+
),
|
|
64
64
|
).toEqual(false);
|
|
65
65
|
|
|
66
66
|
// Insistent equals impl
|
|
67
67
|
class Yes implements Equals {
|
|
68
|
-
equals(
|
|
68
|
+
equals(_other: unknown): _other is this {
|
|
69
69
|
return true;
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
class No implements Equals {
|
|
73
|
-
equals(
|
|
73
|
+
equals(_other: unknown): _other is this {
|
|
74
74
|
return false;
|
|
75
75
|
}
|
|
76
76
|
}
|