cerceis-lib
Version:
Contains list of quality of life functions that is written in TypeScript and es6
173 lines (172 loc) • 5.79 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/array/index.ts
var array_exports = {};
__export(array_exports, {
FromArray: () => FromArray
});
module.exports = __toCommonJS(array_exports);
var FromArray = {
/**
* Returns one or more random elements from an array.
* @param arr Input array.
* @param noOfResult Number of results to return (default 1).
* @param returnIndex Return index positions instead of values.
*/
getRandom(arr, noOfResult = 1, returnIndex = false) {
if (!Array.isArray(arr)) throw new Error("Input must be an array");
if (returnIndex) {
const result2 = [];
for (let i = 0; i < noOfResult; i++)
result2.push(Math.floor(Math.random() * arr.length));
return result2;
}
const result = [];
for (let i = 0; i < noOfResult; i++)
result.push(arr[Math.floor(Math.random() * arr.length)]);
return result;
},
/**
* Find and return the largest N numbers.
* @param numbers Array of numbers.
* @param n Number of results to return (default 1).
* @param returnIndex Return indices instead of values.
*/
getLargest(numbers, n = 1, returnIndex = false) {
const results = [];
if (returnIndex) {
const indexed = numbers.map((v, i) => ({ v, i })).sort((a, b) => b.v - a.v);
for (let i = 0; i < n && i < indexed.length; i++) results.push(indexed[i].i);
} else {
const sorted = [...numbers].sort((a, b) => b - a);
for (let i = 0; i < n && i < sorted.length; i++) results.push(sorted[i]);
}
return results;
},
/**
* Find and return the smallest N numbers.
* @param numbers Array of numbers.
* @param n Number of results to return (default 1).
* @param returnIndex Return indices instead of values.
*/
getSmallest(numbers, n = 1, returnIndex = false) {
const results = [];
if (returnIndex) {
const indexed = numbers.map((v, i) => ({ v, i })).sort((a, b) => a.v - b.v);
for (let i = 0; i < n && i < indexed.length; i++) results.push(indexed[i].i);
} else {
const sorted = [...numbers].sort((a, b) => a - b);
for (let i = 0; i < n && i < sorted.length; i++) results.push(sorted[i]);
}
return results;
},
/**
* Return the intersection of two arrays.
* @param arrA Array A.
* @param arrB Array B.
* @param duplicated Include duplicate matches.
*/
getIntersect(arrA, arrB, duplicated = false) {
const seen = /* @__PURE__ */ new Map();
const result = [];
for (const item of arrA) seen.set(String(item), 1);
for (const item of arrB) {
const key = String(item);
if (seen.has(key)) {
if (!duplicated && seen.get(key) === 1) {
result.push(item);
seen.set(key, 2);
} else if (duplicated) {
result.push(item);
}
}
}
return result;
},
/**
* Randomly shuffle an array in-place (Fisher-Yates).
* @param arr Array to shuffle.
*/
shuffle(arr) {
let currentIndex = arr.length;
while (currentIndex !== 0) {
const randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[arr[currentIndex], arr[randomIndex]] = [arr[randomIndex], arr[currentIndex]];
}
return arr;
},
/**
* The Thanos snap — removes roughly half the elements randomly.
* Mutates the original array.
* @param arr Input array.
*/
thanosSnap(arr) {
const targetLen = arr.length % 2 === 0 ? arr.length / 2 : (arr.length - 1) / 2;
while (arr.length !== targetLen)
arr.splice(Math.floor(Math.random() * arr.length), 1);
return arr;
},
/**
* Convert a 2D array ([[key, value], ...]) into an object.
* @param arr Input 2D array.
*/
toObject(arr) {
const obj = {};
for (const [k, v] of arr) obj[k] = v;
return obj;
},
/**
* Split an array into groups by ratio.
* ex) [1,2,3,4] split [1,3] → {1:[1], 2:[2,3,4]}
* Remainder goes into "extra" if it doesn't fit.
* @param arr Array to split.
* @param ratio Ratio to split into.
*/
splitInto(arr, ratio) {
if (ratio.length === 0) return { 1: arr };
const filtered = ratio.filter((r) => r !== 0 && !isNaN(r));
const unitLen = Math.floor(1 / filtered.reduce((a, b) => a + b) * arr.length);
const rs = {};
let group = 1;
let offset = 0;
for (const r of ratio) {
const len = r * unitLen;
rs[group] = arr.slice(offset, offset + len);
offset += rs[group].length;
group++;
}
if (offset < arr.length) rs["extra"] = arr.slice(offset);
return rs;
},
/**
* Log array elements to console, optionally limited to a range.
* @param arr Array to log.
* @param from Starting index (inclusive), default 0.
* @param to Ending index (exclusive), default arr.length.
*/
log(arr, from = 0, to = arr.length) {
for (let i = from; i < to; i++) console.log(arr[i]);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FromArray
});
//# sourceMappingURL=index.js.map