utilitish 0.0.18 → 0.0.20
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/array-constructor.js +10 -12
- package/dist/array/array-constructor.spec.js +2 -4
- package/dist/array/array-prototype.js +55 -57
- package/dist/array/array-prototype.spec.js +1 -3
- package/dist/index.js +7 -13
- package/dist/map/map-prototype.js +12 -14
- package/dist/map/map-prototype.spec.js +1 -3
- package/dist/object/object-prototype.js +8 -10
- package/dist/object/object-prototype.spec.js +1 -3
- package/dist/set/set-prototype.js +10 -12
- package/dist/set/set-prototype.spec.js +1 -3
- package/dist/string/string-prototype.js +35 -37
- package/dist/string/string-prototype.spec.js +23 -25
- package/dist/utils/core.utils.js +6 -15
- package/dist/utils/core.utils.spec.js +29 -31
- package/dist/utils/logic.utils.js +6 -9
- package/dist/utils/logic.utils.spec.js +13 -15
- package/dist/utils/slugify.config.js +9 -21
- package/dist/utils/slugify.config.spec.js +40 -42
- package/package.json +8 -1
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const core_utils_1 = require("../utils/core.utils");
|
|
4
|
-
const core_utils_2 = require("./../utils/core.utils");
|
|
1
|
+
import { defineStaticIfNotExists } from '../utils/core.utils';
|
|
2
|
+
import { utilitishError } from './../utils/core.utils';
|
|
5
3
|
/**
|
|
6
4
|
* @see Array.zip
|
|
7
5
|
*/
|
|
8
|
-
|
|
6
|
+
defineStaticIfNotExists(Array, 'zip', function (...arrays) {
|
|
9
7
|
if (arrays.length === 0)
|
|
10
8
|
return [];
|
|
11
9
|
const maxLen = Math.max(...arrays.map((arr) => arr.length));
|
|
@@ -14,13 +12,13 @@ const core_utils_2 = require("./../utils/core.utils");
|
|
|
14
12
|
/**
|
|
15
13
|
* @see Array.range
|
|
16
14
|
*/
|
|
17
|
-
|
|
15
|
+
defineStaticIfNotExists(Array, 'range', function (start, end, step = 1) {
|
|
18
16
|
if (end === undefined) {
|
|
19
17
|
end = start;
|
|
20
18
|
start = 0;
|
|
21
19
|
}
|
|
22
20
|
if (step === 0)
|
|
23
|
-
|
|
21
|
+
utilitishError('Array.range', 'step must not be 0', undefined, RangeError);
|
|
24
22
|
const result = [];
|
|
25
23
|
const condition = step > 0 ? (i) => i < end : (i) => i > end;
|
|
26
24
|
for (let i = start; condition(i); i += step) {
|
|
@@ -31,19 +29,19 @@ const core_utils_2 = require("./../utils/core.utils");
|
|
|
31
29
|
/**
|
|
32
30
|
* @see Array.repeat
|
|
33
31
|
*/
|
|
34
|
-
|
|
32
|
+
defineStaticIfNotExists(Array, 'repeat', function (length, value) {
|
|
35
33
|
if (typeof length !== 'number')
|
|
36
|
-
|
|
34
|
+
utilitishError('Array.repeat', 'n must be a number', length);
|
|
37
35
|
if (!Number.isInteger(length))
|
|
38
|
-
|
|
36
|
+
utilitishError('Array.repeat', 'n must be an integer', length);
|
|
39
37
|
if (length < 0)
|
|
40
|
-
|
|
38
|
+
utilitishError('Array.repeat', 'n must be non-negative', length, RangeError);
|
|
41
39
|
return Array.from({ length }, () => (typeof value === 'function' ? value() : value));
|
|
42
40
|
});
|
|
43
41
|
/**
|
|
44
42
|
* @see Array.create
|
|
45
43
|
*/
|
|
46
|
-
|
|
44
|
+
defineStaticIfNotExists(Array, 'create', function (valueOrFactory, ...sizes) {
|
|
47
45
|
if (sizes.length === 0)
|
|
48
46
|
return [];
|
|
49
47
|
if (!sizes.every((s) => Number.isInteger(s) && s >= 0)) {
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require("../array/array-constructor");
|
|
4
|
-
require("./array-constructor");
|
|
1
|
+
import '../array/array-constructor';
|
|
2
|
+
import './array-constructor';
|
|
5
3
|
describe('Array', () => {
|
|
6
4
|
describe('range()', () => {
|
|
7
5
|
it('should generate a range from 0 to n-1 if only start is given', () => {
|
|
@@ -1,47 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const core_utils_1 = require("../utils/core.utils");
|
|
4
|
-
const logic_utils_1 = require("../utils/logic.utils");
|
|
1
|
+
import { defineIfNotExists, isNumberOrString, resolveSelector, utilitishError } from '../utils/core.utils';
|
|
2
|
+
import { sortBy } from '../utils/logic.utils';
|
|
5
3
|
/**
|
|
6
4
|
* @see Array.prototype.first
|
|
7
5
|
*/
|
|
8
|
-
|
|
6
|
+
defineIfNotExists(Array.prototype, 'first', function () {
|
|
9
7
|
return this[0];
|
|
10
8
|
});
|
|
11
9
|
/**
|
|
12
10
|
* @see Array.prototype.last
|
|
13
11
|
*/
|
|
14
|
-
|
|
12
|
+
defineIfNotExists(Array.prototype, 'last', function () {
|
|
15
13
|
return this[this.length - 1];
|
|
16
14
|
});
|
|
17
15
|
/**
|
|
18
16
|
* @see Array.prototype.sum
|
|
19
17
|
*/
|
|
20
|
-
|
|
18
|
+
defineIfNotExists(Array.prototype, 'sum', function (selector) {
|
|
21
19
|
if (this.length === 0)
|
|
22
20
|
return 0;
|
|
23
|
-
const getValue =
|
|
21
|
+
const getValue = resolveSelector(selector, (item) => item);
|
|
24
22
|
if (!this.every((item) => typeof getValue(item) === 'number')) {
|
|
25
|
-
|
|
23
|
+
utilitishError('Array.prototype.sum', 'requires a selector that returns a number unless array is number[]');
|
|
26
24
|
}
|
|
27
25
|
return this.reduce((acc, item) => getValue(item) + acc, 0);
|
|
28
26
|
});
|
|
29
27
|
/**
|
|
30
28
|
* @see Array.prototype.unique
|
|
31
29
|
*/
|
|
32
|
-
|
|
30
|
+
defineIfNotExists(Array.prototype, 'unique', function () {
|
|
33
31
|
return [...new Set(this)];
|
|
34
32
|
});
|
|
35
33
|
/**
|
|
36
34
|
* @see Array.prototype.chunk
|
|
37
35
|
*/
|
|
38
|
-
|
|
36
|
+
defineIfNotExists(Array.prototype, 'chunk', function (size) {
|
|
39
37
|
if (typeof size !== 'number')
|
|
40
|
-
|
|
38
|
+
utilitishError('Array.prototype.chunk', 'size must be a number', size);
|
|
41
39
|
if (!Number.isInteger(size))
|
|
42
|
-
|
|
40
|
+
utilitishError('Array.prototype.chunk', 'size must be an integer', size);
|
|
43
41
|
if (size <= 0)
|
|
44
|
-
|
|
42
|
+
utilitishError('Array.prototype.chunk', 'size must be positive', size, RangeError);
|
|
45
43
|
const result = [];
|
|
46
44
|
for (let i = 0; i < this.length; i += size) {
|
|
47
45
|
result.push(this.slice(i, i + size));
|
|
@@ -51,20 +49,20 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
51
49
|
/**
|
|
52
50
|
* @see Array.prototype.average
|
|
53
51
|
*/
|
|
54
|
-
|
|
52
|
+
defineIfNotExists(Array.prototype, 'average', function (selector) {
|
|
55
53
|
if (this.length === 0)
|
|
56
54
|
return 0;
|
|
57
|
-
const getValue =
|
|
55
|
+
const getValue = resolveSelector(selector, (item) => item);
|
|
58
56
|
if (!this.every((item) => typeof getValue(item) === 'number')) {
|
|
59
|
-
|
|
57
|
+
utilitishError('Array.prototype.average', 'requires a selector that returns a number unless array is number[]');
|
|
60
58
|
}
|
|
61
59
|
return this.reduce((acc, item) => getValue(item) + acc, 0) / this.length;
|
|
62
60
|
});
|
|
63
61
|
/**
|
|
64
62
|
* @see Array.prototype.groupBy
|
|
65
63
|
*/
|
|
66
|
-
|
|
67
|
-
const getKey =
|
|
64
|
+
defineIfNotExists(Array.prototype, 'groupBy', function (selector) {
|
|
65
|
+
const getKey = resolveSelector(selector, (item) => item);
|
|
68
66
|
const map = new Map();
|
|
69
67
|
for (const item of this) {
|
|
70
68
|
const key = getKey(item);
|
|
@@ -78,31 +76,31 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
78
76
|
/**
|
|
79
77
|
* @see Array.prototype.compact
|
|
80
78
|
*/
|
|
81
|
-
|
|
79
|
+
defineIfNotExists(Array.prototype, 'compact', function () {
|
|
82
80
|
return this.filter(Boolean);
|
|
83
81
|
});
|
|
84
82
|
/**
|
|
85
83
|
* @see Array.prototype.enumerate
|
|
86
84
|
*/
|
|
87
|
-
|
|
85
|
+
defineIfNotExists(Array.prototype, 'enumerate', function () {
|
|
88
86
|
return this.map((value, index) => [value, index]);
|
|
89
87
|
});
|
|
90
88
|
/**
|
|
91
89
|
* @see Array.prototype.sortAsc
|
|
92
90
|
* @see Array.prototype.sortDesc
|
|
93
91
|
*/
|
|
94
|
-
|
|
92
|
+
defineIfNotExists(Array.prototype, 'sortBy', function (direction, selector) {
|
|
95
93
|
if (this.length === 0)
|
|
96
94
|
return this.slice();
|
|
97
|
-
const getValue =
|
|
98
|
-
if (!selector && !this.every((item) =>
|
|
99
|
-
|
|
95
|
+
const getValue = resolveSelector(selector, (item) => item);
|
|
96
|
+
if (!selector && !this.every((item) => isNumberOrString(item))) {
|
|
97
|
+
utilitishError('Array.prototype.sortBy', 'array elements must be number or string if no selector is provided');
|
|
100
98
|
}
|
|
101
99
|
return this.slice().sort((a, b) => {
|
|
102
100
|
const valA = getValue(a);
|
|
103
101
|
const valB = getValue(b);
|
|
104
|
-
if (!
|
|
105
|
-
|
|
102
|
+
if (!isNumberOrString(valA) || !isNumberOrString(valB)) {
|
|
103
|
+
utilitishError('Array.prototype.sortBy', 'selector must return number or string');
|
|
106
104
|
}
|
|
107
105
|
if (valA > valB)
|
|
108
106
|
return direction === 'asc' ? 1 : -1;
|
|
@@ -114,31 +112,31 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
114
112
|
/**
|
|
115
113
|
* @see Array.prototype.sortAsc
|
|
116
114
|
*/
|
|
117
|
-
|
|
118
|
-
return
|
|
115
|
+
defineIfNotExists(Array.prototype, 'sortAsc', function (selector) {
|
|
116
|
+
return sortBy(this, 'asc', selector);
|
|
119
117
|
});
|
|
120
118
|
/**
|
|
121
119
|
* @see Array.prototype.sortDesc
|
|
122
120
|
*/
|
|
123
|
-
|
|
124
|
-
return
|
|
121
|
+
defineIfNotExists(Array.prototype, 'sortDesc', function (selector) {
|
|
122
|
+
return sortBy(this, 'desc', selector);
|
|
125
123
|
});
|
|
126
124
|
/**
|
|
127
125
|
* @see Array.prototype.swap
|
|
128
126
|
*/
|
|
129
|
-
|
|
127
|
+
defineIfNotExists(Array.prototype, 'swap', function (i, j) {
|
|
130
128
|
if (typeof i !== 'number')
|
|
131
|
-
|
|
129
|
+
utilitishError('Array.prototype.swap', 'i must be a number', i);
|
|
132
130
|
if (typeof j !== 'number')
|
|
133
|
-
|
|
131
|
+
utilitishError('Array.prototype.swap', 'j must be a number', j);
|
|
134
132
|
if (!Number.isInteger(i))
|
|
135
|
-
|
|
133
|
+
utilitishError('Array.prototype.swap', 'i must be an integer', i);
|
|
136
134
|
if (!Number.isInteger(j))
|
|
137
|
-
|
|
135
|
+
utilitishError('Array.prototype.swap', 'j must be an integer', j);
|
|
138
136
|
if (i < 0 || i >= this.length)
|
|
139
|
-
|
|
137
|
+
utilitishError('Array.prototype.swap', 'i is out of bounds', i, RangeError);
|
|
140
138
|
if (j < 0 || j >= this.length)
|
|
141
|
-
|
|
139
|
+
utilitishError('Array.prototype.swap', 'j is out of bounds', j, RangeError);
|
|
142
140
|
if (i !== j) {
|
|
143
141
|
const temp = this[i];
|
|
144
142
|
this[i] = this[j];
|
|
@@ -149,7 +147,7 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
149
147
|
/**
|
|
150
148
|
* @see Array.prototype.shuffle
|
|
151
149
|
*/
|
|
152
|
-
|
|
150
|
+
defineIfNotExists(Array.prototype, 'shuffle', function () {
|
|
153
151
|
const arr = this.slice();
|
|
154
152
|
for (let i = arr.length - 1; i > 0; i--) {
|
|
155
153
|
const j = Math.floor(Math.random() * (i + 1));
|
|
@@ -160,13 +158,13 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
160
158
|
/**
|
|
161
159
|
* @see Array.prototype.toMap
|
|
162
160
|
*/
|
|
163
|
-
|
|
161
|
+
defineIfNotExists(Array.prototype, 'toMap', function (keySelector, valueSelector) {
|
|
164
162
|
if (!keySelector && this.length && this.every((item) => Array.isArray(item) && item.length === 2)) {
|
|
165
163
|
return new Map(this);
|
|
166
164
|
}
|
|
167
165
|
const map = new Map();
|
|
168
|
-
const getKey =
|
|
169
|
-
const getValue =
|
|
166
|
+
const getKey = resolveSelector(keySelector, (_, index) => index);
|
|
167
|
+
const getValue = resolveSelector(valueSelector, (item) => item);
|
|
170
168
|
for (let i = 0; i < this.length; i++) {
|
|
171
169
|
const item = this[i];
|
|
172
170
|
const key = getKey(item, i);
|
|
@@ -178,14 +176,14 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
178
176
|
/**
|
|
179
177
|
* @see Array.prototype.toObject
|
|
180
178
|
*/
|
|
181
|
-
|
|
179
|
+
defineIfNotExists(Array.prototype, 'toObject', function (keySelector, valueSelector) {
|
|
182
180
|
let entries;
|
|
183
181
|
if (!keySelector && this.length && this.every((item) => Array.isArray(item) && item.length === 2)) {
|
|
184
182
|
entries = this;
|
|
185
183
|
}
|
|
186
184
|
else {
|
|
187
|
-
const getKey =
|
|
188
|
-
const getValue =
|
|
185
|
+
const getKey = resolveSelector(keySelector, (_, index) => index);
|
|
186
|
+
const getValue = resolveSelector(valueSelector, (item) => item);
|
|
189
187
|
entries = this.map((item, index) => [getKey(item, index), getValue(item)]);
|
|
190
188
|
}
|
|
191
189
|
// Validate keys
|
|
@@ -203,15 +201,15 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
203
201
|
/**
|
|
204
202
|
* @see Array.prototype.toSet
|
|
205
203
|
*/
|
|
206
|
-
|
|
207
|
-
const getValue =
|
|
204
|
+
defineIfNotExists(Array.prototype, 'toSet', function (selector) {
|
|
205
|
+
const getValue = resolveSelector(selector, (item) => item);
|
|
208
206
|
return new Set(this.map(getValue));
|
|
209
207
|
});
|
|
210
208
|
/**
|
|
211
209
|
* @see Array.prototype.countBy
|
|
212
210
|
*/
|
|
213
|
-
|
|
214
|
-
const getKey =
|
|
211
|
+
defineIfNotExists(Array.prototype, 'countBy', function (selector) {
|
|
212
|
+
const getKey = resolveSelector(selector, (item) => item);
|
|
215
213
|
const map = new Map();
|
|
216
214
|
for (const item of this) {
|
|
217
215
|
const key = getKey(item);
|
|
@@ -222,21 +220,21 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
222
220
|
/**
|
|
223
221
|
* @see Array.prototype.slugifyIncludes
|
|
224
222
|
*/
|
|
225
|
-
|
|
223
|
+
defineIfNotExists(Array.prototype, 'slugifyIncludes', function (value) {
|
|
226
224
|
if (typeof value !== 'string')
|
|
227
|
-
|
|
225
|
+
utilitishError('Array.prototype.slugifyIncludes', 'must be a string', value);
|
|
228
226
|
const slugified = value.slugify();
|
|
229
227
|
return this.some((item) => {
|
|
230
228
|
if (typeof item !== 'string')
|
|
231
|
-
|
|
229
|
+
utilitishError('Array.prototype.slugifyIncludes', 'all array items must be strings', item);
|
|
232
230
|
return item.slugify() === slugified;
|
|
233
231
|
});
|
|
234
232
|
});
|
|
235
233
|
/**
|
|
236
234
|
* @see Array.prototype.groupByMany
|
|
237
235
|
*/
|
|
238
|
-
|
|
239
|
-
const getKeys =
|
|
236
|
+
defineIfNotExists(Array.prototype, 'groupByMany', function (selector) {
|
|
237
|
+
const getKeys = resolveSelector(selector);
|
|
240
238
|
const result = new Map();
|
|
241
239
|
for (const item of this) {
|
|
242
240
|
const keys = getKeys(item);
|
|
@@ -251,10 +249,10 @@ const logic_utils_1 = require("../utils/logic.utils");
|
|
|
251
249
|
/**
|
|
252
250
|
* @see Array.prototype.classify
|
|
253
251
|
*/
|
|
254
|
-
|
|
252
|
+
defineIfNotExists(Array.prototype, 'classify', function (predicates) {
|
|
255
253
|
if (typeof predicates !== 'object' || predicates === null)
|
|
256
|
-
|
|
257
|
-
const resolvedPredicates = Object.fromEntries(Object.entries(predicates).map(([key, fn]) => [key,
|
|
254
|
+
utilitishError('Array.prototype.classify', 'predicates must be a non-null object', predicates);
|
|
255
|
+
const resolvedPredicates = Object.fromEntries(Object.entries(predicates).map(([key, fn]) => [key, resolveSelector(fn)]));
|
|
258
256
|
const entries = Object.entries(resolvedPredicates);
|
|
259
257
|
const result = new Map(entries.map(([key]) => [key, []]));
|
|
260
258
|
for (const item of this) {
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
require("./array-prototype");
|
|
1
|
+
import './array-prototype';
|
|
4
2
|
describe('Array.prototype', () => {
|
|
5
3
|
describe('first()', () => {
|
|
6
4
|
it('should return first element of non-empty array', () => {
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
require("./object/object-prototype");
|
|
8
|
-
require("./set/set-prototype");
|
|
9
|
-
require("./string/string-prototype");
|
|
1
|
+
import './array/array-constructor';
|
|
2
|
+
import './array/array-prototype';
|
|
3
|
+
import './map/map-prototype';
|
|
4
|
+
import './object/object-prototype';
|
|
5
|
+
import './set/set-prototype';
|
|
6
|
+
import './string/string-prototype';
|
|
10
7
|
// Export slugify configuration functions
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "getSlugifyConfig", { enumerable: true, get: function () { return slugify_config_1.getSlugifyConfig; } });
|
|
13
|
-
Object.defineProperty(exports, "resetSlugifyConfig", { enumerable: true, get: function () { return slugify_config_1.resetSlugifyConfig; } });
|
|
14
|
-
Object.defineProperty(exports, "setSlugifyConfig", { enumerable: true, get: function () { return slugify_config_1.setSlugifyConfig; } });
|
|
8
|
+
export { getSlugifyConfig, resetSlugifyConfig, setSlugifyConfig } from './utils/slugify.config';
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const core_utils_1 = require("../utils/core.utils");
|
|
1
|
+
import { defineIfNotExists, utilitishError } from '../utils/core.utils';
|
|
4
2
|
/**
|
|
5
3
|
* @see Map.prototype.toList
|
|
6
4
|
*/
|
|
7
|
-
|
|
5
|
+
defineIfNotExists(Map.prototype, 'toList', function (type = 'entries') {
|
|
8
6
|
switch (type) {
|
|
9
7
|
case 'keys':
|
|
10
8
|
return Array.from(this.keys());
|
|
@@ -14,7 +12,7 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
14
12
|
const obj = {};
|
|
15
13
|
for (const [key, value] of this) {
|
|
16
14
|
if (typeof key !== 'string' && typeof key !== 'number' && typeof key !== 'symbol') {
|
|
17
|
-
|
|
15
|
+
utilitishError('Map.prototype.toList', `only supports string, number, or symbol keys`, key);
|
|
18
16
|
}
|
|
19
17
|
obj[String(key)] = value;
|
|
20
18
|
}
|
|
@@ -23,22 +21,22 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
23
21
|
case 'entries':
|
|
24
22
|
return Array.from(this.entries());
|
|
25
23
|
default:
|
|
26
|
-
return
|
|
24
|
+
return utilitishError('Map.prototype.toList', `unknown type`, type);
|
|
27
25
|
}
|
|
28
26
|
});
|
|
29
27
|
/**
|
|
30
28
|
* @see Map.prototype.toObject
|
|
31
29
|
*/
|
|
32
|
-
|
|
30
|
+
defineIfNotExists(Map.prototype, 'toObject', function () {
|
|
33
31
|
const entries = Array.from(this.entries());
|
|
34
32
|
for (const [key] of entries) {
|
|
35
33
|
if (key === null)
|
|
36
|
-
|
|
34
|
+
utilitishError('Map.prototype.toObject', 'key cannot be null');
|
|
37
35
|
if (key === undefined)
|
|
38
|
-
|
|
36
|
+
utilitishError('Map.prototype.toObject', 'key cannot be undefined');
|
|
39
37
|
const keyType = typeof key;
|
|
40
38
|
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
|
|
41
|
-
|
|
39
|
+
utilitishError('Map.prototype.toObject', `keys must be string, number, or symbol`, key);
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
42
|
return Object.fromEntries(entries);
|
|
@@ -46,18 +44,18 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
46
44
|
/**
|
|
47
45
|
* @see Map.prototype.ensureArray
|
|
48
46
|
*/
|
|
49
|
-
|
|
47
|
+
defineIfNotExists(Map.prototype, 'ensureArray', function (key) {
|
|
50
48
|
if (key === null)
|
|
51
|
-
|
|
49
|
+
utilitishError('Map.prototype.ensureArray', 'key cannot be null');
|
|
52
50
|
if (key === undefined)
|
|
53
|
-
|
|
51
|
+
utilitishError('Map.prototype.ensureArray', 'key cannot be undefined');
|
|
54
52
|
let arr = this.get(key);
|
|
55
53
|
if (arr === undefined) {
|
|
56
54
|
arr = [];
|
|
57
55
|
this.set(key, arr);
|
|
58
56
|
}
|
|
59
57
|
if (!Array.isArray(arr)) {
|
|
60
|
-
|
|
58
|
+
utilitishError('Map.prototype.ensureArray', 'value for the key is not an array', arr);
|
|
61
59
|
}
|
|
62
60
|
return arr;
|
|
63
61
|
});
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const core_utils_1 = require("../utils/core.utils");
|
|
1
|
+
import { defineIfNotExists, utilitishError } from '../utils/core.utils';
|
|
4
2
|
/**
|
|
5
3
|
* @see Object.prototype.deepClone
|
|
6
4
|
*/
|
|
7
|
-
|
|
5
|
+
defineIfNotExists(Object.prototype, 'deepClone', function () {
|
|
8
6
|
return structuredClone(this);
|
|
9
7
|
});
|
|
10
8
|
/**
|
|
11
9
|
* @see Object.prototype.deepMerge
|
|
12
10
|
*/
|
|
13
|
-
|
|
11
|
+
defineIfNotExists(Object.prototype, 'deepMerge', function (source) {
|
|
14
12
|
if (typeof source !== 'object')
|
|
15
|
-
|
|
13
|
+
utilitishError('Object.prototype.deepMerge', 'source must be an object', source);
|
|
16
14
|
if (source === null)
|
|
17
|
-
|
|
15
|
+
utilitishError('Object.prototype.deepMerge', 'source cannot be null', source);
|
|
18
16
|
const isObject = (val) => val !== null && typeof val === 'object' && !Array.isArray(val);
|
|
19
17
|
const merge = (target, source) => {
|
|
20
18
|
for (const key of Object.keys(source)) {
|
|
@@ -34,7 +32,7 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
34
32
|
/**
|
|
35
33
|
* @see Object.prototype.deepEquals
|
|
36
34
|
*/
|
|
37
|
-
|
|
35
|
+
defineIfNotExists(Object.prototype, 'deepEquals', function (other) {
|
|
38
36
|
function eq(a, b) {
|
|
39
37
|
// Functions: always false (even if code is the same)
|
|
40
38
|
if (typeof a === 'function' || typeof b === 'function') {
|
|
@@ -89,7 +87,7 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
89
87
|
/**
|
|
90
88
|
* @see Object.prototype.stableStringify
|
|
91
89
|
*/
|
|
92
|
-
|
|
90
|
+
defineIfNotExists(Object.prototype, 'stableStringify', function () {
|
|
93
91
|
const stringify = (v) => {
|
|
94
92
|
if (v === null || typeof v !== 'object') {
|
|
95
93
|
return JSON.stringify(v);
|
|
@@ -107,7 +105,7 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
107
105
|
/**
|
|
108
106
|
* @see Object.prototype.stableHash
|
|
109
107
|
*/
|
|
110
|
-
|
|
108
|
+
defineIfNotExists(Object.prototype, 'stableHash', function () {
|
|
111
109
|
const str = this.stableStringify();
|
|
112
110
|
let hash = 2166136261;
|
|
113
111
|
for (let i = 0; i < str.length; i++) {
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const core_utils_1 = require("../utils/core.utils");
|
|
1
|
+
import { defineIfNotExists, utilitishError } from '../utils/core.utils';
|
|
4
2
|
/**
|
|
5
3
|
* @see Set.prototype.toList
|
|
6
4
|
*/
|
|
7
|
-
|
|
5
|
+
defineIfNotExists(Set.prototype, 'toList', function () {
|
|
8
6
|
// Always returns a new array, even for empty sets
|
|
9
7
|
return Array.from(this);
|
|
10
8
|
});
|
|
11
9
|
/**
|
|
12
10
|
* @see Set.prototype.hasAny
|
|
13
11
|
*/
|
|
14
|
-
|
|
12
|
+
defineIfNotExists(Set.prototype, 'hasAny', function (...items) {
|
|
15
13
|
if (!Array.isArray(items))
|
|
16
|
-
|
|
14
|
+
utilitishError('Set.prototype.hasAny', 'arguments must be an array', items);
|
|
17
15
|
if (items.length === 0)
|
|
18
16
|
return false;
|
|
19
17
|
return items.some((item) => this.has(item));
|
|
@@ -21,7 +19,7 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
21
19
|
/**
|
|
22
20
|
* @see Set.prototype.includes
|
|
23
21
|
*/
|
|
24
|
-
|
|
22
|
+
defineIfNotExists(Set.prototype, 'includes', function (...args) {
|
|
25
23
|
let values;
|
|
26
24
|
if (args.length === 0)
|
|
27
25
|
return true; // empty means "all included" (like [].every)
|
|
@@ -32,17 +30,17 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
32
30
|
values = args;
|
|
33
31
|
}
|
|
34
32
|
if (!Array.isArray(values))
|
|
35
|
-
|
|
33
|
+
utilitishError('Set.prototype.includes', 'arguments must be an array or a Set', args);
|
|
36
34
|
return values.every((item) => this.has(item));
|
|
37
35
|
});
|
|
38
36
|
/**
|
|
39
37
|
* @see Set.prototype.union
|
|
40
38
|
*/
|
|
41
|
-
|
|
39
|
+
defineIfNotExists(Set.prototype, 'union', function (...others) {
|
|
42
40
|
const result = new Set(this);
|
|
43
41
|
for (const other of others) {
|
|
44
42
|
if (!(other instanceof Set))
|
|
45
|
-
|
|
43
|
+
utilitishError('Set.prototype.union', 'arguments must be Sets', other);
|
|
46
44
|
for (const item of other) {
|
|
47
45
|
result.add(item);
|
|
48
46
|
}
|
|
@@ -52,9 +50,9 @@ const core_utils_1 = require("../utils/core.utils");
|
|
|
52
50
|
/**
|
|
53
51
|
* @see Set.prototype.intersection
|
|
54
52
|
*/
|
|
55
|
-
|
|
53
|
+
defineIfNotExists(Set.prototype, 'intersection', function (...others) {
|
|
56
54
|
if (others.some((s) => !(s instanceof Set)))
|
|
57
|
-
|
|
55
|
+
utilitishError('Set.prototype.intersection', 'arguments must be Sets', others);
|
|
58
56
|
const result = new Set();
|
|
59
57
|
for (const item of this) {
|
|
60
58
|
if (others.every((set) => set.has(item))) {
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
require("../set/set-prototype");
|
|
1
|
+
import '../set/set-prototype';
|
|
4
2
|
describe('Set.prototype', () => {
|
|
5
3
|
describe('toList()', () => {
|
|
6
4
|
it('should return array with all values in insertion order', () => {
|