@stnekroman/tstools
Version:
Set of handy tools for TypeScript development
75 lines (74 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Arrays = void 0;
var Arrays;
(function (Arrays) {
function deleteItem(arr, item) {
const index = arr.indexOf(item);
if (index !== -1) {
arr.splice(index, 1);
return true;
}
else {
return false;
}
}
Arrays.deleteItem = deleteItem;
function pushAll(dst, src) {
for (const item of src) {
dst.push(item);
}
return dst;
}
Arrays.pushAll = pushAll;
function shuffle(arr) {
for (let index = arr.length - 1; index > 0; index--) {
const randomIndex = Math.floor(Math.random() * (index - 1));
const temp = arr[index];
arr[index] = arr[randomIndex];
arr[randomIndex] = temp;
}
return arr;
}
Arrays.shuffle = shuffle;
function haveIntersection(array1, array2) {
if (array1.length > 10 || array2.length > 10) {
if (array1.length > array2.length)
[array1, array2] = [array2, array1];
const set = new Set(array1);
for (const item of array2) {
if (set.has(item)) {
return true;
}
}
return false;
}
else {
for (const item1 of array1) {
for (const item2 of array2) {
if (item1 === item2) {
return true;
}
}
}
}
return false;
}
Arrays.haveIntersection = haveIntersection;
function filterUntil(arr, filter, includeLast = false) {
const result = [];
for (const item of arr) {
if (filter(item)) {
result.push(item);
}
else {
if (includeLast) {
result.push(item);
}
break;
}
}
return result;
}
Arrays.filterUntil = filterUntil;
})(Arrays || (exports.Arrays = Arrays = {}));