randomword
Version:
Random pronounceable Englishy word of desired length.
40 lines (29 loc) • 1.05 kB
JavaScript
;
const Crypto = require( 'crypto' );
( function () {
'use strong';
const MIN = 1;
const ceil = Math.ceil;
const max = Math.max;
const random = Math.random;
const round = Math.round;
const salt = Math.abs( ( Math.hypot( ...Crypto.randomBytes( 7 ) ) % 1 ) * Number.MAX_SAFE_INTEGER );
const vowels = [ 'a', 'e', 'i', 'o', 'u' ];
const cons = [ 'b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'tr', 'cr', 'br', 'fr', 'th', 'dr', 'ch', 'ph', 'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl' ];
const num_vowels = vowels.length;
const num_cons = cons.length;
function randInt() {
return round( random() * salt );
}
function randomword( _length ) {
let length = max( parseInt( _length ) || MIN, MIN );
let units = ceil( length / 2 );
let word = [];
for ( let i = 0; i < units; i++ ) {
word.push( cons[ randInt() % num_cons ] );
word.push( vowels[ randInt() % num_vowels ] );
}
return word.join( '' ).substr( 0, length );
}
module.exports = randomword;
} ).call( this );