@xtia/alea-rc 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/entry/browser.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import { createAleaFromByteSource } from "../internal/factories.js";
2
2
  export * from "./common.js";
3
- export const cryptoAlea = createAleaFromByteSource(arr => globalThis.crypto.getRandomValues(arr));
3
+ export const cryptoAlea = createAleaFromByteSource((arr) => globalThis.crypto.getRandomValues(arr));
@@ -55,7 +55,7 @@ export declare class Alea {
55
55
  */
56
56
  string(length: number, charset: string): string;
57
57
  /**
58
- * Generates a phrase from a table and a root string
58
+ * Generate a phrase from a table and a root string
59
59
  * @example
60
60
  * ```ts
61
61
  * const message = alea.phrase({
package/internal/alea.js CHANGED
@@ -83,7 +83,7 @@ export class Alea {
83
83
  return chars.join("");
84
84
  }
85
85
  /**
86
- * Generates a phrase from a table and a root string
86
+ * Generate a phrase from a table and a root string
87
87
  * @example
88
88
  * ```ts
89
89
  * const message = alea.phrase({
@@ -1 +1 @@
1
- export declare function murmur3_32(key: string, seed?: number): number;
1
+ export declare function hashSeed(seed: string): number;
package/internal/util.js CHANGED
@@ -1,41 +1,15 @@
1
- export function murmur3_32(key, seed = 0) {
2
- let remainder = key.length & 3;
3
- let bytes = key.length - remainder;
4
- let h1 = seed >>> 0;
5
- const c1 = 0xcc9e2d51;
6
- const c2 = 0x1b873593;
7
- let i = 0;
8
- while (i < bytes) {
9
- let k1 = (key.charCodeAt(i) & 0xff)
10
- | ((key.charCodeAt(i + 1) & 0xff) << 8)
11
- | ((key.charCodeAt(i + 2) & 0xff) << 16)
12
- | ((key.charCodeAt(i + 3) & 0xff) << 24);
13
- i += 4;
14
- k1 = Math.imul(k1, c1);
15
- k1 = (k1 << 15) | (k1 >>> 17);
16
- k1 = Math.imul(k1, c2);
17
- h1 ^= k1;
18
- h1 = (h1 << 13) | (h1 >>> 19);
19
- h1 = (Math.imul(h1, 5) + 0xe6546b64) | 0;
1
+ const encoder = new TextEncoder();
2
+ export function hashSeed(seed) {
3
+ let hash = 0;
4
+ for (let i = 0; i < seed.length; i++) {
5
+ const char = seed.charCodeAt(i);
6
+ hash = Math.imul(hash, 33) ^ char;
7
+ hash = hash | 0;
20
8
  }
21
- let k1 = 0;
22
- switch (remainder) {
23
- case 3:
24
- k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
25
- case 2:
26
- k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
27
- case 1:
28
- k1 ^= (key.charCodeAt(i) & 0xff);
29
- k1 = Math.imul(k1, c1);
30
- k1 = (k1 << 15) | (k1 >>> 17);
31
- k1 = Math.imul(k1, c2);
32
- h1 ^= k1;
33
- }
34
- h1 ^= key.length;
35
- h1 ^= h1 >>> 16;
36
- h1 = Math.imul(h1, 0x85ebca6b);
37
- h1 ^= h1 >>> 13;
38
- h1 = Math.imul(h1, 0xc2b2ae35);
39
- h1 ^= h1 >>> 16;
40
- return h1 >>> 0;
9
+ hash ^= hash >>> 16;
10
+ hash = Math.imul(hash, 0x85ebca6b);
11
+ hash ^= hash >>> 13;
12
+ hash = Math.imul(hash, 0xc2b2ae35);
13
+ hash ^= hash >>> 16;
14
+ return hash >>> 0;
41
15
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtia/alea-rc",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "RNG utilities",
5
5
  "repository": {
6
6
  "url": "https://github.com/tiadrop/alea",
@@ -1,5 +1,5 @@
1
1
  import { Alea } from "../internal/alea.js";
2
- import { murmur3_32 } from "../internal/util.js";
2
+ import { hashSeed } from "../internal/util.js";
3
3
  /**
4
4
  * Create an Alea instance using a Mulberry32 source
5
5
  *
@@ -8,7 +8,7 @@ import { murmur3_32 } from "../internal/util.js";
8
8
  * @returns Alea instance using Mulberry32
9
9
  */
10
10
  export const mulberry32 = (seed) => {
11
- let nseed = typeof seed == "string" ? murmur3_32(seed) : (seed >>> 0);
11
+ let nseed = typeof seed == "string" ? hashSeed(seed) : (seed >>> 0);
12
12
  return new Alea(() => {
13
13
  nseed |= 0;
14
14
  nseed = nseed + 0x6D2B79F5 | 0;
package/prng/sfc32.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Alea } from "../internal/alea.js";
2
- import { murmur3_32 } from "../internal/util.js";
2
+ import { hashSeed } from "../internal/util.js";
3
3
  /**
4
4
  * Create an Alea instance using a Small Fast Counter (SFC32) source
5
5
  *
@@ -11,7 +11,7 @@ import { murmur3_32 } from "../internal/util.js";
11
11
  * @returns Alea isntance using SFC32
12
12
  */
13
13
  export function sfc32(a, b, c, d) {
14
- const toWord = (v) => typeof v === "number" ? (v >>> 0) : murmur3_32(String(v));
14
+ const toWord = (v) => typeof v === "number" ? (v >>> 0) : hashSeed(String(v));
15
15
  let s0 = toWord(a) | 0;
16
16
  let s1 = toWord(b) | 0;
17
17
  let s2 = toWord(c) | 0;
@@ -1,5 +1,5 @@
1
1
  import { Alea } from "../internal/alea.js";
2
- import { murmur3_32 } from "../internal/util.js";
2
+ import { hashSeed } from "../internal/util.js";
3
3
  function rotl(x, k) {
4
4
  return (x << k) | (x >>> (32 - k));
5
5
  }
@@ -14,11 +14,11 @@ function rotl(x, k) {
14
14
  * @returns Alea instance using Xoshiro128++
15
15
  */
16
16
  export function xoshiro128pp(a, b, c, d) {
17
- const toWord = (v) => typeof v === "number" ? v >>> 0 : murmur3_32(String(v));
18
- let s0 = toWord(a) | 0;
19
- let s1 = toWord(b) | 0;
20
- let s2 = toWord(c) | 0;
21
- let s3 = toWord(d) | 0;
17
+ const toWord = (v) => (typeof v === "number" ? v >>> 0 : hashSeed(String(v))) | 0;
18
+ let s0 = toWord(a);
19
+ let s1 = toWord(b);
20
+ let s2 = toWord(c);
21
+ let s3 = toWord(d);
22
22
  // requires at least one non-zero value
23
23
  if (s0 === 0 && s1 === 0 && s2 === 0 && s3 === 0) {
24
24
  s0 = 1;