UNPKG

qms-js

Version:

A common library that can be used for fe and be

46 lines 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var helper_1 = require("../common/helper"); Array.prototype.groupBy = function (groupFunc) { var map = new Map(); this.forEach(function (item) { var key = groupFunc(item); var collection = map.get(key); if (!collection) { map.set(key, [item]); } else { collection.push(item); } }); return map; }; Array.prototype.merge = function (keyFunc, newItems, mergeFunc) { var _this = this; newItems.forEach(function (item) { var key = keyFunc(item); var cur = _this.find(function (i) { return keyFunc(i) == key; }); if (!cur) _this.push(item); if (key && mergeFunc && cur) { mergeFunc(cur, item); } }); }; Array.prototype.shuffle = function () { if (this.length < 2) return this; for (var i = this.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = this[i]; this[i] = this[j]; this[j] = temp; } return this; }; Array.prototype.random = function () { var i = helper_1.default.randomNumber(0, this.length - 1); var item = this[i]; return item; }; //# sourceMappingURL=array.js.map