@onesy/utils
Version:
21 lines (19 loc) • 768 B
JavaScript
import pick from './pick';
import clamp from './clamp';
import shuffle from './shuffle';
const randomString = function () {
let length_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1e1;
const length = clamp(length_, 4);
let result = '';
const lowercase = 'abcdefghijklmnopqrstuvwxyz';
const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const specials = '!@#$%^&*()_+{}:"<>?\|[];\',./`~';
const numbers = '0123456789';
const lowPriorityLength = Math.ceil(length * 0.1);
result += pick(lowercase, length - lowPriorityLength * 3);
result += pick(uppercase, lowPriorityLength);
result += pick(specials, lowPriorityLength);
result += pick(numbers, lowPriorityLength);
return shuffle(result);
};
export default randomString;