diginext-utils 2.1.20 → 2.1.21
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/Checker.d.ts +8 -8
- package/dist/Checker.js +31 -31
- package/dist/EventDispatcher.d.ts +13 -13
- package/dist/EventDispatcher.js +51 -51
- package/dist/FileUpload.d.ts +12 -12
- package/dist/FileUpload.js +64 -64
- package/dist/Slug.d.ts +9 -9
- package/dist/Slug.js +333 -333
- package/dist/Timer.d.ts +4 -4
- package/dist/Timer.js +8 -8
- package/dist/Validation.d.ts +5 -5
- package/dist/Validation.js +38 -38
- package/dist/array.d.ts +113 -113
- package/dist/array.js +297 -297
- package/dist/color.d.ts +25 -25
- package/dist/color.js +95 -95
- package/dist/console/enableConsole.d.ts +2 -2
- package/dist/console/enableConsole.js +13 -13
- package/dist/console/index.d.ts +11 -11
- package/dist/console/index.js +17 -17
- package/dist/console/log.d.ts +15 -15
- package/dist/console/log.js +44 -44
- package/dist/device/browser.d.ts +5 -5
- package/dist/device/browser.js +37 -37
- package/dist/device/camera.d.ts +7 -7
- package/dist/device/camera.js +171 -171
- package/dist/device/index.d.ts +5 -5
- package/dist/device/index.js +224 -224
- package/dist/device/os.d.ts +11 -11
- package/dist/device/os.js +37 -37
- package/dist/file/createDir.d.ts +5 -5
- package/dist/file/createDir.js +15 -15
- package/dist/file/fileMove.d.ts +4 -4
- package/dist/file/fileMove.js +32 -32
- package/dist/file/findFilesByExt.d.ts +13 -13
- package/dist/file/findFilesByExt.js +49 -49
- package/dist/file/getAllFiles.d.ts +1 -0
- package/dist/file/getAllFiles.js +27 -0
- package/dist/file/getAllFiles.js.map +1 -0
- package/dist/file/index.d.ts +9 -9
- package/dist/file/index.js +10 -10
- package/dist/gameboi/index.d.ts +10 -10
- package/dist/gameboi/index.js +67 -64
- package/dist/gameboi/index.js.map +1 -1
- package/dist/images/downloadByUrl.d.ts +1 -1
- package/dist/images/downloadByUrl.js +55 -55
- package/dist/images/index.d.ts +2 -2
- package/dist/images/index.js +8 -8
- package/dist/images/loadImage.d.ts +1 -1
- package/dist/images/loadImage.js +17 -17
- package/dist/images/resize.d.ts +2 -2
- package/dist/images/resize.js +37 -37
- package/dist/images/upload.d.ts +10 -10
- package/dist/images/upload.js +26 -26
- package/dist/index.d.ts +37 -37
- package/dist/index.js +62 -62
- package/dist/json.d.ts +8 -8
- package/dist/json.js +31 -31
- package/dist/math/diffDate.d.ts +5 -5
- package/dist/math/diffDate.js +13 -13
- package/dist/math/index.d.ts +30 -30
- package/dist/math/index.js +92 -92
- package/dist/math/positiveNumber.d.ts +2 -2
- package/dist/math/positiveNumber.js +16 -16
- package/dist/name/en.d.ts +5 -5
- package/dist/name/en.js +25 -25
- package/dist/name/index.d.ts +12 -12
- package/dist/name/index.js +12 -12
- package/dist/name/vi.d.ts +5 -5
- package/dist/name/vi.js +27 -27
- package/dist/object.d.ts +59 -59
- package/dist/object.js +79 -79
- package/dist/permission/index.d.ts +8 -8
- package/dist/permission/index.js +6 -6
- package/dist/permission/requestCamera.d.ts +5 -5
- package/dist/permission/requestCamera.js +42 -42
- package/dist/permission/requestDeviceOrientationControl.d.ts +2 -2
- package/dist/permission/requestDeviceOrientationControl.js +38 -38
- package/dist/response/index.d.ts +49 -49
- package/dist/response/index.js +51 -51
- package/dist/string/convertPathnameAndQuery.d.ts +4 -4
- package/dist/string/convertPathnameAndQuery.js +24 -24
- package/dist/string/formatNumber.d.ts +6 -6
- package/dist/string/formatNumber.js +14 -14
- package/dist/string/generatePassword.d.ts +9 -9
- package/dist/string/generatePassword.js +23 -23
- package/dist/string/generateUUID.d.ts +2 -2
- package/dist/string/generateUUID.js +36 -36
- package/dist/string/getTextBetweenCharByIndex.d.ts +1 -1
- package/dist/string/getTextBetweenCharByIndex.js +20 -20
- package/dist/string/index.d.ts +65 -65
- package/dist/string/index.js +161 -161
- package/dist/string/indexesOf.d.ts +1 -1
- package/dist/string/indexesOf.js +14 -14
- package/dist/string/makeDaySlug.d.ts +31 -31
- package/dist/string/makeDaySlug.js +22 -22
- package/dist/string/random.d.ts +18 -18
- package/dist/string/random.js +40 -40
- package/dist/string/trimNull.d.ts +2 -2
- package/dist/string/trimNull.js +20 -20
- package/dist/string/url.d.ts +22 -22
- package/dist/string/url.js +74 -74
- package/esm/Checker.d.ts +8 -8
- package/esm/Checker.js +27 -27
- package/esm/EventDispatcher.d.ts +13 -13
- package/esm/EventDispatcher.js +48 -48
- package/esm/FileUpload.d.ts +12 -12
- package/esm/FileUpload.js +61 -61
- package/esm/Slug.d.ts +9 -9
- package/esm/Slug.js +329 -329
- package/esm/Timer.d.ts +4 -4
- package/esm/Timer.js +6 -6
- package/esm/Validation.d.ts +5 -5
- package/esm/Validation.js +36 -36
- package/esm/array.d.ts +113 -113
- package/esm/array.js +275 -275
- package/esm/color.d.ts +25 -25
- package/esm/color.js +85 -85
- package/esm/console/enableConsole.d.ts +2 -2
- package/esm/console/enableConsole.js +6 -6
- package/esm/console/index.d.ts +11 -11
- package/esm/console/index.js +12 -12
- package/esm/console/log.d.ts +15 -15
- package/esm/console/log.js +32 -32
- package/esm/device/browser.d.ts +5 -5
- package/esm/device/browser.js +29 -29
- package/esm/device/camera.d.ts +7 -7
- package/esm/device/camera.js +167 -167
- package/esm/device/index.d.ts +5 -5
- package/esm/device/index.js +216 -216
- package/esm/device/os.d.ts +11 -11
- package/esm/device/os.js +27 -27
- package/esm/file/createDir.d.ts +5 -5
- package/esm/file/createDir.js +12 -12
- package/esm/file/fileMove.d.ts +4 -4
- package/esm/file/fileMove.js +28 -28
- package/esm/file/findFilesByExt.d.ts +13 -13
- package/esm/file/findFilesByExt.js +45 -45
- package/esm/file/getAllFiles.d.ts +1 -0
- package/esm/file/getAllFiles.js +21 -0
- package/esm/file/getAllFiles.js.map +1 -0
- package/esm/file/index.d.ts +9 -9
- package/esm/file/index.js +5 -5
- package/esm/gameboi/index.d.ts +10 -10
- package/esm/gameboi/index.js +61 -61
- package/esm/gameboi/index.js.map +1 -1
- package/esm/images/downloadByUrl.d.ts +1 -1
- package/esm/images/downloadByUrl.js +52 -52
- package/esm/images/index.d.ts +2 -2
- package/esm/images/index.js +3 -3
- package/esm/images/loadImage.d.ts +1 -1
- package/esm/images/loadImage.js +14 -14
- package/esm/images/resize.d.ts +2 -2
- package/esm/images/resize.js +30 -30
- package/esm/images/upload.d.ts +10 -10
- package/esm/images/upload.js +19 -19
- package/esm/index.d.ts +37 -37
- package/esm/index.js +37 -37
- package/esm/json.d.ts +8 -8
- package/esm/json.js +27 -27
- package/esm/math/diffDate.d.ts +5 -5
- package/esm/math/diffDate.js +9 -9
- package/esm/math/index.d.ts +30 -30
- package/esm/math/index.js +77 -77
- package/esm/math/positiveNumber.d.ts +2 -2
- package/esm/math/positiveNumber.js +12 -12
- package/esm/name/en.d.ts +5 -5
- package/esm/name/en.js +21 -21
- package/esm/name/index.d.ts +12 -12
- package/esm/name/index.js +5 -5
- package/esm/name/vi.d.ts +5 -5
- package/esm/name/vi.js +23 -23
- package/esm/object.d.ts +59 -59
- package/esm/object.js +69 -69
- package/esm/permission/index.d.ts +8 -8
- package/esm/permission/index.js +4 -4
- package/esm/permission/requestCamera.d.ts +5 -5
- package/esm/permission/requestCamera.js +38 -38
- package/esm/permission/requestDeviceOrientationControl.d.ts +2 -2
- package/esm/permission/requestDeviceOrientationControl.js +34 -34
- package/esm/response/index.d.ts +49 -49
- package/esm/response/index.js +45 -45
- package/esm/string/convertPathnameAndQuery.d.ts +4 -4
- package/esm/string/convertPathnameAndQuery.js +21 -21
- package/esm/string/formatNumber.d.ts +6 -6
- package/esm/string/formatNumber.js +10 -10
- package/esm/string/generatePassword.d.ts +9 -9
- package/esm/string/generatePassword.js +19 -19
- package/esm/string/generateUUID.d.ts +2 -2
- package/esm/string/generateUUID.js +32 -32
- package/esm/string/getTextBetweenCharByIndex.d.ts +1 -1
- package/esm/string/getTextBetweenCharByIndex.js +14 -14
- package/esm/string/index.d.ts +65 -65
- package/esm/string/index.js +150 -150
- package/esm/string/indexesOf.d.ts +1 -1
- package/esm/string/indexesOf.js +11 -11
- package/esm/string/makeDaySlug.d.ts +31 -31
- package/esm/string/makeDaySlug.js +15 -15
- package/esm/string/random.d.ts +18 -18
- package/esm/string/random.js +33 -33
- package/esm/string/trimNull.d.ts +2 -2
- package/esm/string/trimNull.js +13 -13
- package/esm/string/url.d.ts +22 -22
- package/esm/string/url.js +64 -64
- package/package.json +4 -4
package/dist/array.js
CHANGED
|
@@ -1,298 +1,298 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.moveArray = exports.moveIndex = exports.shuffle = exports.getHalfRandom = exports.getRandom = exports.removeItemByKey = exports.removeItem = exports.allMatchInArray = exports.mergeAndMakeUniqueElement = exports.randomElement = exports.randomIndex = exports.lastElement = exports.firstElement = exports.sortElementByNumber = exports.sortElementByString = exports.maxArray = exports.minArray = exports.averageArray = exports.sumArray = void 0;
|
|
4
|
-
const math_1 = require("./math");
|
|
5
|
-
const sumArray = (array, key) => {
|
|
6
|
-
if (!array) {
|
|
7
|
-
console.warn("ARRAY NOT EXITED !");
|
|
8
|
-
return 0;
|
|
9
|
-
}
|
|
10
|
-
if (key)
|
|
11
|
-
return array.reduce((c, v) => c + v[key], 0);
|
|
12
|
-
else
|
|
13
|
-
return array.reduce((c, v) => c + v, 0);
|
|
14
|
-
};
|
|
15
|
-
exports.sumArray = sumArray;
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
* @param {Array} array
|
|
19
|
-
* @param {string} key
|
|
20
|
-
* @returns {Number}
|
|
21
|
-
*/
|
|
22
|
-
const averageArray = (array, key) => {
|
|
23
|
-
if (!array) {
|
|
24
|
-
console.warn("ARRAY NOT EXITED !");
|
|
25
|
-
return 0;
|
|
26
|
-
}
|
|
27
|
-
return (0, exports.sumArray)(array, key) / array.length || 0;
|
|
28
|
-
};
|
|
29
|
-
exports.averageArray = averageArray;
|
|
30
|
-
/**
|
|
31
|
-
*
|
|
32
|
-
* @param {Array} array
|
|
33
|
-
* @param {string} key
|
|
34
|
-
* @returns {Number}
|
|
35
|
-
*/
|
|
36
|
-
const minArray = (array, key) => {
|
|
37
|
-
if (!array) {
|
|
38
|
-
console.warn("ARRAY NOT EXITED !");
|
|
39
|
-
return 0;
|
|
40
|
-
}
|
|
41
|
-
if (array.length > 0) {
|
|
42
|
-
if (key)
|
|
43
|
-
return array.reduce((c, v) => (c < v[key] ? c : v[key]));
|
|
44
|
-
else
|
|
45
|
-
return array.reduce((c, v) => (c < v ? c : v));
|
|
46
|
-
}
|
|
47
|
-
return 0;
|
|
48
|
-
};
|
|
49
|
-
exports.minArray = minArray;
|
|
50
|
-
/**
|
|
51
|
-
*
|
|
52
|
-
* @param {Array} array
|
|
53
|
-
* @param {string} key
|
|
54
|
-
* @returns {Number}
|
|
55
|
-
*/
|
|
56
|
-
const maxArray = (array, key) => {
|
|
57
|
-
if (!array) {
|
|
58
|
-
console.warn("ARRAY NOT EXITED !");
|
|
59
|
-
return 0;
|
|
60
|
-
}
|
|
61
|
-
if (array.length > 0) {
|
|
62
|
-
if (key)
|
|
63
|
-
return array.reduce((c, v) => (c > v[key] ? c : v[key]));
|
|
64
|
-
else
|
|
65
|
-
return array.reduce((c, v) => (c > v ? c : v));
|
|
66
|
-
}
|
|
67
|
-
return 0;
|
|
68
|
-
};
|
|
69
|
-
exports.maxArray = maxArray;
|
|
70
|
-
/**
|
|
71
|
-
*
|
|
72
|
-
* @param {Array} array
|
|
73
|
-
* @param {string} key
|
|
74
|
-
* @returns {Array}
|
|
75
|
-
*/
|
|
76
|
-
const sortElementByString = (array, key) => {
|
|
77
|
-
if (!Array.isArray(array))
|
|
78
|
-
return [];
|
|
79
|
-
if (key)
|
|
80
|
-
return array.sort((x, y) => {
|
|
81
|
-
var a = x[key].toUpperCase(), b = y[key].toUpperCase();
|
|
82
|
-
return a == b ? 0 : a > b ? 1 : -1;
|
|
83
|
-
});
|
|
84
|
-
console.log("NO KEY");
|
|
85
|
-
};
|
|
86
|
-
exports.sortElementByString = sortElementByString;
|
|
87
|
-
/**
|
|
88
|
-
*
|
|
89
|
-
* @param {Array} array
|
|
90
|
-
* @param {string} key
|
|
91
|
-
* @returns {Array}
|
|
92
|
-
*/
|
|
93
|
-
const sortElementByNumber = (array, key) => {
|
|
94
|
-
if (!Array.isArray(array))
|
|
95
|
-
return [];
|
|
96
|
-
if (key)
|
|
97
|
-
return array.sort((a, b) => {
|
|
98
|
-
return a[key] - b[key];
|
|
99
|
-
});
|
|
100
|
-
console.log("NO KEY");
|
|
101
|
-
};
|
|
102
|
-
exports.sortElementByNumber = sortElementByNumber;
|
|
103
|
-
/**
|
|
104
|
-
*
|
|
105
|
-
* @param {Array} array
|
|
106
|
-
* @returns {any}
|
|
107
|
-
*/
|
|
108
|
-
const firstElement = (array) => {
|
|
109
|
-
if (array)
|
|
110
|
-
if (array.length || array.length > 0)
|
|
111
|
-
return array[0];
|
|
112
|
-
return null;
|
|
113
|
-
};
|
|
114
|
-
exports.firstElement = firstElement;
|
|
115
|
-
/**
|
|
116
|
-
*
|
|
117
|
-
* @param {Array} array
|
|
118
|
-
* @returns {any}
|
|
119
|
-
*/
|
|
120
|
-
const lastElement = (array) => {
|
|
121
|
-
if (array)
|
|
122
|
-
if (array.length || array.length > 0)
|
|
123
|
-
return array[array.length - 1];
|
|
124
|
-
return null;
|
|
125
|
-
};
|
|
126
|
-
exports.lastElement = lastElement;
|
|
127
|
-
/**
|
|
128
|
-
*
|
|
129
|
-
* @param {Array} array
|
|
130
|
-
* @returns {any}
|
|
131
|
-
*/
|
|
132
|
-
const randomIndex = (array) => {
|
|
133
|
-
if (array)
|
|
134
|
-
return (0, math_1.randInt)(0, array.length - 1);
|
|
135
|
-
return -1;
|
|
136
|
-
};
|
|
137
|
-
exports.randomIndex = randomIndex;
|
|
138
|
-
/**
|
|
139
|
-
*
|
|
140
|
-
* @param {Array} array
|
|
141
|
-
* @returns {any}
|
|
142
|
-
*/
|
|
143
|
-
const randomElement = (array) => {
|
|
144
|
-
if (array)
|
|
145
|
-
return array[(0, exports.randomIndex)(array)];
|
|
146
|
-
return null;
|
|
147
|
-
};
|
|
148
|
-
exports.randomElement = randomElement;
|
|
149
|
-
/**
|
|
150
|
-
* Remove same elements from 2 arrays
|
|
151
|
-
*/
|
|
152
|
-
const mergeAndMakeUniqueElement = (list1, list2, key) => {
|
|
153
|
-
if (!list1 || !list2)
|
|
154
|
-
return;
|
|
155
|
-
if (key) {
|
|
156
|
-
return list1.concat(list2).filter((item, index, self) => {
|
|
157
|
-
return self.findIndex((x) => x[key] == item[key]) === index;
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
return list1.concat(list2).filter((x, index, self) => {
|
|
162
|
-
return self.indexOf(x) === index;
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
exports.mergeAndMakeUniqueElement = mergeAndMakeUniqueElement;
|
|
167
|
-
/**
|
|
168
|
-
* check target == toMatch
|
|
169
|
-
* @param {Array} target
|
|
170
|
-
* @param {Array} toMatch
|
|
171
|
-
* @returns {Boolean}
|
|
172
|
-
*/
|
|
173
|
-
const allMatchInArray = (target, toMatch) => {
|
|
174
|
-
if (!target || !toMatch)
|
|
175
|
-
return false;
|
|
176
|
-
const found = toMatch.every((item) => {
|
|
177
|
-
return target.includes(item);
|
|
178
|
-
});
|
|
179
|
-
return found;
|
|
180
|
-
};
|
|
181
|
-
exports.allMatchInArray = allMatchInArray;
|
|
182
|
-
const removeItem = (item, array) => {
|
|
183
|
-
const index = array.indexOf(item);
|
|
184
|
-
if (index == -1) {
|
|
185
|
-
console.warn("[ArrayExtra.removeItem] Item not found.");
|
|
186
|
-
return array;
|
|
187
|
-
}
|
|
188
|
-
array.splice(index, 1);
|
|
189
|
-
return array;
|
|
190
|
-
};
|
|
191
|
-
exports.removeItem = removeItem;
|
|
192
|
-
const removeItemByKey = (key, value, array) => {
|
|
193
|
-
const foundIndex = array.findIndex((item) => {
|
|
194
|
-
return item[key] == value;
|
|
195
|
-
});
|
|
196
|
-
if (foundIndex < 0) {
|
|
197
|
-
console.warn("[ArrayExtra.removeItemByKey] Item not found.", key, value);
|
|
198
|
-
return array;
|
|
199
|
-
}
|
|
200
|
-
array.splice(foundIndex, 1);
|
|
201
|
-
return array;
|
|
202
|
-
};
|
|
203
|
-
exports.removeItemByKey = removeItemByKey;
|
|
204
|
-
const getRandom = (array, n) => {
|
|
205
|
-
n = n || array.length;
|
|
206
|
-
var result = new Array(n), len = array.length, taken = new Array(len);
|
|
207
|
-
if (n > len)
|
|
208
|
-
throw new RangeError("getRandom: more elements taken than available");
|
|
209
|
-
while (n--) {
|
|
210
|
-
var x = Math.floor(Math.random() * len);
|
|
211
|
-
result[n] = array[x in taken ? taken[x] : x];
|
|
212
|
-
taken[x] = --len in taken ? taken[len] : len;
|
|
213
|
-
}
|
|
214
|
-
return result;
|
|
215
|
-
};
|
|
216
|
-
exports.getRandom = getRandom;
|
|
217
|
-
/**
|
|
218
|
-
* Get an array with shuffle element
|
|
219
|
-
*/
|
|
220
|
-
const getHalfRandom = (array, n) => {
|
|
221
|
-
var n = Math.ceil(array.length / 2);
|
|
222
|
-
return (0, exports.getRandom)(array, n);
|
|
223
|
-
};
|
|
224
|
-
exports.getHalfRandom = getHalfRandom;
|
|
225
|
-
/**
|
|
226
|
-
* Make array shuffle itself
|
|
227
|
-
*/
|
|
228
|
-
const shuffle = (array) => {
|
|
229
|
-
var i = array.length, j, temp;
|
|
230
|
-
if (array.length < 1)
|
|
231
|
-
return array;
|
|
232
|
-
while (--i) {
|
|
233
|
-
j = Math.floor(Math.random() * (i + 1));
|
|
234
|
-
temp = array[i];
|
|
235
|
-
array[i] = array[j];
|
|
236
|
-
array[j] = temp;
|
|
237
|
-
}
|
|
238
|
-
return array;
|
|
239
|
-
};
|
|
240
|
-
exports.shuffle = shuffle;
|
|
241
|
-
/**
|
|
242
|
-
*
|
|
243
|
-
* @param {Array} array
|
|
244
|
-
* @param {Number} oldIndex
|
|
245
|
-
* @param {Number} newIndex
|
|
246
|
-
* @returns {Array}
|
|
247
|
-
*/
|
|
248
|
-
const moveIndex = (array, oldIndex, newIndex) => {
|
|
249
|
-
if (newIndex >= array.length) {
|
|
250
|
-
var k = newIndex - array.length + 1;
|
|
251
|
-
while (k--) {
|
|
252
|
-
array.push(undefined);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
|
|
256
|
-
return array;
|
|
257
|
-
};
|
|
258
|
-
exports.moveIndex = moveIndex;
|
|
259
|
-
const moveArray = (array, oldIndex, newIndex) => {
|
|
260
|
-
while (oldIndex < 0) {
|
|
261
|
-
oldIndex += array.length;
|
|
262
|
-
}
|
|
263
|
-
while (newIndex < 0) {
|
|
264
|
-
newIndex += array.length;
|
|
265
|
-
}
|
|
266
|
-
if (newIndex >= array.length) {
|
|
267
|
-
var k = newIndex - array.length;
|
|
268
|
-
while (k-- + 1) {
|
|
269
|
-
array.push(999);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
|
|
273
|
-
return array;
|
|
274
|
-
};
|
|
275
|
-
exports.moveArray = moveArray;
|
|
276
|
-
const xarray = {
|
|
277
|
-
sumArray: exports.sumArray,
|
|
278
|
-
averageArray: exports.averageArray,
|
|
279
|
-
minArray: exports.minArray,
|
|
280
|
-
maxArray: exports.maxArray,
|
|
281
|
-
sortElementByString: exports.sortElementByString,
|
|
282
|
-
sortElementByNumber: exports.sortElementByNumber,
|
|
283
|
-
firstElement: exports.firstElement,
|
|
284
|
-
lastElement: exports.lastElement,
|
|
285
|
-
randomIndex: exports.randomIndex,
|
|
286
|
-
randomElement: exports.randomElement,
|
|
287
|
-
mergeAndMakeUniqueElement: exports.mergeAndMakeUniqueElement,
|
|
288
|
-
allMatchInArray: exports.allMatchInArray,
|
|
289
|
-
removeItem: exports.removeItem,
|
|
290
|
-
removeItemByKey: exports.removeItemByKey,
|
|
291
|
-
getRandom: exports.getRandom,
|
|
292
|
-
getHalfRandom: exports.getHalfRandom,
|
|
293
|
-
shuffle: exports.shuffle,
|
|
294
|
-
moveIndex: exports.moveIndex,
|
|
295
|
-
moveArray: exports.moveArray,
|
|
296
|
-
};
|
|
297
|
-
exports.default = xarray;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.moveArray = exports.moveIndex = exports.shuffle = exports.getHalfRandom = exports.getRandom = exports.removeItemByKey = exports.removeItem = exports.allMatchInArray = exports.mergeAndMakeUniqueElement = exports.randomElement = exports.randomIndex = exports.lastElement = exports.firstElement = exports.sortElementByNumber = exports.sortElementByString = exports.maxArray = exports.minArray = exports.averageArray = exports.sumArray = void 0;
|
|
4
|
+
const math_1 = require("./math");
|
|
5
|
+
const sumArray = (array, key) => {
|
|
6
|
+
if (!array) {
|
|
7
|
+
console.warn("ARRAY NOT EXITED !");
|
|
8
|
+
return 0;
|
|
9
|
+
}
|
|
10
|
+
if (key)
|
|
11
|
+
return array.reduce((c, v) => c + v[key], 0);
|
|
12
|
+
else
|
|
13
|
+
return array.reduce((c, v) => c + v, 0);
|
|
14
|
+
};
|
|
15
|
+
exports.sumArray = sumArray;
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param {Array} array
|
|
19
|
+
* @param {string} key
|
|
20
|
+
* @returns {Number}
|
|
21
|
+
*/
|
|
22
|
+
const averageArray = (array, key) => {
|
|
23
|
+
if (!array) {
|
|
24
|
+
console.warn("ARRAY NOT EXITED !");
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
return (0, exports.sumArray)(array, key) / array.length || 0;
|
|
28
|
+
};
|
|
29
|
+
exports.averageArray = averageArray;
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* @param {Array} array
|
|
33
|
+
* @param {string} key
|
|
34
|
+
* @returns {Number}
|
|
35
|
+
*/
|
|
36
|
+
const minArray = (array, key) => {
|
|
37
|
+
if (!array) {
|
|
38
|
+
console.warn("ARRAY NOT EXITED !");
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
if (array.length > 0) {
|
|
42
|
+
if (key)
|
|
43
|
+
return array.reduce((c, v) => (c < v[key] ? c : v[key]));
|
|
44
|
+
else
|
|
45
|
+
return array.reduce((c, v) => (c < v ? c : v));
|
|
46
|
+
}
|
|
47
|
+
return 0;
|
|
48
|
+
};
|
|
49
|
+
exports.minArray = minArray;
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @param {Array} array
|
|
53
|
+
* @param {string} key
|
|
54
|
+
* @returns {Number}
|
|
55
|
+
*/
|
|
56
|
+
const maxArray = (array, key) => {
|
|
57
|
+
if (!array) {
|
|
58
|
+
console.warn("ARRAY NOT EXITED !");
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
if (array.length > 0) {
|
|
62
|
+
if (key)
|
|
63
|
+
return array.reduce((c, v) => (c > v[key] ? c : v[key]));
|
|
64
|
+
else
|
|
65
|
+
return array.reduce((c, v) => (c > v ? c : v));
|
|
66
|
+
}
|
|
67
|
+
return 0;
|
|
68
|
+
};
|
|
69
|
+
exports.maxArray = maxArray;
|
|
70
|
+
/**
|
|
71
|
+
*
|
|
72
|
+
* @param {Array} array
|
|
73
|
+
* @param {string} key
|
|
74
|
+
* @returns {Array}
|
|
75
|
+
*/
|
|
76
|
+
const sortElementByString = (array, key) => {
|
|
77
|
+
if (!Array.isArray(array))
|
|
78
|
+
return [];
|
|
79
|
+
if (key)
|
|
80
|
+
return array.sort((x, y) => {
|
|
81
|
+
var a = x[key].toUpperCase(), b = y[key].toUpperCase();
|
|
82
|
+
return a == b ? 0 : a > b ? 1 : -1;
|
|
83
|
+
});
|
|
84
|
+
console.log("NO KEY");
|
|
85
|
+
};
|
|
86
|
+
exports.sortElementByString = sortElementByString;
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* @param {Array} array
|
|
90
|
+
* @param {string} key
|
|
91
|
+
* @returns {Array}
|
|
92
|
+
*/
|
|
93
|
+
const sortElementByNumber = (array, key) => {
|
|
94
|
+
if (!Array.isArray(array))
|
|
95
|
+
return [];
|
|
96
|
+
if (key)
|
|
97
|
+
return array.sort((a, b) => {
|
|
98
|
+
return a[key] - b[key];
|
|
99
|
+
});
|
|
100
|
+
console.log("NO KEY");
|
|
101
|
+
};
|
|
102
|
+
exports.sortElementByNumber = sortElementByNumber;
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* @param {Array} array
|
|
106
|
+
* @returns {any}
|
|
107
|
+
*/
|
|
108
|
+
const firstElement = (array) => {
|
|
109
|
+
if (array)
|
|
110
|
+
if (array.length || array.length > 0)
|
|
111
|
+
return array[0];
|
|
112
|
+
return null;
|
|
113
|
+
};
|
|
114
|
+
exports.firstElement = firstElement;
|
|
115
|
+
/**
|
|
116
|
+
*
|
|
117
|
+
* @param {Array} array
|
|
118
|
+
* @returns {any}
|
|
119
|
+
*/
|
|
120
|
+
const lastElement = (array) => {
|
|
121
|
+
if (array)
|
|
122
|
+
if (array.length || array.length > 0)
|
|
123
|
+
return array[array.length - 1];
|
|
124
|
+
return null;
|
|
125
|
+
};
|
|
126
|
+
exports.lastElement = lastElement;
|
|
127
|
+
/**
|
|
128
|
+
*
|
|
129
|
+
* @param {Array} array
|
|
130
|
+
* @returns {any}
|
|
131
|
+
*/
|
|
132
|
+
const randomIndex = (array) => {
|
|
133
|
+
if (array)
|
|
134
|
+
return (0, math_1.randInt)(0, array.length - 1);
|
|
135
|
+
return -1;
|
|
136
|
+
};
|
|
137
|
+
exports.randomIndex = randomIndex;
|
|
138
|
+
/**
|
|
139
|
+
*
|
|
140
|
+
* @param {Array} array
|
|
141
|
+
* @returns {any}
|
|
142
|
+
*/
|
|
143
|
+
const randomElement = (array) => {
|
|
144
|
+
if (array)
|
|
145
|
+
return array[(0, exports.randomIndex)(array)];
|
|
146
|
+
return null;
|
|
147
|
+
};
|
|
148
|
+
exports.randomElement = randomElement;
|
|
149
|
+
/**
|
|
150
|
+
* Remove same elements from 2 arrays
|
|
151
|
+
*/
|
|
152
|
+
const mergeAndMakeUniqueElement = (list1, list2, key) => {
|
|
153
|
+
if (!list1 || !list2)
|
|
154
|
+
return;
|
|
155
|
+
if (key) {
|
|
156
|
+
return list1.concat(list2).filter((item, index, self) => {
|
|
157
|
+
return self.findIndex((x) => x[key] == item[key]) === index;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
return list1.concat(list2).filter((x, index, self) => {
|
|
162
|
+
return self.indexOf(x) === index;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
exports.mergeAndMakeUniqueElement = mergeAndMakeUniqueElement;
|
|
167
|
+
/**
|
|
168
|
+
* check target == toMatch
|
|
169
|
+
* @param {Array} target
|
|
170
|
+
* @param {Array} toMatch
|
|
171
|
+
* @returns {Boolean}
|
|
172
|
+
*/
|
|
173
|
+
const allMatchInArray = (target, toMatch) => {
|
|
174
|
+
if (!target || !toMatch)
|
|
175
|
+
return false;
|
|
176
|
+
const found = toMatch.every((item) => {
|
|
177
|
+
return target.includes(item);
|
|
178
|
+
});
|
|
179
|
+
return found;
|
|
180
|
+
};
|
|
181
|
+
exports.allMatchInArray = allMatchInArray;
|
|
182
|
+
const removeItem = (item, array) => {
|
|
183
|
+
const index = array.indexOf(item);
|
|
184
|
+
if (index == -1) {
|
|
185
|
+
console.warn("[ArrayExtra.removeItem] Item not found.");
|
|
186
|
+
return array;
|
|
187
|
+
}
|
|
188
|
+
array.splice(index, 1);
|
|
189
|
+
return array;
|
|
190
|
+
};
|
|
191
|
+
exports.removeItem = removeItem;
|
|
192
|
+
const removeItemByKey = (key, value, array) => {
|
|
193
|
+
const foundIndex = array.findIndex((item) => {
|
|
194
|
+
return item[key] == value;
|
|
195
|
+
});
|
|
196
|
+
if (foundIndex < 0) {
|
|
197
|
+
console.warn("[ArrayExtra.removeItemByKey] Item not found.", key, value);
|
|
198
|
+
return array;
|
|
199
|
+
}
|
|
200
|
+
array.splice(foundIndex, 1);
|
|
201
|
+
return array;
|
|
202
|
+
};
|
|
203
|
+
exports.removeItemByKey = removeItemByKey;
|
|
204
|
+
const getRandom = (array, n) => {
|
|
205
|
+
n = n || array.length;
|
|
206
|
+
var result = new Array(n), len = array.length, taken = new Array(len);
|
|
207
|
+
if (n > len)
|
|
208
|
+
throw new RangeError("getRandom: more elements taken than available");
|
|
209
|
+
while (n--) {
|
|
210
|
+
var x = Math.floor(Math.random() * len);
|
|
211
|
+
result[n] = array[x in taken ? taken[x] : x];
|
|
212
|
+
taken[x] = --len in taken ? taken[len] : len;
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
};
|
|
216
|
+
exports.getRandom = getRandom;
|
|
217
|
+
/**
|
|
218
|
+
* Get an array with shuffle element
|
|
219
|
+
*/
|
|
220
|
+
const getHalfRandom = (array, n) => {
|
|
221
|
+
var n = Math.ceil(array.length / 2);
|
|
222
|
+
return (0, exports.getRandom)(array, n);
|
|
223
|
+
};
|
|
224
|
+
exports.getHalfRandom = getHalfRandom;
|
|
225
|
+
/**
|
|
226
|
+
* Make array shuffle itself
|
|
227
|
+
*/
|
|
228
|
+
const shuffle = (array) => {
|
|
229
|
+
var i = array.length, j, temp;
|
|
230
|
+
if (array.length < 1)
|
|
231
|
+
return array;
|
|
232
|
+
while (--i) {
|
|
233
|
+
j = Math.floor(Math.random() * (i + 1));
|
|
234
|
+
temp = array[i];
|
|
235
|
+
array[i] = array[j];
|
|
236
|
+
array[j] = temp;
|
|
237
|
+
}
|
|
238
|
+
return array;
|
|
239
|
+
};
|
|
240
|
+
exports.shuffle = shuffle;
|
|
241
|
+
/**
|
|
242
|
+
*
|
|
243
|
+
* @param {Array} array
|
|
244
|
+
* @param {Number} oldIndex
|
|
245
|
+
* @param {Number} newIndex
|
|
246
|
+
* @returns {Array}
|
|
247
|
+
*/
|
|
248
|
+
const moveIndex = (array, oldIndex, newIndex) => {
|
|
249
|
+
if (newIndex >= array.length) {
|
|
250
|
+
var k = newIndex - array.length + 1;
|
|
251
|
+
while (k--) {
|
|
252
|
+
array.push(undefined);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
|
|
256
|
+
return array;
|
|
257
|
+
};
|
|
258
|
+
exports.moveIndex = moveIndex;
|
|
259
|
+
const moveArray = (array, oldIndex, newIndex) => {
|
|
260
|
+
while (oldIndex < 0) {
|
|
261
|
+
oldIndex += array.length;
|
|
262
|
+
}
|
|
263
|
+
while (newIndex < 0) {
|
|
264
|
+
newIndex += array.length;
|
|
265
|
+
}
|
|
266
|
+
if (newIndex >= array.length) {
|
|
267
|
+
var k = newIndex - array.length;
|
|
268
|
+
while (k-- + 1) {
|
|
269
|
+
array.push(999);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
|
|
273
|
+
return array;
|
|
274
|
+
};
|
|
275
|
+
exports.moveArray = moveArray;
|
|
276
|
+
const xarray = {
|
|
277
|
+
sumArray: exports.sumArray,
|
|
278
|
+
averageArray: exports.averageArray,
|
|
279
|
+
minArray: exports.minArray,
|
|
280
|
+
maxArray: exports.maxArray,
|
|
281
|
+
sortElementByString: exports.sortElementByString,
|
|
282
|
+
sortElementByNumber: exports.sortElementByNumber,
|
|
283
|
+
firstElement: exports.firstElement,
|
|
284
|
+
lastElement: exports.lastElement,
|
|
285
|
+
randomIndex: exports.randomIndex,
|
|
286
|
+
randomElement: exports.randomElement,
|
|
287
|
+
mergeAndMakeUniqueElement: exports.mergeAndMakeUniqueElement,
|
|
288
|
+
allMatchInArray: exports.allMatchInArray,
|
|
289
|
+
removeItem: exports.removeItem,
|
|
290
|
+
removeItemByKey: exports.removeItemByKey,
|
|
291
|
+
getRandom: exports.getRandom,
|
|
292
|
+
getHalfRandom: exports.getHalfRandom,
|
|
293
|
+
shuffle: exports.shuffle,
|
|
294
|
+
moveIndex: exports.moveIndex,
|
|
295
|
+
moveArray: exports.moveArray,
|
|
296
|
+
};
|
|
297
|
+
exports.default = xarray;
|
|
298
298
|
//# sourceMappingURL=array.js.map
|
package/dist/color.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export declare const random: (hex?: boolean) => string;
|
|
2
|
-
export declare const pSBC: (p: any, c0: any, c1?: any, l?: any) => string | null;
|
|
3
|
-
export declare const hexToRgb: (hex: string) => {
|
|
4
|
-
r: number;
|
|
5
|
-
g: number;
|
|
6
|
-
b: number;
|
|
7
|
-
} | null;
|
|
8
|
-
export declare const hexDarken: (hex: string, amount: number) => string | null;
|
|
9
|
-
export declare const hexLighten: (hex: string, amount: number) => string | null;
|
|
10
|
-
export declare const RGBToHex: (rgb: string | any) => string | null;
|
|
11
|
-
export declare const hexToVBColor: (rrggbb: string) => number;
|
|
12
|
-
declare const color: {
|
|
13
|
-
random: (hex?: boolean) => string;
|
|
14
|
-
pSBC: (p: any, c0: any, c1?: any, l?: any) => string | null;
|
|
15
|
-
hexToRgb: (hex: string) => {
|
|
16
|
-
r: number;
|
|
17
|
-
g: number;
|
|
18
|
-
b: number;
|
|
19
|
-
} | null;
|
|
20
|
-
hexDarken: (hex: string, amount: number) => string | null;
|
|
21
|
-
hexLighten: (hex: string, amount: number) => string | null;
|
|
22
|
-
RGBToHex: (rgb: string | any) => string | null;
|
|
23
|
-
hexToVBColor: (rrggbb: string) => number;
|
|
24
|
-
};
|
|
25
|
-
export default color;
|
|
1
|
+
export declare const random: (hex?: boolean) => string;
|
|
2
|
+
export declare const pSBC: (p: any, c0: any, c1?: any, l?: any) => string | null;
|
|
3
|
+
export declare const hexToRgb: (hex: string) => {
|
|
4
|
+
r: number;
|
|
5
|
+
g: number;
|
|
6
|
+
b: number;
|
|
7
|
+
} | null;
|
|
8
|
+
export declare const hexDarken: (hex: string, amount: number) => string | null;
|
|
9
|
+
export declare const hexLighten: (hex: string, amount: number) => string | null;
|
|
10
|
+
export declare const RGBToHex: (rgb: string | any) => string | null;
|
|
11
|
+
export declare const hexToVBColor: (rrggbb: string) => number;
|
|
12
|
+
declare const color: {
|
|
13
|
+
random: (hex?: boolean) => string;
|
|
14
|
+
pSBC: (p: any, c0: any, c1?: any, l?: any) => string | null;
|
|
15
|
+
hexToRgb: (hex: string) => {
|
|
16
|
+
r: number;
|
|
17
|
+
g: number;
|
|
18
|
+
b: number;
|
|
19
|
+
} | null;
|
|
20
|
+
hexDarken: (hex: string, amount: number) => string | null;
|
|
21
|
+
hexLighten: (hex: string, amount: number) => string | null;
|
|
22
|
+
RGBToHex: (rgb: string | any) => string | null;
|
|
23
|
+
hexToVBColor: (rrggbb: string) => number;
|
|
24
|
+
};
|
|
25
|
+
export default color;
|