utilitish 0.0.6 → 0.0.7
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 +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 -84
- 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 +69 -9
- 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 +61 -8
- package/dist/object/object-prototype.js +9 -0
- package/dist/object/object-prototype.spec.d.ts +1 -0
- package/dist/object/object-prototype.spec.js +110 -0
- package/dist/set/set-prototype.d.ts +83 -5
- 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 +143 -24
- 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,25 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const core_utils_1 = require("../utils/core.utils");
|
|
4
|
+
const logic_utils_1 = require("../utils/logic.utils");
|
|
5
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'first', function () {
|
|
5
6
|
return this[0];
|
|
6
7
|
});
|
|
7
|
-
(0,
|
|
8
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'last', function () {
|
|
8
9
|
return this[this.length - 1];
|
|
9
10
|
});
|
|
10
|
-
(0,
|
|
11
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'sum', function (selector) {
|
|
11
12
|
if (this.length === 0)
|
|
12
13
|
return 0;
|
|
13
|
-
const getValue = (0,
|
|
14
|
+
const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
|
|
14
15
|
if (this.every((item) => typeof getValue(item) === 'number')) {
|
|
15
16
|
return this.reduce((acc, item) => getValue(item) + acc, 0);
|
|
16
17
|
}
|
|
17
|
-
throw new TypeError('Array.prototype.sum() requires a
|
|
18
|
+
throw new TypeError('Array.prototype.sum() requires a selector who return a number unless array is number[]');
|
|
18
19
|
});
|
|
19
|
-
(0,
|
|
20
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'unique', function () {
|
|
20
21
|
return [...new Set(this)];
|
|
21
22
|
});
|
|
22
|
-
(0,
|
|
23
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'chunk', function (size) {
|
|
23
24
|
if (typeof size !== 'number' || !Number.isInteger(size) || size <= 0) {
|
|
24
25
|
throw new TypeError('Chunk size must be a positive integer');
|
|
25
26
|
}
|
|
@@ -29,17 +30,17 @@ const utils_1 = require("../utils");
|
|
|
29
30
|
}
|
|
30
31
|
return result;
|
|
31
32
|
});
|
|
32
|
-
(0,
|
|
33
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'average', function (selector) {
|
|
33
34
|
if (this.length === 0)
|
|
34
35
|
return 0;
|
|
35
|
-
const getValue = (0,
|
|
36
|
+
const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
|
|
36
37
|
if (this.every((item) => typeof getValue(item) === 'number')) {
|
|
37
38
|
return this.reduce((acc, item) => getValue(item) + acc, 0) / this.length;
|
|
38
39
|
}
|
|
39
|
-
throw new TypeError('Array.prototype.average() requires a
|
|
40
|
+
throw new TypeError('Array.prototype.average() requires a selector who return a number unless array is number[]');
|
|
40
41
|
});
|
|
41
|
-
(0,
|
|
42
|
-
const getKey = (0,
|
|
42
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'groupBy', function (selector) {
|
|
43
|
+
const getKey = (0, core_utils_1.resolveSelector)(selector, (item) => item);
|
|
43
44
|
const map = new Map();
|
|
44
45
|
for (const item of this) {
|
|
45
46
|
const key = getKey(item);
|
|
@@ -50,24 +51,24 @@ const utils_1 = require("../utils");
|
|
|
50
51
|
}
|
|
51
52
|
return map;
|
|
52
53
|
});
|
|
53
|
-
(0,
|
|
54
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'compact', function () {
|
|
54
55
|
return this.filter(Boolean);
|
|
55
56
|
});
|
|
56
|
-
(0,
|
|
57
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'enumerate', function () {
|
|
57
58
|
return this.map((value, index) => [value, index]);
|
|
58
59
|
});
|
|
59
|
-
(0,
|
|
60
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'sortBy', function (direction, selector) {
|
|
60
61
|
if (this.length === 0)
|
|
61
62
|
return this.slice();
|
|
62
|
-
const getValue = (0,
|
|
63
|
-
if (!selector && !this.every((item) => (0,
|
|
63
|
+
const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
|
|
64
|
+
if (!selector && !this.every((item) => (0, core_utils_1.isNumberOrString)(item))) {
|
|
64
65
|
throw new TypeError('Array elements must be number or string if no selector is provided');
|
|
65
66
|
}
|
|
66
67
|
return this.slice().sort((a, b) => {
|
|
67
68
|
const valA = getValue(a);
|
|
68
69
|
const valB = getValue(b);
|
|
69
|
-
if (!(0,
|
|
70
|
-
throw new TypeError('
|
|
70
|
+
if (!(0, core_utils_1.isNumberOrString)(valA) || !(0, core_utils_1.isNumberOrString)(valB)) {
|
|
71
|
+
throw new TypeError('Selector must return number or string');
|
|
71
72
|
}
|
|
72
73
|
if (valA > valB)
|
|
73
74
|
return direction === 'asc' ? 1 : -1;
|
|
@@ -76,13 +77,13 @@ const utils_1 = require("../utils");
|
|
|
76
77
|
return 0;
|
|
77
78
|
});
|
|
78
79
|
});
|
|
79
|
-
(0,
|
|
80
|
-
return sortBy(this, 'asc', selector);
|
|
80
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'sortAsc', function (selector) {
|
|
81
|
+
return (0, logic_utils_1.sortBy)(this, 'asc', selector);
|
|
81
82
|
});
|
|
82
|
-
(0,
|
|
83
|
-
return sortBy(this, 'desc', selector);
|
|
83
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'sortDesc', function (selector) {
|
|
84
|
+
return (0, logic_utils_1.sortBy)(this, 'desc', selector);
|
|
84
85
|
});
|
|
85
|
-
(0,
|
|
86
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'swap', function (i, j) {
|
|
86
87
|
if (typeof i !== 'number' || typeof j !== 'number' || !Number.isInteger(i) || !Number.isInteger(j)) {
|
|
87
88
|
throw new TypeError('Indices must be integers');
|
|
88
89
|
}
|
|
@@ -96,7 +97,7 @@ const utils_1 = require("../utils");
|
|
|
96
97
|
}
|
|
97
98
|
return this;
|
|
98
99
|
});
|
|
99
|
-
(0,
|
|
100
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'shuffle', function () {
|
|
100
101
|
const arr = this.slice();
|
|
101
102
|
for (let i = arr.length - 1; i > 0; i--) {
|
|
102
103
|
const j = Math.floor(Math.random() * (i + 1));
|
|
@@ -104,13 +105,13 @@ const utils_1 = require("../utils");
|
|
|
104
105
|
}
|
|
105
106
|
return arr;
|
|
106
107
|
});
|
|
107
|
-
(0,
|
|
108
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'toMap', function (keySelector, valueSelector) {
|
|
108
109
|
if (!keySelector && this.length && this.every((item) => Array.isArray(item) && item.length === 2)) {
|
|
109
110
|
return new Map(this);
|
|
110
111
|
}
|
|
111
112
|
const map = new Map();
|
|
112
|
-
const getKey = (0,
|
|
113
|
-
const getValue = (0,
|
|
113
|
+
const getKey = (0, core_utils_1.resolveSelector)(keySelector, (_, index) => index);
|
|
114
|
+
const getValue = (0, core_utils_1.resolveSelector)(valueSelector, (item) => item);
|
|
114
115
|
for (let i = 0; i < this.length; i++) {
|
|
115
116
|
const item = this[i];
|
|
116
117
|
const key = getKey(item, i);
|
|
@@ -119,12 +120,16 @@ const utils_1 = require("../utils");
|
|
|
119
120
|
}
|
|
120
121
|
return map;
|
|
121
122
|
});
|
|
122
|
-
(0,
|
|
123
|
-
const
|
|
123
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'toObject', function (keySelector, valueSelector) {
|
|
124
|
+
const map = this.toMap(keySelector, valueSelector);
|
|
125
|
+
return (0, logic_utils_1.mapToObject)(map);
|
|
126
|
+
});
|
|
127
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'toSet', function (selector) {
|
|
128
|
+
const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
|
|
124
129
|
return new Set(this.map(getValue));
|
|
125
130
|
});
|
|
126
|
-
(0,
|
|
127
|
-
const getKey = (0,
|
|
131
|
+
(0, core_utils_1.defineIfNotExists)(Array.prototype, 'countBy', function (selector) {
|
|
132
|
+
const getKey = (0, core_utils_1.resolveSelector)(selector, (item) => item);
|
|
128
133
|
const map = new Map();
|
|
129
134
|
for (const item of this) {
|
|
130
135
|
const key = getKey(item);
|
|
@@ -132,23 +137,3 @@ const utils_1 = require("../utils");
|
|
|
132
137
|
}
|
|
133
138
|
return map;
|
|
134
139
|
});
|
|
135
|
-
function sortBy(arr, direction, selector) {
|
|
136
|
-
if (arr.length === 0)
|
|
137
|
-
return arr.slice();
|
|
138
|
-
const getValue = (0, utils_1.resolveSelector)(selector, (item) => item);
|
|
139
|
-
if (!selector && !arr.every((item) => (0, utils_1.isNumberOrString)(item))) {
|
|
140
|
-
throw new TypeError('Array elements must be number or string if no selector is provided');
|
|
141
|
-
}
|
|
142
|
-
return arr.slice().sort((a, b) => {
|
|
143
|
-
const valA = getValue(a);
|
|
144
|
-
const valB = getValue(b);
|
|
145
|
-
if (!(0, utils_1.isNumberOrString)(valA) || !(0, utils_1.isNumberOrString)(valB)) {
|
|
146
|
-
throw new TypeError('Callback must return number or string');
|
|
147
|
-
}
|
|
148
|
-
if (valA > valB)
|
|
149
|
-
return direction === 'asc' ? 1 : -1;
|
|
150
|
-
if (valA < valB)
|
|
151
|
-
return direction === 'asc' ? -1 : 1;
|
|
152
|
-
return 0;
|
|
153
|
-
});
|
|
154
|
-
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './array-prototype';
|