@bodil/monkey-business 0.3.3 → 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 +36 -136
- package/dist/index.js +37 -207
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +32 -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 +71 -18
- package/src/array.ts +128 -0
- package/src/es-iterator-helpers.d.ts +24 -0
- package/src/index.test.ts +39 -7
- package/src/index.ts +78 -407
- 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":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAgB,MAAM,mBAAmB,CAAC;AAExE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAa,MAAM,YAAY,CAAC;AAErD,OAAO,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,UAAU,CAAC;AAE5C;;GAEG;AACH,MAAM,UAAU,MAAM,CAAoB,KAAQ,EAAE,KAAa;IAC7D,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CAAC,yBAAyB,KAAK,oBAAoB,CAAC,CAAC;IAC5E,CAAC;IACD,IAAI,KAAK,GAAG,IAAI,CAAC,MAAM,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,UAAU,CAAC,uBAAuB,KAAK,gBAAgB,CAAC,CAAC;IACvE,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAC7B,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAEzB,KAAQ,EACR,GAAe,EACf,aAA6B,MAAM;IAEnC,OAAO,CAAC,MAAS,CAAC,CAAC,IAAI,CACnB,IAAI,EACJ,KAAK,EACL,CAAC,UAAU,KAAK,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,CACpE,CAAC;AACN,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAoB,KAAQ;IAC9C,OAAO,CAAC,WAAc,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAEpB,SAA8D;IAE9D,OAAO,CAAC,WAAc,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC;AAClE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAoB,KAAa;IACxD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CAAC,8BAA8B,KAAK,oBAAoB,CAAC,CAAC;IACjF,CAAC;IACD,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QACpC,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;IAC5C,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,OAAO;IACnB,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,KAAQ,EAAuB,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;AAC/E,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW;IACvB,OAAO,IAAI,CAAC,MAAM,CACd,CAAC,KAAQ,EAA2B,EAAE,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,CAC/E,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAoB,KAAa;IAChD,OAAO,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,MAAM,CAElB,KAAc,EACd,WAAoC,MAAM,CAAC,EAAE;IAE7C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;QACxD,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC/C,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACvC,OAAO,KAAK,CAAC;QACjB,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,199 +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";
|
|
3
|
+
import type { Option } from "@bodil/opt";
|
|
7
4
|
declare global {
|
|
8
5
|
/**
|
|
9
6
|
* Added methods on the {@link Object} global object.
|
|
10
7
|
*/
|
|
11
8
|
interface ObjectConstructor {
|
|
12
|
-
/**
|
|
13
|
-
* Checked key lookup. Returns {@link None} if a key isn't present.
|
|
14
|
-
* Returns a {@link Some} if the key is present, even if the value is
|
|
15
|
-
* `undefined`.
|
|
16
|
-
*/
|
|
9
|
+
/** {@inheritDoc object.get} */
|
|
17
10
|
get<O extends object, K extends keyof O>(obj: O, key: K): Option<O[K]>;
|
|
18
|
-
/**
|
|
19
|
-
* Create a new object by mapping an existing object's `Object.entries`
|
|
20
|
-
* through a function.
|
|
21
|
-
*/
|
|
11
|
+
/** {@inheritDoc object.mapEntries} */
|
|
22
12
|
mapEntries<A, B>(obj: Record<string, A>, fn: (entry: [key: string, value: A]) => [string, B]): Record<string, B>;
|
|
23
13
|
}
|
|
24
14
|
/**
|
|
25
15
|
* Added methods on {@link Map}s.
|
|
26
16
|
*/
|
|
27
17
|
interface Map<K, V> {
|
|
28
|
-
/**
|
|
29
|
-
* If `key` exists in the map, return the value associated with it.
|
|
30
|
-
* Otherwise, call `defaultValue()` to create a new value, insert that
|
|
31
|
-
* into the map under `key`, and return it.
|
|
32
|
-
*/
|
|
18
|
+
/** {@inheritDoc map.getOrInsertComputed} */
|
|
33
19
|
getOrInsertComputed(key: K, defaultValue: (key: K) => V): V;
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
*
|
|
38
|
-
* @see {@link Map.delete}
|
|
39
|
-
*/
|
|
20
|
+
/** {@inheritDoc map.getOrSet} */
|
|
21
|
+
getOrSet(key: K, defaultValue: (key: K) => V): V;
|
|
22
|
+
/** {@inheritDoc map.remove} */
|
|
40
23
|
remove(key: K): Option<V>;
|
|
41
24
|
}
|
|
42
25
|
/**
|
|
43
26
|
* Added methods on {@link WeakMap}s.
|
|
44
27
|
*/
|
|
45
28
|
interface WeakMap<K extends WeakKey, V> {
|
|
46
|
-
/**
|
|
47
|
-
* If `key` exists in the map, return the value associated with it.
|
|
48
|
-
* Otherwise, call `defaultValue()` to create a new value, insert that
|
|
49
|
-
* into the map under `key`, and return it.
|
|
50
|
-
*/
|
|
29
|
+
/** {@inheritDoc weakmap.getOrInsertComputed} */
|
|
51
30
|
getOrInsertComputed(key: K, defaultValue: (key: K) => V): V;
|
|
52
|
-
/**
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
* @see {@link WeakMap.delete}
|
|
57
|
-
*/
|
|
31
|
+
/** {@inheritDoc weakmap.getOrSet} */
|
|
32
|
+
getOrSet(key: K, defaultValue: (key: K) => V): V;
|
|
33
|
+
/** {@inheritDoc weakmap.remove} */
|
|
58
34
|
remove(key: K): Option<V>;
|
|
59
35
|
}
|
|
60
36
|
/**
|
|
61
37
|
* Added methods on {@link Array}s.
|
|
62
38
|
*/
|
|
63
39
|
interface Array<T> {
|
|
64
|
-
/**
|
|
65
|
-
* Insert `value` at `index` in the array.
|
|
66
|
-
*/
|
|
40
|
+
/** {@inheritDoc array.insert} */
|
|
67
41
|
insert(value: T, index: number): Array<T>;
|
|
68
|
-
/**
|
|
69
|
-
* Insert `value` into the array at the correct position according to
|
|
70
|
-
* the ordering function `cmp`. The array must already be ordered
|
|
71
|
-
* accordingly.
|
|
72
|
-
*
|
|
73
|
-
* If `duplicates` is `"low"`, the correct position in case of
|
|
74
|
-
* duplicates is considered to be the index of the first occurrence of a
|
|
75
|
-
* duplicate. If it is `"high"`, which is the default, the correct
|
|
76
|
-
* position is considered to be the index after the last occurrence of a
|
|
77
|
-
* duplicate.
|
|
78
|
-
*/
|
|
42
|
+
/** {@inheritDoc array.insertOrdered} */
|
|
79
43
|
insertOrdered(value: T, cmp: OrderFn<T>, duplicates?: "low" | "high"): Array<T>;
|
|
80
|
-
/**
|
|
81
|
-
* Remove the first occurrence of a given value from an array.
|
|
82
|
-
*
|
|
83
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
84
|
-
* no matches were found.
|
|
85
|
-
*/
|
|
44
|
+
/** {@inheritDoc array.remove} */
|
|
86
45
|
remove(value: T): Option<T>;
|
|
87
|
-
/**
|
|
88
|
-
* Remove the first value for which the provided predicate function
|
|
89
|
-
* returns `true` from the array.
|
|
90
|
-
*
|
|
91
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
92
|
-
* no matches were found.
|
|
93
|
-
*/
|
|
46
|
+
/** {@inheritDoc array.removeFn} */
|
|
94
47
|
removeFn(predicate: (value: T, index: number, obj: Array<T>) => boolean): Option<T>;
|
|
95
|
-
/**
|
|
96
|
-
* Remove the value at the given index from the array.
|
|
97
|
-
*
|
|
98
|
-
* Returns an {@link Option} containing the removed value, or {@link None} if
|
|
99
|
-
* the index was out of bounds.
|
|
100
|
-
*/
|
|
48
|
+
/** {@inheritDoc array.removeIndex} */
|
|
101
49
|
removeIndex(index: number): Option<T>;
|
|
102
|
-
/**
|
|
103
|
-
* Return a copy of the array with any `undefined` values removed.
|
|
104
|
-
*/
|
|
50
|
+
/** {@inheritDoc array.present} */
|
|
105
51
|
present(): Array<Present<T>>;
|
|
106
|
-
/**
|
|
107
|
-
* Return a copy of the array with any `null` or `undefined` values removed.
|
|
108
|
-
*/
|
|
52
|
+
/** {@inheritDoc array.nonNullable} */
|
|
109
53
|
nonNullable(): Array<NonNullable<T>>;
|
|
110
|
-
/**
|
|
111
|
-
* Checked index operator. Returns {@link None} if an index isn't
|
|
112
|
-
* present, even for sparse arrays. Returns a {@link Some} if the index
|
|
113
|
-
* is present, even if the value is `undefined`.
|
|
114
|
-
*/
|
|
54
|
+
/** {@inheritDoc array.get} */
|
|
115
55
|
get(index: number): Option<T>;
|
|
116
|
-
/**
|
|
117
|
-
* Test whether a value is an array with the same contents as this one.
|
|
118
|
-
*/
|
|
56
|
+
/** {@inheritDoc array.equals} */
|
|
119
57
|
equals<U = unknown>(other: unknown, equalsFn?: (a: T, b: U) => boolean): other is Array<T>;
|
|
120
58
|
}
|
|
121
59
|
/**
|
|
122
60
|
* Added methods on the {@link Iterator} global object.
|
|
123
61
|
*/
|
|
124
62
|
interface IteratorConstructor {
|
|
125
|
-
/**
|
|
126
|
-
* Construct an empty iterator.
|
|
127
|
-
*/
|
|
63
|
+
/** {@inheritDoc iterator.empty} */
|
|
128
64
|
empty<T>(): IteratorObject<T>;
|
|
129
|
-
/**
|
|
130
|
-
* Sequence multiple iterators into a single one.
|
|
131
|
-
* @see https://github.com/tc39/proposal-iterator-sequencing
|
|
132
|
-
*/
|
|
65
|
+
/** {@inheritDoc iterator.concat} */
|
|
133
66
|
concat<T>(...inputs: Array<Iterable<T>>): IteratorObject<T>;
|
|
134
|
-
/**
|
|
135
|
-
* @see https://github.com/tc39/proposal-joint-iteration
|
|
136
|
-
*/
|
|
67
|
+
/** {@inheritDoc iterator.zip} */
|
|
137
68
|
zip<T>(inputs: Iterable<Iterable<T>>): IteratorObject<Array<T>>;
|
|
138
|
-
/**
|
|
139
|
-
* @see https://github.com/tc39/proposal-joint-iteration
|
|
140
|
-
*/
|
|
69
|
+
/** {@inheritDoc iterator.zipKeyed} */
|
|
141
70
|
zipKeyed<T>(inputs: Record<string, Iterable<T>>): IteratorObject<Record<string, T>>;
|
|
142
71
|
}
|
|
143
72
|
/**
|
|
144
73
|
* Added methods on {@link IteratorObject}s.
|
|
145
74
|
*/
|
|
146
75
|
interface IteratorObject<T, TReturn, TNext> {
|
|
147
|
-
/**
|
|
148
|
-
* Get the next value from the iterator, wrapped in an {@link Option}
|
|
149
|
-
* type. If the iterator has no more values, return {@link None}.
|
|
150
|
-
*/
|
|
76
|
+
/** {@inheritDoc iterator.takeOne} */
|
|
151
77
|
takeOne(): Option<T>;
|
|
152
|
-
/**
|
|
153
|
-
* Take items from the iterator only until the first time `predicate` returns false.
|
|
154
|
-
*/
|
|
78
|
+
/** {@inheritDoc iterator.takeWhile} */
|
|
155
79
|
takeWhile<S extends T>(predicate: (value: T, index: number) => value is S): IteratorObject<S, undefined>;
|
|
156
80
|
/** @ignore */
|
|
157
81
|
takeWhile(predicate: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
158
|
-
/**
|
|
159
|
-
* Ignore all items matching `predicate` until the first item that
|
|
160
|
-
* doesn't match.
|
|
161
|
-
*/
|
|
82
|
+
/** {@inheritDoc iterator.skipWhile} */
|
|
162
83
|
skipWhile(predicate: (value: T, index: number) => boolean): IteratorObject<T, undefined>;
|
|
163
|
-
/**
|
|
164
|
-
* Iterate until calling `mapFn` on an item returns a value, then return
|
|
165
|
-
* that value. Return `undefined` if `mapFn` never returns a value.
|
|
166
|
-
*/
|
|
84
|
+
/** {@inheritDoc iterator.findMap} */
|
|
167
85
|
findMap<U>(mapFn: (value: T) => NonNullable<U> | undefined): NonNullable<U> | undefined;
|
|
168
|
-
/**
|
|
169
|
-
|
|
170
|
-
|
|
86
|
+
/** {@inheritDoc iterator.chunks} */
|
|
87
|
+
chunks(size: number): IteratorObject<Array<T>, undefined>;
|
|
88
|
+
/** {@inheritDoc iterator.partition} */
|
|
171
89
|
partition(size: number): IteratorObject<Array<T>, undefined>;
|
|
172
|
-
/**
|
|
173
|
-
* As {@link IteratorObject#partition}, but if the input length isn't
|
|
174
|
-
* divisible by `size`, ensure the *first* array is the shortest one
|
|
175
|
-
* rather than the last.
|
|
176
|
-
*
|
|
177
|
-
* Be warned that this requires the entire input iterable to be read up
|
|
178
|
-
* front to decide its length, so this function is not lazy.
|
|
179
|
-
*/
|
|
90
|
+
/** {@inheritDoc iterator.frontBiasedPartition} */
|
|
180
91
|
frontBiasedPartition(size: number): Array<Array<T>>;
|
|
181
|
-
/**
|
|
182
|
-
* Filter out any items which are `undefined`.
|
|
183
|
-
*/
|
|
92
|
+
/** {@inheritDoc iterator.present} */
|
|
184
93
|
present(): IteratorObject<Present<T>, undefined>;
|
|
185
|
-
/**
|
|
186
|
-
* Filter out any items which are `null` or `undefined`.
|
|
187
|
-
*/
|
|
94
|
+
/** {@inheritDoc iterator.nonNullable} */
|
|
188
95
|
nonNullable(): IteratorObject<NonNullable<T>, undefined>;
|
|
189
|
-
/**
|
|
190
|
-
* Test whether the given Iterable has the same contents as this
|
|
191
|
-
* iterator.
|
|
192
|
-
*
|
|
193
|
-
* Note that this will consume this iterator up to the point
|
|
194
|
-
* where the values diverge. You should not rely on any particular state
|
|
195
|
-
* of the iterator afterwards, but consider it spent.
|
|
196
|
-
*/
|
|
96
|
+
/** {@inheritDoc iterator.equals} */
|
|
197
97
|
equals<T = unknown, U = unknown>(value: Iterable<U>, equalsFn?: (a: T, b: U) => boolean): boolean;
|
|
198
98
|
}
|
|
199
99
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,100 +1,38 @@
|
|
|
1
|
-
/* eslint-disable jsdoc/no-undefined-types */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
3
|
-
// deno-lint-ignore-file no-this-alias
|
|
4
|
-
import { None, Option, Some } from "@bodil/opt";
|
|
5
|
-
import { bisectHigh, bisectLow } from "@bodil/core/order";
|
|
6
|
-
import { assert } from "@bodil/core/assert";
|
|
7
1
|
import defineProperties from "define-properties";
|
|
8
2
|
import shimUnscopables from "es-shim-unscopables";
|
|
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,130 +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
|
-
const iter = this;
|
|
176
|
-
let done = false;
|
|
177
|
-
return Iterator.from({
|
|
178
|
-
next() {
|
|
179
|
-
if (done) {
|
|
180
|
-
return { done: true, value: undefined };
|
|
181
|
-
}
|
|
182
|
-
const part = [];
|
|
183
|
-
while (true) {
|
|
184
|
-
const result = iter.next();
|
|
185
|
-
if (result.done === true) {
|
|
186
|
-
done = true;
|
|
187
|
-
return part.length > 0
|
|
188
|
-
? { done: false, value: part }
|
|
189
|
-
: { done: true, value: undefined };
|
|
190
|
-
}
|
|
191
|
-
part.push(result.value);
|
|
192
|
-
if (part.length === size) {
|
|
193
|
-
return { done: false, value: part };
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
},
|
|
197
|
-
});
|
|
198
|
-
},
|
|
199
|
-
frontBiasedPartition(size) {
|
|
200
|
-
const input = this.toArray();
|
|
201
|
-
let inputPos = 0;
|
|
202
|
-
const firstLength = input.length % size;
|
|
203
|
-
const output = [];
|
|
204
|
-
if (firstLength > 0) {
|
|
205
|
-
output.push(input.slice(0, firstLength));
|
|
206
|
-
inputPos = firstLength;
|
|
207
|
-
}
|
|
208
|
-
while (inputPos < input.length) {
|
|
209
|
-
output.push(input.slice(inputPos, inputPos + size));
|
|
210
|
-
inputPos += size;
|
|
211
|
-
}
|
|
212
|
-
assert(inputPos === input.length);
|
|
213
|
-
return output;
|
|
214
|
-
},
|
|
215
|
-
present() {
|
|
216
|
-
return Iterator.from(this.filter((item) => item !== undefined));
|
|
217
|
-
},
|
|
218
|
-
nonNullable() {
|
|
219
|
-
return Iterator.from(this.filter((item) => item !== undefined && item !== null));
|
|
220
|
-
},
|
|
221
|
-
equals(value, equalsFn = Object.is) {
|
|
222
|
-
const other = Iterator.from(value);
|
|
223
|
-
while (true) {
|
|
224
|
-
const a = this.next();
|
|
225
|
-
const b = other.next();
|
|
226
|
-
if (a.done !== b.done ||
|
|
227
|
-
(a.done === false && b.done === false && !equalsFn(a.value, b.value))) {
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
if (a.done === true && b.done === true) {
|
|
231
|
-
return true;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
},
|
|
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,
|
|
235
65
|
});
|
|
236
66
|
//# sourceMappingURL=index.js.map
|