summaryrefslogtreecommitdiff
path: root/node_modules/locutus/php/strings/str_shuffle.js
blob: ce204ba9b9c74a7c105e5213cdfdf287f098711b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';

module.exports = function str_shuffle(str) {
  // eslint-disable-line camelcase
  //  discuss at: http://locutus.io/php/str_shuffle/
  // original by: Brett Zamir (http://brett-zamir.me)
  //   example 1: var $shuffled = str_shuffle("abcdef")
  //   example 1: var $result = $shuffled.length
  //   returns 1: 6

  if (arguments.length === 0) {
    throw new Error('Wrong parameter count for str_shuffle()');
  }

  if (str === null) {
    return '';
  }

  str += '';

  var newStr = '';
  var rand;
  var 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;
};
//# sourceMappingURL=str_shuffle.js.map