@zelgadis87/utils-core 4.3.1 → 4.3.3
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/tsconfig.tsbuildinfo +1 -1
- package/dist/types/arrays.d.ts +2 -0
- package/dist/types/strings.d.ts +5 -0
- package/dist/utils/index.d.ts +0 -2
- package/esbuild/index.cjs +400 -389
- package/esbuild/index.mjs +395 -388
- package/package.json +1 -1
- package/src/time/TimeDuration.ts +1 -2
- package/src/types/arrays.ts +7 -0
- package/src/types/strings.ts +49 -1
- package/src/utils/index.ts +0 -2
- package/dist/utils/pad.d.ts +0 -3
- package/dist/utils/pluralize.d.ts +0 -1
- package/src/utils/pad.ts +0 -20
- package/src/utils/pluralize.ts +0 -20
package/esbuild/index.mjs
CHANGED
|
@@ -219,6 +219,12 @@ function sortedArray(arr, sortFn) {
|
|
|
219
219
|
function includes(arr, item, fromIndex) {
|
|
220
220
|
return arr.includes(item, fromIndex);
|
|
221
221
|
}
|
|
222
|
+
function mapTruthys(arr, mapper) {
|
|
223
|
+
return arr.map(mapper).filter((value) => value !== void 0);
|
|
224
|
+
}
|
|
225
|
+
function flatMapTruthys(arr, mapper) {
|
|
226
|
+
return arr.flatMap(mapper).filter((value) => value !== void 0);
|
|
227
|
+
}
|
|
222
228
|
|
|
223
229
|
// src/types/booleans.ts
|
|
224
230
|
function isTrue(x) {
|
|
@@ -381,6 +387,29 @@ function stringToNumber(s) {
|
|
|
381
387
|
return null;
|
|
382
388
|
return Number(s);
|
|
383
389
|
}
|
|
390
|
+
function pad(str, n, char, where = "left") {
|
|
391
|
+
const length = ensureDefined(str).length;
|
|
392
|
+
if (length >= ensureDefined(n)) return str;
|
|
393
|
+
if (ensureDefined(char).length !== 1)
|
|
394
|
+
throw new Error("Illegal pad character");
|
|
395
|
+
const padding = repeat(char, n - length);
|
|
396
|
+
return (where === "left" ? padding : "") + str + (where === "right" ? padding : "");
|
|
397
|
+
}
|
|
398
|
+
function padLeft(str, n, char) {
|
|
399
|
+
return pad(str, n, char, "left");
|
|
400
|
+
}
|
|
401
|
+
function padRight(str, n, char) {
|
|
402
|
+
return pad(str, n, char, "right");
|
|
403
|
+
}
|
|
404
|
+
function ellipsis(str, maxLength, padChar, padWhere = "left") {
|
|
405
|
+
if (maxLength < 4)
|
|
406
|
+
throw new Error("Invalid argument maxLength");
|
|
407
|
+
if (str.length <= maxLength) {
|
|
408
|
+
return pad(str, maxLength, padChar, padWhere);
|
|
409
|
+
} else {
|
|
410
|
+
return str.substring(0, maxLength - 3) + "...";
|
|
411
|
+
}
|
|
412
|
+
}
|
|
384
413
|
var StringParts = class _StringParts {
|
|
385
414
|
_parts;
|
|
386
415
|
constructor(...parts) {
|
|
@@ -821,397 +850,25 @@ function randomPercentage(min2, max2) {
|
|
|
821
850
|
return randomInterval(min2, max2) / 100;
|
|
822
851
|
}
|
|
823
852
|
|
|
824
|
-
// src/
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
function constant(v) {
|
|
831
|
-
return () => v;
|
|
832
|
-
}
|
|
833
|
-
function identity(t) {
|
|
834
|
-
return t;
|
|
835
|
-
}
|
|
836
|
-
var constantNull = constant(null);
|
|
837
|
-
var constantTrue = constant(true);
|
|
838
|
-
var constantFalse = constant(false);
|
|
839
|
-
var constantZero = constant(0);
|
|
840
|
-
var constantOne = constant(1);
|
|
841
|
-
|
|
842
|
-
// src/utils/entries.ts
|
|
843
|
-
function dictToEntries(obj) {
|
|
844
|
-
return Object.entries(obj);
|
|
845
|
-
}
|
|
846
|
-
function entriesToDict(entries) {
|
|
847
|
-
return Object.fromEntries(entries);
|
|
848
|
-
}
|
|
849
|
-
function mapEntries(dict, mapper) {
|
|
850
|
-
return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
// src/utils/groupBy.ts
|
|
854
|
-
function groupByString(arr, field) {
|
|
855
|
-
return groupByStringWith(arr, (t) => t[field]);
|
|
856
|
-
}
|
|
857
|
-
function groupByNumber(arr, field) {
|
|
858
|
-
return groupByNumberWith(arr, (t) => t[field]);
|
|
859
|
-
}
|
|
860
|
-
function groupBySymbol(arr, field) {
|
|
861
|
-
return groupBySymbolWith(arr, (t) => t[field]);
|
|
862
|
-
}
|
|
863
|
-
function groupByStringWith(arr, getter) {
|
|
864
|
-
return doGroupByWith(arr, getter);
|
|
865
|
-
}
|
|
866
|
-
function groupByNumberWith(arr, getter) {
|
|
867
|
-
return doGroupByWith(arr, getter);
|
|
868
|
-
}
|
|
869
|
-
function groupBySymbolWith(arr, getter) {
|
|
870
|
-
return doGroupByWith(arr, getter);
|
|
871
|
-
}
|
|
872
|
-
function doGroupByWith(arr, getter) {
|
|
873
|
-
return arr.reduce((dict, cur) => {
|
|
874
|
-
const key = getter(cur);
|
|
875
|
-
if (key !== null && key !== void 0) {
|
|
876
|
-
const arr2 = dict[key] ?? [];
|
|
877
|
-
arr2.push(cur);
|
|
878
|
-
dict[key] = arr2;
|
|
879
|
-
}
|
|
880
|
-
return dict;
|
|
881
|
-
}, {});
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
// src/utils/iff.ts
|
|
885
|
-
function iff(firstPredicate, valueIfTrue) {
|
|
886
|
-
if (firstPredicate === true || firstPredicate instanceof Function && firstPredicate() === true) {
|
|
887
|
-
const ret = {
|
|
888
|
-
elseIf: () => ret,
|
|
889
|
-
otherwise: () => valueIfTrue
|
|
890
|
-
};
|
|
891
|
-
return ret;
|
|
892
|
-
} else {
|
|
893
|
-
const ret = {
|
|
894
|
-
elseIf: (elseIf, valueIfElseIfTrue) => iff(elseIf, valueIfElseIfTrue),
|
|
895
|
-
otherwise: (valueIfElse) => valueIfElse
|
|
896
|
-
};
|
|
897
|
-
return ret;
|
|
853
|
+
// src/time/TimeDuration.ts
|
|
854
|
+
var TimeDuration = class _TimeDuration extends TimeBase {
|
|
855
|
+
constructor(value, unit) {
|
|
856
|
+
super(value, unit);
|
|
857
|
+
if (value < 0)
|
|
858
|
+
throw new Error("Duration cannot be less than 0");
|
|
898
859
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
// src/utils/indexBy.ts
|
|
902
|
-
function indexByString(arr, field) {
|
|
903
|
-
return indexByStringWith(arr, (t) => t[field]);
|
|
904
|
-
}
|
|
905
|
-
function indexByNumber(arr, field) {
|
|
906
|
-
return indexByNumberWith(arr, (t) => t[field]);
|
|
907
|
-
}
|
|
908
|
-
function indexBySymbol(arr, field) {
|
|
909
|
-
return indexBySymbolWith(arr, (t) => t[field]);
|
|
910
|
-
}
|
|
911
|
-
function indexByStringWith(arr, getter) {
|
|
912
|
-
return doIndexByWith(arr, getter);
|
|
913
|
-
}
|
|
914
|
-
function indexByNumberWith(arr, getter) {
|
|
915
|
-
return doIndexByWith(arr, getter);
|
|
916
|
-
}
|
|
917
|
-
function indexBySymbolWith(arr, getter) {
|
|
918
|
-
return doIndexByWith(arr, getter);
|
|
919
|
-
}
|
|
920
|
-
function doIndexByWith(arr, getter) {
|
|
921
|
-
return arr.reduce((dict, cur) => {
|
|
922
|
-
const key = getter(cur);
|
|
923
|
-
if (key !== null && key !== void 0)
|
|
924
|
-
dict[key] = cur;
|
|
925
|
-
return dict;
|
|
926
|
-
}, {});
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
// src/utils/jsonCloneDeep.ts
|
|
930
|
-
function jsonCloneDeep(a) {
|
|
931
|
-
if (null === a || "object" !== typeof a) return a;
|
|
932
|
-
if (a instanceof Date) {
|
|
933
|
-
return new Date(a.getTime());
|
|
934
|
-
} else if (a instanceof Array) {
|
|
935
|
-
const copy = [];
|
|
936
|
-
for (let i = 0, len = a.length; i < len; i++) {
|
|
937
|
-
copy[i] = jsonCloneDeep(a[i]);
|
|
938
|
-
}
|
|
939
|
-
return copy;
|
|
940
|
-
} else if (a instanceof Object) {
|
|
941
|
-
const copy = {};
|
|
942
|
-
for (let attr in a) {
|
|
943
|
-
if (a.hasOwnProperty(attr))
|
|
944
|
-
copy[attr] = jsonCloneDeep(a[attr]);
|
|
945
|
-
}
|
|
946
|
-
return copy;
|
|
860
|
+
create(value, unit) {
|
|
861
|
+
return new _TimeDuration(value, unit);
|
|
947
862
|
}
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
// src/utils/math.ts
|
|
952
|
-
function clamp(n, min2, max2) {
|
|
953
|
-
if (min2 === null || max2 === null || n === null || isNaN(min2) || isNaN(max2) || isNaN(n) || min2 > max2)
|
|
954
|
-
throw new Error();
|
|
955
|
-
if (n > max2) {
|
|
956
|
-
return max2;
|
|
957
|
-
} else if (n < min2) {
|
|
958
|
-
return min2;
|
|
959
|
-
} else {
|
|
960
|
-
return n;
|
|
863
|
+
addDuration(duration) {
|
|
864
|
+
return this.addUnits(duration.ms, TimeUnit.MILLISECONDS);
|
|
961
865
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
const f = 1 / arr.length;
|
|
965
|
-
return arr.reduce((tot, cur) => tot + cur * f, 0);
|
|
966
|
-
}
|
|
967
|
-
function sum(arr) {
|
|
968
|
-
return arr.reduce((tot, cur) => tot + cur, 0);
|
|
969
|
-
}
|
|
970
|
-
function sumBy(arr, getter) {
|
|
971
|
-
return sum(arr.map(getter));
|
|
972
|
-
}
|
|
973
|
-
function min(arr) {
|
|
974
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
975
|
-
return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
|
|
976
|
-
}
|
|
977
|
-
function minBy(arr, getter) {
|
|
978
|
-
return min(arr.map(getter));
|
|
979
|
-
}
|
|
980
|
-
function max(arr) {
|
|
981
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
982
|
-
return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
|
|
983
|
-
}
|
|
984
|
-
function maxBy(arr, getter) {
|
|
985
|
-
return max(arr.map(getter));
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
// src/utils/noop.ts
|
|
989
|
-
function noop() {
|
|
990
|
-
}
|
|
991
|
-
|
|
992
|
-
// src/utils/omit.ts
|
|
993
|
-
function omit(o, ...keys) {
|
|
994
|
-
return keys.reduce((obj, key) => {
|
|
995
|
-
delete obj[key];
|
|
996
|
-
return obj;
|
|
997
|
-
}, { ...o });
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
// src/utils/pad.ts
|
|
1001
|
-
function pad(str, n, char, where = "left") {
|
|
1002
|
-
const length = ensureDefined(str).length;
|
|
1003
|
-
if (length >= ensureDefined(n)) return str;
|
|
1004
|
-
if (ensureDefined(char).length !== 1)
|
|
1005
|
-
throw new Error("Illegal pad character");
|
|
1006
|
-
const padding = repeat(char, n - length);
|
|
1007
|
-
return (where === "left" ? padding : "") + str + (where === "right" ? padding : "");
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
// src/utils/pluralize.ts
|
|
1011
|
-
function pluralize(n, singular, plural) {
|
|
1012
|
-
if (!singular || !singular.length)
|
|
1013
|
-
throw new Error();
|
|
1014
|
-
if (n === 1)
|
|
1015
|
-
return singular;
|
|
1016
|
-
plural = plural ?? singular + "s";
|
|
1017
|
-
const firstUppercase = singular.charAt(0) === singular.charAt(0).toUpperCase();
|
|
1018
|
-
if (firstUppercase) {
|
|
1019
|
-
const PLURAL = plural.toUpperCase();
|
|
1020
|
-
const isAllUppercase = plural === PLURAL;
|
|
1021
|
-
plural = isAllUppercase ? PLURAL : plural.charAt(0).toUpperCase() + plural.slice(1).toLowerCase();
|
|
866
|
+
removeDuration(duration) {
|
|
867
|
+
return this.removeUnits(duration.ms, TimeUnit.MILLISECONDS);
|
|
1022
868
|
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
// src/utils/round.ts
|
|
1027
|
-
var roundModes = {
|
|
1028
|
-
"toNearest": Math.round,
|
|
1029
|
-
"toLower": Math.floor,
|
|
1030
|
-
"toUpper": Math.ceil,
|
|
1031
|
-
"towardsZero": (n) => n > 0 ? Math.floor(n) : Math.ceil(n),
|
|
1032
|
-
"awayFromZero": (n) => n > 0 ? Math.ceil(n) : Math.floor(n)
|
|
1033
|
-
};
|
|
1034
|
-
function round(n, precision = 0, mode = "toNearest") {
|
|
1035
|
-
const base = 10;
|
|
1036
|
-
const power = Math.pow(base, precision);
|
|
1037
|
-
return roundModes[mode](n * power) / power;
|
|
1038
|
-
}
|
|
1039
|
-
var roundToNearest = (n, precision = 0) => round(n, precision, "toNearest");
|
|
1040
|
-
var roundToLower = (n, precision = 0) => round(n, precision, "toLower");
|
|
1041
|
-
var roundToUpper = (n, precision = 0) => round(n, precision, "toUpper");
|
|
1042
|
-
var roundTowardsZero = (n, precision = 0) => round(n, precision, "towardsZero");
|
|
1043
|
-
var roundAwayFromZero = (n, precision = 0) => round(n, precision, "awayFromZero");
|
|
1044
|
-
|
|
1045
|
-
// src/utils/sortBy.ts
|
|
1046
|
-
function sortBy(keyOrGetter, direction = "ASC", nulls = "LAST") {
|
|
1047
|
-
const directionNum = direction === "ASC" ? -1 : 1;
|
|
1048
|
-
const nullsNum = nulls === "LAST" ? -1 : 1;
|
|
1049
|
-
const sortByNum = (a, b) => {
|
|
1050
|
-
if (a === b)
|
|
1051
|
-
return -1;
|
|
1052
|
-
const aDefined = !(a === null || a === void 0 || isNaN(a));
|
|
1053
|
-
const bDefined = !(b === null || b === void 0 || isNaN(b));
|
|
1054
|
-
if (!aDefined && !bDefined)
|
|
1055
|
-
return -1;
|
|
1056
|
-
if (aDefined !== bDefined)
|
|
1057
|
-
return nullsNum * (aDefined ? 1 : -1);
|
|
1058
|
-
return (a > b ? -1 : 1) * directionNum;
|
|
1059
|
-
};
|
|
1060
|
-
if (typeof keyOrGetter === "function") {
|
|
1061
|
-
return (a, b) => sortByNum(keyOrGetter(a), keyOrGetter(b));
|
|
1062
|
-
} else {
|
|
1063
|
-
return (a, b) => sortByNum(a[keyOrGetter], b[keyOrGetter]);
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
// src/async/RateThrottler.ts
|
|
1068
|
-
var RateThrottler = class {
|
|
1069
|
-
constructor(_frequency) {
|
|
1070
|
-
this._frequency = _frequency;
|
|
1071
|
-
const initialOffset = this.cooldown.divideBy(_frequency.times);
|
|
1072
|
-
this._availableSlots = new Array(_frequency.times).fill(0).map((_, i) => TimeInstant.now().addMs(i * initialOffset.ms));
|
|
1073
|
-
this._waitingRequests = [];
|
|
1074
|
-
}
|
|
1075
|
-
_availableSlots;
|
|
1076
|
-
_waitingRequests;
|
|
1077
|
-
get cooldown() {
|
|
1078
|
-
return this._frequency.period;
|
|
1079
|
-
}
|
|
1080
|
-
async execute(fn) {
|
|
1081
|
-
const wrappedFn = async () => {
|
|
1082
|
-
const slot = this._availableSlots.shift();
|
|
1083
|
-
try {
|
|
1084
|
-
return slot.promise().then(() => fn());
|
|
1085
|
-
} finally {
|
|
1086
|
-
this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
|
|
1087
|
-
if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
|
|
1088
|
-
}
|
|
1089
|
-
};
|
|
1090
|
-
if (this._availableSlots.length > 0) {
|
|
1091
|
-
return wrappedFn();
|
|
1092
|
-
} else {
|
|
1093
|
-
const waitingRequest = new Deferred_default();
|
|
1094
|
-
this._waitingRequests.push(waitingRequest);
|
|
1095
|
-
return waitingRequest.then(() => wrappedFn());
|
|
1096
|
-
}
|
|
1097
|
-
}
|
|
1098
|
-
};
|
|
1099
|
-
|
|
1100
|
-
// src/async/Semaphore.ts
|
|
1101
|
-
var Semaphore = class {
|
|
1102
|
-
constructor(_availableSlots = 1) {
|
|
1103
|
-
this._availableSlots = _availableSlots;
|
|
1104
|
-
this._queuedRequests = [];
|
|
1105
|
-
}
|
|
1106
|
-
_queuedRequests;
|
|
1107
|
-
_inProgress = 0;
|
|
1108
|
-
async _awaitSlot() {
|
|
1109
|
-
if (this._availableSlots > 0) {
|
|
1110
|
-
this._availableSlots -= 1;
|
|
1111
|
-
this._inProgress += 1;
|
|
1112
|
-
return void 0;
|
|
1113
|
-
} else {
|
|
1114
|
-
const deferred = new Deferred_default();
|
|
1115
|
-
this._queuedRequests.push(deferred);
|
|
1116
|
-
return deferred.asPromise();
|
|
1117
|
-
}
|
|
1118
|
-
}
|
|
1119
|
-
_releaseSlot() {
|
|
1120
|
-
const waitingSlotToResolve = this._queuedRequests.shift();
|
|
1121
|
-
if (waitingSlotToResolve) {
|
|
1122
|
-
waitingSlotToResolve.resolve();
|
|
1123
|
-
} else {
|
|
1124
|
-
this._availableSlots += 1;
|
|
1125
|
-
this._inProgress -= 1;
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
async execute(fn, cooldown = TimeDuration.ZERO) {
|
|
1129
|
-
return this._awaitSlot().then(() => fn()).finally(() => {
|
|
1130
|
-
void cooldown.promise().then(() => this._releaseSlot());
|
|
1131
|
-
});
|
|
1132
|
-
}
|
|
1133
|
-
get availableSlots() {
|
|
1134
|
-
return this._availableSlots;
|
|
1135
|
-
}
|
|
1136
|
-
get queueSize() {
|
|
1137
|
-
return this._queuedRequests.length;
|
|
1138
|
-
}
|
|
1139
|
-
get inProgressSize() {
|
|
1140
|
-
return this._inProgress;
|
|
1141
|
-
}
|
|
1142
|
-
};
|
|
1143
|
-
|
|
1144
|
-
// src/utils/throttle.ts
|
|
1145
|
-
function throttle(fn, frequence) {
|
|
1146
|
-
const semaphore = new RateThrottler(frequence);
|
|
1147
|
-
return (...t) => {
|
|
1148
|
-
return semaphore.execute(async () => fn(...t));
|
|
1149
|
-
};
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
|
-
// src/utils/uniqBy.ts
|
|
1153
|
-
function uniqBy(arr, getter) {
|
|
1154
|
-
return arr.reduce((dict, cur) => {
|
|
1155
|
-
const key = getter(cur);
|
|
1156
|
-
if (dict.keys.includes(key)) {
|
|
1157
|
-
return dict;
|
|
1158
|
-
} else {
|
|
1159
|
-
return {
|
|
1160
|
-
keys: [...dict.keys, key],
|
|
1161
|
-
values: [...dict.values, cur]
|
|
1162
|
-
};
|
|
1163
|
-
}
|
|
1164
|
-
}, { keys: [], values: [] }).values;
|
|
1165
|
-
}
|
|
1166
|
-
|
|
1167
|
-
// src/utils/uniq.ts
|
|
1168
|
-
function uniq(arr) {
|
|
1169
|
-
return uniqBy(arr, identity);
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
// src/utils/uniqByKey.ts
|
|
1173
|
-
function uniqByKey(arr, key) {
|
|
1174
|
-
return uniqBy(arr, (item) => item[key]);
|
|
1175
|
-
}
|
|
1176
|
-
|
|
1177
|
-
// src/utils/withTryCatch.ts
|
|
1178
|
-
function withTryCatch(fn) {
|
|
1179
|
-
try {
|
|
1180
|
-
return fn();
|
|
1181
|
-
} catch (e) {
|
|
1182
|
-
return asError(e);
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
// src/utils/withTryCatchAsync.ts
|
|
1187
|
-
async function withTryCatchAsync(fn) {
|
|
1188
|
-
return fn().catch((e) => asError(e));
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
|
-
// src/utils/wrap.ts
|
|
1192
|
-
function wrap(str, delimiter) {
|
|
1193
|
-
return delimiter + str + delimiter;
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
// src/time/TimeDuration.ts
|
|
1197
|
-
var TimeDuration = class _TimeDuration extends TimeBase {
|
|
1198
|
-
constructor(value, unit) {
|
|
1199
|
-
super(value, unit);
|
|
1200
|
-
if (value < 0)
|
|
1201
|
-
throw new Error("Duration cannot be less than 0");
|
|
1202
|
-
}
|
|
1203
|
-
create(value, unit) {
|
|
1204
|
-
return new _TimeDuration(value, unit);
|
|
1205
|
-
}
|
|
1206
|
-
addDuration(duration) {
|
|
1207
|
-
return this.addUnits(duration.ms, TimeUnit.MILLISECONDS);
|
|
1208
|
-
}
|
|
1209
|
-
removeDuration(duration) {
|
|
1210
|
-
return this.removeUnits(duration.ms, TimeUnit.MILLISECONDS);
|
|
1211
|
-
}
|
|
1212
|
-
removeUnits(n, unit) {
|
|
1213
|
-
const nn = Math.min(n, this.getUnit(unit));
|
|
1214
|
-
return super.removeUnits(nn, unit);
|
|
869
|
+
removeUnits(n, unit) {
|
|
870
|
+
const nn = Math.min(n, this.getUnit(unit));
|
|
871
|
+
return super.removeUnits(nn, unit);
|
|
1215
872
|
}
|
|
1216
873
|
/** @deprecated: Use multiplyBy instead **/
|
|
1217
874
|
multiply(times) {
|
|
@@ -1857,6 +1514,10 @@ function isTimeInstant(x) {
|
|
|
1857
1514
|
}
|
|
1858
1515
|
var TimeInstant_default = TimeInstant;
|
|
1859
1516
|
|
|
1517
|
+
// src/utils/noop.ts
|
|
1518
|
+
function noop() {
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1860
1521
|
// src/Logger.ts
|
|
1861
1522
|
var LEVELS = ["log", "debug", "info", "warn", "error"];
|
|
1862
1523
|
var timestamp = () => TimeInstant_default.now().asTimeString();
|
|
@@ -2008,6 +1669,96 @@ var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
|
|
|
2008
1669
|
}
|
|
2009
1670
|
};
|
|
2010
1671
|
|
|
1672
|
+
// src/async/RateThrottler.ts
|
|
1673
|
+
var RateThrottler = class {
|
|
1674
|
+
constructor(_frequency) {
|
|
1675
|
+
this._frequency = _frequency;
|
|
1676
|
+
const initialOffset = this.cooldown.divideBy(_frequency.times);
|
|
1677
|
+
this._availableSlots = new Array(_frequency.times).fill(0).map((_, i) => TimeInstant.now().addMs(i * initialOffset.ms));
|
|
1678
|
+
this._waitingRequests = [];
|
|
1679
|
+
}
|
|
1680
|
+
_availableSlots;
|
|
1681
|
+
_waitingRequests;
|
|
1682
|
+
get cooldown() {
|
|
1683
|
+
return this._frequency.period;
|
|
1684
|
+
}
|
|
1685
|
+
async execute(fn) {
|
|
1686
|
+
const wrappedFn = async () => {
|
|
1687
|
+
const slot = this._availableSlots.shift();
|
|
1688
|
+
try {
|
|
1689
|
+
return slot.promise().then(() => fn());
|
|
1690
|
+
} finally {
|
|
1691
|
+
this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
|
|
1692
|
+
if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
|
|
1693
|
+
}
|
|
1694
|
+
};
|
|
1695
|
+
if (this._availableSlots.length > 0) {
|
|
1696
|
+
return wrappedFn();
|
|
1697
|
+
} else {
|
|
1698
|
+
const waitingRequest = new Deferred_default();
|
|
1699
|
+
this._waitingRequests.push(waitingRequest);
|
|
1700
|
+
return waitingRequest.then(() => wrappedFn());
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
};
|
|
1704
|
+
|
|
1705
|
+
// src/async/Semaphore.ts
|
|
1706
|
+
var Semaphore = class {
|
|
1707
|
+
constructor(_availableSlots = 1) {
|
|
1708
|
+
this._availableSlots = _availableSlots;
|
|
1709
|
+
this._queuedRequests = [];
|
|
1710
|
+
}
|
|
1711
|
+
_queuedRequests;
|
|
1712
|
+
_inProgress = 0;
|
|
1713
|
+
async _awaitSlot() {
|
|
1714
|
+
if (this._availableSlots > 0) {
|
|
1715
|
+
this._availableSlots -= 1;
|
|
1716
|
+
this._inProgress += 1;
|
|
1717
|
+
return void 0;
|
|
1718
|
+
} else {
|
|
1719
|
+
const deferred = new Deferred_default();
|
|
1720
|
+
this._queuedRequests.push(deferred);
|
|
1721
|
+
return deferred.asPromise();
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
_releaseSlot() {
|
|
1725
|
+
const waitingSlotToResolve = this._queuedRequests.shift();
|
|
1726
|
+
if (waitingSlotToResolve) {
|
|
1727
|
+
waitingSlotToResolve.resolve();
|
|
1728
|
+
} else {
|
|
1729
|
+
this._availableSlots += 1;
|
|
1730
|
+
this._inProgress -= 1;
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
async execute(fn, cooldown = TimeDuration.ZERO) {
|
|
1734
|
+
return this._awaitSlot().then(() => fn()).finally(() => {
|
|
1735
|
+
void cooldown.promise().then(() => this._releaseSlot());
|
|
1736
|
+
});
|
|
1737
|
+
}
|
|
1738
|
+
get availableSlots() {
|
|
1739
|
+
return this._availableSlots;
|
|
1740
|
+
}
|
|
1741
|
+
get queueSize() {
|
|
1742
|
+
return this._queuedRequests.length;
|
|
1743
|
+
}
|
|
1744
|
+
get inProgressSize() {
|
|
1745
|
+
return this._inProgress;
|
|
1746
|
+
}
|
|
1747
|
+
};
|
|
1748
|
+
|
|
1749
|
+
// src/utils/constant.ts
|
|
1750
|
+
function constant(v) {
|
|
1751
|
+
return () => v;
|
|
1752
|
+
}
|
|
1753
|
+
function identity(t) {
|
|
1754
|
+
return t;
|
|
1755
|
+
}
|
|
1756
|
+
var constantNull = constant(null);
|
|
1757
|
+
var constantTrue = constant(true);
|
|
1758
|
+
var constantFalse = constant(false);
|
|
1759
|
+
var constantZero = constant(0);
|
|
1760
|
+
var constantOne = constant(1);
|
|
1761
|
+
|
|
2011
1762
|
// src/sorting/ComparisonChain.ts
|
|
2012
1763
|
var defaultCompareValueOptions = {
|
|
2013
1764
|
nullsFirst: false,
|
|
@@ -2456,6 +2207,258 @@ var TimeRange = class _TimeRange {
|
|
|
2456
2207
|
}
|
|
2457
2208
|
};
|
|
2458
2209
|
|
|
2210
|
+
// src/utils/bindThis.ts
|
|
2211
|
+
function bindThis(fn, _this) {
|
|
2212
|
+
return fn.bind(_this);
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
// src/utils/entries.ts
|
|
2216
|
+
function dictToEntries(obj) {
|
|
2217
|
+
return Object.entries(obj);
|
|
2218
|
+
}
|
|
2219
|
+
function entriesToDict(entries) {
|
|
2220
|
+
return Object.fromEntries(entries);
|
|
2221
|
+
}
|
|
2222
|
+
function mapEntries(dict, mapper) {
|
|
2223
|
+
return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
// src/utils/groupBy.ts
|
|
2227
|
+
function groupByString(arr, field) {
|
|
2228
|
+
return groupByStringWith(arr, (t) => t[field]);
|
|
2229
|
+
}
|
|
2230
|
+
function groupByNumber(arr, field) {
|
|
2231
|
+
return groupByNumberWith(arr, (t) => t[field]);
|
|
2232
|
+
}
|
|
2233
|
+
function groupBySymbol(arr, field) {
|
|
2234
|
+
return groupBySymbolWith(arr, (t) => t[field]);
|
|
2235
|
+
}
|
|
2236
|
+
function groupByStringWith(arr, getter) {
|
|
2237
|
+
return doGroupByWith(arr, getter);
|
|
2238
|
+
}
|
|
2239
|
+
function groupByNumberWith(arr, getter) {
|
|
2240
|
+
return doGroupByWith(arr, getter);
|
|
2241
|
+
}
|
|
2242
|
+
function groupBySymbolWith(arr, getter) {
|
|
2243
|
+
return doGroupByWith(arr, getter);
|
|
2244
|
+
}
|
|
2245
|
+
function doGroupByWith(arr, getter) {
|
|
2246
|
+
return arr.reduce((dict, cur) => {
|
|
2247
|
+
const key = getter(cur);
|
|
2248
|
+
if (key !== null && key !== void 0) {
|
|
2249
|
+
const arr2 = dict[key] ?? [];
|
|
2250
|
+
arr2.push(cur);
|
|
2251
|
+
dict[key] = arr2;
|
|
2252
|
+
}
|
|
2253
|
+
return dict;
|
|
2254
|
+
}, {});
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
// src/utils/iff.ts
|
|
2258
|
+
function iff(firstPredicate, valueIfTrue) {
|
|
2259
|
+
if (firstPredicate === true || firstPredicate instanceof Function && firstPredicate() === true) {
|
|
2260
|
+
const ret = {
|
|
2261
|
+
elseIf: () => ret,
|
|
2262
|
+
otherwise: () => valueIfTrue
|
|
2263
|
+
};
|
|
2264
|
+
return ret;
|
|
2265
|
+
} else {
|
|
2266
|
+
const ret = {
|
|
2267
|
+
elseIf: (elseIf, valueIfElseIfTrue) => iff(elseIf, valueIfElseIfTrue),
|
|
2268
|
+
otherwise: (valueIfElse) => valueIfElse
|
|
2269
|
+
};
|
|
2270
|
+
return ret;
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
|
|
2274
|
+
// src/utils/indexBy.ts
|
|
2275
|
+
function indexByString(arr, field) {
|
|
2276
|
+
return indexByStringWith(arr, (t) => t[field]);
|
|
2277
|
+
}
|
|
2278
|
+
function indexByNumber(arr, field) {
|
|
2279
|
+
return indexByNumberWith(arr, (t) => t[field]);
|
|
2280
|
+
}
|
|
2281
|
+
function indexBySymbol(arr, field) {
|
|
2282
|
+
return indexBySymbolWith(arr, (t) => t[field]);
|
|
2283
|
+
}
|
|
2284
|
+
function indexByStringWith(arr, getter) {
|
|
2285
|
+
return doIndexByWith(arr, getter);
|
|
2286
|
+
}
|
|
2287
|
+
function indexByNumberWith(arr, getter) {
|
|
2288
|
+
return doIndexByWith(arr, getter);
|
|
2289
|
+
}
|
|
2290
|
+
function indexBySymbolWith(arr, getter) {
|
|
2291
|
+
return doIndexByWith(arr, getter);
|
|
2292
|
+
}
|
|
2293
|
+
function doIndexByWith(arr, getter) {
|
|
2294
|
+
return arr.reduce((dict, cur) => {
|
|
2295
|
+
const key = getter(cur);
|
|
2296
|
+
if (key !== null && key !== void 0)
|
|
2297
|
+
dict[key] = cur;
|
|
2298
|
+
return dict;
|
|
2299
|
+
}, {});
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
// src/utils/jsonCloneDeep.ts
|
|
2303
|
+
function jsonCloneDeep(a) {
|
|
2304
|
+
if (null === a || "object" !== typeof a) return a;
|
|
2305
|
+
if (a instanceof Date) {
|
|
2306
|
+
return new Date(a.getTime());
|
|
2307
|
+
} else if (a instanceof Array) {
|
|
2308
|
+
const copy = [];
|
|
2309
|
+
for (let i = 0, len = a.length; i < len; i++) {
|
|
2310
|
+
copy[i] = jsonCloneDeep(a[i]);
|
|
2311
|
+
}
|
|
2312
|
+
return copy;
|
|
2313
|
+
} else if (a instanceof Object) {
|
|
2314
|
+
const copy = {};
|
|
2315
|
+
for (let attr in a) {
|
|
2316
|
+
if (a.hasOwnProperty(attr))
|
|
2317
|
+
copy[attr] = jsonCloneDeep(a[attr]);
|
|
2318
|
+
}
|
|
2319
|
+
return copy;
|
|
2320
|
+
}
|
|
2321
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
// src/utils/math.ts
|
|
2325
|
+
function clamp(n, min2, max2) {
|
|
2326
|
+
if (min2 === null || max2 === null || n === null || isNaN(min2) || isNaN(max2) || isNaN(n) || min2 > max2)
|
|
2327
|
+
throw new Error();
|
|
2328
|
+
if (n > max2) {
|
|
2329
|
+
return max2;
|
|
2330
|
+
} else if (n < min2) {
|
|
2331
|
+
return min2;
|
|
2332
|
+
} else {
|
|
2333
|
+
return n;
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
function average(arr) {
|
|
2337
|
+
const f = 1 / arr.length;
|
|
2338
|
+
return arr.reduce((tot, cur) => tot + cur * f, 0);
|
|
2339
|
+
}
|
|
2340
|
+
function sum(arr) {
|
|
2341
|
+
return arr.reduce((tot, cur) => tot + cur, 0);
|
|
2342
|
+
}
|
|
2343
|
+
function sumBy(arr, getter) {
|
|
2344
|
+
return sum(arr.map(getter));
|
|
2345
|
+
}
|
|
2346
|
+
function min(arr) {
|
|
2347
|
+
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
2348
|
+
return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
|
|
2349
|
+
}
|
|
2350
|
+
function minBy(arr, getter) {
|
|
2351
|
+
return min(arr.map(getter));
|
|
2352
|
+
}
|
|
2353
|
+
function max(arr) {
|
|
2354
|
+
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
2355
|
+
return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
|
|
2356
|
+
}
|
|
2357
|
+
function maxBy(arr, getter) {
|
|
2358
|
+
return max(arr.map(getter));
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
// src/utils/omit.ts
|
|
2362
|
+
function omit(o, ...keys) {
|
|
2363
|
+
return keys.reduce((obj, key) => {
|
|
2364
|
+
delete obj[key];
|
|
2365
|
+
return obj;
|
|
2366
|
+
}, { ...o });
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
// src/utils/round.ts
|
|
2370
|
+
var roundModes = {
|
|
2371
|
+
"toNearest": Math.round,
|
|
2372
|
+
"toLower": Math.floor,
|
|
2373
|
+
"toUpper": Math.ceil,
|
|
2374
|
+
"towardsZero": (n) => n > 0 ? Math.floor(n) : Math.ceil(n),
|
|
2375
|
+
"awayFromZero": (n) => n > 0 ? Math.ceil(n) : Math.floor(n)
|
|
2376
|
+
};
|
|
2377
|
+
function round(n, precision = 0, mode = "toNearest") {
|
|
2378
|
+
const base = 10;
|
|
2379
|
+
const power = Math.pow(base, precision);
|
|
2380
|
+
return roundModes[mode](n * power) / power;
|
|
2381
|
+
}
|
|
2382
|
+
var roundToNearest = (n, precision = 0) => round(n, precision, "toNearest");
|
|
2383
|
+
var roundToLower = (n, precision = 0) => round(n, precision, "toLower");
|
|
2384
|
+
var roundToUpper = (n, precision = 0) => round(n, precision, "toUpper");
|
|
2385
|
+
var roundTowardsZero = (n, precision = 0) => round(n, precision, "towardsZero");
|
|
2386
|
+
var roundAwayFromZero = (n, precision = 0) => round(n, precision, "awayFromZero");
|
|
2387
|
+
|
|
2388
|
+
// src/utils/sortBy.ts
|
|
2389
|
+
function sortBy(keyOrGetter, direction = "ASC", nulls = "LAST") {
|
|
2390
|
+
const directionNum = direction === "ASC" ? -1 : 1;
|
|
2391
|
+
const nullsNum = nulls === "LAST" ? -1 : 1;
|
|
2392
|
+
const sortByNum = (a, b) => {
|
|
2393
|
+
if (a === b)
|
|
2394
|
+
return -1;
|
|
2395
|
+
const aDefined = !(a === null || a === void 0 || isNaN(a));
|
|
2396
|
+
const bDefined = !(b === null || b === void 0 || isNaN(b));
|
|
2397
|
+
if (!aDefined && !bDefined)
|
|
2398
|
+
return -1;
|
|
2399
|
+
if (aDefined !== bDefined)
|
|
2400
|
+
return nullsNum * (aDefined ? 1 : -1);
|
|
2401
|
+
return (a > b ? -1 : 1) * directionNum;
|
|
2402
|
+
};
|
|
2403
|
+
if (typeof keyOrGetter === "function") {
|
|
2404
|
+
return (a, b) => sortByNum(keyOrGetter(a), keyOrGetter(b));
|
|
2405
|
+
} else {
|
|
2406
|
+
return (a, b) => sortByNum(a[keyOrGetter], b[keyOrGetter]);
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
// src/utils/throttle.ts
|
|
2411
|
+
function throttle(fn, frequence) {
|
|
2412
|
+
const semaphore = new RateThrottler(frequence);
|
|
2413
|
+
return (...t) => {
|
|
2414
|
+
return semaphore.execute(async () => fn(...t));
|
|
2415
|
+
};
|
|
2416
|
+
}
|
|
2417
|
+
|
|
2418
|
+
// src/utils/uniqBy.ts
|
|
2419
|
+
function uniqBy(arr, getter) {
|
|
2420
|
+
return arr.reduce((dict, cur) => {
|
|
2421
|
+
const key = getter(cur);
|
|
2422
|
+
if (dict.keys.includes(key)) {
|
|
2423
|
+
return dict;
|
|
2424
|
+
} else {
|
|
2425
|
+
return {
|
|
2426
|
+
keys: [...dict.keys, key],
|
|
2427
|
+
values: [...dict.values, cur]
|
|
2428
|
+
};
|
|
2429
|
+
}
|
|
2430
|
+
}, { keys: [], values: [] }).values;
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2433
|
+
// src/utils/uniq.ts
|
|
2434
|
+
function uniq(arr) {
|
|
2435
|
+
return uniqBy(arr, identity);
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2438
|
+
// src/utils/uniqByKey.ts
|
|
2439
|
+
function uniqByKey(arr, key) {
|
|
2440
|
+
return uniqBy(arr, (item) => item[key]);
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
// src/utils/withTryCatch.ts
|
|
2444
|
+
function withTryCatch(fn) {
|
|
2445
|
+
try {
|
|
2446
|
+
return fn();
|
|
2447
|
+
} catch (e) {
|
|
2448
|
+
return asError(e);
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
// src/utils/withTryCatchAsync.ts
|
|
2453
|
+
async function withTryCatchAsync(fn) {
|
|
2454
|
+
return fn().catch((e) => asError(e));
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
// src/utils/wrap.ts
|
|
2458
|
+
function wrap(str, delimiter) {
|
|
2459
|
+
return delimiter + str + delimiter;
|
|
2460
|
+
}
|
|
2461
|
+
|
|
2459
2462
|
// src/upgrade/errors.ts
|
|
2460
2463
|
var UnavailableUpgradeError = class extends Error {
|
|
2461
2464
|
constructor(data, from, to) {
|
|
@@ -2625,6 +2628,7 @@ export {
|
|
|
2625
2628
|
dictToEntries,
|
|
2626
2629
|
dictToList,
|
|
2627
2630
|
divideBy,
|
|
2631
|
+
ellipsis,
|
|
2628
2632
|
ensureArray,
|
|
2629
2633
|
ensureDefined,
|
|
2630
2634
|
ensureNegativeNumber,
|
|
@@ -2636,6 +2640,7 @@ export {
|
|
|
2636
2640
|
extendArray,
|
|
2637
2641
|
extendArrayWith,
|
|
2638
2642
|
fill,
|
|
2643
|
+
flatMapTruthys,
|
|
2639
2644
|
groupByNumber,
|
|
2640
2645
|
groupByNumberWith,
|
|
2641
2646
|
groupByString,
|
|
@@ -2675,6 +2680,7 @@ export {
|
|
|
2675
2680
|
last,
|
|
2676
2681
|
mapDefined,
|
|
2677
2682
|
mapEntries,
|
|
2683
|
+
mapTruthys,
|
|
2678
2684
|
max,
|
|
2679
2685
|
maxBy,
|
|
2680
2686
|
min,
|
|
@@ -2683,8 +2689,9 @@ export {
|
|
|
2683
2689
|
noop,
|
|
2684
2690
|
omit,
|
|
2685
2691
|
pad,
|
|
2692
|
+
padLeft,
|
|
2693
|
+
padRight,
|
|
2686
2694
|
pick,
|
|
2687
|
-
pluralize,
|
|
2688
2695
|
promiseSequence,
|
|
2689
2696
|
randomInterval,
|
|
2690
2697
|
randomPercentage,
|