@bodil/monkey-business 0.3.4 → 0.3.5
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/array.d.ts +59 -0
- package/dist/array.js +100 -0
- package/dist/array.js.map +1 -0
- package/dist/index.d.ts +35 -142
- package/dist/index.js +37 -185
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +25 -3
- package/dist/index.test.js.map +1 -1
- package/dist/iterator.d.ts +78 -0
- package/dist/iterator.js +176 -0
- package/dist/iterator.js.map +1 -0
- package/dist/map.d.ts +19 -0
- package/dist/map.js +31 -0
- package/dist/map.js.map +1 -0
- package/dist/object.d.ts +12 -0
- package/dist/object.js +17 -0
- package/dist/object.js.map +1 -0
- package/dist/weakmap.d.ts +19 -0
- package/dist/weakmap.js +31 -0
- package/dist/weakmap.js.map +1 -0
- package/package.json +63 -11
- package/src/array.ts +128 -0
- package/src/es-iterator-helpers.d.ts +24 -0
- package/src/index.test.ts +30 -7
- package/src/index.ts +77 -390
- package/src/iterator.ts +223 -0
- package/src/map.ts +37 -0
- package/src/object.ts +21 -0
- package/src/weakmap.ts +37 -0
package/dist/array.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type OrderFn } from "@bodil/core/order";
|
|
2
|
+
import type { Present } from "@bodil/core/types";
|
|
3
|
+
import { Option } from "@bodil/opt";
|
|
4
|
+
/**
|
|
5
|
+
* Insert `value` at `index` in the array.
|
|
6
|
+
*/
|
|
7
|
+
export declare function insert<T>(this: Array<T>, value: T, index: number): Array<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Insert `value` into the array at the correct position according to
|
|
10
|
+
* the ordering function `cmp`. The array must already be ordered
|
|
11
|
+
* accordingly.
|
|
12
|
+
*
|
|
13
|
+
* If `duplicates` is `"low"`, the correct position in case of
|
|
14
|
+
* duplicates is considered to be the index of the first occurrence of a
|
|
15
|
+
* duplicate. If it is `"high"`, which is the default, the correct
|
|
16
|
+
* position is considered to be the index after the last occurrence of a
|
|
17
|
+
* duplicate.
|
|
18
|
+
*/
|
|
19
|
+
export declare function insertOrdered<T>(this: Array<T>, value: T, cmp: OrderFn<T>, duplicates?: "low" | "high"): Array<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Remove the first occurrence of a given value from an array.
|
|
22
|
+
*
|
|
23
|
+
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
24
|
+
* no matches were found.
|
|
25
|
+
*/
|
|
26
|
+
export declare function remove<T>(this: Array<T>, value: T): Option<T>;
|
|
27
|
+
/**
|
|
28
|
+
* Remove the first value for which the provided predicate function
|
|
29
|
+
* returns `true` from the array.
|
|
30
|
+
*
|
|
31
|
+
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
32
|
+
* no matches were found.
|
|
33
|
+
*/
|
|
34
|
+
export declare function removeFn<T>(this: Array<T>, predicate: (value: T, index: number, obj: Array<T>) => boolean): Option<T>;
|
|
35
|
+
/**
|
|
36
|
+
* Remove the value at the given index from the array.
|
|
37
|
+
*
|
|
38
|
+
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
39
|
+
* the index was out of bounds.
|
|
40
|
+
*/
|
|
41
|
+
export declare function removeIndex<T>(this: Array<T>, index: number): Option<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Return a copy of the array with any `undefined` values removed.
|
|
44
|
+
*/
|
|
45
|
+
export declare function present<T>(this: Array<T>): Array<Present<T>>;
|
|
46
|
+
/**
|
|
47
|
+
* Return a copy of the array with any `null` or `undefined` values removed.
|
|
48
|
+
*/
|
|
49
|
+
export declare function nonNullable<T>(this: Array<T>): Array<NonNullable<T>>;
|
|
50
|
+
/**
|
|
51
|
+
* Checked index operator. Returns {@link None} if an index isn't
|
|
52
|
+
* present, even for sparse arrays. Returns a {@link Some} if the index
|
|
53
|
+
* is present, even if the value is `undefined`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function get<T>(this: Array<T>, index: number): Option<T>;
|
|
56
|
+
/**
|
|
57
|
+
* Test whether a value is an array with the same contents as this one.
|
|
58
|
+
*/
|
|
59
|
+
export declare function equals<T, U = unknown>(this: Array<T>, other: unknown, equalsFn?: (a: T, b: U) => boolean): other is Array<T>;
|
package/dist/array.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { bisectHigh, bisectLow } from "@bodil/core/order";
|
|
2
|
+
import { None, Option } from "@bodil/opt";
|
|
3
|
+
import { get as objectGet } from "./object";
|
|
4
|
+
/**
|
|
5
|
+
* Insert `value` at `index` in the array.
|
|
6
|
+
*/
|
|
7
|
+
export function insert(value, index) {
|
|
8
|
+
if (!Number.isInteger(index)) {
|
|
9
|
+
throw new TypeError(`Array.insert(): index ${index} is not an integer`);
|
|
10
|
+
}
|
|
11
|
+
if (index > this.length || index < 0) {
|
|
12
|
+
throw new RangeError(`Array.insert: index ${index} out of bounds`);
|
|
13
|
+
}
|
|
14
|
+
this.splice(index, 0, value);
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Insert `value` into the array at the correct position according to
|
|
19
|
+
* the ordering function `cmp`. The array must already be ordered
|
|
20
|
+
* accordingly.
|
|
21
|
+
*
|
|
22
|
+
* If `duplicates` is `"low"`, the correct position in case of
|
|
23
|
+
* duplicates is considered to be the index of the first occurrence of a
|
|
24
|
+
* duplicate. If it is `"high"`, which is the default, the correct
|
|
25
|
+
* position is considered to be the index after the last occurrence of a
|
|
26
|
+
* duplicate.
|
|
27
|
+
*/
|
|
28
|
+
export function insertOrdered(value, cmp, duplicates = "high") {
|
|
29
|
+
return (insert).call(this, value, (duplicates === "low" ? bisectLow : bisectHigh)(cmp, this, value));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Remove the first occurrence of a given value from an array.
|
|
33
|
+
*
|
|
34
|
+
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
35
|
+
* no matches were found.
|
|
36
|
+
*/
|
|
37
|
+
export function remove(value) {
|
|
38
|
+
return (removeIndex).call(this, this.indexOf(value));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Remove the first value for which the provided predicate function
|
|
42
|
+
* returns `true` from the array.
|
|
43
|
+
*
|
|
44
|
+
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
45
|
+
* no matches were found.
|
|
46
|
+
*/
|
|
47
|
+
export function removeFn(predicate) {
|
|
48
|
+
return (removeIndex).call(this, this.findIndex(predicate));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Remove the value at the given index from the array.
|
|
52
|
+
*
|
|
53
|
+
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
54
|
+
* the index was out of bounds.
|
|
55
|
+
*/
|
|
56
|
+
export function removeIndex(index) {
|
|
57
|
+
if (!Number.isInteger(index)) {
|
|
58
|
+
throw new TypeError(`Array.removeIndex(): index ${index} is not an integer`);
|
|
59
|
+
}
|
|
60
|
+
if (index < 0 || index >= this.length) {
|
|
61
|
+
return None;
|
|
62
|
+
}
|
|
63
|
+
const removed = this.splice(index, 1).pop();
|
|
64
|
+
return Option.from(removed);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Return a copy of the array with any `undefined` values removed.
|
|
68
|
+
*/
|
|
69
|
+
export function present() {
|
|
70
|
+
return this.filter((value) => value !== undefined);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Return a copy of the array with any `null` or `undefined` values removed.
|
|
74
|
+
*/
|
|
75
|
+
export function nonNullable() {
|
|
76
|
+
return this.filter((value) => value !== undefined && value !== null);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Checked index operator. Returns {@link None} if an index isn't
|
|
80
|
+
* present, even for sparse arrays. Returns a {@link Some} if the index
|
|
81
|
+
* is present, even if the value is `undefined`.
|
|
82
|
+
*/
|
|
83
|
+
export function get(index) {
|
|
84
|
+
return objectGet(this, index);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Test whether a value is an array with the same contents as this one.
|
|
88
|
+
*/
|
|
89
|
+
export function equals(other, equalsFn = Object.is) {
|
|
90
|
+
if (!Array.isArray(other) || this.length !== other.length) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
for (let index = 0; index < this.length; index++) {
|
|
94
|
+
if (!equalsFn(this[index], other[index])) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":"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"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,206 +1,99 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type OrderFn } from "@bodil/core/order";
|
|
1
|
+
import type { OrderFn } from "@bodil/core/order";
|
|
3
2
|
import type { Present } from "@bodil/core/types";
|
|
4
|
-
import "
|
|
5
|
-
import "es-iterator-helpers/Iterator.zip/auto";
|
|
6
|
-
import "es-iterator-helpers/Iterator.zipKeyed/auto";
|
|
7
|
-
import "es-iterator-helpers/Iterator.prototype.chunks/auto";
|
|
3
|
+
import type { Option } from "@bodil/opt";
|
|
8
4
|
declare global {
|
|
9
5
|
/**
|
|
10
6
|
* Added methods on the {@link Object} global object.
|
|
11
7
|
*/
|
|
12
8
|
interface ObjectConstructor {
|
|
13
|
-
/**
|
|
14
|
-
* Checked key lookup. Returns {@link None} if a key isn't present.
|
|
15
|
-
* Returns a {@link Some} if the key is present, even if the value is
|
|
16
|
-
* `undefined`.
|
|
17
|
-
*/
|
|
9
|
+
/** {@inheritDoc object.get} */
|
|
18
10
|
get<O extends object, K extends keyof O>(obj: O, key: K): Option<O[K]>;
|
|
19
|
-
/**
|
|
20
|
-
* Create a new object by mapping an existing object's `Object.entries`
|
|
21
|
-
* through a function.
|
|
22
|
-
*/
|
|
11
|
+
/** {@inheritDoc object.mapEntries} */
|
|
23
12
|
mapEntries<A, B>(obj: Record<string, A>, fn: (entry: [key: string, value: A]) => [string, B]): Record<string, B>;
|
|
24
13
|
}
|
|
25
14
|
/**
|
|
26
15
|
* Added methods on {@link Map}s.
|
|
27
16
|
*/
|
|
28
17
|
interface Map<K, V> {
|
|
29
|
-
/**
|
|
30
|
-
* If `key` exists in the map, return the value associated with it.
|
|
31
|
-
* Otherwise, call `defaultValue()` to create a new value, insert that
|
|
32
|
-
* into the map under `key`, and return it.
|
|
33
|
-
*/
|
|
18
|
+
/** {@inheritDoc map.getOrInsertComputed} */
|
|
34
19
|
getOrInsertComputed(key: K, defaultValue: (key: K) => V): V;
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
* @see {@link Map.delete}
|
|
40
|
-
*/
|
|
20
|
+
/** {@inheritDoc map.getOrSet} */
|
|
21
|
+
getOrSet(key: K, defaultValue: (key: K) => V): V;
|
|
22
|
+
/** {@inheritDoc map.remove} */
|
|
41
23
|
remove(key: K): Option<V>;
|
|
42
24
|
}
|
|
43
25
|
/**
|
|
44
26
|
* Added methods on {@link WeakMap}s.
|
|
45
27
|
*/
|
|
46
28
|
interface WeakMap<K extends WeakKey, V> {
|
|
47
|
-
/**
|
|
48
|
-
* If `key` exists in the map, return the value associated with it.
|
|
49
|
-
* Otherwise, call `defaultValue()` to create a new value, insert that
|
|
50
|
-
* into the map under `key`, and return it.
|
|
51
|
-
*/
|
|
29
|
+
/** {@inheritDoc weakmap.getOrInsertComputed} */
|
|
52
30
|
getOrInsertComputed(key: K, defaultValue: (key: K) => V): V;
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
*
|
|
57
|
-
* @see {@link WeakMap.delete}
|
|
58
|
-
*/
|
|
31
|
+
/** {@inheritDoc weakmap.getOrSet} */
|
|
32
|
+
getOrSet(key: K, defaultValue: (key: K) => V): V;
|
|
33
|
+
/** {@inheritDoc weakmap.remove} */
|
|
59
34
|
remove(key: K): Option<V>;
|
|
60
35
|
}
|
|
61
36
|
/**
|
|
62
37
|
* Added methods on {@link Array}s.
|
|
63
38
|
*/
|
|
64
39
|
interface Array<T> {
|
|
65
|
-
/**
|
|
66
|
-
* Insert `value` at `index` in the array.
|
|
67
|
-
*/
|
|
40
|
+
/** {@inheritDoc array.insert} */
|
|
68
41
|
insert(value: T, index: number): Array<T>;
|
|
69
|
-
/**
|
|
70
|
-
* Insert `value` into the array at the correct position according to
|
|
71
|
-
* the ordering function `cmp`. The array must already be ordered
|
|
72
|
-
* accordingly.
|
|
73
|
-
*
|
|
74
|
-
* If `duplicates` is `"low"`, the correct position in case of
|
|
75
|
-
* duplicates is considered to be the index of the first occurrence of a
|
|
76
|
-
* duplicate. If it is `"high"`, which is the default, the correct
|
|
77
|
-
* position is considered to be the index after the last occurrence of a
|
|
78
|
-
* duplicate.
|
|
79
|
-
*/
|
|
42
|
+
/** {@inheritDoc array.insertOrdered} */
|
|
80
43
|
insertOrdered(value: T, cmp: OrderFn<T>, duplicates?: "low" | "high"): Array<T>;
|
|
81
|
-
/**
|
|
82
|
-
* Remove the first occurrence of a given value from an array.
|
|
83
|
-
*
|
|
84
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
85
|
-
* no matches were found.
|
|
86
|
-
*/
|
|
44
|
+
/** {@inheritDoc array.remove} */
|
|
87
45
|
remove(value: T): Option<T>;
|
|
88
|
-
/**
|
|
89
|
-
* Remove the first value for which the provided predicate function
|
|
90
|
-
* returns `true` from the array.
|
|
91
|
-
*
|
|
92
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
93
|
-
* no matches were found.
|
|
94
|
-
*/
|
|
46
|
+
/** {@inheritDoc array.removeFn} */
|
|
95
47
|
removeFn(predicate: (value: T, index: number, obj: Array<T>) => boolean): Option<T>;
|
|
96
|
-
/**
|
|
97
|
-
* Remove the value at the given index from the array.
|
|
98
|
-
*
|
|
99
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
100
|
-
* the index was out of bounds.
|
|
101
|
-
*/
|
|
48
|
+
/** {@inheritDoc array.removeIndex} */
|
|
102
49
|
removeIndex(index: number): Option<T>;
|
|
103
|
-
/**
|
|
104
|
-
* Return a copy of the array with any `undefined` values removed.
|
|
105
|
-
*/
|
|
50
|
+
/** {@inheritDoc array.present} */
|
|
106
51
|
present(): Array<Present<T>>;
|
|
107
|
-
/**
|
|
108
|
-
* Return a copy of the array with any `null` or `undefined` values removed.
|
|
109
|
-
*/
|
|
52
|
+
/** {@inheritDoc array.nonNullable} */
|
|
110
53
|
nonNullable(): Array<NonNullable<T>>;
|
|
111
|
-
/**
|
|
112
|
-
* Checked index operator. Returns {@link None} if an index isn't
|
|
113
|
-
* present, even for sparse arrays. Returns a {@link Some} if the index
|
|
114
|
-
* is present, even if the value is `undefined`.
|
|
115
|
-
*/
|
|
54
|
+
/** {@inheritDoc array.get} */
|
|
116
55
|
get(index: number): Option<T>;
|
|
117
|
-
/**
|
|
118
|
-
* Test whether a value is an array with the same contents as this one.
|
|
119
|
-
*/
|
|
56
|
+
/** {@inheritDoc array.equals} */
|
|
120
57
|
equals<U = unknown>(other: unknown, equalsFn?: (a: T, b: U) => boolean): other is Array<T>;
|
|
121
58
|
}
|
|
122
59
|
/**
|
|
123
60
|
* Added methods on the {@link Iterator} global object.
|
|
124
61
|
*/
|
|
125
62
|
interface IteratorConstructor {
|
|
126
|
-
/**
|
|
127
|
-
* Construct an empty iterator.
|
|
128
|
-
*/
|
|
63
|
+
/** {@inheritDoc iterator.empty} */
|
|
129
64
|
empty<T>(): IteratorObject<T>;
|
|
130
|
-
/**
|
|
131
|
-
* Sequence multiple iterators into a single one.
|
|
132
|
-
* @see https://github.com/tc39/proposal-iterator-sequencing
|
|
133
|
-
*/
|
|
65
|
+
/** {@inheritDoc iterator.concat} */
|
|
134
66
|
concat<T>(...inputs: Array<Iterable<T>>): IteratorObject<T>;
|
|
135
|
-
/**
|
|
136
|
-
* @see https://github.com/tc39/proposal-joint-iteration
|
|
137
|
-
*/
|
|
67
|
+
/** {@inheritDoc iterator.zip} */
|
|
138
68
|
zip<T>(inputs: Iterable<Iterable<T>>): IteratorObject<Array<T>>;
|
|
139
|
-
/**
|
|
140
|
-
* @see https://github.com/tc39/proposal-joint-iteration
|
|
141
|
-
*/
|
|
69
|
+
/** {@inheritDoc iterator.zipKeyed} */
|
|
142
70
|
zipKeyed<T>(inputs: Record<string, Iterable<T>>): IteratorObject<Record<string, T>>;
|
|
143
71
|
}
|
|
144
72
|
/**
|
|
145
73
|
* Added methods on {@link IteratorObject}s.
|
|
146
74
|
*/
|
|
147
75
|
interface IteratorObject<T, TReturn, TNext> {
|
|
148
|
-
/**
|
|
149
|
-
* Get the next value from the iterator, wrapped in an {@link Option}
|
|
150
|
-
* type. If the iterator has no more values, return {@link None}.
|
|
151
|
-
*/
|
|
76
|
+
/** {@inheritDoc iterator.takeOne} */
|
|
152
77
|
takeOne(): Option<T>;
|
|
153
|
-
/**
|
|
154
|
-
* Take items from the iterator only until the first time `predicate` returns false.
|
|
155
|
-
*/
|
|
78
|
+
/** {@inheritDoc iterator.takeWhile} */
|
|
156
79
|
takeWhile<S extends T>(predicate: (value: T, index: number) => value is S): IteratorObject<S, undefined>;
|
|
157
80
|
/** @ignore */
|
|
158
81
|
takeWhile(predicate: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
159
|
-
/**
|
|
160
|
-
* Ignore all items matching `predicate` until the first item that
|
|
161
|
-
* doesn't match.
|
|
162
|
-
*/
|
|
82
|
+
/** {@inheritDoc iterator.skipWhile} */
|
|
163
83
|
skipWhile(predicate: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
164
|
-
/**
|
|
165
|
-
* Iterate until calling `mapFn` on an item returns a value, then return
|
|
166
|
-
* that value. Return `undefined` if `mapFn` never returns a value.
|
|
167
|
-
*/
|
|
84
|
+
/** {@inheritDoc iterator.findMap} */
|
|
168
85
|
findMap<U>(mapFn: (value: T) => NonNullable<U> | undefined): NonNullable<U> | undefined;
|
|
169
|
-
/**
|
|
170
|
-
* Split the input up into arrays of at most `size` items.
|
|
171
|
-
*/
|
|
86
|
+
/** {@inheritDoc iterator.chunks} */
|
|
172
87
|
chunks(size: number): IteratorObject<Array<T>, undefined>;
|
|
173
|
-
/**
|
|
174
|
-
* Split the input up into arrays of at most `size` items.
|
|
175
|
-
*
|
|
176
|
-
* @deprecated This has been standardised as {@link IteratorObject#chunks}.
|
|
177
|
-
*/
|
|
88
|
+
/** {@inheritDoc iterator.partition} */
|
|
178
89
|
partition(size: number): IteratorObject<Array<T>, undefined>;
|
|
179
|
-
/**
|
|
180
|
-
* As {@link IteratorObject#partition}, but if the input length isn't
|
|
181
|
-
* divisible by `size`, ensure the *first* array is the shortest one
|
|
182
|
-
* rather than the last.
|
|
183
|
-
*
|
|
184
|
-
* Be warned that this requires the entire input iterable to be read up
|
|
185
|
-
* front to decide its length, so this function is not lazy.
|
|
186
|
-
*/
|
|
90
|
+
/** {@inheritDoc iterator.frontBiasedPartition} */
|
|
187
91
|
frontBiasedPartition(size: number): Array<Array<T>>;
|
|
188
|
-
/**
|
|
189
|
-
* Filter out any items which are `undefined`.
|
|
190
|
-
*/
|
|
92
|
+
/** {@inheritDoc iterator.present} */
|
|
191
93
|
present(): IteratorObject<Present<T>, undefined>;
|
|
192
|
-
/**
|
|
193
|
-
* Filter out any items which are `null` or `undefined`.
|
|
194
|
-
*/
|
|
94
|
+
/** {@inheritDoc iterator.nonNullable} */
|
|
195
95
|
nonNullable(): IteratorObject<NonNullable<T>, undefined>;
|
|
196
|
-
/**
|
|
197
|
-
* Test whether the given Iterable has the same contents as this
|
|
198
|
-
* iterator.
|
|
199
|
-
*
|
|
200
|
-
* Note that this will consume this iterator up to the point
|
|
201
|
-
* where the values diverge. You should not rely on any particular state
|
|
202
|
-
* of the iterator afterwards, but consider it spent.
|
|
203
|
-
*/
|
|
96
|
+
/** {@inheritDoc iterator.equals} */
|
|
204
97
|
equals<T = unknown, U = unknown>(value: Iterable<U>, equalsFn?: (a: T, b: U) => boolean): boolean;
|
|
205
98
|
}
|
|
206
99
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,100 +1,38 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
2
|
-
// deno-lint-ignore-file no-this-alias
|
|
3
|
-
import { None, Option, Some } from "@bodil/opt";
|
|
4
|
-
import { bisectHigh, bisectLow } from "@bodil/core/order";
|
|
5
|
-
import { assert } from "@bodil/core/assert";
|
|
6
1
|
import defineProperties from "define-properties";
|
|
7
2
|
import shimUnscopables from "es-shim-unscopables";
|
|
8
|
-
import "
|
|
9
|
-
import "
|
|
10
|
-
import "
|
|
11
|
-
import "
|
|
3
|
+
import * as array from "./array";
|
|
4
|
+
import * as iterator from "./iterator";
|
|
5
|
+
import * as map from "./map";
|
|
6
|
+
import * as object from "./object";
|
|
7
|
+
import * as weakmap from "./weakmap";
|
|
12
8
|
// Object
|
|
13
9
|
defineProperties(Object, {
|
|
14
|
-
get
|
|
15
|
-
|
|
16
|
-
},
|
|
17
|
-
mapEntries(obj, fn) {
|
|
18
|
-
return Object.fromEntries(Object.entries(obj).map(fn));
|
|
19
|
-
},
|
|
10
|
+
get: object.get,
|
|
11
|
+
mapEntries: object.mapEntries,
|
|
20
12
|
});
|
|
21
13
|
// Map
|
|
22
14
|
defineProperties(Map.prototype, {
|
|
23
|
-
getOrInsertComputed
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
const newValue = defaultValue(key);
|
|
29
|
-
this.set(key, newValue);
|
|
30
|
-
return newValue;
|
|
31
|
-
},
|
|
32
|
-
remove(key) {
|
|
33
|
-
const result = this.get(key);
|
|
34
|
-
return this.delete(key) ? Some(result) : None;
|
|
35
|
-
},
|
|
15
|
+
getOrInsertComputed: map.getOrInsertComputed,
|
|
16
|
+
getOrSet: map.getOrSet,
|
|
17
|
+
remove: map.remove,
|
|
36
18
|
});
|
|
37
19
|
// WeakMap
|
|
38
20
|
defineProperties(WeakMap.prototype, {
|
|
39
|
-
getOrInsertComputed
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
const newValue = defaultValue(key);
|
|
45
|
-
this.set(key, newValue);
|
|
46
|
-
return newValue;
|
|
47
|
-
},
|
|
48
|
-
remove(key) {
|
|
49
|
-
const result = this.get(key);
|
|
50
|
-
return this.delete(key) ? Some(result) : None;
|
|
51
|
-
},
|
|
21
|
+
getOrInsertComputed: weakmap.getOrInsertComputed,
|
|
22
|
+
getOrSet: weakmap.getOrSet,
|
|
23
|
+
remove: weakmap.remove,
|
|
52
24
|
});
|
|
53
25
|
// Array
|
|
54
26
|
defineProperties(Array.prototype, {
|
|
55
|
-
remove
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
if (index < 0 || index >= this.length) {
|
|
66
|
-
return None;
|
|
67
|
-
}
|
|
68
|
-
const removed = this.splice(index, 1).pop();
|
|
69
|
-
return Option.from(removed);
|
|
70
|
-
},
|
|
71
|
-
insert(value, index) {
|
|
72
|
-
if (!Number.isInteger(index)) {
|
|
73
|
-
throw new TypeError(`Array.insert(): index ${index} is not an integer`);
|
|
74
|
-
}
|
|
75
|
-
if (index > this.length || index < 0) {
|
|
76
|
-
throw new RangeError(`Array.insert: index ${index} out of bounds`);
|
|
77
|
-
}
|
|
78
|
-
this.splice(index, 0, value);
|
|
79
|
-
return this;
|
|
80
|
-
},
|
|
81
|
-
insertOrdered(value, cmp, duplicates = "high") {
|
|
82
|
-
return this.insert(value, (duplicates === "low" ? bisectLow : bisectHigh)(cmp, this, value));
|
|
83
|
-
},
|
|
84
|
-
present() {
|
|
85
|
-
return this.filter((value) => value !== undefined);
|
|
86
|
-
},
|
|
87
|
-
nonNullable() {
|
|
88
|
-
return this.filter((value) => value !== undefined);
|
|
89
|
-
},
|
|
90
|
-
get(index) {
|
|
91
|
-
return Object.get(this, index);
|
|
92
|
-
},
|
|
93
|
-
equals(other, equalsFn) {
|
|
94
|
-
return (Array.isArray(other) &&
|
|
95
|
-
this.length === other.length &&
|
|
96
|
-
Iterator.from(this).equals(other, equalsFn));
|
|
97
|
-
},
|
|
27
|
+
remove: array.remove,
|
|
28
|
+
removeFn: array.removeFn,
|
|
29
|
+
removeIndex: array.removeIndex,
|
|
30
|
+
insert: array.insert,
|
|
31
|
+
insertOrdered: array.insertOrdered,
|
|
32
|
+
present: array.present,
|
|
33
|
+
nonNullable: array.nonNullable,
|
|
34
|
+
get: array.get,
|
|
35
|
+
equals: array.equals,
|
|
98
36
|
});
|
|
99
37
|
shimUnscopables("remove");
|
|
100
38
|
shimUnscopables("removeFn");
|
|
@@ -107,108 +45,22 @@ shimUnscopables("get");
|
|
|
107
45
|
shimUnscopables("equals");
|
|
108
46
|
// Iterator
|
|
109
47
|
defineProperties(Iterator, {
|
|
110
|
-
empty
|
|
111
|
-
|
|
112
|
-
|
|
48
|
+
empty: iterator.empty,
|
|
49
|
+
concat: iterator.concat,
|
|
50
|
+
zip: iterator.zip,
|
|
51
|
+
zipKeyed: iterator.zipKeyed,
|
|
113
52
|
});
|
|
114
53
|
defineProperties(Iterator.prototype, {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const result = iter.next();
|
|
127
|
-
if (result.done === true) {
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
|
-
if (predicate(result.value, index++)) {
|
|
131
|
-
return result;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
done = true;
|
|
135
|
-
return { done: true, value: undefined };
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
},
|
|
139
|
-
skipWhile(predicate) {
|
|
140
|
-
const iter = this;
|
|
141
|
-
let index = 0;
|
|
142
|
-
let skipped = false;
|
|
143
|
-
return Iterator.from({
|
|
144
|
-
next() {
|
|
145
|
-
if (skipped) {
|
|
146
|
-
return iter.next();
|
|
147
|
-
}
|
|
148
|
-
while (true) {
|
|
149
|
-
const result = iter.next();
|
|
150
|
-
if (result.done === true) {
|
|
151
|
-
return result;
|
|
152
|
-
}
|
|
153
|
-
if (!predicate(result.value, index++)) {
|
|
154
|
-
skipped = true;
|
|
155
|
-
return result;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
},
|
|
161
|
-
findMap(mapFn) {
|
|
162
|
-
let index = 0;
|
|
163
|
-
while (true) {
|
|
164
|
-
const result = this.next();
|
|
165
|
-
if (result.done === true) {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
const mapped = mapFn(result.value, index++);
|
|
169
|
-
if (mapped !== undefined) {
|
|
170
|
-
return mapped;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
partition(size) {
|
|
175
|
-
return this.chunks(size);
|
|
176
|
-
},
|
|
177
|
-
frontBiasedPartition(size) {
|
|
178
|
-
const input = this.toArray();
|
|
179
|
-
let inputPos = 0;
|
|
180
|
-
const firstLength = input.length % size;
|
|
181
|
-
const output = [];
|
|
182
|
-
if (firstLength > 0) {
|
|
183
|
-
output.push(input.slice(0, firstLength));
|
|
184
|
-
inputPos = firstLength;
|
|
185
|
-
}
|
|
186
|
-
while (inputPos < input.length) {
|
|
187
|
-
output.push(input.slice(inputPos, inputPos + size));
|
|
188
|
-
inputPos += size;
|
|
189
|
-
}
|
|
190
|
-
assert(inputPos === input.length);
|
|
191
|
-
return output;
|
|
192
|
-
},
|
|
193
|
-
present() {
|
|
194
|
-
return Iterator.from(this.filter((item) => item !== undefined));
|
|
195
|
-
},
|
|
196
|
-
nonNullable() {
|
|
197
|
-
return Iterator.from(this.filter((item) => item !== undefined && item !== null));
|
|
198
|
-
},
|
|
199
|
-
equals(value, equalsFn = Object.is) {
|
|
200
|
-
const other = Iterator.from(value);
|
|
201
|
-
while (true) {
|
|
202
|
-
const a = this.next();
|
|
203
|
-
const b = other.next();
|
|
204
|
-
if (a.done !== b.done ||
|
|
205
|
-
(a.done === false && b.done === false && !equalsFn(a.value, b.value))) {
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
if (a.done === true && b.done === true) {
|
|
209
|
-
return true;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
},
|
|
54
|
+
chunks: iterator.chunks,
|
|
55
|
+
takeOne: iterator.takeOne,
|
|
56
|
+
takeWhile: iterator.takeWhile,
|
|
57
|
+
skipWhile: iterator.skipWhile,
|
|
58
|
+
findMap: iterator.findMap,
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated
|
|
60
|
+
partition: iterator.partition,
|
|
61
|
+
frontBiasedPartition: iterator.frontBiasedPartition,
|
|
62
|
+
present: iterator.present,
|
|
63
|
+
nonNullable: iterator.nonNullable,
|
|
64
|
+
equals: iterator.equals,
|
|
213
65
|
});
|
|
214
66
|
//# sourceMappingURL=index.js.map
|