@yantrix/utils
Version:
Shared code for Yantrix framework
121 lines (117 loc) • 3.97 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/casts.ts
function unifyObjectKey(key) {
return Number.parseInt(String(key));
}
__name(unifyObjectKey, "unifyObjectKey");
// src/isomorphic.ts
function microtime() {
let mt = 0;
if (typeof process !== "undefined" && process?.hrtime)
mt = ((/* @__PURE__ */ new Date()).getTime() * 1e9 + process.hrtime()[1]) / 864e11;
else if (performance)
mt = performance.now();
else mt = ((/* @__PURE__ */ new Date()).getTime() + Math.random()) / 86400;
return Number.parseInt(Math.floor(mt * 1e5).toString(10));
}
__name(microtime, "microtime");
// src/fixtures.ts
function randomString(length = 10) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
__name(randomString, "randomString");
function randomInteger(min = 1, max = 100) {
return Math.floor(Math.random() * (max - min)) + min;
}
__name(randomInteger, "randomInteger");
var randomDecimal = /* @__PURE__ */ __name((min = -1e4, max = 1e4) => Math.random() * (max - min) + min, "randomDecimal");
function randomValueFunction() {
const randomValueFunctions = [randomString, randomInteger, randomDecimal];
const randomIndex = Math.floor(Math.random() * randomValueFunctions.length);
return randomValueFunctions[randomIndex];
}
__name(randomValueFunction, "randomValueFunction");
function randomValue() {
return randomValueFunction()();
}
__name(randomValue, "randomValue");
function randomArray(valueType, amount = randomInteger(1, 20)) {
return Array.from({ length: amount }, valueType);
}
__name(randomArray, "randomArray");
function sampleRange(min = 1, max = 100) {
return min + Math.floor(Math.random() * (max - min + 1));
}
__name(sampleRange, "sampleRange");
function pickFromArray(arr, n = 1) {
const acc = [];
if (!arr?.length || n <= 0)
return acc;
const a = JSON.parse(JSON.stringify(arr));
while (acc.length < n) acc.push(...a.splice(Math.floor(Math.random() * a.length), 1));
return acc;
}
__name(pickFromArray, "pickFromArray");
function popFromArray(arr, n = 1) {
const acc = [];
if (!arr?.length || n <= 0)
return acc;
let i = 0;
while (i++ < n) {
const v = arr.pop();
if (v == null)
break;
acc.push(v);
}
return acc;
}
__name(popFromArray, "popFromArray");
function sampleArray(item, n) {
if (item === null) {
return Array.from({ length: n }).fill(null).map((_, ix) => ix + 1);
}
if (item instanceof Function) {
return Array.from({ length: n }).fill(null).map((_, ix) => item(ix));
}
return Array.from({ length: n }).fill(item);
}
__name(sampleArray, "sampleArray");
function uniqId(length = 10) {
const keys = [...microtime().toString(36)];
while (keys.length < length) keys.push(...sampleRange(0, 35).toString(36));
return keys.slice(0, length).join("").toUpperCase();
}
__name(uniqId, "uniqId");
// src/predicates.ts
var isNumber = /* @__PURE__ */ __name((t) => Number.isFinite(t), "isNumber");
var isPositiveNumber = /* @__PURE__ */ __name((t) => isNumber(t) && t >= 0, "isPositiveNumber");
var isPositiveInteger = /* @__PURE__ */ __name((t) => isPositiveNumber(t) && Number.isSafeInteger(t), "isPositiveInteger");
var isInteger = /* @__PURE__ */ __name((t) => Number.isSafeInteger(t), "isInteger");
var isStaticMethodsAutomata = /* @__PURE__ */ __name((Automata) => Object.prototype.hasOwnProperty.call(Automata, "id"), "isStaticMethodsAutomata");
export {
isInteger,
isNumber,
isPositiveInteger,
isPositiveNumber,
isStaticMethodsAutomata,
microtime,
pickFromArray,
popFromArray,
randomArray,
randomDecimal,
randomInteger,
randomString,
randomValue,
randomValueFunction,
sampleArray,
sampleRange,
unifyObjectKey,
uniqId
};
//# sourceMappingURL=index.js.map