]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
fix: work over GetYoDigits to support length > 10
authorSassNinja <kai.falkowski@gmail.com>
Thu, 24 Oct 2019 15:49:14 +0000 (17:49 +0200)
committerSassNinja <kai.falkowski@gmail.com>
Thu, 24 Oct 2019 15:49:14 +0000 (17:49 +0200)
Prior to this there was unnecessary complicated math used and it didn't work if length > 10

js/foundation.core.utils.js

index b6778b72b3c4123e479e610f925a1d9692dd5f0b..4c6893eb2f360aeb8e6ed663afad81ec18c6c6e1 100644 (file)
@@ -19,9 +19,13 @@ function rtl() {
  * @default {String} '' - if no plugin name is provided, nothing is appended to the uid.
  * @returns {String} - unique id
  */
-function GetYoDigits(length, namespace){
-  length = length || 6;
-  return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1) + (namespace ? `-${namespace}` : '');
+function GetYoDigits(length = 6, namespace){
+  let str = '';
+  const chars = '0123456789abcdefghijklmnopqrstuvwxyz';
+  for (let i = 0; i < length; i++) {
+    str += chars[Math.floor(Math.random() * chars.length)];
+  }
+  return namespace ? `${str}-${namespace}` : str;
 }
 
 /**