@rimbu/base 1.1.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bun/arr.mts +144 -27
- package/dist/cjs/arr.cjs +229 -166
- package/dist/cjs/arr.cjs.map +1 -0
- package/dist/cjs/arr.d.cts +33 -0
- package/dist/cjs/entry.cjs +9 -32
- package/dist/cjs/entry.cjs.map +1 -0
- package/dist/cjs/index.cjs +9 -246
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +5 -0
- package/dist/cjs/internal.cjs +4 -31
- package/dist/cjs/internal.cjs.map +1 -0
- package/dist/cjs/internal.d.cts +1 -0
- package/dist/cjs/plain-object.cjs +22 -32
- package/dist/cjs/plain-object.cjs.map +1 -0
- package/dist/cjs/rimbu-error.cjs +45 -66
- package/dist/cjs/rimbu-error.cjs.map +1 -0
- package/dist/cjs/token.cjs +4 -29
- package/dist/cjs/token.cjs.map +1 -0
- package/dist/esm/arr.d.mts +33 -0
- package/dist/esm/arr.mjs +86 -25
- package/dist/esm/arr.mjs.map +1 -1
- package/dist/esm/entry.d.mts +2 -0
- package/dist/esm/plain-object.d.mts +62 -0
- package/dist/esm/rimbu-error.d.mts +16 -0
- package/dist/esm/token.d.mts +2 -0
- package/package.json +19 -14
- package/src/arr.mts +144 -27
- package/dist/types/arr.d.mts +0 -17
- /package/dist/{types/entry.d.mts → cjs/entry.d.cts} +0 -0
- /package/dist/{types/plain-object.d.mts → cjs/plain-object.d.cts} +0 -0
- /package/dist/{types/rimbu-error.d.mts → cjs/rimbu-error.d.cts} +0 -0
- /package/dist/{types/token.d.mts → cjs/token.d.cts} +0 -0
- /package/dist/{types → esm}/index.d.mts +0 -0
- /package/dist/{types → esm}/internal.d.mts +0 -0
package/dist/bun/arr.mts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TraverseState, Update, type ArrayNonEmpty } from '@rimbu/common';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
export function _appendNew<T>(array: readonly T[], value: T): ArrayNonEmpty<T> {
|
|
4
|
+
return array.toSpliced(array.length, 0, value) as ArrayNonEmpty<T>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function _appendOld<T>(array: readonly T[], value: T): ArrayNonEmpty<T> {
|
|
5
8
|
const clone = array.slice();
|
|
6
9
|
clone.push(value);
|
|
7
10
|
return clone as ArrayNonEmpty<T>;
|
|
8
11
|
}
|
|
9
12
|
|
|
13
|
+
// Returns a copy of the array with the given value appended
|
|
14
|
+
export const append = `toSpliced` in Array.prototype ? _appendNew : _appendOld;
|
|
15
|
+
|
|
10
16
|
// Returns the concatenation of the two arrays, potentially reusing the input array if one of the arrays is empty
|
|
11
17
|
export function concat<T>(
|
|
12
18
|
first: readonly T[],
|
|
@@ -17,23 +23,41 @@ export function concat<T>(
|
|
|
17
23
|
return first.concat(second);
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
export function _reverseNew<T>(
|
|
27
|
+
array: readonly T[],
|
|
28
|
+
start?: number,
|
|
29
|
+
end?: number
|
|
30
|
+
): T[] {
|
|
31
|
+
const source =
|
|
32
|
+
undefined !== start || undefined !== end
|
|
33
|
+
? array.slice(start ?? 0, (end ?? array.length - 1) + 1)
|
|
34
|
+
: array;
|
|
35
|
+
|
|
36
|
+
return source.toReversed();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function _reverseOld<T>(
|
|
22
40
|
array: readonly T[],
|
|
23
|
-
start
|
|
24
|
-
end
|
|
41
|
+
start?: number,
|
|
42
|
+
end?: number
|
|
25
43
|
): T[] {
|
|
26
|
-
const
|
|
44
|
+
const _start = start ?? 0;
|
|
45
|
+
const _end = end ?? array.length - 1;
|
|
46
|
+
const length = _end - _start + 1;
|
|
27
47
|
const res = [] as T[];
|
|
28
48
|
|
|
29
|
-
let arrayIndex =
|
|
49
|
+
let arrayIndex = _start - 1;
|
|
30
50
|
let resIndex = length - 1;
|
|
31
51
|
|
|
32
|
-
while (++arrayIndex <=
|
|
52
|
+
while (++arrayIndex <= _end) res[resIndex--] = array[arrayIndex];
|
|
33
53
|
|
|
34
54
|
return res;
|
|
35
55
|
}
|
|
36
56
|
|
|
57
|
+
// Returns an copy of the array between the start and end indices, with the elements in reversed order.
|
|
58
|
+
export const reverse =
|
|
59
|
+
'toReversed' in Array.prototype ? _reverseNew : _reverseOld;
|
|
60
|
+
|
|
37
61
|
// Performs given function on each element of the array, in reverse order if 'reversed' is true.
|
|
38
62
|
export function forEach<T>(
|
|
39
63
|
array: readonly T[],
|
|
@@ -67,6 +91,11 @@ export function map<T, R>(
|
|
|
67
91
|
f: (value: T, index: number) => R,
|
|
68
92
|
indexOffset = 0
|
|
69
93
|
): R[] {
|
|
94
|
+
if (indexOffset === 0) {
|
|
95
|
+
// without offset, can use standard array map
|
|
96
|
+
return array.map(f);
|
|
97
|
+
}
|
|
98
|
+
|
|
70
99
|
const result: R[] = [];
|
|
71
100
|
|
|
72
101
|
let index = indexOffset;
|
|
@@ -95,59 +124,136 @@ export function reverseMap<T, R>(
|
|
|
95
124
|
return result;
|
|
96
125
|
}
|
|
97
126
|
|
|
98
|
-
|
|
99
|
-
|
|
127
|
+
export function _prependNew<T>(
|
|
128
|
+
array: readonly T[],
|
|
129
|
+
value: T
|
|
130
|
+
): ArrayNonEmpty<T> {
|
|
131
|
+
return array.toSpliced(0, 0, value) as ArrayNonEmpty<T>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function _prependOld<T>(
|
|
135
|
+
array: readonly T[],
|
|
136
|
+
value: T
|
|
137
|
+
): ArrayNonEmpty<T> {
|
|
100
138
|
const clone = array.slice();
|
|
101
139
|
clone.unshift(value);
|
|
102
140
|
return clone as ArrayNonEmpty<T>;
|
|
103
141
|
}
|
|
104
142
|
|
|
105
|
-
// Returns the
|
|
106
|
-
export
|
|
143
|
+
// Returns a copy of the given array with the given value added at the start
|
|
144
|
+
export const prepend =
|
|
145
|
+
`toSpliced` in Array.prototype ? _prependNew : _prependOld;
|
|
146
|
+
|
|
147
|
+
export function _lastNew<T>(arr: readonly T[]): T {
|
|
148
|
+
return arr.at(-1)!;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function _lastOld<T>(arr: readonly T[]): T {
|
|
107
152
|
return arr[arr.length - 1];
|
|
108
153
|
}
|
|
109
154
|
|
|
110
|
-
// Returns
|
|
111
|
-
|
|
112
|
-
|
|
155
|
+
// Returns the last element of the array
|
|
156
|
+
export const last = `at` in Array.prototype ? _lastNew : _lastOld;
|
|
157
|
+
|
|
158
|
+
export function _updateNew<T>(
|
|
159
|
+
arr: readonly T[],
|
|
160
|
+
index: number,
|
|
161
|
+
updater: Update<T>
|
|
162
|
+
): readonly T[] {
|
|
163
|
+
if (index < 0 || index >= arr.length) {
|
|
164
|
+
return arr;
|
|
165
|
+
}
|
|
166
|
+
const curValue = arr[index];
|
|
167
|
+
|
|
168
|
+
const newValue = Update(curValue, updater);
|
|
169
|
+
if (Object.is(newValue, curValue)) {
|
|
170
|
+
return arr;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return arr.with(index, newValue);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function _updateOld<T>(
|
|
113
177
|
arr: readonly T[],
|
|
114
178
|
index: number,
|
|
115
179
|
updater: Update<T>
|
|
116
180
|
): readonly T[] {
|
|
117
|
-
if (index < 0 || index >= arr.length)
|
|
181
|
+
if (index < 0 || index >= arr.length) {
|
|
182
|
+
return arr;
|
|
183
|
+
}
|
|
118
184
|
const curValue = arr[index];
|
|
119
185
|
|
|
120
186
|
const newValue = Update(curValue, updater);
|
|
121
|
-
if (Object.is(newValue, curValue))
|
|
187
|
+
if (Object.is(newValue, curValue)) {
|
|
188
|
+
return arr;
|
|
189
|
+
}
|
|
190
|
+
|
|
122
191
|
const newArr = arr.slice();
|
|
123
192
|
newArr[index] = newValue;
|
|
124
193
|
return newArr;
|
|
125
194
|
}
|
|
126
195
|
|
|
127
|
-
// Returns a copy of the array where the element at given index is replaced by
|
|
196
|
+
// Returns a copy of the array where the element at given index is replaced by the given updater.
|
|
128
197
|
// If the new element is the same as the old element, the original array is returned
|
|
129
|
-
export
|
|
198
|
+
export const update = `with` in Array.prototype ? _updateNew : _updateOld;
|
|
199
|
+
|
|
200
|
+
export function _modNew<T>(
|
|
130
201
|
arr: readonly T[],
|
|
131
202
|
index: number,
|
|
132
203
|
f: (value: T) => T
|
|
133
204
|
): readonly T[] {
|
|
134
|
-
if (index < 0 || index >= arr.length)
|
|
205
|
+
if (index < 0 || index >= arr.length) {
|
|
206
|
+
return arr;
|
|
207
|
+
}
|
|
135
208
|
|
|
136
209
|
const curValue = arr[index];
|
|
137
210
|
const newValue = f(curValue);
|
|
138
|
-
|
|
211
|
+
|
|
212
|
+
if (Object.is(newValue, curValue)) {
|
|
213
|
+
return arr;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return arr.with(index, newValue);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function _modOld<T>(
|
|
220
|
+
arr: readonly T[],
|
|
221
|
+
index: number,
|
|
222
|
+
f: (value: T) => T
|
|
223
|
+
): readonly T[] {
|
|
224
|
+
if (index < 0 || index >= arr.length) {
|
|
225
|
+
return arr;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const curValue = arr[index];
|
|
229
|
+
const newValue = f(curValue);
|
|
230
|
+
|
|
231
|
+
if (Object.is(newValue, curValue)) {
|
|
232
|
+
return arr;
|
|
233
|
+
}
|
|
234
|
+
|
|
139
235
|
const newArr = arr.slice();
|
|
140
236
|
newArr[index] = newValue;
|
|
141
237
|
return newArr;
|
|
142
238
|
}
|
|
143
239
|
|
|
144
|
-
// Returns a copy of the array where at given index
|
|
145
|
-
|
|
240
|
+
// Returns a copy of the array where the element at given index is replaced by applying given function.
|
|
241
|
+
// If the new element is the same as the old element, the original array is returned
|
|
242
|
+
export const mod = `with` in Array.prototype ? _modNew : _modOld;
|
|
243
|
+
|
|
244
|
+
export function _insertNew<T>(arr: readonly T[], index: number, value: T): T[] {
|
|
245
|
+
return arr.toSpliced(index, 0, value);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function _insertOld<T>(arr: readonly T[], index: number, value: T): T[] {
|
|
146
249
|
const clone = arr.slice();
|
|
147
250
|
clone.splice(index, 0, value);
|
|
148
251
|
return clone;
|
|
149
252
|
}
|
|
150
253
|
|
|
254
|
+
// Returns a copy of the array where at given index the given value is inserted
|
|
255
|
+
export const insert = `toSpliced` in Array.prototype ? _insertNew : _insertOld;
|
|
256
|
+
|
|
151
257
|
// Returns a copy of the array, without its first element
|
|
152
258
|
export function tail<T>(arr: readonly T[]): T[] {
|
|
153
259
|
return arr.slice(1);
|
|
@@ -158,8 +264,16 @@ export function init<T>(arr: readonly T[]): T[] {
|
|
|
158
264
|
return arr.slice(0, arr.length - 1);
|
|
159
265
|
}
|
|
160
266
|
|
|
161
|
-
|
|
162
|
-
|
|
267
|
+
export function _spliceNew<T>(
|
|
268
|
+
arr: readonly T[],
|
|
269
|
+
start: number,
|
|
270
|
+
deleteCount: number,
|
|
271
|
+
...items: T[]
|
|
272
|
+
): T[] {
|
|
273
|
+
return arr.toSpliced(start, deleteCount, ...items);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function _spliceOld<T>(
|
|
163
277
|
arr: readonly T[],
|
|
164
278
|
start: number,
|
|
165
279
|
deleteCount: number,
|
|
@@ -170,6 +284,9 @@ export function splice<T>(
|
|
|
170
284
|
return clone;
|
|
171
285
|
}
|
|
172
286
|
|
|
287
|
+
// Immutable version of the array .splice command, always returns a new array
|
|
288
|
+
export const splice = `toSpliced` in Array.prototype ? _spliceNew : _spliceOld;
|
|
289
|
+
|
|
173
290
|
// Returns a copy of the array, where its 'sparse' property is kept (sparse = not all indices have a value)
|
|
174
291
|
export function copySparse<T>(arr: readonly T[]): T[] {
|
|
175
292
|
const clone: T[] = [];
|
package/dist/cjs/arr.cjs
CHANGED
|
@@ -1,177 +1,240 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// src/arr.mts
|
|
21
|
-
var arr_exports = {};
|
|
22
|
-
__export(arr_exports, {
|
|
23
|
-
append: () => append,
|
|
24
|
-
concat: () => concat,
|
|
25
|
-
copySparse: () => copySparse,
|
|
26
|
-
forEach: () => forEach,
|
|
27
|
-
init: () => init,
|
|
28
|
-
insert: () => insert,
|
|
29
|
-
last: () => last,
|
|
30
|
-
map: () => map,
|
|
31
|
-
mapSparse: () => mapSparse,
|
|
32
|
-
mod: () => mod,
|
|
33
|
-
prepend: () => prepend,
|
|
34
|
-
reverse: () => reverse,
|
|
35
|
-
reverseMap: () => reverseMap,
|
|
36
|
-
splice: () => splice,
|
|
37
|
-
tail: () => tail,
|
|
38
|
-
update: () => update
|
|
39
|
-
});
|
|
40
|
-
module.exports = __toCommonJS(arr_exports);
|
|
41
|
-
var import_common = require("@rimbu/common");
|
|
42
|
-
function append(array, value) {
|
|
43
|
-
const clone = array.slice();
|
|
44
|
-
clone.push(value);
|
|
45
|
-
return clone;
|
|
46
|
-
}
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mapSparse = exports.copySparse = exports.splice = exports._spliceOld = exports._spliceNew = exports.init = exports.tail = exports.insert = exports._insertOld = exports._insertNew = exports.mod = exports._modOld = exports._modNew = exports.update = exports._updateOld = exports._updateNew = exports.last = exports._lastOld = exports._lastNew = exports.prepend = exports._prependOld = exports._prependNew = exports.reverseMap = exports.map = exports.forEach = exports.reverse = exports._reverseOld = exports._reverseNew = exports.concat = exports.append = exports._appendOld = exports._appendNew = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var common_1 = require("@rimbu/common");
|
|
6
|
+
function _appendNew(array, value) {
|
|
7
|
+
return array.toSpliced(array.length, 0, value);
|
|
8
|
+
}
|
|
9
|
+
exports._appendNew = _appendNew;
|
|
10
|
+
function _appendOld(array, value) {
|
|
11
|
+
var clone = array.slice();
|
|
12
|
+
clone.push(value);
|
|
13
|
+
return clone;
|
|
14
|
+
}
|
|
15
|
+
exports._appendOld = _appendOld;
|
|
16
|
+
// Returns a copy of the array with the given value appended
|
|
17
|
+
exports.append = "toSpliced" in Array.prototype ? _appendNew : _appendOld;
|
|
18
|
+
// Returns the concatenation of the two arrays, potentially reusing the input array if one of the arrays is empty
|
|
47
19
|
function concat(first, second) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
while (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
function
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
20
|
+
if (first.length === 0)
|
|
21
|
+
return second;
|
|
22
|
+
if (second.length === 0)
|
|
23
|
+
return first;
|
|
24
|
+
return first.concat(second);
|
|
25
|
+
}
|
|
26
|
+
exports.concat = concat;
|
|
27
|
+
function _reverseNew(array, start, end) {
|
|
28
|
+
var source = undefined !== start || undefined !== end
|
|
29
|
+
? array.slice(start !== null && start !== void 0 ? start : 0, (end !== null && end !== void 0 ? end : array.length - 1) + 1)
|
|
30
|
+
: array;
|
|
31
|
+
return source.toReversed();
|
|
32
|
+
}
|
|
33
|
+
exports._reverseNew = _reverseNew;
|
|
34
|
+
function _reverseOld(array, start, end) {
|
|
35
|
+
var _start = start !== null && start !== void 0 ? start : 0;
|
|
36
|
+
var _end = end !== null && end !== void 0 ? end : array.length - 1;
|
|
37
|
+
var length = _end - _start + 1;
|
|
38
|
+
var res = [];
|
|
39
|
+
var arrayIndex = _start - 1;
|
|
40
|
+
var resIndex = length - 1;
|
|
41
|
+
while (++arrayIndex <= _end)
|
|
42
|
+
res[resIndex--] = array[arrayIndex];
|
|
43
|
+
return res;
|
|
44
|
+
}
|
|
45
|
+
exports._reverseOld = _reverseOld;
|
|
46
|
+
// Returns an copy of the array between the start and end indices, with the elements in reversed order.
|
|
47
|
+
exports.reverse = 'toReversed' in Array.prototype ? _reverseNew : _reverseOld;
|
|
48
|
+
// Performs given function on each element of the array, in reverse order if 'reversed' is true.
|
|
49
|
+
function forEach(array, f, state, reversed) {
|
|
50
|
+
if (state === void 0) { state = (0, common_1.TraverseState)(); }
|
|
51
|
+
if (reversed === void 0) { reversed = false; }
|
|
52
|
+
if (state.halted)
|
|
53
|
+
return;
|
|
54
|
+
var halt = state.halt;
|
|
55
|
+
if (reversed) {
|
|
56
|
+
var i = array.length;
|
|
57
|
+
while (!state.halted && --i >= 0) {
|
|
58
|
+
f(array[i], state.nextIndex(), halt);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
var length = array.length;
|
|
63
|
+
var i = -1;
|
|
64
|
+
while (!state.halted && ++i < length) {
|
|
65
|
+
f(array[i], state.nextIndex(), halt);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.forEach = forEach;
|
|
70
|
+
// Returns a copy of the array where given function is applied to each element
|
|
71
|
+
function map(array, f, indexOffset) {
|
|
72
|
+
if (indexOffset === void 0) { indexOffset = 0; }
|
|
73
|
+
if (indexOffset === 0) {
|
|
74
|
+
// without offset, can use standard array map
|
|
75
|
+
return array.map(f);
|
|
76
|
+
}
|
|
77
|
+
var result = [];
|
|
78
|
+
var index = indexOffset;
|
|
79
|
+
var i = -1;
|
|
80
|
+
var length = array.length;
|
|
81
|
+
while (++i < length) {
|
|
82
|
+
result[i] = f(array[i], index++);
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
exports.map = map;
|
|
87
|
+
// Returns a copy of the array where given functio is applied to each element in reverse order
|
|
88
|
+
function reverseMap(array, f, indexOffset) {
|
|
89
|
+
if (indexOffset === void 0) { indexOffset = 0; }
|
|
90
|
+
var result = [];
|
|
91
|
+
var index = indexOffset;
|
|
92
|
+
var arrayIndex = array.length;
|
|
93
|
+
var resultIndex = 0;
|
|
94
|
+
while (--arrayIndex >= 0)
|
|
95
|
+
result[resultIndex++] = f(array[arrayIndex], index++);
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
exports.reverseMap = reverseMap;
|
|
99
|
+
function _prependNew(array, value) {
|
|
100
|
+
return array.toSpliced(0, 0, value);
|
|
101
|
+
}
|
|
102
|
+
exports._prependNew = _prependNew;
|
|
103
|
+
function _prependOld(array, value) {
|
|
104
|
+
var clone = array.slice();
|
|
105
|
+
clone.unshift(value);
|
|
106
|
+
return clone;
|
|
107
|
+
}
|
|
108
|
+
exports._prependOld = _prependOld;
|
|
109
|
+
// Returns a copy of the given array with the given value added at the start
|
|
110
|
+
exports.prepend = "toSpliced" in Array.prototype ? _prependNew : _prependOld;
|
|
111
|
+
function _lastNew(arr) {
|
|
112
|
+
return arr.at(-1);
|
|
113
|
+
}
|
|
114
|
+
exports._lastNew = _lastNew;
|
|
115
|
+
function _lastOld(arr) {
|
|
116
|
+
return arr[arr.length - 1];
|
|
117
|
+
}
|
|
118
|
+
exports._lastOld = _lastOld;
|
|
119
|
+
// Returns the last element of the array
|
|
120
|
+
exports.last = "at" in Array.prototype ? _lastNew : _lastOld;
|
|
121
|
+
function _updateNew(arr, index, updater) {
|
|
122
|
+
if (index < 0 || index >= arr.length) {
|
|
123
|
+
return arr;
|
|
124
|
+
}
|
|
125
|
+
var curValue = arr[index];
|
|
126
|
+
var newValue = (0, common_1.Update)(curValue, updater);
|
|
127
|
+
if (Object.is(newValue, curValue)) {
|
|
128
|
+
return arr;
|
|
129
|
+
}
|
|
130
|
+
return arr.with(index, newValue);
|
|
131
|
+
}
|
|
132
|
+
exports._updateNew = _updateNew;
|
|
133
|
+
function _updateOld(arr, index, updater) {
|
|
134
|
+
if (index < 0 || index >= arr.length) {
|
|
135
|
+
return arr;
|
|
136
|
+
}
|
|
137
|
+
var curValue = arr[index];
|
|
138
|
+
var newValue = (0, common_1.Update)(curValue, updater);
|
|
139
|
+
if (Object.is(newValue, curValue)) {
|
|
140
|
+
return arr;
|
|
141
|
+
}
|
|
142
|
+
var newArr = arr.slice();
|
|
143
|
+
newArr[index] = newValue;
|
|
144
|
+
return newArr;
|
|
145
|
+
}
|
|
146
|
+
exports._updateOld = _updateOld;
|
|
147
|
+
// Returns a copy of the array where the element at given index is replaced by the given updater.
|
|
148
|
+
// If the new element is the same as the old element, the original array is returned
|
|
149
|
+
exports.update = "with" in Array.prototype ? _updateNew : _updateOld;
|
|
150
|
+
function _modNew(arr, index, f) {
|
|
151
|
+
if (index < 0 || index >= arr.length) {
|
|
152
|
+
return arr;
|
|
153
|
+
}
|
|
154
|
+
var curValue = arr[index];
|
|
155
|
+
var newValue = f(curValue);
|
|
156
|
+
if (Object.is(newValue, curValue)) {
|
|
157
|
+
return arr;
|
|
158
|
+
}
|
|
159
|
+
return arr.with(index, newValue);
|
|
133
160
|
}
|
|
161
|
+
exports._modNew = _modNew;
|
|
162
|
+
function _modOld(arr, index, f) {
|
|
163
|
+
if (index < 0 || index >= arr.length) {
|
|
164
|
+
return arr;
|
|
165
|
+
}
|
|
166
|
+
var curValue = arr[index];
|
|
167
|
+
var newValue = f(curValue);
|
|
168
|
+
if (Object.is(newValue, curValue)) {
|
|
169
|
+
return arr;
|
|
170
|
+
}
|
|
171
|
+
var newArr = arr.slice();
|
|
172
|
+
newArr[index] = newValue;
|
|
173
|
+
return newArr;
|
|
174
|
+
}
|
|
175
|
+
exports._modOld = _modOld;
|
|
176
|
+
// Returns a copy of the array where the element at given index is replaced by applying given function.
|
|
177
|
+
// If the new element is the same as the old element, the original array is returned
|
|
178
|
+
exports.mod = "with" in Array.prototype ? _modNew : _modOld;
|
|
179
|
+
function _insertNew(arr, index, value) {
|
|
180
|
+
return arr.toSpliced(index, 0, value);
|
|
181
|
+
}
|
|
182
|
+
exports._insertNew = _insertNew;
|
|
183
|
+
function _insertOld(arr, index, value) {
|
|
184
|
+
var clone = arr.slice();
|
|
185
|
+
clone.splice(index, 0, value);
|
|
186
|
+
return clone;
|
|
187
|
+
}
|
|
188
|
+
exports._insertOld = _insertOld;
|
|
189
|
+
// Returns a copy of the array where at given index the given value is inserted
|
|
190
|
+
exports.insert = "toSpliced" in Array.prototype ? _insertNew : _insertOld;
|
|
191
|
+
// Returns a copy of the array, without its first element
|
|
134
192
|
function tail(arr) {
|
|
135
|
-
|
|
193
|
+
return arr.slice(1);
|
|
136
194
|
}
|
|
195
|
+
exports.tail = tail;
|
|
196
|
+
// Returns a copy of the array, without its last element
|
|
137
197
|
function init(arr) {
|
|
138
|
-
|
|
198
|
+
return arr.slice(0, arr.length - 1);
|
|
139
199
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
200
|
+
exports.init = init;
|
|
201
|
+
function _spliceNew(arr, start, deleteCount) {
|
|
202
|
+
var items = [];
|
|
203
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
204
|
+
items[_i - 3] = arguments[_i];
|
|
205
|
+
}
|
|
206
|
+
return arr.toSpliced.apply(arr, tslib_1.__spreadArray([start, deleteCount], tslib_1.__read(items), false));
|
|
144
207
|
}
|
|
208
|
+
exports._spliceNew = _spliceNew;
|
|
209
|
+
function _spliceOld(arr, start, deleteCount) {
|
|
210
|
+
var items = [];
|
|
211
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
212
|
+
items[_i - 3] = arguments[_i];
|
|
213
|
+
}
|
|
214
|
+
var clone = arr.slice();
|
|
215
|
+
clone.splice.apply(clone, tslib_1.__spreadArray([start, deleteCount], tslib_1.__read(items), false));
|
|
216
|
+
return clone;
|
|
217
|
+
}
|
|
218
|
+
exports._spliceOld = _spliceOld;
|
|
219
|
+
// Immutable version of the array .splice command, always returns a new array
|
|
220
|
+
exports.splice = "toSpliced" in Array.prototype ? _spliceNew : _spliceOld;
|
|
221
|
+
// Returns a copy of the array, where its 'sparse' property is kept (sparse = not all indices have a value)
|
|
145
222
|
function copySparse(arr) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
223
|
+
var clone = [];
|
|
224
|
+
for (var key in arr) {
|
|
225
|
+
clone[key] = arr[key];
|
|
226
|
+
}
|
|
227
|
+
return clone;
|
|
151
228
|
}
|
|
229
|
+
exports.copySparse = copySparse;
|
|
230
|
+
// Returns a copy of the array with given function applied to each element, where its 'sparse' property is kept
|
|
231
|
+
// (sparse = not all indices have a value)
|
|
152
232
|
function mapSparse(arr, f) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
append,
|
|
162
|
-
concat,
|
|
163
|
-
copySparse,
|
|
164
|
-
forEach,
|
|
165
|
-
init,
|
|
166
|
-
insert,
|
|
167
|
-
last,
|
|
168
|
-
map,
|
|
169
|
-
mapSparse,
|
|
170
|
-
mod,
|
|
171
|
-
prepend,
|
|
172
|
-
reverse,
|
|
173
|
-
reverseMap,
|
|
174
|
-
splice,
|
|
175
|
-
tail,
|
|
176
|
-
update
|
|
177
|
-
});
|
|
233
|
+
var result = Array(arr.length);
|
|
234
|
+
for (var key in arr) {
|
|
235
|
+
result[key] = f(arr[key], key);
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
exports.mapSparse = mapSparse;
|
|
240
|
+
//# sourceMappingURL=arr.cjs.map
|