@onesy/utils
Version:
20 lines (15 loc) • 552 B
JavaScript
import is from './is';
import clamp from './clamp';
import random from './random';
const pick = function (value) {
let min = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
let max = arguments.length > 2 ? arguments[2] : undefined;
if (is('string', value)) {
let result = '';
let toPick = clamp(min, 1);
if (max > 0 && max > min) toPick = random(min, max);
for (let i = 0; i < toPick; i++) result += value.charAt(Math.floor(Math.random() * value.length));
return result;
}
};
export default pick;