@bodil/core 0.5.4 → 0.5.6
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.js +217 -0
- package/dist/assert.js.map +1 -0
- package/dist/assert.test.d.ts +1 -0
- package/dist/assert.test.js +51 -0
- package/dist/assert.test.js.map +1 -0
- package/dist/async/abortable-promise.d.ts +71 -0
- package/dist/async/abortable-promise.js +203 -0
- package/dist/async/abortable-promise.js.map +1 -0
- package/dist/async/abortable-promise.test.d.ts +1 -0
- package/dist/async/abortable-promise.test.js +134 -0
- package/dist/async/abortable-promise.test.js.map +1 -0
- package/dist/async/emitter.d.ts +10 -0
- package/dist/async/emitter.js +22 -0
- package/dist/async/emitter.js.map +1 -0
- package/dist/async/emitter.test.d.ts +1 -0
- package/dist/async/emitter.test.js +49 -0
- package/dist/async/emitter.test.js.map +1 -0
- package/dist/async.d.ts +28 -0
- package/dist/async.js +52 -0
- package/dist/async.js.map +1 -0
- package/dist/collections/lfu-cache.d.ts +45 -0
- package/dist/collections/lfu-cache.js +107 -0
- package/dist/collections/lfu-cache.js.map +1 -0
- package/dist/collections/lfu-cache.test.d.ts +1 -0
- package/dist/collections/lfu-cache.test.js +29 -0
- package/dist/collections/lfu-cache.test.js.map +1 -0
- package/dist/collections/linked-list.d.ts +47 -0
- package/dist/collections/linked-list.js +129 -0
- package/dist/collections/linked-list.js.map +1 -0
- package/dist/collections/lru-cache.d.ts +45 -0
- package/dist/collections/lru-cache.js +84 -0
- package/dist/collections/lru-cache.js.map +1 -0
- package/dist/collections/lru-cache.test.d.ts +1 -0
- package/dist/collections/lru-cache.test.js +27 -0
- package/dist/collections/lru-cache.test.js.map +1 -0
- package/dist/collections.d.ts +7 -0
- package/dist/collections.js +8 -0
- package/dist/collections.js.map +1 -0
- package/dist/decorators.d.ts +12 -0
- package/dist/decorators.js +28 -0
- package/dist/decorators.js.map +1 -0
- package/dist/decorators.test.d.ts +1 -0
- package/dist/decorators.test.js +71 -0
- package/dist/decorators.test.js.map +1 -0
- package/dist/disposable.d.ts +115 -0
- package/dist/disposable.js +251 -0
- package/dist/disposable.js.map +1 -0
- package/dist/disposable.test.d.ts +1 -0
- package/dist/disposable.test.js +54 -0
- package/dist/disposable.test.js.map +1 -0
- package/dist/fun.d.ts +40 -0
- package/dist/fun.js +40 -0
- package/dist/fun.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/order.d.ts +55 -0
- package/dist/order.js +98 -0
- package/dist/order.js.map +1 -0
- package/dist/order.test.d.ts +1 -0
- package/dist/order.test.js +63 -0
- package/dist/order.test.js.map +1 -0
- package/dist/time.d.ts +80 -0
- package/dist/time.js +130 -0
- package/dist/time.js.map +1 -0
- package/dist/time.test.d.ts +1 -0
- package/dist/time.test.js +38 -0
- package/dist/time.test.js.map +1 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/package.json +18 -18
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tools for working with {@link Disposable}s and things that should have been
|
|
3
|
+
* {@link Disposable}.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { assert } from "./assert";
|
|
8
|
+
// @ts-ignore polyfill Symbol.dispose if missing
|
|
9
|
+
Symbol.dispose ??= Symbol.for("Symbol.dispose");
|
|
10
|
+
/**
|
|
11
|
+
* Test whether something implements {@link Disposable}.
|
|
12
|
+
*/
|
|
13
|
+
export function isDisposable(value) {
|
|
14
|
+
try {
|
|
15
|
+
return typeof value[Symbol.dispose] === "function";
|
|
16
|
+
}
|
|
17
|
+
catch (_e) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function extractDisposeFn(value) {
|
|
22
|
+
if (typeof value === "function") {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
if (typeof value === "object") {
|
|
26
|
+
if (typeof value[Symbol.dispose] === "function") {
|
|
27
|
+
return value[Symbol.dispose].bind(value);
|
|
28
|
+
}
|
|
29
|
+
if (typeof value.unsubscribe === "function") {
|
|
30
|
+
return value.unsubscribe.bind(value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
throw new Error("toDisposable(): don't know how to convert given value to disposable");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Construct a new {@link Disposable} out of any {@link Disposifiable}.
|
|
37
|
+
*
|
|
38
|
+
* This constructs a new object which implements {@link Disposable}, and nothing
|
|
39
|
+
* else. The new object's dispose method will dispose of the original
|
|
40
|
+
* {@link Disposifiable}, and ensure that this happens only once, regardless of
|
|
41
|
+
* how many times the dispose method is called.
|
|
42
|
+
*/
|
|
43
|
+
export function toDisposable(value) {
|
|
44
|
+
let disposed = false;
|
|
45
|
+
const disposeFn = extractDisposeFn(value);
|
|
46
|
+
return {
|
|
47
|
+
[Symbol.dispose]() {
|
|
48
|
+
if (!disposed) {
|
|
49
|
+
disposed = true;
|
|
50
|
+
disposeFn();
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Call {@link setTimeout} with the provided callback and return a
|
|
57
|
+
* {@link Disposable} which will cancel the timeout when disposed.
|
|
58
|
+
*/
|
|
59
|
+
export function timeout(fn, timeout) {
|
|
60
|
+
let id = globalThis.setTimeout(() => {
|
|
61
|
+
id = null;
|
|
62
|
+
disposable[Symbol.dispose]();
|
|
63
|
+
fn();
|
|
64
|
+
}, typeof timeout === "number" ? timeout : timeout.total("milliseconds"));
|
|
65
|
+
const disposable = toDisposable(() => {
|
|
66
|
+
if (id !== null) {
|
|
67
|
+
clearTimeout(id);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
return disposable;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Call {@link setInterval} with the provided callback and return a
|
|
74
|
+
* {@link Disposable} which will cancel the interval when disposed.
|
|
75
|
+
*/
|
|
76
|
+
export function interval(fn, interval) {
|
|
77
|
+
const id = globalThis.setInterval(fn, typeof interval === "number" ? interval : interval.total("milliseconds"));
|
|
78
|
+
return toDisposable(() => {
|
|
79
|
+
clearInterval(id);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
export function idleCallback(callback, options) {
|
|
83
|
+
let id = globalThis.requestIdleCallback((deadline) => {
|
|
84
|
+
id = null;
|
|
85
|
+
disposable[Symbol.dispose]();
|
|
86
|
+
callback(deadline);
|
|
87
|
+
}, options);
|
|
88
|
+
const disposable = toDisposable(() => {
|
|
89
|
+
if (id !== null) {
|
|
90
|
+
globalThis.cancelIdleCallback(id);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return disposable;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* An implementation of {@link AbortController} which also implements
|
|
97
|
+
* {@link Disposable}.
|
|
98
|
+
*
|
|
99
|
+
* Disposing the controller automatically triggers the abort signal.
|
|
100
|
+
*/
|
|
101
|
+
export class DisposableAbortController {
|
|
102
|
+
#controller = new AbortController();
|
|
103
|
+
get signal() {
|
|
104
|
+
return this.#controller.signal;
|
|
105
|
+
}
|
|
106
|
+
abort(reason) {
|
|
107
|
+
this.#controller.abort(reason);
|
|
108
|
+
this.#controller = new AbortController();
|
|
109
|
+
}
|
|
110
|
+
[Symbol.dispose]() {
|
|
111
|
+
this.abort("disposed");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create an {@link AbortController} which also implements {@link Disposable}.
|
|
116
|
+
*
|
|
117
|
+
* Disposing the controller automatically triggers the abort signal.
|
|
118
|
+
*
|
|
119
|
+
* @returns A {@link DisposableAbortController}.
|
|
120
|
+
*/
|
|
121
|
+
export function abortController() {
|
|
122
|
+
return new DisposableAbortController();
|
|
123
|
+
}
|
|
124
|
+
class DisposableContextNative {
|
|
125
|
+
constructor() {
|
|
126
|
+
this.stack = new DisposableStack();
|
|
127
|
+
this.dispose = this[Symbol.dispose];
|
|
128
|
+
}
|
|
129
|
+
[Symbol.dispose]() {
|
|
130
|
+
try {
|
|
131
|
+
this.stack[Symbol.dispose]();
|
|
132
|
+
}
|
|
133
|
+
finally {
|
|
134
|
+
this.stack = new DisposableStack();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
use(resource) {
|
|
138
|
+
if (resource === undefined || resource === null) {
|
|
139
|
+
return resource;
|
|
140
|
+
}
|
|
141
|
+
if (isDisposable(resource)) {
|
|
142
|
+
this.stack.use(resource);
|
|
143
|
+
return resource;
|
|
144
|
+
}
|
|
145
|
+
const dispose = extractDisposeFn(resource);
|
|
146
|
+
const wrappedDisposable = {
|
|
147
|
+
[Symbol.dispose]() {
|
|
148
|
+
dispose();
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
this.stack.use(wrappedDisposable);
|
|
152
|
+
return wrappedDisposable;
|
|
153
|
+
}
|
|
154
|
+
adopt(value, onDispose) {
|
|
155
|
+
this.stack.adopt(value, onDispose);
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
class DisposableContextPolyfill {
|
|
160
|
+
constructor() {
|
|
161
|
+
this.disposables = new Set();
|
|
162
|
+
this.dispose = this[Symbol.dispose];
|
|
163
|
+
}
|
|
164
|
+
[Symbol.dispose]() {
|
|
165
|
+
if (this.disposables.size === 0) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const errors = [];
|
|
169
|
+
for (const disposable of Array.from(this.disposables).reverse()) {
|
|
170
|
+
try {
|
|
171
|
+
disposable[Symbol.dispose]();
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
errors.push(e);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
assert(this.disposables.size === 0, "DisposableContext: disposables remaining in context after dispose, this isn't supposed to happen");
|
|
178
|
+
if (errors.length > 0) {
|
|
179
|
+
throw errors.reduce((prev, next) => new SuppressedError(next, prev));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
use(resource) {
|
|
183
|
+
if (resource === undefined || resource === null) {
|
|
184
|
+
return resource;
|
|
185
|
+
}
|
|
186
|
+
let disposables = this.disposables;
|
|
187
|
+
const dispose = extractDisposeFn(resource);
|
|
188
|
+
const wrappedDisposable = {
|
|
189
|
+
[Symbol.dispose]() {
|
|
190
|
+
try {
|
|
191
|
+
if (disposables?.delete(wrappedDisposable) === true) {
|
|
192
|
+
dispose();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
disposables = undefined;
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
disposables.add(wrappedDisposable);
|
|
201
|
+
return wrappedDisposable;
|
|
202
|
+
}
|
|
203
|
+
adopt(value, onDispose) {
|
|
204
|
+
this.use(() => onDispose(value));
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* A reentrant version of {@link DisposableStack}.
|
|
210
|
+
*
|
|
211
|
+
* This maintains a list of {@link Disposable}s, added with the
|
|
212
|
+
* {@link DisposableContext.use} method, which will all be disposed when you
|
|
213
|
+
* dispose of the {@link DisposableContext} itself.
|
|
214
|
+
*
|
|
215
|
+
* Where this differs from {@link DisposableStack} is that disposing it only
|
|
216
|
+
* resets it after disposing of its dependents, and you can reuse it as many
|
|
217
|
+
* times as you like. This is very handy for managing lifecycle resources, eg. a
|
|
218
|
+
* web component can allocate resources in its `connectedCallback` method and
|
|
219
|
+
* during its connected lifecycle phase which can all be disposed in
|
|
220
|
+
* `disconnectedCallback` with a single call to
|
|
221
|
+
* {@link DisposableContext.dispose}, and it's instantly ready to go again on
|
|
222
|
+
* reconnect.
|
|
223
|
+
*
|
|
224
|
+
* @class
|
|
225
|
+
*/
|
|
226
|
+
export const DisposableContext = Object.hasOwn(globalThis, "DisposableStack")
|
|
227
|
+
? DisposableContextNative
|
|
228
|
+
: DisposableContextPolyfill;
|
|
229
|
+
/**
|
|
230
|
+
* A class mixin which provides a {@link Disposable} implementation.
|
|
231
|
+
*
|
|
232
|
+
* The `use(resource)` method can be used to register {@link Disposifiable}
|
|
233
|
+
* resources which will be disposed when the object's dispose method is called.
|
|
234
|
+
*/
|
|
235
|
+
export const AutoDisposable = (superclass) => class extends superclass {
|
|
236
|
+
constructor() {
|
|
237
|
+
super(...arguments);
|
|
238
|
+
this.__context = new DisposableContext();
|
|
239
|
+
}
|
|
240
|
+
[Symbol.dispose]() {
|
|
241
|
+
if (this.__context !== undefined) {
|
|
242
|
+
this.__context.dispose();
|
|
243
|
+
this.__context = undefined;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
use(resource) {
|
|
247
|
+
assert(this.__context, "AutoDisposable: use() called after dispose");
|
|
248
|
+
return this.__context.use(resource);
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
//# sourceMappingURL=disposable.js.map
|
|
@@ -0,0 +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,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C;IACxC,CAAC;CACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { DisposableContext } from "./disposable";
|
|
3
|
+
test("disposes resources", () => {
|
|
4
|
+
const context = new DisposableContext();
|
|
5
|
+
let d1 = false;
|
|
6
|
+
const r1 = () => {
|
|
7
|
+
d1 = true;
|
|
8
|
+
};
|
|
9
|
+
let d2 = false;
|
|
10
|
+
const r2 = {
|
|
11
|
+
[Symbol.dispose]() {
|
|
12
|
+
d2 = true;
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
context.use(r1);
|
|
16
|
+
expect(d1).equals(false);
|
|
17
|
+
context.dispose();
|
|
18
|
+
expect(d1).equals(true);
|
|
19
|
+
d1 = false;
|
|
20
|
+
context.use(r2);
|
|
21
|
+
expect(d2).equals(false);
|
|
22
|
+
context.dispose();
|
|
23
|
+
expect(d2).equals(true);
|
|
24
|
+
expect(d1).equals(false);
|
|
25
|
+
});
|
|
26
|
+
test("disposer errors", () => {
|
|
27
|
+
const context = new DisposableContext();
|
|
28
|
+
context.use(() => {
|
|
29
|
+
throw new TypeError("welp");
|
|
30
|
+
});
|
|
31
|
+
expect(() => context.dispose()).toThrowError(TypeError);
|
|
32
|
+
context.use(() => {
|
|
33
|
+
throw new TypeError("welp");
|
|
34
|
+
});
|
|
35
|
+
context.use(() => {
|
|
36
|
+
throw new TypeError("wolp");
|
|
37
|
+
});
|
|
38
|
+
expect(() => context.dispose()).toThrowError(SuppressedError);
|
|
39
|
+
});
|
|
40
|
+
test("using", () => {
|
|
41
|
+
const called = [];
|
|
42
|
+
{
|
|
43
|
+
using context = new DisposableContext();
|
|
44
|
+
context.use(() => called.push("welp"));
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
using context = new DisposableContext();
|
|
48
|
+
context.use(() => called.push("wolp"));
|
|
49
|
+
throw new Error("oops");
|
|
50
|
+
}
|
|
51
|
+
catch (_e) { }
|
|
52
|
+
expect(called).deep.equals(["welp", "wolp"]);
|
|
53
|
+
});
|
|
54
|
+
//# sourceMappingURL=disposable.test.js.map
|
|
@@ -0,0 +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;AAEjD,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE;IAC5B,MAAM,OAAO,GAAG,IAAI,iBAAiB,EAAE,CAAC;IACxC,IAAI,EAAE,GAAG,KAAK,CAAC;IACf,MAAM,EAAE,GAAG,GAAG,EAAE;QACZ,EAAE,GAAG,IAAI,CAAC;IACd,CAAC,CAAC;IACF,IAAI,EAAE,GAAG,KAAK,CAAC;IACf,MAAM,EAAE,GAAe;QACnB,CAAC,MAAM,CAAC,OAAO,CAAC;YACZ,EAAE,GAAG,IAAI,CAAC;QACd,CAAC;KACJ,CAAC;IACF,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAChB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACzB,OAAO,CAAC,OAAO,EAAE,CAAC;IAClB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxB,EAAE,GAAG,KAAK,CAAC;IACX,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAChB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACzB,OAAO,CAAC,OAAO,EAAE,CAAC;IAClB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACxB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC7B,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,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AACjD,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/fun.js
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 function always(value) {
|
|
15
|
+
return () => value;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The identity function: it just returns its argument unchanged.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts @import.meta.vitest
|
|
22
|
+
* expect(id(1337)).toEqual(1337);
|
|
23
|
+
* expect(id("Hello Joe!")).toEqual("Hello Joe!");
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function id(value) {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A function which does nothing and returns `void`.
|
|
31
|
+
*/
|
|
32
|
+
export function nop() {
|
|
33
|
+
// nop
|
|
34
|
+
}
|
|
35
|
+
export function sum(a, b) {
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
38
|
+
return a + b;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=fun.js.map
|
package/dist/fun.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fun.js","sourceRoot":"","sources":["../src/fun.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAI,KAAQ;IAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACvB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC1B,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG;IACf,MAAM;AACV,CAAC;AAcD,MAAM,UAAU,GAAG,CAAqC,CAAI,EAAE,CAAI;IAC9D,aAAa;IACb,qEAAqE;IACrE,OAAO,CAAC,GAAG,CAAC,CAAC;AACjB,CAAC"}
|
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/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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 };
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,WAAW,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,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;AAEjC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC"}
|
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>;
|
package/dist/order.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorting and ordering.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import { None, Some } from "@bodil/opt";
|
|
6
|
+
export var Cmp;
|
|
7
|
+
(function (Cmp) {
|
|
8
|
+
/** Less than. */
|
|
9
|
+
Cmp[Cmp["LT"] = -1] = "LT";
|
|
10
|
+
/** Equal to. */
|
|
11
|
+
Cmp[Cmp["EQ"] = 0] = "EQ";
|
|
12
|
+
/** Greater than. */
|
|
13
|
+
Cmp[Cmp["GT"] = 1] = "GT";
|
|
14
|
+
})(Cmp || (Cmp = {}));
|
|
15
|
+
/**
|
|
16
|
+
* An order function which sorts {@link OperatorComparable} items in ascending
|
|
17
|
+
* order.
|
|
18
|
+
*/
|
|
19
|
+
export function ascending(left, right) {
|
|
20
|
+
return left < right ? Cmp.LT : left === right ? Cmp.EQ : Cmp.GT;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* An order function which sorts {@link OperatorComparable} items in descending
|
|
24
|
+
* order.
|
|
25
|
+
*/
|
|
26
|
+
export function descending(left, right) {
|
|
27
|
+
return left < right ? Cmp.GT : left === right ? Cmp.EQ : Cmp.LT;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Take an order function and invert its order.
|
|
31
|
+
*/
|
|
32
|
+
export function reverse(fn) {
|
|
33
|
+
return (left, right) => {
|
|
34
|
+
switch (fn(left, right)) {
|
|
35
|
+
case Cmp.LT:
|
|
36
|
+
return Cmp.GT;
|
|
37
|
+
case Cmp.GT:
|
|
38
|
+
return Cmp.LT;
|
|
39
|
+
case Cmp.EQ:
|
|
40
|
+
return Cmp.EQ;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
46
|
+
* has been sorted, find the lowest index at which the value could be inserted
|
|
47
|
+
* while maintaining sort order.
|
|
48
|
+
*/
|
|
49
|
+
export function bisectLow(cmp, array, key) {
|
|
50
|
+
let low = 0, high = array.length;
|
|
51
|
+
while (low < high) {
|
|
52
|
+
const mid = Math.floor((low + high) / 2);
|
|
53
|
+
if (cmp(key, array[mid]) === Cmp.GT) {
|
|
54
|
+
low = mid + 1;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
high = mid;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return low;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
64
|
+
* has been sorted, find the highest index at which the value could be inserted
|
|
65
|
+
* while maintaining sort order.
|
|
66
|
+
*/
|
|
67
|
+
export function bisectHigh(cmp, array, key) {
|
|
68
|
+
let low = 0, high = array.length;
|
|
69
|
+
while (low < high) {
|
|
70
|
+
const mid = Math.floor((low + high) / 2);
|
|
71
|
+
if (cmp(key, array[mid]) !== Cmp.LT) {
|
|
72
|
+
low = mid + 1;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
high = mid;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return low;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
82
|
+
* has been sorted, find the index of the specific value if it exists in the
|
|
83
|
+
* array.
|
|
84
|
+
*/
|
|
85
|
+
export function find(cmp, array, key) {
|
|
86
|
+
let index = bisectLow(cmp, array, key);
|
|
87
|
+
while (index < array.length) {
|
|
88
|
+
if (Object.is(key, array[index])) {
|
|
89
|
+
return Some(index);
|
|
90
|
+
}
|
|
91
|
+
index++;
|
|
92
|
+
if (index >= array.length || cmp(key, array[index]) !== Cmp.EQ) {
|
|
93
|
+
return None;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return None;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=order.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order.js","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,IAAI,EAAe,IAAI,EAAE,MAAM,YAAY,CAAC;AAErD,MAAM,CAAN,IAAY,GAOX;AAPD,WAAY,GAAG;IACX,iBAAiB;IACjB,0BAAO,CAAA;IACP,gBAAgB;IAChB,yBAAM,CAAA;IACN,oBAAoB;IACpB,yBAAM,CAAA;AACV,CAAC,EAPW,GAAG,KAAH,GAAG,QAOd;AAcD;;;GAGG;AACH,MAAM,UAAU,SAAS,CAA+B,IAAO,EAAE,KAAQ;IACrE,OAAO,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import * as Order from "./order";
|
|
3
|
+
import "@bodil/opt-vitest";
|
|
4
|
+
const bisectLowNum = (array, key) => Order.bisectLow(Order.ascending, array, key);
|
|
5
|
+
const bisectHighNum = (array, key) => Order.bisectHigh(Order.ascending, array, key);
|
|
6
|
+
test("bisectLow", () => {
|
|
7
|
+
const list = [2, 4, 6, 8, 10];
|
|
8
|
+
expect(bisectLowNum(list, 1)).toEqual(0);
|
|
9
|
+
expect(bisectLowNum(list, 5)).toEqual(2);
|
|
10
|
+
expect(bisectLowNum(list, 6)).toEqual(2);
|
|
11
|
+
expect(bisectLowNum(list, 7)).toEqual(3);
|
|
12
|
+
expect(bisectLowNum(list, 12)).toEqual(5);
|
|
13
|
+
expect(bisectLowNum([], 1)).toEqual(0);
|
|
14
|
+
});
|
|
15
|
+
test("bisectLow dupes", () => {
|
|
16
|
+
const list = [1, 2, 3, 3, 3, 3, 4, 5];
|
|
17
|
+
expect(bisectLowNum(list, 0)).toEqual(0);
|
|
18
|
+
expect(bisectLowNum(list, 1)).toEqual(0);
|
|
19
|
+
expect(bisectLowNum(list, 5)).toEqual(7);
|
|
20
|
+
expect(bisectLowNum(list, 6)).toEqual(8);
|
|
21
|
+
// Should always return the dupe with the lowest index.
|
|
22
|
+
expect(bisectLowNum(list, 3)).toEqual(2);
|
|
23
|
+
});
|
|
24
|
+
test("bisectHigh", () => {
|
|
25
|
+
const list = [2, 4, 6, 8, 10];
|
|
26
|
+
expect(bisectHighNum(list, 1)).toEqual(0);
|
|
27
|
+
expect(bisectHighNum(list, 5)).toEqual(2);
|
|
28
|
+
expect(bisectHighNum(list, 6)).toEqual(3);
|
|
29
|
+
expect(bisectHighNum(list, 7)).toEqual(3);
|
|
30
|
+
expect(bisectHighNum(list, 12)).toEqual(5);
|
|
31
|
+
expect(bisectHighNum([], 1)).toEqual(0);
|
|
32
|
+
});
|
|
33
|
+
test("bisectHigh dupes", () => {
|
|
34
|
+
const list = [1, 2, 3, 3, 3, 3, 4, 5];
|
|
35
|
+
expect(bisectHighNum(list, 0)).toEqual(0);
|
|
36
|
+
expect(bisectHighNum(list, 1)).toEqual(1);
|
|
37
|
+
expect(bisectHighNum(list, 5)).toEqual(8);
|
|
38
|
+
expect(bisectHighNum(list, 6)).toEqual(8);
|
|
39
|
+
// Should always return the index after the last dupe.
|
|
40
|
+
expect(bisectHighNum(list, 3)).toEqual(6);
|
|
41
|
+
});
|
|
42
|
+
test("find", () => {
|
|
43
|
+
const e0 = { foo: 0 };
|
|
44
|
+
const e1 = { foo: 1 };
|
|
45
|
+
const e2 = { foo: 1 };
|
|
46
|
+
const e3 = { foo: 2 };
|
|
47
|
+
const eCmp = (a, b) => Order.ascending(a.foo, b.foo);
|
|
48
|
+
const findE = (array, key) => Order.find(eCmp, array, key);
|
|
49
|
+
const list = [e0, e1, e2, e3];
|
|
50
|
+
// Entries not in the list
|
|
51
|
+
expect(findE(list, { foo: 5 })).isNone();
|
|
52
|
+
expect(findE(list, { foo: -1 })).isNone();
|
|
53
|
+
// Entries in the list
|
|
54
|
+
expect(findE(list, e0)).isSome(0);
|
|
55
|
+
expect(findE(list, e1)).isSome(1);
|
|
56
|
+
expect(findE(list, e2)).isSome(2);
|
|
57
|
+
expect(findE(list, e3)).isSome(3);
|
|
58
|
+
// Entries comparing to entries in the list
|
|
59
|
+
expect(findE(list, { foo: 0 })).isNone();
|
|
60
|
+
expect(findE(list, { foo: 1 })).isNone();
|
|
61
|
+
expect(findE(list, { foo: 2 })).isNone();
|
|
62
|
+
});
|
|
63
|
+
//# sourceMappingURL=order.test.js.map
|