@mark1russell7/collections 0.6.0 → 0.7.0
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/build/index.d.ts +32 -214
- package/build/index.d.ts.map +1 -1
- package/build/index.js +43 -233
- package/build/index.js.map +1 -1
- package/package.json +4 -1
- package/src/behaviors/safe.ts +78 -132
- package/src/impl/hash-map.ts +18 -28
- package/src/index.ts +0 -16
- package/src/core/effects.ts +0 -327
package/build/index.js
CHANGED
|
@@ -1,168 +1,5 @@
|
|
|
1
1
|
import { compose as compose$1 } from "./core/layers.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The singleton None value.
|
|
5
|
-
*/
|
|
6
|
-
const None = Object.freeze({ _tag: "None" });
|
|
7
|
-
/**
|
|
8
|
-
* This function constructs a Some value containing the given value.
|
|
9
|
-
*/
|
|
10
|
-
const Some = (value) => Object.freeze({
|
|
11
|
-
_tag: "Some",
|
|
12
|
-
value
|
|
13
|
-
});
|
|
14
|
-
/**
|
|
15
|
-
* Type guard to check if an Option is None.
|
|
16
|
-
*/
|
|
17
|
-
const isNone = (opt) => opt._tag === "None";
|
|
18
|
-
/**
|
|
19
|
-
* Type guard to check if an Option is Some.
|
|
20
|
-
*/
|
|
21
|
-
const isSome = (opt) => opt._tag === "Some";
|
|
22
|
-
/**
|
|
23
|
-
* This function extracts the value from a Some, or gives the default value if None.
|
|
24
|
-
*/
|
|
25
|
-
const getOrElse = (opt, defaultValue) => isSome(opt) ? opt.value : defaultValue;
|
|
26
|
-
/**
|
|
27
|
-
* This function extracts the value from a Some, or throws an error if None.
|
|
28
|
-
*/
|
|
29
|
-
const unwrap = (opt) => {
|
|
30
|
-
if (isSome(opt)) return opt.value;
|
|
31
|
-
throw new Error("Called unwrap on None");
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* This function maps a function over the value in a Some, or gives None.
|
|
35
|
-
*/
|
|
36
|
-
const mapOption = (opt, f) => isSome(opt) ? Some(f(opt.value)) : None;
|
|
37
|
-
/**
|
|
38
|
-
* Chains Option-returning operations.
|
|
39
|
-
*/
|
|
40
|
-
const flatMapOption = (opt, f) => isSome(opt) ? f(opt.value) : None;
|
|
41
|
-
/**
|
|
42
|
-
* This function converts a nullable value to an Option.
|
|
43
|
-
*/
|
|
44
|
-
const fromNullable = (value) => value == null ? None : Some(value);
|
|
45
|
-
/**
|
|
46
|
-
* This function converts an Option to a nullable value.
|
|
47
|
-
*/
|
|
48
|
-
const toNullable = (opt) => isSome(opt) ? opt.value : void 0;
|
|
49
|
-
/**
|
|
50
|
-
* This function constructs an Ok result containing the given value.
|
|
51
|
-
*/
|
|
52
|
-
const Ok = (value) => Object.freeze({
|
|
53
|
-
_tag: "Ok",
|
|
54
|
-
value,
|
|
55
|
-
match: (f) => f.Ok(value)
|
|
56
|
-
});
|
|
57
|
-
/**
|
|
58
|
-
* This function constructs an Err result containing the given error.
|
|
59
|
-
*/
|
|
60
|
-
const Err = (error) => Object.freeze({
|
|
61
|
-
_tag: "Err",
|
|
62
|
-
error,
|
|
63
|
-
match: (f) => f.Err(error)
|
|
64
|
-
});
|
|
65
|
-
/**
|
|
66
|
-
* Type guard to check if a Result is Ok.
|
|
67
|
-
*/
|
|
68
|
-
const isOk = (result) => result._tag === "Ok";
|
|
69
|
-
/**
|
|
70
|
-
* Type guard to check if a Result is Err.
|
|
71
|
-
*/
|
|
72
|
-
const isErr = (result) => result._tag === "Err";
|
|
73
|
-
/**
|
|
74
|
-
* This function extracts the value from an Ok, or gives the default value if Err.
|
|
75
|
-
*/
|
|
76
|
-
const getOrElseResult = (result, defaultValue) => isOk(result) ? result.value : defaultValue;
|
|
77
|
-
/**
|
|
78
|
-
* This function extracts the value from an Ok, or throws the error if Err.
|
|
79
|
-
*/
|
|
80
|
-
const unwrapResult = (result) => {
|
|
81
|
-
if (isOk(result)) return result.value;
|
|
82
|
-
throw result.error;
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* This function maps a function over the value in an Ok, or gives the Err unchanged.
|
|
86
|
-
*/
|
|
87
|
-
const mapResult = (result, f) => isOk(result) ? Ok(f(result.value)) : result;
|
|
88
|
-
/**
|
|
89
|
-
* This function maps a function over the error in an Err, or gives the Ok unchanged.
|
|
90
|
-
*/
|
|
91
|
-
const mapError = (result, f) => isErr(result) ? Err(f(result.error)) : result;
|
|
92
|
-
/**
|
|
93
|
-
* Chains Result-returning operations.
|
|
94
|
-
*/
|
|
95
|
-
const flatMapResult = (result, f) => isOk(result) ? f(result.value) : result;
|
|
96
|
-
/**
|
|
97
|
-
* This function converts a throwing function into a Result-returning function.
|
|
98
|
-
*/
|
|
99
|
-
const tryCatch = (fn, onError) => {
|
|
100
|
-
try {
|
|
101
|
-
return Ok(fn());
|
|
102
|
-
} catch (error) {
|
|
103
|
-
return Err(onError ? onError(error) : error);
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* This function converts an async throwing function into a Result-returning async function.
|
|
108
|
-
*/
|
|
109
|
-
const tryCatchAsync = async (fn, onError) => {
|
|
110
|
-
try {
|
|
111
|
-
return Ok(await fn());
|
|
112
|
-
} catch (error) {
|
|
113
|
-
return Err(onError ? onError(error) : error);
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
/**
|
|
117
|
-
* This function maps a function over an async Option.
|
|
118
|
-
*/
|
|
119
|
-
const mapAsyncOption = async (opt, f) => {
|
|
120
|
-
const resolved = await opt;
|
|
121
|
-
if (isSome(resolved)) return Some(await f(resolved.value));
|
|
122
|
-
return None;
|
|
123
|
-
};
|
|
124
|
-
/**
|
|
125
|
-
* This function maps a function over an async Result.
|
|
126
|
-
*/
|
|
127
|
-
const mapAsyncResult = async (result, f) => {
|
|
128
|
-
const resolved = await result;
|
|
129
|
-
if (isOk(resolved)) return Ok(await f(resolved.value));
|
|
130
|
-
return resolved;
|
|
131
|
-
};
|
|
132
|
-
/**
|
|
133
|
-
* This function converts an Option to a Result.
|
|
134
|
-
* None becomes Err with the given error.
|
|
135
|
-
*/
|
|
136
|
-
const optionToResult = (opt, error) => isSome(opt) ? Ok(opt.value) : Err(error);
|
|
137
|
-
/**
|
|
138
|
-
* This function converts a Result to an Option.
|
|
139
|
-
* Err becomes None, discarding the error.
|
|
140
|
-
*/
|
|
141
|
-
const resultToOption = (result) => isOk(result) ? Some(result.value) : None;
|
|
142
|
-
/**
|
|
143
|
-
* This function combines multiple Options into an Option of an array.
|
|
144
|
-
* This function gives None if any input is None.
|
|
145
|
-
*/
|
|
146
|
-
const sequence = (opts) => {
|
|
147
|
-
const values = [];
|
|
148
|
-
for (const opt of opts) {
|
|
149
|
-
if (isNone(opt)) return None;
|
|
150
|
-
values.push(opt.value);
|
|
151
|
-
}
|
|
152
|
-
return Some(values);
|
|
153
|
-
};
|
|
154
|
-
/**
|
|
155
|
-
* This function combines multiple Results into a Result of an array.
|
|
156
|
-
* This function gives the first Err encountered, or Ok with all values.
|
|
157
|
-
*/
|
|
158
|
-
const sequenceResults = (results) => {
|
|
159
|
-
const values = [];
|
|
160
|
-
for (const result of results) {
|
|
161
|
-
if (isErr(result)) return result;
|
|
162
|
-
values.push(result.value);
|
|
163
|
-
}
|
|
164
|
-
return Ok(values);
|
|
165
|
-
};
|
|
2
|
+
import { none as none$1, some as some$1, toOptional, tryCatch } from "@mark1russell7/optional";
|
|
166
3
|
// src/core/policies.ts
|
|
167
4
|
/**
|
|
168
5
|
* Default overflow policy: throw an error.
|
|
@@ -1899,32 +1736,29 @@ var HashMap = class {
|
|
|
1899
1736
|
return this._valueEq;
|
|
1900
1737
|
}
|
|
1901
1738
|
has(key) {
|
|
1902
|
-
|
|
1903
|
-
const index = this.bucketIndex(hash);
|
|
1904
|
-
let node = this._buckets[index] ?? null;
|
|
1905
|
-
while (node !== null) {
|
|
1906
|
-
if (node.hash === hash && this._keyEq(node.key, key)) return true;
|
|
1907
|
-
node = node.next;
|
|
1908
|
-
}
|
|
1909
|
-
return false;
|
|
1739
|
+
return this.findNode(key) !== null;
|
|
1910
1740
|
}
|
|
1911
1741
|
get(key) {
|
|
1912
|
-
const
|
|
1913
|
-
if (
|
|
1914
|
-
return value;
|
|
1742
|
+
const node = this.findNode(key);
|
|
1743
|
+
if (node === null) throw new Error(`Key not found: ${key}`);
|
|
1744
|
+
return node.value;
|
|
1915
1745
|
}
|
|
1916
1746
|
getOrUndefined(key) {
|
|
1747
|
+
return this.findNode(key)?.value;
|
|
1748
|
+
}
|
|
1749
|
+
getOrDefault(key, defaultValue) {
|
|
1750
|
+
const node = this.findNode(key);
|
|
1751
|
+
return node !== null ? node.value : defaultValue;
|
|
1752
|
+
}
|
|
1753
|
+
/** This method finds the node of a key, or gives `null`. */
|
|
1754
|
+
findNode(key) {
|
|
1917
1755
|
const hash = this._keyHash(key);
|
|
1918
|
-
|
|
1919
|
-
let node = this._buckets[index] ?? null;
|
|
1756
|
+
let node = this._buckets[this.bucketIndex(hash)] ?? null;
|
|
1920
1757
|
while (node !== null) {
|
|
1921
|
-
if (node.hash === hash && this._keyEq(node.key, key)) return node
|
|
1758
|
+
if (node.hash === hash && this._keyEq(node.key, key)) return node;
|
|
1922
1759
|
node = node.next;
|
|
1923
1760
|
}
|
|
1924
|
-
|
|
1925
|
-
getOrDefault(key, defaultValue) {
|
|
1926
|
-
const value = this.getOrUndefined(key);
|
|
1927
|
-
return value !== void 0 ? value : defaultValue;
|
|
1761
|
+
return null;
|
|
1928
1762
|
}
|
|
1929
1763
|
containsValue(value) {
|
|
1930
1764
|
for (const bucket of this._buckets) {
|
|
@@ -5094,16 +4928,13 @@ const unmodifiableCollection = readonlyCollection;
|
|
|
5094
4928
|
/**
|
|
5095
4929
|
* This function makes a safe List middleware.
|
|
5096
4930
|
*
|
|
5097
|
-
* This function exposes a `safe` property with
|
|
4931
|
+
* This function exposes a `safe` property with Optional/Result-based operations.
|
|
5098
4932
|
*/
|
|
5099
4933
|
function safeList() {
|
|
5100
4934
|
return (next) => {
|
|
5101
4935
|
return Object.assign(next, { safe: {
|
|
5102
4936
|
get(index) {
|
|
5103
|
-
return tryCatch(() => next.get(index)
|
|
5104
|
-
Ok: (value) => Some(value),
|
|
5105
|
-
Err: () => None
|
|
5106
|
-
});
|
|
4937
|
+
return toOptional(tryCatch(() => next.get(index)));
|
|
5107
4938
|
},
|
|
5108
4939
|
set(index, element) {
|
|
5109
4940
|
return tryCatch(() => next.set(index, element), (error) => String(error));
|
|
@@ -5112,24 +4943,18 @@ function safeList() {
|
|
|
5112
4943
|
return tryCatch(() => next.removeAt(index), (error) => String(error));
|
|
5113
4944
|
},
|
|
5114
4945
|
first() {
|
|
5115
|
-
return next.isEmpty ?
|
|
4946
|
+
return next.isEmpty ? none$1 : some$1(next.first());
|
|
5116
4947
|
},
|
|
5117
4948
|
last() {
|
|
5118
|
-
return next.isEmpty ?
|
|
4949
|
+
return next.isEmpty ? none$1 : some$1(next.last());
|
|
5119
4950
|
},
|
|
5120
4951
|
pop() {
|
|
5121
|
-
if (next.isEmpty) return
|
|
5122
|
-
return tryCatch(() => next.pop()
|
|
5123
|
-
Ok: (value) => Some(value),
|
|
5124
|
-
Err: () => None
|
|
5125
|
-
});
|
|
4952
|
+
if (next.isEmpty) return none$1;
|
|
4953
|
+
return toOptional(tryCatch(() => next.pop()));
|
|
5126
4954
|
},
|
|
5127
4955
|
shift() {
|
|
5128
|
-
if (next.isEmpty) return
|
|
5129
|
-
return tryCatch(() => next.shift()
|
|
5130
|
-
Ok: (value) => Some(value),
|
|
5131
|
-
Err: () => None
|
|
5132
|
-
});
|
|
4956
|
+
if (next.isEmpty) return none$1;
|
|
4957
|
+
return toOptional(tryCatch(() => next.shift()));
|
|
5133
4958
|
}
|
|
5134
4959
|
} });
|
|
5135
4960
|
};
|
|
@@ -5141,25 +4966,16 @@ function safeQueue() {
|
|
|
5141
4966
|
return (next) => {
|
|
5142
4967
|
return Object.assign(next, { safe: {
|
|
5143
4968
|
peek() {
|
|
5144
|
-
if (next.isEmpty) return
|
|
5145
|
-
return tryCatch(() => next.peek()
|
|
5146
|
-
Ok: (value) => Some(value),
|
|
5147
|
-
Err: () => None
|
|
5148
|
-
});
|
|
4969
|
+
if (next.isEmpty) return none$1;
|
|
4970
|
+
return toOptional(tryCatch(() => next.peek()));
|
|
5149
4971
|
},
|
|
5150
4972
|
poll() {
|
|
5151
|
-
if (next.isEmpty) return
|
|
5152
|
-
return tryCatch(() => next.poll()
|
|
5153
|
-
Ok: (value) => Some(value),
|
|
5154
|
-
Err: () => None
|
|
5155
|
-
});
|
|
4973
|
+
if (next.isEmpty) return none$1;
|
|
4974
|
+
return toOptional(tryCatch(() => next.poll()));
|
|
5156
4975
|
},
|
|
5157
4976
|
dequeue() {
|
|
5158
|
-
if (next.isEmpty) return
|
|
5159
|
-
return tryCatch(() => next.dequeue()
|
|
5160
|
-
Ok: (value) => Some(value),
|
|
5161
|
-
Err: () => None
|
|
5162
|
-
});
|
|
4977
|
+
if (next.isEmpty) return none$1;
|
|
4978
|
+
return toOptional(tryCatch(() => next.dequeue()));
|
|
5163
4979
|
}
|
|
5164
4980
|
} });
|
|
5165
4981
|
};
|
|
@@ -5171,39 +4987,33 @@ function safeDeque() {
|
|
|
5171
4987
|
return (next) => {
|
|
5172
4988
|
return Object.assign(next, { safe: {
|
|
5173
4989
|
peek() {
|
|
5174
|
-
return next.isEmpty ?
|
|
4990
|
+
return next.isEmpty ? none$1 : some$1(next.peekFirst());
|
|
5175
4991
|
},
|
|
5176
4992
|
poll() {
|
|
5177
|
-
if (next.isEmpty) return
|
|
5178
|
-
return
|
|
4993
|
+
if (next.isEmpty) return none$1;
|
|
4994
|
+
return some$1(next.pollFirst());
|
|
5179
4995
|
},
|
|
5180
4996
|
dequeue() {
|
|
5181
|
-
if (next.isEmpty) return
|
|
5182
|
-
return tryCatch(() => next.dequeue()
|
|
5183
|
-
Ok: (value) => Some(value),
|
|
5184
|
-
Err: () => None
|
|
5185
|
-
});
|
|
4997
|
+
if (next.isEmpty) return none$1;
|
|
4998
|
+
return toOptional(tryCatch(() => next.dequeue()));
|
|
5186
4999
|
},
|
|
5187
5000
|
peekFirst() {
|
|
5188
|
-
return next.isEmpty ?
|
|
5001
|
+
return next.isEmpty ? none$1 : some$1(next.peekFirst());
|
|
5189
5002
|
},
|
|
5190
5003
|
peekLast() {
|
|
5191
|
-
return next.isEmpty ?
|
|
5004
|
+
return next.isEmpty ? none$1 : some$1(next.peekLast());
|
|
5192
5005
|
},
|
|
5193
5006
|
pollFirst() {
|
|
5194
5007
|
const result = next.pollFirst();
|
|
5195
|
-
return result !== void 0 ?
|
|
5008
|
+
return result !== void 0 ? some$1(result) : none$1;
|
|
5196
5009
|
},
|
|
5197
5010
|
pollLast() {
|
|
5198
5011
|
const result = next.pollLast();
|
|
5199
|
-
return result !== void 0 ?
|
|
5012
|
+
return result !== void 0 ? some$1(result) : none$1;
|
|
5200
5013
|
},
|
|
5201
5014
|
pop() {
|
|
5202
|
-
if (next.isEmpty) return
|
|
5203
|
-
return tryCatch(() => next.pop()
|
|
5204
|
-
Ok: (value) => Some(value),
|
|
5205
|
-
Err: () => None
|
|
5206
|
-
});
|
|
5015
|
+
if (next.isEmpty) return none$1;
|
|
5016
|
+
return toOptional(tryCatch(() => next.pop()));
|
|
5207
5017
|
}
|
|
5208
5018
|
} });
|
|
5209
5019
|
};
|
|
@@ -5215,7 +5025,7 @@ function safeMap() {
|
|
|
5215
5025
|
return (next) => {
|
|
5216
5026
|
return Object.assign(next, { safe: {
|
|
5217
5027
|
get(key) {
|
|
5218
|
-
return next.has(key) ?
|
|
5028
|
+
return next.has(key) ? some$1(next.get(key)) : none$1;
|
|
5219
5029
|
},
|
|
5220
5030
|
set(key, value) {
|
|
5221
5031
|
return tryCatch(() => next.set(key, value), (error) => String(error));
|
|
@@ -8098,6 +7908,6 @@ function stringHashCode(str) {
|
|
|
8098
7908
|
}
|
|
8099
7909
|
return hash;
|
|
8100
7910
|
}
|
|
8101
|
-
export { ArrayDeque, ArrayList, AsyncEmitter, AsyncQueue, Channel, CollectionBuilder, DEFAULT_CONFLICT_POLICY, DEFAULT_EVICTION_POLICY, DEFAULT_GROWTH_POLICY, DEFAULT_OVERFLOW_POLICY, DEFAULT_RETRY_POLICY, DEFAULT_UNDERFLOW_POLICY, DEFAULT_VALIDATION_POLICY, Emitter,
|
|
7911
|
+
export { ArrayDeque, ArrayList, AsyncEmitter, AsyncQueue, Channel, CollectionBuilder, DEFAULT_CONFLICT_POLICY, DEFAULT_EVICTION_POLICY, DEFAULT_GROWTH_POLICY, DEFAULT_OVERFLOW_POLICY, DEFAULT_RETRY_POLICY, DEFAULT_UNDERFLOW_POLICY, DEFAULT_VALIDATION_POLICY, Emitter, EventBatcher, HashMap, HashSet, KEY_DIGITS, LRUCache, LinkedHashMap, LinkedList, PriorityQueue, StorageListeners, SummaryTree, TTLCache, TreeMap, TreeSet, arrayContains, arrayDeque, arrayIterator, arrayList, arrayRemove, arrayRemoveAll, asSettled, asyncQueue, average, averagingNumber, blockMethods, booleanCompare, bounded, boundedDeque, boundedList, boundedMap, boundedQueue, buffered, buildList, buildMap, bundle, channel, checkBounds, checkInsertBounds, checkRangeBounds, chunk, clamp, collect, toArray as collectToArray, toList as collectToList, toMap as collectToMap, toSet as collectToSet, combineHashes, compareKeys, comparing, comparingKey, compose, concat, count, counting, createEmitter, dateCompare, deepEq, defaultCompare, defaultEq, defaultHash, delegate, distinct, distinctConsecutive, dropIndex, emptyList, emptyMap, emptyQueue, enhanceMethod, enumerate, eqBy, evented, eventedDeque, eventedList, eventedMap, eventedQueue, every, fanIn, fanOut, filter, filtering, find, findIndex, findLastIndex, first, flatMap, flatMapping, flatten, forwardEvent, groupBy, groupingBy, groupingByWith, hasCapability, hashBy, hashMap, hashNumber, hashSet, hashString, immutableListOf, immutableMapOf, interceptMethods, isValidKey, toMap$1 as iterToMap, joining, keyBetween, keysBetween, keysForDrop, last, linkedHashMap, linkedList, listOf, lruCache, lruMap, map, mapOf, mapping, max, maxBy, merge, min, minBy, mutableListOf, mutableMapOf, nCopies, none, nullsFirst, nullsLast, numberCompare, onWhen, partition, partitioningBy, pipe, pipeline, priorityQueue, queueOf, range, rangeTo, readWriteLock, readonly, readonlyCollection, readonlyList, readonlyMap, readonlyQueue, reduce, reducing, reducingWith, reentrantSynchronized, reportListenerError, requireNonEmpty, requireNonNull, reverse, reverseArrayIterator, reversed, safe, safeDeque, safeList, safeMap, safeQueue, scan, select, shallowEq, singletonList, singletonMap, skip, skipWhile, some, sort, stringCompare, stringCompareIgnoreCase, stringHashCode, sum, summarizingNumber, summaryTree, summingNumber, synchronized, synchronizedCollection, synchronizedList, synchronizedMap, synchronizedSet, take, takeWhile, tap, teeing, thenComparing, ticker, timeout, toArray$1 as toArray, toList$1 as toList, toMap$2 as toMap, toSet$1 as toSet, treeMap, treeSet, ttlCache, ttlCollection, ttlMap, unbuffered, unmodifiableCollection, unmodifiableList, unmodifiableMap, window, workerPool, zip };
|
|
8102
7912
|
|
|
8103
7913
|
//# sourceMappingURL=index.js.map
|