@bodil/core 0.0.3 → 0.1.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.js +2 -3
- package/dist/assert.js.map +1 -1
- package/dist/index.d.ts +0 -130
- package/dist/index.js +0 -170
- package/dist/index.js.map +1 -1
- package/dist/order.test.js +3 -4
- package/dist/order.test.js.map +1 -1
- package/package.json +10 -9
- package/src/assert.ts +115 -0
- package/src/async.ts +41 -0
- package/src/date.ts +145 -0
- package/src/event.ts +51 -0
- package/src/fun.ts +6 -0
- package/src/index.ts +11 -0
- package/src/order.test.ts +84 -0
- package/src/order.ts +108 -0
- package/src/types.ts +6 -0
package/dist/assert.js
CHANGED
|
@@ -7,7 +7,6 @@ export class AssertionError extends Error {
|
|
|
7
7
|
* bundle.
|
|
8
8
|
*/
|
|
9
9
|
export function assert(assertion, message = "runtime assertion failed") {
|
|
10
|
-
// deno-lint-ignore no-explicit-any
|
|
11
10
|
if (import.meta.env === undefined ||
|
|
12
11
|
import.meta.env.DEV) {
|
|
13
12
|
if (assertion === false) {
|
|
@@ -27,7 +26,7 @@ export function assertAlways(assertion, message) {
|
|
|
27
26
|
* Throws an exception used to mark unreachable code.
|
|
28
27
|
*/
|
|
29
28
|
export function unreachable() {
|
|
30
|
-
throw new Error("Congratulations! You have reached a part of the code thought to be unreachable.");
|
|
29
|
+
throw new Error("Congratulations! You have reached a part of the code previously thought to be unreachable.");
|
|
31
30
|
}
|
|
32
31
|
/**
|
|
33
32
|
* Assert that a type is `never` when type checking. Throws an
|
|
@@ -57,7 +56,7 @@ export function assertNever(_value) {
|
|
|
57
56
|
* Throws an exception otherwise.
|
|
58
57
|
*/
|
|
59
58
|
export function present(value) {
|
|
60
|
-
|
|
59
|
+
assertAlways(value !== null && value !== undefined);
|
|
61
60
|
return value;
|
|
62
61
|
}
|
|
63
62
|
/**
|
package/dist/assert.js.map
CHANGED
|
@@ -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,
|
|
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;AAED;;GAEG;AACH,MAAM,UAAU,WAAW;IACvB,MAAM,IAAI,KAAK,CACX,4FAA4F,CAC/F,CAAC;AACN,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,WAAW,CAAkB,MAAS;IAClD,OAAO,WAAW,EAAE,CAAC;AACzB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAI,KAAQ;IAC/B,YAAY,CAAC,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC;IACpD,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,KAAc;IAClC,OAAO,CACH,KAAK,KAAK,IAAI;QACd,KAAK,KAAK,SAAS;QACnB,KAAK,KAAK,KAAK;QACf,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC;QACjD,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAC/C,CAAC;AACN,CAAC;AAED;;GAEG;AACH,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/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { type OrderFn } from "./order";
|
|
2
|
-
import type { Present } from "./types";
|
|
3
1
|
import * as Assert from "./assert";
|
|
4
2
|
import * as Async from "./async";
|
|
5
3
|
import * as Date from "./date";
|
|
@@ -10,131 +8,3 @@ import * as Types from "./types";
|
|
|
10
8
|
export { Assert, Async, Date, Event, Fun, Order, Types };
|
|
11
9
|
import { None, Some, Option, Ok, Err, Result } from "@bodil/opt";
|
|
12
10
|
export { None, Some, Option, Ok, Err, Result };
|
|
13
|
-
declare global {
|
|
14
|
-
interface ObjectConstructor {
|
|
15
|
-
/**
|
|
16
|
-
* Checked key lookup. Returns {@link None} if a key isn't present.
|
|
17
|
-
* Returns a {@link Some} if the key is present, even if the value is
|
|
18
|
-
* `undefined`.
|
|
19
|
-
*/
|
|
20
|
-
get<O extends object, K extends keyof O>(obj: O, key: K): Option<O[K]>;
|
|
21
|
-
/**
|
|
22
|
-
* Create a new object by mapping an existing object's `Object.entries`
|
|
23
|
-
* through a function.
|
|
24
|
-
*/
|
|
25
|
-
mapEntries<A, B>(obj: Record<string, A>, fn: (entry: [key: string, value: A]) => [string, B]): Record<string, B>;
|
|
26
|
-
}
|
|
27
|
-
interface Map<K, V> {
|
|
28
|
-
/**
|
|
29
|
-
* If `key` exists in the map, return the value associated with it.
|
|
30
|
-
* Otherwise, call `value()` to create a new value, insert that into the
|
|
31
|
-
* map under `key`, and return it.
|
|
32
|
-
*/
|
|
33
|
-
getOrSet(key: K, defaultValue: () => V): V;
|
|
34
|
-
}
|
|
35
|
-
interface Array<T> {
|
|
36
|
-
/**
|
|
37
|
-
* Insert `value` at `index` in the array.
|
|
38
|
-
*/
|
|
39
|
-
insert(value: T, index: number): Array<T>;
|
|
40
|
-
/**
|
|
41
|
-
* Insert `value` into the array at the correct position according to
|
|
42
|
-
* the ordering function `cmp`. The array must already be ordered
|
|
43
|
-
* accordingly.
|
|
44
|
-
*
|
|
45
|
-
* If `duplicates` is `"low"`, the correct position in case of
|
|
46
|
-
* duplicates is considered to be the index of the first occurrence of a
|
|
47
|
-
* duplicate. If it is `"high"`, which is the default, the correct
|
|
48
|
-
* position is considered to be the index after the last occurrence of a
|
|
49
|
-
* duplicate.
|
|
50
|
-
*/
|
|
51
|
-
insertOrdered(value: T, cmp: OrderFn<T>, duplicates?: "low" | "high"): Array<T>;
|
|
52
|
-
/**
|
|
53
|
-
* Remove the first occurrence of a given value from an array.
|
|
54
|
-
*
|
|
55
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
56
|
-
* no matches were found.
|
|
57
|
-
*/
|
|
58
|
-
remove(value: T): Option<T>;
|
|
59
|
-
/**
|
|
60
|
-
* Remove the first value for which the provided predicate function
|
|
61
|
-
* returns `true` from the array.
|
|
62
|
-
*
|
|
63
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
64
|
-
* no matches were found.
|
|
65
|
-
*/
|
|
66
|
-
removeFn(predicate: (value: T, index: number, obj: Array<T>) => boolean): Option<T>;
|
|
67
|
-
/**
|
|
68
|
-
* Remove the value at the given index from the array.
|
|
69
|
-
*
|
|
70
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
71
|
-
* the index was out of bounds.
|
|
72
|
-
*/
|
|
73
|
-
removeIndex(index: number): Option<T>;
|
|
74
|
-
/**
|
|
75
|
-
* Return a copy of the array with any `undefined` values removed.
|
|
76
|
-
*/
|
|
77
|
-
present(): Array<Present<T>>;
|
|
78
|
-
/**
|
|
79
|
-
* Return a copy of the array with any `null` or `undefined` values removed.
|
|
80
|
-
*/
|
|
81
|
-
nonNullable(): Array<NonNullable<T>>;
|
|
82
|
-
/**
|
|
83
|
-
* Checked index operator. Returns {@link None} if an index isn't
|
|
84
|
-
* present, even for sparse arrays. Returns a {@link Some} if the index
|
|
85
|
-
* is present, even if the value is `undefined`.
|
|
86
|
-
*/
|
|
87
|
-
get(index: number): Option<T>;
|
|
88
|
-
}
|
|
89
|
-
interface IteratorConstructor {
|
|
90
|
-
/**
|
|
91
|
-
* Construct an empty iterator.
|
|
92
|
-
*/
|
|
93
|
-
empty<T>(): IteratorObject<T>;
|
|
94
|
-
}
|
|
95
|
-
interface IteratorObject<T, TReturn, TNext> {
|
|
96
|
-
/**
|
|
97
|
-
* Get the next value from the iterator, wrapped in an {@link Option}
|
|
98
|
-
* type. If the iterator has no more values, return {@link None}.
|
|
99
|
-
*
|
|
100
|
-
* This is the nicely utility typed version of {@link Iterator#next}.
|
|
101
|
-
*/
|
|
102
|
-
takeOne(): Option<T>;
|
|
103
|
-
/**
|
|
104
|
-
* Take items from the iterator only until the first time `predicate` returns false.
|
|
105
|
-
*/
|
|
106
|
-
takeWhile<S extends T>(predicate: (value: T, index: number) => value is S): IteratorObject<S, undefined>;
|
|
107
|
-
takeWhile(predicate: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
108
|
-
/**
|
|
109
|
-
* Ignore all items matching `predicate` until the first item that
|
|
110
|
-
* doesn't match.
|
|
111
|
-
*/
|
|
112
|
-
skipWhile(predicate: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
113
|
-
/**
|
|
114
|
-
* Iterate until calling `mapFn` on an item returns a value, then return
|
|
115
|
-
* that value. Return `undefined` if `mapFn` never returns a value.
|
|
116
|
-
*/
|
|
117
|
-
findMap<U>(mapFn: (value: T) => NonNullable<U> | undefined): NonNullable<U> | undefined;
|
|
118
|
-
/**
|
|
119
|
-
* Split the input up into arrays of at most `size` items.
|
|
120
|
-
*/
|
|
121
|
-
partition(size: number): IteratorObject<Array<T>, undefined>;
|
|
122
|
-
/**
|
|
123
|
-
* As {@link IteratorObject#partition}, but if the input length isn't
|
|
124
|
-
* divisible by `size`, ensure the *first* array is the shortest one
|
|
125
|
-
* rather than the last.
|
|
126
|
-
*
|
|
127
|
-
* Be warned that this requires the entire input iterable to be read up
|
|
128
|
-
* front to decide its length, so this function is not lazy.
|
|
129
|
-
*/
|
|
130
|
-
frontBiasedPartition(size: number): Array<Array<T>>;
|
|
131
|
-
/**
|
|
132
|
-
* Filter out any items which are `undefined`.
|
|
133
|
-
*/
|
|
134
|
-
present(): IteratorObject<Present<T>, undefined>;
|
|
135
|
-
/**
|
|
136
|
-
* Filter out any items which are `null` or `undefined`.
|
|
137
|
-
*/
|
|
138
|
-
nonNullable(): IteratorObject<NonNullable<T>, undefined>;
|
|
139
|
-
}
|
|
140
|
-
}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
2
|
-
// deno-lint-ignore-file no-this-alias
|
|
3
|
-
import { bisectHigh, bisectLow } from "./order";
|
|
4
|
-
import { assert } from "./assert";
|
|
5
1
|
import * as Assert from "./assert";
|
|
6
2
|
import * as Async from "./async";
|
|
7
3
|
import * as Date from "./date";
|
|
@@ -12,170 +8,4 @@ import * as Types from "./types";
|
|
|
12
8
|
export { Assert, Async, Date, Event, Fun, Order, Types };
|
|
13
9
|
import { None, Some, Option, Ok, Err, Result } from "@bodil/opt";
|
|
14
10
|
export { None, Some, Option, Ok, Err, Result };
|
|
15
|
-
// Object
|
|
16
|
-
Object.get = function get(obj, key) {
|
|
17
|
-
return Object.hasOwn(obj, key) ? Some(obj[key]) : None;
|
|
18
|
-
};
|
|
19
|
-
Object.mapEntries = function mapEntries(obj, fn) {
|
|
20
|
-
return Object.fromEntries(Object.entries(obj).map(fn));
|
|
21
|
-
};
|
|
22
|
-
// Map
|
|
23
|
-
Map.prototype.getOrSet = function getOrSet(key, defaultValue) {
|
|
24
|
-
const result = this.get(key);
|
|
25
|
-
if (result !== undefined) {
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
const newValue = defaultValue();
|
|
29
|
-
this.set(key, newValue);
|
|
30
|
-
return newValue;
|
|
31
|
-
};
|
|
32
|
-
// Array
|
|
33
|
-
Array.prototype.remove = function remove(value) {
|
|
34
|
-
return this.removeIndex(this.indexOf(value));
|
|
35
|
-
};
|
|
36
|
-
Array.prototype.removeFn = function remove(predicate) {
|
|
37
|
-
return this.removeIndex(this.findIndex(predicate));
|
|
38
|
-
};
|
|
39
|
-
Array.prototype.removeIndex = function remove(index) {
|
|
40
|
-
if (!Number.isInteger(index)) {
|
|
41
|
-
throw new TypeError(`Array.removeIndex(): index ${index} is not an integer`);
|
|
42
|
-
}
|
|
43
|
-
if (index < 0 || index >= this.length) {
|
|
44
|
-
return None;
|
|
45
|
-
}
|
|
46
|
-
const removed = this.splice(index, 1).pop();
|
|
47
|
-
return Option.from(removed);
|
|
48
|
-
};
|
|
49
|
-
Array.prototype.insert = function insert(value, index) {
|
|
50
|
-
if (!Number.isInteger(index)) {
|
|
51
|
-
throw new TypeError(`Array.insert(): index ${index} is not an integer`);
|
|
52
|
-
}
|
|
53
|
-
if (index > this.length || index < 0) {
|
|
54
|
-
throw new RangeError(`Array.insert: index ${index} out of bounds`);
|
|
55
|
-
}
|
|
56
|
-
this.splice(index, 0, value);
|
|
57
|
-
return this;
|
|
58
|
-
};
|
|
59
|
-
Array.prototype.insertOrdered = function insertOrdered(value, cmp, duplicates = "high") {
|
|
60
|
-
return this.insert(value, (duplicates === "low" ? bisectLow : bisectHigh)(cmp, this, value));
|
|
61
|
-
};
|
|
62
|
-
Array.prototype.present = function present() {
|
|
63
|
-
return this.filter((value) => value !== undefined);
|
|
64
|
-
};
|
|
65
|
-
Array.prototype.nonNullable = function nonNullable() {
|
|
66
|
-
return this.filter((value) => value !== undefined);
|
|
67
|
-
};
|
|
68
|
-
Array.prototype.get = function get(index) {
|
|
69
|
-
return Object.get(this, index);
|
|
70
|
-
};
|
|
71
|
-
// Iterator
|
|
72
|
-
Iterator.empty = function empty() {
|
|
73
|
-
return Iterator.from([]);
|
|
74
|
-
};
|
|
75
|
-
Iterator.prototype.takeOne = function takeOne() {
|
|
76
|
-
const result = this.next();
|
|
77
|
-
return result.done === true ? None : Some(result.value);
|
|
78
|
-
};
|
|
79
|
-
Iterator.prototype.takeWhile = function takeWhile(predicate) {
|
|
80
|
-
const iter = this;
|
|
81
|
-
let index = 0;
|
|
82
|
-
let done = false;
|
|
83
|
-
return Iterator.from({
|
|
84
|
-
next() {
|
|
85
|
-
if (!done) {
|
|
86
|
-
const result = iter.next();
|
|
87
|
-
if (result.done === true) {
|
|
88
|
-
return result;
|
|
89
|
-
}
|
|
90
|
-
if (predicate(result.value, index++)) {
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
done = true;
|
|
95
|
-
return { done: true, value: undefined };
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
Iterator.prototype.skipWhile = function skipWhile(predicate) {
|
|
100
|
-
const iter = this;
|
|
101
|
-
let index = 0;
|
|
102
|
-
let skipped = false;
|
|
103
|
-
return Iterator.from({
|
|
104
|
-
next() {
|
|
105
|
-
if (skipped) {
|
|
106
|
-
return iter.next();
|
|
107
|
-
}
|
|
108
|
-
while (true) {
|
|
109
|
-
const result = iter.next();
|
|
110
|
-
if (result.done === true) {
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
if (!predicate(result.value, index++)) {
|
|
114
|
-
skipped = true;
|
|
115
|
-
return result;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
};
|
|
121
|
-
Iterator.prototype.findMap = function findMap(mapFn) {
|
|
122
|
-
let index = 0;
|
|
123
|
-
while (true) {
|
|
124
|
-
const result = this.next();
|
|
125
|
-
if (result.done === true) {
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
const mapped = mapFn(result.value, index++);
|
|
129
|
-
if (mapped !== undefined) {
|
|
130
|
-
return mapped;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
Iterator.prototype.partition = function partition(size) {
|
|
135
|
-
const iter = this;
|
|
136
|
-
let done = false;
|
|
137
|
-
return Iterator.from({
|
|
138
|
-
next() {
|
|
139
|
-
if (done) {
|
|
140
|
-
return { done: true, value: undefined };
|
|
141
|
-
}
|
|
142
|
-
const part = [];
|
|
143
|
-
while (true) {
|
|
144
|
-
const result = iter.next();
|
|
145
|
-
if (result.done === true) {
|
|
146
|
-
done = true;
|
|
147
|
-
return part.length > 0
|
|
148
|
-
? { done: false, value: part }
|
|
149
|
-
: { done: true, value: undefined };
|
|
150
|
-
}
|
|
151
|
-
part.push(result.value);
|
|
152
|
-
if (part.length === size) {
|
|
153
|
-
return { done: false, value: part };
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
},
|
|
157
|
-
});
|
|
158
|
-
};
|
|
159
|
-
Iterator.prototype.frontBiasedPartition = function frontBiasedPartition(size) {
|
|
160
|
-
const input = this.toArray();
|
|
161
|
-
let inputPos = 0;
|
|
162
|
-
const firstLength = input.length % size;
|
|
163
|
-
const output = [];
|
|
164
|
-
if (firstLength > 0) {
|
|
165
|
-
output.push(input.slice(0, firstLength));
|
|
166
|
-
inputPos = firstLength;
|
|
167
|
-
}
|
|
168
|
-
while (inputPos < input.length) {
|
|
169
|
-
output.push(input.slice(inputPos, inputPos + size));
|
|
170
|
-
inputPos += size;
|
|
171
|
-
}
|
|
172
|
-
assert(inputPos === input.length);
|
|
173
|
-
return output;
|
|
174
|
-
};
|
|
175
|
-
Iterator.prototype.present = function present() {
|
|
176
|
-
return Iterator.from(this.filter((item) => item !== undefined));
|
|
177
|
-
};
|
|
178
|
-
Iterator.prototype.nonNullable = function nonNullable() {
|
|
179
|
-
return Iterator.from(this.filter((item) => item !== undefined && item !== null));
|
|
180
|
-
};
|
|
181
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,
|
|
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"}
|
package/dist/order.test.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { test, expect } from "vitest";
|
|
2
2
|
import * as Order from "./order";
|
|
3
3
|
import "@bodil/opt-vitest";
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const bisectHighNum = (array, key) => Order.bisectHigh(cmp, array, key);
|
|
4
|
+
const bisectLowNum = (array, key) => Order.bisectLow(Order.ascending, array, key);
|
|
5
|
+
const bisectHighNum = (array, key) => Order.bisectHigh(Order.ascending, array, key);
|
|
7
6
|
test("bisectLow", () => {
|
|
8
7
|
const list = [2, 4, 6, 8, 10];
|
|
9
8
|
expect(bisectLowNum(list, 1)).toEqual(0);
|
|
@@ -45,7 +44,7 @@ test("find", () => {
|
|
|
45
44
|
const e1 = { foo: 1 };
|
|
46
45
|
const e2 = { foo: 1 };
|
|
47
46
|
const e3 = { foo: 2 };
|
|
48
|
-
const eCmp = (a, b) =>
|
|
47
|
+
const eCmp = (a, b) => Order.ascending(a.foo, b.foo);
|
|
49
48
|
const findE = (array, key) => Order.find(eCmp, array, key);
|
|
50
49
|
const list = [e0, e1, e2, e3];
|
|
51
50
|
// Entries not in the list
|
package/dist/order.test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order.test.js","sourceRoot":"","sources":["../src/order.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,OAAO,mBAAmB,CAAC;AAE3B,MAAM,
|
|
1
|
+
{"version":3,"file":"order.test.js","sourceRoot":"","sources":["../src/order.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,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C7C,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bodil/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "My TypeScript standard library",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"module": "dist/index.js",
|
|
11
11
|
"types": "dist/index.d.ts",
|
|
12
12
|
"files": [
|
|
13
|
-
"dist"
|
|
13
|
+
"dist",
|
|
14
|
+
"src"
|
|
14
15
|
],
|
|
15
16
|
"exports": {
|
|
16
17
|
".": {
|
|
@@ -71,15 +72,15 @@
|
|
|
71
72
|
"devDependencies": {
|
|
72
73
|
"@bodil/opt-vitest": "^1.0.0",
|
|
73
74
|
"@eslint/eslintrc": "^3.2.0",
|
|
74
|
-
"@eslint/js": "^9.
|
|
75
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
76
|
-
"@typescript-eslint/parser": "^8.
|
|
77
|
-
"eslint": "^9.
|
|
78
|
-
"eslint-config-prettier": "^
|
|
79
|
-
"eslint-plugin-jsdoc": "^50.6.
|
|
75
|
+
"@eslint/js": "^9.18.0",
|
|
76
|
+
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
77
|
+
"@typescript-eslint/parser": "^8.21.0",
|
|
78
|
+
"eslint": "^9.18.0",
|
|
79
|
+
"eslint-config-prettier": "^10.0.1",
|
|
80
|
+
"eslint-plugin-jsdoc": "^50.6.2",
|
|
80
81
|
"globals": "^15.14.0",
|
|
81
82
|
"typescript": "^5.7.3",
|
|
82
|
-
"vitest": "^
|
|
83
|
+
"vitest": "^3.0.2"
|
|
83
84
|
},
|
|
84
85
|
"scripts": {
|
|
85
86
|
"build": "tsc",
|
package/src/assert.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
export class AssertionError extends Error {}
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that a condition is true. Throws an exception if it's not.
|
|
5
|
+
*
|
|
6
|
+
* When using Vite, this function generates no code when building a production
|
|
7
|
+
* bundle.
|
|
8
|
+
*/
|
|
9
|
+
export function assert(
|
|
10
|
+
assertion: unknown,
|
|
11
|
+
message: string = "runtime assertion failed"
|
|
12
|
+
): asserts assertion {
|
|
13
|
+
if (
|
|
14
|
+
(import.meta as unknown as { env: unknown }).env === undefined ||
|
|
15
|
+
(import.meta as unknown as { env: { DEV: boolean } }).env.DEV
|
|
16
|
+
) {
|
|
17
|
+
if (assertion === false) {
|
|
18
|
+
throw new AssertionError(message);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* As {@link assert}, but isn't skipped in production.
|
|
25
|
+
*/
|
|
26
|
+
export function assertAlways(assertion: unknown, message?: string): asserts assertion {
|
|
27
|
+
if (assertion === false) {
|
|
28
|
+
throw new Error(message ?? "runtime assertion failed");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Throws an exception used to mark unreachable code.
|
|
34
|
+
*/
|
|
35
|
+
export function unreachable(): never {
|
|
36
|
+
throw new Error(
|
|
37
|
+
"Congratulations! You have reached a part of the code previously thought to be unreachable."
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Assert that a type is `never` when type checking. Throws an
|
|
43
|
+
* {@link unreachable} exception if it's reached at runtime. This is meant as an
|
|
44
|
+
* exhaustiveness check.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* type State = "on" | "off";
|
|
48
|
+
* function checkState(state: State) {
|
|
49
|
+
* if (state === "on") {
|
|
50
|
+
* console.log("it's on");
|
|
51
|
+
* } else if (state === "off") {
|
|
52
|
+
* console.log("it's off");
|
|
53
|
+
* } else {
|
|
54
|
+
* // this would fail to type check if you
|
|
55
|
+
* // haven't tested all valid `State` values
|
|
56
|
+
* // in the above if statement:
|
|
57
|
+
* assertNever<typeof state>();
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
*/
|
|
61
|
+
export function assertNever<T extends never>(_value: T): T {
|
|
62
|
+
return unreachable();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Returns the given value unchanged if it's neither null nor undefined.
|
|
67
|
+
* Throws an exception otherwise.
|
|
68
|
+
*/
|
|
69
|
+
export function present<A>(value: A): NonNullable<A> {
|
|
70
|
+
assertAlways(value !== null && value !== undefined);
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Tests whether a value is null, undefined, false, an empty array or an empty
|
|
76
|
+
* string.
|
|
77
|
+
*/
|
|
78
|
+
export function isEmpty(value: unknown): value is null | undefined | "" | [] | false {
|
|
79
|
+
return (
|
|
80
|
+
value === null ||
|
|
81
|
+
value === undefined ||
|
|
82
|
+
value === false ||
|
|
83
|
+
(typeof value === "string" && value.length === 0) ||
|
|
84
|
+
(Array.isArray(value) && value.length === 0)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Tests whether a value is null or undefined.
|
|
90
|
+
*/
|
|
91
|
+
export function isNullish(value: unknown): value is null | undefined {
|
|
92
|
+
return value === null || value === undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Tests whether a value is iterable.
|
|
97
|
+
*/
|
|
98
|
+
export function isIterable(value: unknown): value is Iterable<unknown> {
|
|
99
|
+
return (
|
|
100
|
+
typeof value === "object" &&
|
|
101
|
+
value !== null &&
|
|
102
|
+
typeof (value as Iterable<unknown>)[Symbol.iterator] === "function"
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Tests whether a value is async iterable.
|
|
108
|
+
*/
|
|
109
|
+
export function isAsyncIterable(value: unknown): value is AsyncIterable<unknown> {
|
|
110
|
+
return (
|
|
111
|
+
typeof value === "object" &&
|
|
112
|
+
value !== null &&
|
|
113
|
+
typeof (value as AsyncIterable<unknown>)[Symbol.asyncIterator] === "function"
|
|
114
|
+
);
|
|
115
|
+
}
|
package/src/async.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return a promise that will resolve after the specified number of
|
|
3
|
+
* milliseconds.
|
|
4
|
+
*/
|
|
5
|
+
export function sleep(ms: number): Promise<void> {
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
setTimeout(resolve, ms);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface AbortableJob<A> extends Disposable {
|
|
12
|
+
result: Promise<A>;
|
|
13
|
+
abort: (message?: string) => void;
|
|
14
|
+
signal: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Build a promise that can be aborted using an `AbortSignal`.
|
|
19
|
+
*/
|
|
20
|
+
export function abortable<A>(
|
|
21
|
+
fn: (abort: AbortSignal) => Promise<A>,
|
|
22
|
+
abortController: AbortController = new AbortController()
|
|
23
|
+
): AbortableJob<A> {
|
|
24
|
+
return {
|
|
25
|
+
signal: abortController.signal,
|
|
26
|
+
result: fn(abortController.signal).then(
|
|
27
|
+
(result) => {
|
|
28
|
+
abortController.signal.throwIfAborted();
|
|
29
|
+
return result;
|
|
30
|
+
},
|
|
31
|
+
(error) => {
|
|
32
|
+
const aborted: boolean = abortController.signal.aborted;
|
|
33
|
+
return aborted ? abortController.signal.reason : error;
|
|
34
|
+
}
|
|
35
|
+
),
|
|
36
|
+
abort: (message = "promise was aborted"): void =>
|
|
37
|
+
abortController.abort(new DOMException(message, "AbortError")),
|
|
38
|
+
[Symbol.dispose]: () =>
|
|
39
|
+
abortController.abort(new DOMException("promise was disposed", "AbortError")),
|
|
40
|
+
};
|
|
41
|
+
}
|
package/src/date.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
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
|
+
}
|
package/src/event.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Present } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An interface a type can extend to associate a map of event names to event
|
|
5
|
+
* types with itself, in the style of `HTMLElementEventMap` et al. This allows
|
|
6
|
+
* the type system to discover the events available for a specific `EventTarget`
|
|
7
|
+
* descendant.
|
|
8
|
+
*
|
|
9
|
+
* If you need to implement this for a class you're defining, you need to add
|
|
10
|
+
* `__events!: MyEventMap` as a property on the class.
|
|
11
|
+
*/
|
|
12
|
+
export interface DeclareEvents<Events> extends EventTarget {
|
|
13
|
+
__events: Events;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the `Events` type associated with a type that extends
|
|
18
|
+
* `DeclareEvents<Events>`.
|
|
19
|
+
*
|
|
20
|
+
* Using it on a type that doesn't extend `DeclareEvents` results in a type
|
|
21
|
+
* error.
|
|
22
|
+
*/
|
|
23
|
+
export type EventMapFor<T extends DeclareEvents<unknown>> = Present<T["__events"]>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get the `Events` type associated with a type that extends
|
|
27
|
+
* `DeclareEvents<Events>`.
|
|
28
|
+
*
|
|
29
|
+
* A type that doesn't extend `DeclareEvents` resolves to an object which will
|
|
30
|
+
* accept all string keys.
|
|
31
|
+
*/
|
|
32
|
+
export type ExtractEventMap<T> = T extends DeclareEvents<infer Events>
|
|
33
|
+
? Events
|
|
34
|
+
: { [key: string]: Event };
|
|
35
|
+
|
|
36
|
+
declare global {
|
|
37
|
+
interface AbortSignal extends DeclareEvents<AbortSignalEventMap> {}
|
|
38
|
+
interface Animation extends DeclareEvents<AnimationEventMap> {}
|
|
39
|
+
interface BroadcastChannel extends DeclareEvents<BroadcastChannelEventMap> {}
|
|
40
|
+
interface Document extends DeclareEvents<DocumentEventMap> {}
|
|
41
|
+
interface EventSource extends DeclareEvents<EventSourceEventMap> {}
|
|
42
|
+
interface HTMLBodyElement extends DeclareEvents<HTMLBodyElementEventMap> {}
|
|
43
|
+
interface HTMLElement extends DeclareEvents<HTMLElementEventMap> {}
|
|
44
|
+
interface MessagePort extends DeclareEvents<MessagePortEventMap> {}
|
|
45
|
+
interface ShadowRoot extends DeclareEvents<ShadowRootEventMap> {}
|
|
46
|
+
interface SVGElement extends DeclareEvents<SVGElementEventMap> {}
|
|
47
|
+
interface SVGSVGElement extends DeclareEvents<SVGSVGElementEventMap> {}
|
|
48
|
+
interface WebSocket extends DeclareEvents<WebSocketEventMap> {}
|
|
49
|
+
interface Window extends DeclareEvents<WindowEventMap> {}
|
|
50
|
+
interface Worker extends DeclareEvents<WorkerEventMap> {}
|
|
51
|
+
}
|
package/src/fun.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +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 };
|
|
9
|
+
|
|
10
|
+
import { None, Some, Option, Ok, Err, Result } from "@bodil/opt";
|
|
11
|
+
export { None, Some, Option, Ok, Err, Result };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import * as Order from "./order";
|
|
3
|
+
|
|
4
|
+
import "@bodil/opt-vitest";
|
|
5
|
+
|
|
6
|
+
const bisectLowNum = (array: Array<number>, key: number) =>
|
|
7
|
+
Order.bisectLow(Order.ascending, array, key);
|
|
8
|
+
const bisectHighNum = (array: Array<number>, key: number) =>
|
|
9
|
+
Order.bisectHigh(Order.ascending, array, key);
|
|
10
|
+
|
|
11
|
+
test("bisectLow", () => {
|
|
12
|
+
const list = [2, 4, 6, 8, 10];
|
|
13
|
+
|
|
14
|
+
expect(bisectLowNum(list, 1)).toEqual(0);
|
|
15
|
+
expect(bisectLowNum(list, 5)).toEqual(2);
|
|
16
|
+
expect(bisectLowNum(list, 6)).toEqual(2);
|
|
17
|
+
expect(bisectLowNum(list, 7)).toEqual(3);
|
|
18
|
+
expect(bisectLowNum(list, 12)).toEqual(5);
|
|
19
|
+
|
|
20
|
+
expect(bisectLowNum([], 1)).toEqual(0);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("bisectLow dupes", () => {
|
|
24
|
+
const list = [1, 2, 3, 3, 3, 3, 4, 5];
|
|
25
|
+
|
|
26
|
+
expect(bisectLowNum(list, 0)).toEqual(0);
|
|
27
|
+
expect(bisectLowNum(list, 1)).toEqual(0);
|
|
28
|
+
expect(bisectLowNum(list, 5)).toEqual(7);
|
|
29
|
+
expect(bisectLowNum(list, 6)).toEqual(8);
|
|
30
|
+
|
|
31
|
+
// Should always return the dupe with the lowest index.
|
|
32
|
+
expect(bisectLowNum(list, 3)).toEqual(2);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("bisectHigh", () => {
|
|
36
|
+
const list = [2, 4, 6, 8, 10];
|
|
37
|
+
|
|
38
|
+
expect(bisectHighNum(list, 1)).toEqual(0);
|
|
39
|
+
expect(bisectHighNum(list, 5)).toEqual(2);
|
|
40
|
+
expect(bisectHighNum(list, 6)).toEqual(3);
|
|
41
|
+
expect(bisectHighNum(list, 7)).toEqual(3);
|
|
42
|
+
expect(bisectHighNum(list, 12)).toEqual(5);
|
|
43
|
+
|
|
44
|
+
expect(bisectHighNum([], 1)).toEqual(0);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("bisectHigh dupes", () => {
|
|
48
|
+
const list = [1, 2, 3, 3, 3, 3, 4, 5];
|
|
49
|
+
|
|
50
|
+
expect(bisectHighNum(list, 0)).toEqual(0);
|
|
51
|
+
expect(bisectHighNum(list, 1)).toEqual(1);
|
|
52
|
+
expect(bisectHighNum(list, 5)).toEqual(8);
|
|
53
|
+
expect(bisectHighNum(list, 6)).toEqual(8);
|
|
54
|
+
|
|
55
|
+
// Should always return the index after the last dupe.
|
|
56
|
+
expect(bisectHighNum(list, 3)).toEqual(6);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("find", () => {
|
|
60
|
+
type FindTestData = { foo: number };
|
|
61
|
+
const e0 = { foo: 0 };
|
|
62
|
+
const e1 = { foo: 1 };
|
|
63
|
+
const e2 = { foo: 1 };
|
|
64
|
+
const e3 = { foo: 2 };
|
|
65
|
+
const eCmp = (a: FindTestData, b: FindTestData) => Order.ascending(a.foo, b.foo);
|
|
66
|
+
const findE = (array: Array<FindTestData>, key: FindTestData) => Order.find(eCmp, array, key);
|
|
67
|
+
|
|
68
|
+
const list: Array<FindTestData> = [e0, e1, e2, e3];
|
|
69
|
+
|
|
70
|
+
// Entries not in the list
|
|
71
|
+
expect(findE(list, { foo: 5 })).isNone();
|
|
72
|
+
expect(findE(list, { foo: -1 })).isNone();
|
|
73
|
+
|
|
74
|
+
// Entries in the list
|
|
75
|
+
expect(findE(list, e0)).isSome(0);
|
|
76
|
+
expect(findE(list, e1)).isSome(1);
|
|
77
|
+
expect(findE(list, e2)).isSome(2);
|
|
78
|
+
expect(findE(list, e3)).isSome(3);
|
|
79
|
+
|
|
80
|
+
// Entries comparing to entries in the list
|
|
81
|
+
expect(findE(list, { foo: 0 })).isNone();
|
|
82
|
+
expect(findE(list, { foo: 1 })).isNone();
|
|
83
|
+
expect(findE(list, { foo: 2 })).isNone();
|
|
84
|
+
});
|
package/src/order.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { None, type Option, Some } from "@bodil/opt";
|
|
2
|
+
|
|
3
|
+
export enum Cmp {
|
|
4
|
+
LT = -1,
|
|
5
|
+
EQ = 0,
|
|
6
|
+
GT = 1,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* An order function is a function which is used to compare two items, returning
|
|
11
|
+
* a {@link Cmp} to describe the first item in relation to the second, eg.
|
|
12
|
+
* {@link Cmp.GT} says the first item is greater than the second item.
|
|
13
|
+
*/
|
|
14
|
+
export type OrderFn<A> = (left: A, right: A) => Cmp;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The set of types which can be compared using the `<` and `>` operators.
|
|
18
|
+
*/
|
|
19
|
+
export type OperatorComparable = number | string | bigint | Date;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* An order function which sorts {@link OperatorComparable} items in ascending
|
|
23
|
+
* order.
|
|
24
|
+
*/
|
|
25
|
+
export function ascending<A extends OperatorComparable>(left: A, right: A): Cmp {
|
|
26
|
+
return left < right ? Cmp.LT : left === right ? Cmp.EQ : Cmp.GT;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An order function which sorts {@link OperatorComparable} items in descending
|
|
31
|
+
* order.
|
|
32
|
+
*/
|
|
33
|
+
export function descending<A extends OperatorComparable>(left: A, right: A): Cmp {
|
|
34
|
+
return left < right ? Cmp.GT : left === right ? Cmp.EQ : Cmp.LT;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Take an order function and invert its order.
|
|
39
|
+
*/
|
|
40
|
+
export function reverse<T>(fn: OrderFn<T>): OrderFn<T> {
|
|
41
|
+
return (left: T, right: T) => {
|
|
42
|
+
switch (fn(left, right)) {
|
|
43
|
+
case Cmp.LT:
|
|
44
|
+
return Cmp.GT;
|
|
45
|
+
case Cmp.GT:
|
|
46
|
+
return Cmp.LT;
|
|
47
|
+
case Cmp.EQ:
|
|
48
|
+
return Cmp.EQ;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
55
|
+
* has been sorted, find the lowest index at which the value could be inserted
|
|
56
|
+
* while maintaining sort order.
|
|
57
|
+
*/
|
|
58
|
+
export function bisectLow<A>(cmp: OrderFn<A>, array: Array<A>, key: A): number {
|
|
59
|
+
let low = 0,
|
|
60
|
+
high = array.length;
|
|
61
|
+
while (low < high) {
|
|
62
|
+
const mid: number = Math.floor((low + high) / 2);
|
|
63
|
+
if (cmp(key, array[mid]) === Cmp.GT) {
|
|
64
|
+
low = mid + 1;
|
|
65
|
+
} else {
|
|
66
|
+
high = mid;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return low;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
74
|
+
* has been sorted, find the highest index at which the value could be inserted
|
|
75
|
+
* while maintaining sort order.
|
|
76
|
+
*/
|
|
77
|
+
export function bisectHigh<A>(cmp: OrderFn<A>, array: Array<A>, key: A): number {
|
|
78
|
+
let low = 0,
|
|
79
|
+
high = array.length;
|
|
80
|
+
while (low < high) {
|
|
81
|
+
const mid: number = Math.floor((low + high) / 2);
|
|
82
|
+
if (cmp(key, array[mid]) !== Cmp.LT) {
|
|
83
|
+
low = mid + 1;
|
|
84
|
+
} else {
|
|
85
|
+
high = mid;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return low;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Given a value, a sorted array and a comparison function by which the array
|
|
93
|
+
* has been sorted, find the index of the specific value if it exists in the
|
|
94
|
+
* array.
|
|
95
|
+
*/
|
|
96
|
+
export function find<A>(cmp: OrderFn<A>, array: Array<A>, key: A): Option<number> {
|
|
97
|
+
let index = bisectLow(cmp, array, key);
|
|
98
|
+
while (index < array.length) {
|
|
99
|
+
if (Object.is(key, array[index])) {
|
|
100
|
+
return Some(index);
|
|
101
|
+
}
|
|
102
|
+
index++;
|
|
103
|
+
if (index >= array.length || cmp(key, array[index]) !== Cmp.EQ) {
|
|
104
|
+
return None;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return None;
|
|
108
|
+
}
|