@bodil/core 0.1.0 → 0.1.2
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 +2 -2
- package/dist/assert.js +2 -0
- package/dist/assert.js.map +1 -1
- package/package.json +3 -2
- package/src/assert.ts +117 -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.d.ts
CHANGED
|
@@ -45,11 +45,11 @@ export declare function present<A>(value: A): NonNullable<A>;
|
|
|
45
45
|
* Tests whether a value is null, undefined, false, an empty array or an empty
|
|
46
46
|
* string.
|
|
47
47
|
*/
|
|
48
|
-
export declare function isEmpty(value: unknown): value is null | undefined | "" | [] | false;
|
|
48
|
+
export declare function isEmpty(value: unknown): value is null | undefined | void | "" | [] | false;
|
|
49
49
|
/**
|
|
50
50
|
* Tests whether a value is null or undefined.
|
|
51
51
|
*/
|
|
52
|
-
export declare function isNullish(value: unknown): value is null | undefined;
|
|
52
|
+
export declare function isNullish(value: unknown): value is null | undefined | void;
|
|
53
53
|
/**
|
|
54
54
|
* Tests whether a value is iterable.
|
|
55
55
|
*/
|
package/dist/assert.js
CHANGED
|
@@ -63,6 +63,7 @@ export function present(value) {
|
|
|
63
63
|
* Tests whether a value is null, undefined, false, an empty array or an empty
|
|
64
64
|
* string.
|
|
65
65
|
*/
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
66
67
|
export function isEmpty(value) {
|
|
67
68
|
return (value === null ||
|
|
68
69
|
value === undefined ||
|
|
@@ -73,6 +74,7 @@ export function isEmpty(value) {
|
|
|
73
74
|
/**
|
|
74
75
|
* Tests whether a value is null or undefined.
|
|
75
76
|
*/
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
76
78
|
export function isNullish(value) {
|
|
77
79
|
return value === null || value === undefined;
|
|
78
80
|
}
|
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,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,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AI;QACd,OAAQ,KAAgC,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,UAAU,CAChF,CAAC;AACN,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bodil/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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
|
".": {
|
package/src/assert.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
79
|
+
export function isEmpty(value: unknown): value is null | undefined | void | "" | [] | false {
|
|
80
|
+
return (
|
|
81
|
+
value === null ||
|
|
82
|
+
value === undefined ||
|
|
83
|
+
value === false ||
|
|
84
|
+
(typeof value === "string" && value.length === 0) ||
|
|
85
|
+
(Array.isArray(value) && value.length === 0)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Tests whether a value is null or undefined.
|
|
91
|
+
*/
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
93
|
+
export function isNullish(value: unknown): value is null | undefined | void {
|
|
94
|
+
return value === null || value === undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Tests whether a value is iterable.
|
|
99
|
+
*/
|
|
100
|
+
export function isIterable(value: unknown): value is Iterable<unknown> {
|
|
101
|
+
return (
|
|
102
|
+
typeof value === "object" &&
|
|
103
|
+
value !== null &&
|
|
104
|
+
typeof (value as Iterable<unknown>)[Symbol.iterator] === "function"
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Tests whether a value is async iterable.
|
|
110
|
+
*/
|
|
111
|
+
export function isAsyncIterable(value: unknown): value is AsyncIterable<unknown> {
|
|
112
|
+
return (
|
|
113
|
+
typeof value === "object" &&
|
|
114
|
+
value !== null &&
|
|
115
|
+
typeof (value as AsyncIterable<unknown>)[Symbol.asyncIterator] === "function"
|
|
116
|
+
);
|
|
117
|
+
}
|
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
|
+
}
|