@prosopo/util
Advanced tools
Comparing version 0.2.13 to 0.2.14
export * from './util.js'; | ||
export * from './ofLen.js'; | ||
export * from './lodash.js'; | ||
//# sourceMappingURL=index.d.ts.map |
export * from './util.js'; | ||
export * from './ofLen.js'; | ||
export * from './lodash.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -14,3 +14,3 @@ // Copyright 2021-2023 Prosopo (UK) Ltd. | ||
// limitations under the License. | ||
import { at, get, merge, permutations, rng, seedLodash } from '../util.js'; | ||
import { at, get, merge, permutations } from '../util.js'; | ||
import { describe, expect, test } from 'vitest'; | ||
@@ -225,26 +225,3 @@ describe('util', () => { | ||
}); | ||
describe('rng', () => { | ||
test('generates random numbers using a seed', () => { | ||
const seed = 0; | ||
const rand = rng(seed); | ||
const expected = [-1681090547, 408334984, 788430095, 3233831872, 963300000, -299378919, 97582850]; | ||
for (let i = 0; i < expected.length; i++) { | ||
expect(rand.int()).to.equal(expected[i]); | ||
} | ||
}); | ||
}); | ||
describe('seeded_lodash', () => { | ||
test('shuffles an array using a seed', () => { | ||
let array = [1, 2, 3, 4, 5]; | ||
const seed = 0; | ||
const _ = seedLodash(seed); | ||
array = _.shuffle(array); | ||
expect(array).to.deep.equal([4, 2, 1, 3, 5]); | ||
array = _.shuffle(array); | ||
expect(array).to.deep.equal([3, 4, 1, 5, 2]); | ||
array = _.shuffle(array); | ||
expect(array).to.deep.equal([3, 4, 5, 2, 1]); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=util.test.js.map |
@@ -1,13 +0,2 @@ | ||
import _lodash from 'lodash'; | ||
export declare const sleep: (ms: number) => Promise<unknown>; | ||
export declare const setSeedGlobal: (seed: number | string) => void; | ||
export declare const lodash: () => _lodash.LoDashStatic; | ||
export declare const seedLodash: (seed: number | string) => _lodash.LoDashStatic; | ||
export declare const rng: (seed: number | string) => { | ||
double: () => number; | ||
float: () => number; | ||
int: () => number; | ||
int32: () => number; | ||
bool: () => boolean; | ||
}; | ||
export declare function permutations(bins: number[], options?: { | ||
@@ -14,0 +3,0 @@ includeEmpty?: boolean; |
@@ -1,44 +0,3 @@ | ||
import _lodash from 'lodash'; | ||
import seedrandom from 'seedrandom'; | ||
// sleep for some milliseconds | ||
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
// set the seed for the global rng, i.e. seed `Math.random()` | ||
export const setSeedGlobal = (seed) => { | ||
seedrandom(seed.toString(), { global: true }); | ||
}; | ||
// create a new lodash instance with the current Math.random() and other global state | ||
export const lodash = () => { | ||
return _lodash.runInContext(); | ||
}; | ||
// create a new lodash instance with the given seed | ||
export const seedLodash = (seed) => { | ||
// take a snapshot of the current Math.random() fn | ||
const orig = Math.random; | ||
// replace Math.random with the seeded random | ||
seedrandom(seed.toString(), { global: true }); | ||
// runInContext() creates a new lodash instance using the seeded Math.random() | ||
// the context is a snapshot of the state of the global javascript environment, i.e. Math.random() updated to the seedrandom instance | ||
const lodash = _lodash.runInContext(); | ||
// restore the original Math.random() fn | ||
Math.random = orig; | ||
// return the lodash instance with the seeded Math.random() | ||
return lodash; | ||
}; | ||
// create a new rng with the given seed | ||
export const rng = (seed) => { | ||
const rng = seedrandom(seed.toString()); | ||
return { | ||
double: () => rng.double(), | ||
float: () => rng.quick(), | ||
int: () => { | ||
// js only has 53 bits of precision for integers, so we can't use the full 64 bits of the rng | ||
// take two 32 bit integers and combine them into a 53 bit integer | ||
const a = rng.int32(); | ||
const b = rng.int32(); | ||
return (a << 21) + b; | ||
}, | ||
int32: () => rng.int32(), | ||
bool: () => rng.int32() % 2 === 0, | ||
}; | ||
}; | ||
// create a generator that yields the permutations for a set of options | ||
@@ -45,0 +4,0 @@ // E.g. say we have 3 chars which can take 2 values each ('a' or 'b'), then we have 2^3 = 8 permutations: |
{ | ||
"name": "@prosopo/util", | ||
"version": "0.2.13", | ||
"version": "0.2.14", | ||
"author": "PROSOPO LIMITED <info@prosopo.io>", | ||
@@ -26,2 +26,6 @@ "license": "Apache-2.0", | ||
"require": "./dist/cjs/index.cjs" | ||
}, | ||
"./lodash": { | ||
"import": "./dist/lodash.js", | ||
"require": "./dist/lodash.js" | ||
} | ||
@@ -28,0 +32,0 @@ }, |
export * from './util.js' | ||
export * from './ofLen.js' | ||
export * from './lodash.js' |
@@ -14,3 +14,3 @@ // Copyright 2021-2023 Prosopo (UK) Ltd. | ||
// limitations under the License. | ||
import { at, get, merge, permutations, rng, seedLodash } from '../util.js' | ||
import { at, get, merge, permutations } from '../util.js' | ||
import { describe, expect, test } from 'vitest' | ||
@@ -265,27 +265,2 @@ | ||
}) | ||
describe('rng', () => { | ||
test('generates random numbers using a seed', () => { | ||
const seed = 0 | ||
const rand = rng(seed) | ||
const expected = [-1681090547, 408334984, 788430095, 3233831872, 963300000, -299378919, 97582850] | ||
for (let i = 0; i < expected.length; i++) { | ||
expect(rand.int()).to.equal(expected[i]) | ||
} | ||
}) | ||
}) | ||
describe('seeded_lodash', () => { | ||
test('shuffles an array using a seed', () => { | ||
let array = [1, 2, 3, 4, 5] | ||
const seed = 0 | ||
const _ = seedLodash(seed) | ||
array = _.shuffle(array) | ||
expect(array).to.deep.equal([4, 2, 1, 3, 5]) | ||
array = _.shuffle(array) | ||
expect(array).to.deep.equal([3, 4, 1, 5, 2]) | ||
array = _.shuffle(array) | ||
expect(array).to.deep.equal([3, 4, 5, 2, 1]) | ||
}) | ||
}) | ||
}) |
@@ -1,50 +0,4 @@ | ||
import _lodash from 'lodash' | ||
import seedrandom from 'seedrandom' | ||
// sleep for some milliseconds | ||
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
// set the seed for the global rng, i.e. seed `Math.random()` | ||
export const setSeedGlobal = (seed: number | string) => { | ||
seedrandom(seed.toString(), { global: true }) | ||
} | ||
// create a new lodash instance with the current Math.random() and other global state | ||
export const lodash = () => { | ||
return _lodash.runInContext() | ||
} | ||
// create a new lodash instance with the given seed | ||
export const seedLodash = (seed: number | string) => { | ||
// take a snapshot of the current Math.random() fn | ||
const orig = Math.random | ||
// replace Math.random with the seeded random | ||
seedrandom(seed.toString(), { global: true }) | ||
// runInContext() creates a new lodash instance using the seeded Math.random() | ||
// the context is a snapshot of the state of the global javascript environment, i.e. Math.random() updated to the seedrandom instance | ||
const lodash = _lodash.runInContext() | ||
// restore the original Math.random() fn | ||
Math.random = orig | ||
// return the lodash instance with the seeded Math.random() | ||
return lodash | ||
} | ||
// create a new rng with the given seed | ||
export const rng = (seed: number | string) => { | ||
const rng = seedrandom(seed.toString()) | ||
return { | ||
double: () => rng.double(), | ||
float: () => rng.quick(), | ||
int: () => { | ||
// js only has 53 bits of precision for integers, so we can't use the full 64 bits of the rng | ||
// take two 32 bit integers and combine them into a 53 bit integer | ||
const a = rng.int32() | ||
const b = rng.int32() | ||
return (a << 21) + b | ||
}, | ||
int32: () => rng.int32(), | ||
bool: () => rng.int32() % 2 === 0, | ||
} | ||
} | ||
// create a generator that yields the permutations for a set of options | ||
@@ -51,0 +5,0 @@ // E.g. say we have 3 chars which can take 2 values each ('a' or 'b'), then we have 2^3 = 8 permutations: |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1134918
39
37872
0