random-splice
Version:
Get random element from array when looping
27 lines • 987 B
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomSplice = void 0;
var randomSplice = function (input) {
if (!Array.isArray(input)) {
throw new TypeError('Parameter 1 should be of type Array.');
}
var length = input.length;
if (!length) {
throw new Error('No items in the array.');
}
var randomIndex = Math.floor(Math.random() * length);
return input.splice(randomIndex, 1)[0];
};
exports.randomSplice = randomSplice;
exports.default = exports.randomSplice;
});
//# sourceMappingURL=index.js.map