UNPKG

codess

Version:

vscode代码片段管理器,通过 本地包(本地某个文件夹) 或官网的 远程包(代码片段集合) 生成本地项目的 vscode 代码片段配置。

131 lines (130 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayUtils = void 0; const MathUtils_1 = require("./MathUtils"); const ObjectUtils_1 = require("./obj/ObjectUtils"); class ArrayUtils { static removeAt(arr, index) { if (index < 0) { return false; } if (arr.length <= index) return false; arr.splice(index, 1); return true; } static fill(d, length = 0) { return Array.from({ length, }).map(() => { return ObjectUtils_1.ObjectUtils.clone2(d); }); } static at(array, _n) { if (array.at) { return array.at(_n); } if (_n >= 0) { return array[_n]; } else { return array[array.length + _n]; } } static same(x, y) { if (!x || !y) return false; if (x.length != y.length) return false; let m = new Map(); x.forEach((item) => { m.set(item, (m.has(item) ? m.get(item) : 0) + 1); }); y.forEach((item) => { m.set(item, (m.has(item) ? m.get(item) : 0) - 1); }); let b = true; for (let [_, _number] of m) { if (_number != 0) { b = false; break; } } return b; } static has(arr, op) { let index = -1; if (typeof op == 'function') { index = arr.findIndex(op); } else { index = arr.indexOf(op); } return index >= 0; } static upset(_array) { return _array.sort(() => { return Math.random() - 0.5; }); } static random(_array, _n = 1, _weight = {}) { if (!_array || _array.length <= 0) { return; } let _indexArray = _array .map((_, index) => { var _a; return ArrayUtils.fill(index, (_a = _weight[index]) !== null && _a !== void 0 ? _a : 1); }) .reduce((a, b) => { a.push(...b); return a; }, []); let _newArray = []; for (let _i = 0; _i < _n; _i++) { if (_indexArray.length <= 0) { break; } let _index = MathUtils_1.MathUtils.RandomInt(0, _indexArray.length - 1); _newArray.push(_array[_indexArray[_index]]); ArrayUtils.eliminate(_indexArray, _indexArray[_index]); } return _newArray; } static eliminate(array, v) { if (!Array.isArray(v)) { v = [v]; } v.forEach((v) => { let _if = true; while (_if) { let index; switch (true) { case typeof v == 'function': index = array.findIndex(v); break; case v instanceof RegExp: index = array.findIndex((_) => v.test(_)); break; default: index = array.findIndex((_) => _ == v); break; } if (index == -1) { _if = false; } else { array.splice(index, 1); } } }); return array; } static arraify(target) { return Array.isArray(target) ? target : [target]; } static isRepeat(arr) { return arr.length != [...new Set(arr)].length; } } exports.ArrayUtils = ArrayUtils;