@zelgadis87/utils-core 5.3.8 → 5.3.9
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/.rollup/index.cjs +43 -6
- package/.rollup/index.cjs.map +1 -1
- package/.rollup/index.d.ts +23 -1
- package/.rollup/index.mjs +42 -7
- package/.rollup/index.mjs.map +1 -1
- package/.rollup/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +7 -0
- package/package.json +8 -8
- package/src/Optional.ts +9 -0
- package/src/time/RandomTimeDuration.ts +2 -2
- package/src/utils/arrays.ts +4 -3
- package/src/utils/random.ts +31 -2
package/.rollup/index.cjs
CHANGED
|
@@ -362,6 +362,13 @@ class Optional {
|
|
|
362
362
|
static ofNullable(t) {
|
|
363
363
|
return new Optional(t);
|
|
364
364
|
}
|
|
365
|
+
static findInArray(arr, predicate) {
|
|
366
|
+
return Optional.ofNullable(arr.find(predicate));
|
|
367
|
+
}
|
|
368
|
+
static findIndexInArray(arr, predicate) {
|
|
369
|
+
const idx = arr.findIndex(predicate);
|
|
370
|
+
return idx === -1 ? Optional.empty() : Optional.of(idx);
|
|
371
|
+
}
|
|
365
372
|
}
|
|
366
373
|
class ErrorGetEmptyOptional extends Error {
|
|
367
374
|
constructor() {
|
|
@@ -807,12 +814,13 @@ function shallowArrayEquals(a, b) {
|
|
|
807
814
|
}
|
|
808
815
|
return true;
|
|
809
816
|
}
|
|
817
|
+
/** @deprecated[2026.03.01]: Use {@link Optional.findInArray} instead. */
|
|
810
818
|
function findInArray(arr, predicate) {
|
|
811
|
-
return Optional.
|
|
819
|
+
return Optional.findInArray(arr, predicate);
|
|
812
820
|
}
|
|
821
|
+
/** @deprecated[2026.03.01]: Use {@link Optional.findIndexInArray} instead. */
|
|
813
822
|
function findIndexInArray(arr, predicate) {
|
|
814
|
-
|
|
815
|
-
return idx === -1 ? Optional.empty() : Optional.of(idx);
|
|
823
|
+
return Optional.findIndexInArray(arr, predicate);
|
|
816
824
|
}
|
|
817
825
|
function zip(ts, rs) {
|
|
818
826
|
if (ts.length !== rs.length)
|
|
@@ -1134,11 +1142,38 @@ async function awaitAtMost(promise, duration) {
|
|
|
1134
1142
|
}
|
|
1135
1143
|
const NEVER = new Promise(_resolve => { });
|
|
1136
1144
|
|
|
1145
|
+
/**
|
|
1146
|
+
* Returns a random integer in the range [min, max].
|
|
1147
|
+
* @deprecated Use randomIntegerInInterval or randomDecimalInInterval instead
|
|
1148
|
+
*/
|
|
1137
1149
|
function randomNumberInInterval(min, max) {
|
|
1138
1150
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
1139
1151
|
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Returns a random integer in the range [min, max].
|
|
1154
|
+
* @param min - Minimum value (inclusive)
|
|
1155
|
+
* @param max - Maximum value (inclusive)
|
|
1156
|
+
* @returns A random integer between min and max
|
|
1157
|
+
*/
|
|
1158
|
+
function randomIntegerInInterval(min, max) {
|
|
1159
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Returns a random decimal in the range [min, max).
|
|
1163
|
+
* @param min - Minimum value (inclusive)
|
|
1164
|
+
* @param max - Maximum value (exclusive)
|
|
1165
|
+
* @returns A random decimal between min and max
|
|
1166
|
+
*/
|
|
1167
|
+
function randomDecimalInInterval(min, max) {
|
|
1168
|
+
return Math.random() * (max - min) + min;
|
|
1169
|
+
}
|
|
1140
1170
|
const randomId = (length) => {
|
|
1141
|
-
|
|
1171
|
+
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
1172
|
+
let result = '';
|
|
1173
|
+
for (let i = 0; i < length; i++) {
|
|
1174
|
+
result += characters.charAt(randomIntegerInInterval(0, characters.length - 1));
|
|
1175
|
+
}
|
|
1176
|
+
return result;
|
|
1142
1177
|
};
|
|
1143
1178
|
function randomPick(arr) {
|
|
1144
1179
|
return first$1(randomPicks(arr, 1));
|
|
@@ -1147,7 +1182,7 @@ function randomPicks(arr, count) {
|
|
|
1147
1182
|
const available = [...arr];
|
|
1148
1183
|
const result = [];
|
|
1149
1184
|
while (available.length > 0 && count > 0) {
|
|
1150
|
-
const randomIndex =
|
|
1185
|
+
const randomIndex = randomIntegerInInterval(0, available.length - 1);
|
|
1151
1186
|
result.push(available[randomIndex]);
|
|
1152
1187
|
available.splice(randomIndex, 1);
|
|
1153
1188
|
count--;
|
|
@@ -3452,7 +3487,7 @@ const Sorter = {
|
|
|
3452
3487
|
|
|
3453
3488
|
function randomize(unit) {
|
|
3454
3489
|
return (a, b) => {
|
|
3455
|
-
return TimeDuration.fromMs(
|
|
3490
|
+
return TimeDuration.fromMs(randomIntegerInInterval(unit.toMs(a), unit.toMs(b)));
|
|
3456
3491
|
};
|
|
3457
3492
|
}
|
|
3458
3493
|
class RandomTimeDuration {
|
|
@@ -3786,7 +3821,9 @@ exports.pipedInvoke = pipedInvoke;
|
|
|
3786
3821
|
exports.pipedInvokeFromArray = pipedInvokeFromArray;
|
|
3787
3822
|
exports.pluralize = pluralize;
|
|
3788
3823
|
exports.promiseSequence = promiseSequence;
|
|
3824
|
+
exports.randomDecimalInInterval = randomDecimalInInterval;
|
|
3789
3825
|
exports.randomId = randomId;
|
|
3826
|
+
exports.randomIntegerInInterval = randomIntegerInInterval;
|
|
3790
3827
|
exports.randomNumberInInterval = randomNumberInInterval;
|
|
3791
3828
|
exports.randomPick = randomPick;
|
|
3792
3829
|
exports.randomPicks = randomPicks;
|