diff options
Diffstat (limited to 'node_modules/es5-ext/string')
-rw-r--r-- | node_modules/es5-ext/string/#/repeat/shim.js | 18 | ||||
-rw-r--r-- | node_modules/es5-ext/string/index.js | 1 | ||||
-rw-r--r-- | node_modules/es5-ext/string/random-uniq.js | 10 | ||||
-rw-r--r-- | node_modules/es5-ext/string/random.js | 42 |
4 files changed, 58 insertions, 13 deletions
diff --git a/node_modules/es5-ext/string/#/repeat/shim.js b/node_modules/es5-ext/string/#/repeat/shim.js index 7b8d8fbed..ac259a83b 100644 --- a/node_modules/es5-ext/string/#/repeat/shim.js +++ b/node_modules/es5-ext/string/#/repeat/shim.js @@ -1,6 +1,6 @@ -/* eslint no-bitwise: "off" */ - -// Thanks: http://www.2ality.com/2014/01/efficient-string-repeat.html +// Thanks +// @rauchma http://www.2ality.com/2014/01/efficient-string-repeat.html +// @mathiasbynens https://github.com/mathiasbynens/String.prototype.repeat/blob/4a4b567def/repeat.js "use strict"; @@ -12,11 +12,13 @@ module.exports = function (count) { count = toInteger(count); if (count < 0) throw new RangeError("Count must be >= 0"); if (!isFinite(count)) throw new RangeError("Count must be < ∞"); - if (!count) return ""; - if (count === 1) return str; result = ""; - if (count & 1) result += str; - while ((count >>>= 1)) str += str; - return result + str; + while (count) { + if (count % 2) result += str; + if (count > 1) str += str; + // eslint-disable-next-line no-bitwise + count >>= 1; + } + return result; }; diff --git a/node_modules/es5-ext/string/index.js b/node_modules/es5-ext/string/index.js index b2e7e6229..4393588f1 100644 --- a/node_modules/es5-ext/string/index.js +++ b/node_modules/es5-ext/string/index.js @@ -5,6 +5,7 @@ module.exports = { "formatMethod": require("./format-method"), "fromCodePoint": require("./from-code-point"), "isString": require("./is-string"), + "random": require("./random"), "randomUniq": require("./random-uniq"), "raw": require("./raw") }; diff --git a/node_modules/es5-ext/string/random-uniq.js b/node_modules/es5-ext/string/random-uniq.js index ef549d3f6..ea10a8167 100644 --- a/node_modules/es5-ext/string/random-uniq.js +++ b/node_modules/es5-ext/string/random-uniq.js @@ -1,13 +1,13 @@ "use strict"; -var generated = Object.create(null) - - , random = Math.random; +var generated = Object.create(null), random = Math.random; module.exports = function () { var str; do { - str = random().toString(36).slice(2); -} while (generated[str]); + str = random() + .toString(36) + .slice(2); + } while (generated[str]); return str; }; diff --git a/node_modules/es5-ext/string/random.js b/node_modules/es5-ext/string/random.js new file mode 100644 index 000000000..4ecec7fcd --- /dev/null +++ b/node_modules/es5-ext/string/random.js @@ -0,0 +1,42 @@ +"use strict"; + +var isValue = require("../object/is-value") + , toNaturalNumber = require("../number/to-pos-integer"); + +var generated = Object.create(null), random = Math.random, uniqTryLimit = 100; + +var getChunk = function () { + return random() + .toString(36) + .slice(2); +}; + +var getString = function (/* length */) { + var str = getChunk(), length = arguments[0]; + if (!isValue(length)) return str; + while (str.length < length) str += getChunk(); + return str.slice(0, length); +}; + +module.exports = function (/* options */) { + var options = Object(arguments[0]), length = options.length, isUnique = options.isUnique; + + if (isValue(length)) length = toNaturalNumber(length); + + var str = getString(length); + if (isUnique) { + var count = 0; + while (generated[str]) { + if (++count === uniqTryLimit) { + throw new Error( + "Cannot generate random string.\n" + + "String.random is not designed to effectively generate many short and " + + "unique random strings" + ); + } + str = getString(length); + } + generated[str] = true; + } + return str; +}; |