utilitish 0.0.6 → 0.0.8
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.d.ts +1 -1
- package/dist/array/array-constructor.js +5 -5
- package/dist/array/array-constructor.spec.d.ts +2 -0
- package/dist/array/array-constructor.spec.js +130 -0
- package/dist/array/array-prototype.d.ts +341 -85
- package/dist/array/array-prototype.js +38 -53
- package/dist/array/array-prototype.spec.d.ts +1 -0
- package/dist/array/array-prototype.spec.js +536 -0
- package/dist/map/map-prototype.d.ts +70 -10
- package/dist/map/map-prototype.js +16 -3
- package/dist/map/map-prototype.spec.d.ts +1 -0
- package/dist/map/map-prototype.spec.js +261 -0
- package/dist/object/object-prototype.d.ts +100 -9
- package/dist/object/object-prototype.js +43 -14
- package/dist/object/object-prototype.spec.d.ts +1 -0
- package/dist/object/object-prototype.spec.js +184 -0
- package/dist/set/set-prototype.d.ts +84 -6
- package/dist/set/set-prototype.js +21 -6
- package/dist/set/set-prototype.spec.d.ts +1 -0
- package/dist/set/set-prototype.spec.js +122 -0
- package/dist/string/string-prototype.d.ts +144 -25
- package/dist/string/string-prototype.js +41 -11
- package/dist/string/string-prototype.spec.d.ts +1 -0
- package/dist/string/string-prototype.spec.js +115 -0
- package/dist/utils/logic.utils.d.ts +3 -0
- package/dist/utils/logic.utils.js +41 -0
- package/package.json +1 -1
- /package/dist/{utils.d.ts → utils/core.utils.d.ts} +0 -0
- /package/dist/{utils.js → utils/core.utils.js} +0 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export {};
|
|
2
1
|
declare global {
|
|
3
2
|
interface ArrayConstructor {
|
|
4
3
|
/**
|
|
@@ -97,3 +96,4 @@ declare global {
|
|
|
97
96
|
}
|
|
98
97
|
type MultiDimensionalArray<T, Dims extends number[]> = Dims extends [number, ...infer Rest extends number[]] ? MultiDimensionalArray<T[], Rest> : T;
|
|
99
98
|
type WidenLiterals<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
|
|
99
|
+
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const core_utils_1 = require("../utils/core.utils");
|
|
4
4
|
/**
|
|
5
5
|
* @see Array.zip
|
|
6
6
|
*/
|
|
7
|
-
(0,
|
|
7
|
+
(0, core_utils_1.defineStaticIfNotExists)(Array, 'zip', function (...arrays) {
|
|
8
8
|
if (arrays.length === 0)
|
|
9
9
|
return [];
|
|
10
10
|
const maxLen = Math.max(...arrays.map((arr) => arr.length));
|
|
@@ -13,7 +13,7 @@ const utils_1 = require("../utils");
|
|
|
13
13
|
/**
|
|
14
14
|
* @see Array.range
|
|
15
15
|
*/
|
|
16
|
-
(0,
|
|
16
|
+
(0, core_utils_1.defineStaticIfNotExists)(Array, 'range', function (start, end, step = 1) {
|
|
17
17
|
if (end === undefined) {
|
|
18
18
|
end = start;
|
|
19
19
|
start = 0;
|
|
@@ -30,7 +30,7 @@ const utils_1 = require("../utils");
|
|
|
30
30
|
/**
|
|
31
31
|
* @see Array.repeat
|
|
32
32
|
*/
|
|
33
|
-
(0,
|
|
33
|
+
(0, core_utils_1.defineStaticIfNotExists)(Array, 'repeat', function (length, value) {
|
|
34
34
|
if (typeof length !== 'number' || !Number.isInteger(length) || length < 0) {
|
|
35
35
|
throw new TypeError('Length must be a non-negative integer');
|
|
36
36
|
}
|
|
@@ -39,7 +39,7 @@ const utils_1 = require("../utils");
|
|
|
39
39
|
/**
|
|
40
40
|
* @see Array.create
|
|
41
41
|
*/
|
|
42
|
-
(0,
|
|
42
|
+
(0, core_utils_1.defineStaticIfNotExists)(Array, 'create', function (valueOrFactory, ...sizes) {
|
|
43
43
|
if (sizes.length === 0)
|
|
44
44
|
return [];
|
|
45
45
|
if (!sizes.every((s) => Number.isInteger(s) && s >= 0)) {
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("../array/array-constructor");
|
|
4
|
+
require("./array-constructor");
|
|
5
|
+
describe('Array', () => {
|
|
6
|
+
describe('range()', () => {
|
|
7
|
+
it('should generate a range from 0 to n-1 if only start is given', () => {
|
|
8
|
+
expect(Array.range(5)).toEqual([0, 1, 2, 3, 4]);
|
|
9
|
+
});
|
|
10
|
+
it('should generate a range from start to end-1', () => {
|
|
11
|
+
expect(Array.range(2, 6)).toEqual([2, 3, 4, 5]);
|
|
12
|
+
});
|
|
13
|
+
it('should support negative steps', () => {
|
|
14
|
+
expect(Array.range(5, 1, -1)).toEqual([5, 4, 3, 2]);
|
|
15
|
+
});
|
|
16
|
+
it('should return an empty array if start equals end', () => {
|
|
17
|
+
expect(Array.range(3, 3)).toEqual([]);
|
|
18
|
+
});
|
|
19
|
+
it('should return an empty array if step does not reach end', () => {
|
|
20
|
+
expect(Array.range(0, 5, -1)).toEqual([]);
|
|
21
|
+
expect(Array.range(5, 0, 1)).toEqual([]);
|
|
22
|
+
});
|
|
23
|
+
describe('error handling', () => {
|
|
24
|
+
it('should throw if step is 0', () => {
|
|
25
|
+
expect(() => Array.range(0, 5, 0)).toThrowError('step must not be 0');
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
describe('repeat()', () => {
|
|
30
|
+
it('should fill an array with the same value', () => {
|
|
31
|
+
expect(Array.repeat(3, 'a')).toEqual(['a', 'a', 'a']);
|
|
32
|
+
});
|
|
33
|
+
it('should fill an array with values from a function', () => {
|
|
34
|
+
let n = 0;
|
|
35
|
+
expect(Array.repeat(4, () => ++n)).toEqual([1, 2, 3, 4]);
|
|
36
|
+
});
|
|
37
|
+
it('should return an empty array if length is 0', () => {
|
|
38
|
+
expect(Array.repeat(0, 'x')).toEqual([]);
|
|
39
|
+
});
|
|
40
|
+
describe('error handling', () => {
|
|
41
|
+
it('should throw if length is negative or not an integer', () => {
|
|
42
|
+
expect(() => Array.repeat(-1, 'a')).toThrowError();
|
|
43
|
+
expect(() => Array.repeat(1.5, 'a')).toThrowError();
|
|
44
|
+
expect(() => Array.repeat('a', 'a')).toThrowError();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe('zip()', () => {
|
|
49
|
+
it('should zip two arrays of equal length', () => {
|
|
50
|
+
expect(Array.zip([1, 2, 3], ['a', 'b', 'c'])).toEqual([
|
|
51
|
+
[1, 'a'],
|
|
52
|
+
[2, 'b'],
|
|
53
|
+
[3, 'c'],
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
it('should zip arrays of different lengths (fill with undefined)', () => {
|
|
57
|
+
expect(Array.zip([1, 2], ['a', 'b', 'c'], [true, false, true])).toEqual([
|
|
58
|
+
[1, 'a', true],
|
|
59
|
+
[2, 'b', false],
|
|
60
|
+
[undefined, 'c', true],
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
it('should work with a single array', () => {
|
|
64
|
+
expect(Array.zip([1, 2, 3])).toEqual([[1], [2], [3]]);
|
|
65
|
+
});
|
|
66
|
+
it('should fill with undefined for missing elements', () => {
|
|
67
|
+
expect(Array.zip([1], [2, 3, 4])).toEqual([
|
|
68
|
+
[1, 2],
|
|
69
|
+
[undefined, 3],
|
|
70
|
+
[undefined, 4],
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
describe('edge cases', () => {
|
|
74
|
+
it('should return an empty array if no arrays are given', () => {
|
|
75
|
+
expect(Array.zip()).toEqual([]);
|
|
76
|
+
});
|
|
77
|
+
it('should return an array of undefined if all arrays are empty', () => {
|
|
78
|
+
expect(Array.zip([], [])).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
describe('create()', () => {
|
|
83
|
+
it('should create a 1D array', () => {
|
|
84
|
+
expect(Array.create('x', 3)).toEqual(['x', 'x', 'x']);
|
|
85
|
+
});
|
|
86
|
+
it('should create a 2D array', () => {
|
|
87
|
+
expect(Array.create(0, 2, 3)).toEqual([
|
|
88
|
+
[0, 0, 0],
|
|
89
|
+
[0, 0, 0],
|
|
90
|
+
]);
|
|
91
|
+
});
|
|
92
|
+
it('should create a 3D array', () => {
|
|
93
|
+
expect(Array.create(true, 2, 2, 2)).toEqual([
|
|
94
|
+
[
|
|
95
|
+
[true, true],
|
|
96
|
+
[true, true],
|
|
97
|
+
],
|
|
98
|
+
[
|
|
99
|
+
[true, true],
|
|
100
|
+
[true, true],
|
|
101
|
+
],
|
|
102
|
+
]);
|
|
103
|
+
});
|
|
104
|
+
describe('edge cases', () => {
|
|
105
|
+
it('should return [] if no sizes are given', () => {
|
|
106
|
+
expect(Array.create('x')).toEqual([]);
|
|
107
|
+
});
|
|
108
|
+
it('should return [] if any size is 0', () => {
|
|
109
|
+
expect(Array.create('x', 0)).toEqual([]);
|
|
110
|
+
expect(Array.create('x', 2, 0)).toEqual([[], []]);
|
|
111
|
+
});
|
|
112
|
+
it('should return arrays with the same object reference for objects', () => {
|
|
113
|
+
const obj = { a: 1 };
|
|
114
|
+
const arr = Array.create(obj, 2);
|
|
115
|
+
expect(arr[0]).toBe(obj);
|
|
116
|
+
expect(arr[1]).toBe(obj);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
describe('error handling', () => {
|
|
120
|
+
it('should throw if a size is negative', () => {
|
|
121
|
+
expect(() => Array.create('x', -1)).toThrow(TypeError);
|
|
122
|
+
expect(() => Array.create('x', 2, -3)).toThrow(TypeError);
|
|
123
|
+
});
|
|
124
|
+
it('should throw if a size is not an integer', () => {
|
|
125
|
+
expect(() => Array.create('x', 2.5)).toThrow(TypeError);
|
|
126
|
+
expect(() => Array.create('x', 2, 1.1)).toThrow(TypeError);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
});
|