UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

20 lines (19 loc) 717 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var range_1 = require("./range"); /** * Random a non-cryptographic random string from characters a-zA-Z0-9. * @param length the length of the random string * @signature randomString(length) * @example * randomString(5) // => aB92J * @category String */ function randomString(length) { var characterSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var randomChar = function () { return characterSet[Math.floor(Math.random() * characterSet.length)]; }; return range_1.range(0, length).reduce(function (text) { return text + randomChar(); }, ''); } exports.randomString = randomString;