瀏覽代碼

fix: small fixes

Sv443 2 月之前
父節點
當前提交
1e74458d00
共有 2 個文件被更改,包括 5 次插入3 次删除
  1. 2 3
      lib/array.ts
  2. 3 0
      lib/crypto.ts

+ 2 - 3
lib/array.ts

@@ -46,9 +46,8 @@ export function randomizeArray<TItem = unknown>(array: TItem[]): TItem[] {
 
   // shamelessly stolen from https://javascript.info/task/shuffle
   for(let i = retArray.length - 1; i > 0; i--) {
-    const j = Math.floor((randRange(0, 10000) / 10000) * (i + 1));
-    // @ts-ignore
-    [retArray[i], retArray[j]] = [retArray[j], retArray[i]];
+    const j = Math.floor((Math.random() * (i + 1)));
+    [retArray[i], retArray[j]] = [retArray[j], retArray[i]] as [TItem, TItem];
   }
 
   return retArray;

+ 3 - 0
lib/crypto.ts

@@ -81,6 +81,9 @@ export async function computeHash(input: string | ArrayBuffer, algorithm = "SHA-
  * @param randomCase If set to false, the generated ID will be lowercase only - also makes use of the `enhancedEntropy` parameter unless the output doesn't contain letters
  */
 export function randomId(length = 16, radix = 16, enhancedEntropy = false, randomCase = true): string {
+  if(radix < 2 || radix > 36)
+    throw new RangeError("The radix argument must be between 2 and 36");
+
   let arr: string[] = [];
   const caseArr = randomCase ? [0, 1] : [0];