UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

29 lines (28 loc) 879 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.str_shuffle = str_shuffle; function str_shuffle(...providedArgs) { // discuss at: https://locutus.io/php/str_shuffle/ // original by: Brett Zamir (https://brett-zamir.me) // example 1: var $shuffled = str_shuffle("abcdef") // example 1: var $result = $shuffled.length // returns 1: 6 if (providedArgs.length === 0) { throw new Error('Wrong parameter count for str_shuffle()'); } const [input] = providedArgs; if (input === null) { return ''; } let str = String(input); let newStr = ''; let rand; let i = str.length; while (i) { rand = Math.floor(Math.random() * i); newStr += str.charAt(rand); str = str.substring(0, rand) + str.substr(rand + 1); i--; } return newStr; }