@prosopo/util
Advanced tools
Comparing version 0.2.14 to 0.2.15
export * from './util.js'; | ||
export * from './ofLen.js'; | ||
export * from './lodash.js'; | ||
export * from './isMain.js'; | ||
//# sourceMappingURL=index.d.ts.map |
export * from './util.js'; | ||
export * from './ofLen.js'; | ||
export * from './lodash.js'; | ||
export * from './isMain.js'; | ||
//# sourceMappingURL=index.js.map |
import _lodash from 'lodash'; | ||
import seedrandom from 'seedrandom'; | ||
// create a new rng with the given seed | ||
export const rng = (seed) => { | ||
@@ -10,4 +9,2 @@ const rng = seedrandom(seed.toString()); | ||
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(); | ||
@@ -21,24 +18,15 @@ const b = rng.int32(); | ||
}; | ||
// 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; | ||
}; | ||
//# sourceMappingURL=lodash.js.map |
@@ -1,14 +0,1 @@ | ||
// Copyright 2021-2023 Prosopo (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
import { at, get, merge, permutations } from '../util.js'; | ||
@@ -18,20 +5,2 @@ import { describe, expect, test } from 'vitest'; | ||
describe('merge', () => { | ||
// factors: | ||
// - types | ||
// - primitive | ||
// - array | ||
// - object | ||
// - nesting | ||
// - 0 levels | ||
// - 1 levels | ||
// - 2 levels | ||
// - array strat | ||
// - 'update' | ||
// - 'replace' | ||
// - merge nested els in array | ||
// - true | ||
// - false | ||
// - merge nested fields in obj | ||
// - true | ||
// - false | ||
test('array in array', () => { | ||
@@ -106,3 +75,2 @@ expect(merge([ | ||
test('types', () => { | ||
// check the types are picked up correctly by ts | ||
const v1 = at([1, 2, 3], 0); | ||
@@ -172,3 +140,2 @@ const v2 = at([1, 2, 3, undefined], 0); | ||
test('types', () => { | ||
// check the types are picked up correctly by ts | ||
const v1 = get({ a: 1 }, 'a'); | ||
@@ -179,5 +146,3 @@ const v2 = get({ a: 1 }, 'a', false); | ||
const v5 = get({ a: 1, b: undefined }, 'a', false); | ||
// cast from any | ||
const v6 = get(JSON.parse('{"a": 1}'), 'a'); | ||
// cast from unknown | ||
const v7 = get(JSON.parse('{"a": 1}'), 'a'); | ||
@@ -184,0 +149,0 @@ }); |
@@ -1,22 +0,2 @@ | ||
// sleep for some milliseconds | ||
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
// create a generator that yields the permutations for a set of options | ||
// E.g. say we have 3 chars which can take 2 values each ('a' or 'b'), then we have 2^3 = 8 permutations: | ||
// a a a | ||
// a a b | ||
// a b a | ||
// a b b | ||
// b a a | ||
// b a b | ||
// b b a | ||
// b b b | ||
// This function yields each permutation as an array of numbers, where each number is the index of the option for that position | ||
// E.g. for the above example, the first permutation is [0, 0, 0], the second is [0, 0, 1], the third is [0, 1, 0], etc. | ||
// | ||
// The bins param is an array of numbers, where each number is the number of options for that position | ||
// E.g. for the above example, the bins param would be [2, 2, 2] | ||
// | ||
// Note that the bins can be differing sizes, so the first char could have 2 options whereas the second could have 3 options and the fourth char could have 6 options | ||
// | ||
// Optionally include the empty permutation, i.e. [] (useful for when you want to include the empty permutation in a cartesian product) | ||
export function* permutations(bins, options) { | ||
@@ -82,4 +62,2 @@ if (options?.includeEmpty) { | ||
const index = Math.abs(Math.round(random())) % items.length; | ||
// with replacement == allow duplicates | ||
// without replacement == don't allow duplicates | ||
if (options.withReplacement || indicesSet.add(index)) { | ||
@@ -110,16 +88,5 @@ indices.push(index); | ||
}; | ||
// https://stackoverflow.com/questions/63116039/camelcase-to-kebab-case | ||
export const kebabCase = (str) => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase()); | ||
// Merge two objects or arrays together. | ||
// Nesting can be infinitely deep. | ||
// Arrays can be homogeneous or hetrogeneous. | ||
// The destination object/array is mutated directly. | ||
// Arrays can be merged in two ways: | ||
// - update (default): replace elements as required and extend array as required, e.g. [1,2,3] + [4,5] = [4,5,3] | ||
// - replace: treat the array as a primitive value and simply replace as-is, e.g. [1,2,3] + [4,5] = [4,5] | ||
// The 'atomicArrays' option controls whether arrays are treated as primitives or not. E.g. atomicArrays=true is the 'replace' strategy, atomicArrays=false is the 'update' strategy. | ||
// This method treats arrays as an object with numeric keys and merged using the object merge strategy. | ||
export function merge(dest, src, options) { | ||
const atomicArrays = options?.atomicArrays; | ||
// maintain a queue of object sources/destinations to merge | ||
const queue = [ | ||
@@ -137,11 +104,8 @@ { | ||
if (isArray(task.dest)) { | ||
// handling arrays | ||
const src = task.src; | ||
const dest = task.dest; | ||
if (atomicArrays) { | ||
// delete any items beyond the length of src | ||
while (dest.length > src.length) { | ||
dest.pop(); | ||
} | ||
// treat arrays as primitives / atomic | ||
for (let i = 0; i < src.length; i++) { | ||
@@ -152,9 +116,4 @@ dest[i] = src[i]; | ||
else { | ||
// else not treating arrays as primitives / atomic | ||
// so need to merge them | ||
// copy the elements from src into dest | ||
for (let i = 0; i < src.length; i++) { | ||
// if the element is an array or object, then we need to merge it | ||
if ((isArray(dest[i]) && isArray(src[i])) || (isObject(dest[i]) && isObject(src[i]))) { | ||
// need to merge arrays or objects | ||
queue.push({ | ||
@@ -166,5 +125,2 @@ src: src[i], | ||
else { | ||
// primitive, so replace | ||
// or src[i] is array but dest[i] is not, so replace | ||
// or src[i] is object but dest[i] is not, so replace | ||
dest[i] = src[i]; | ||
@@ -178,7 +134,4 @@ } | ||
const destAny = task.dest; | ||
// for every entry in src | ||
for (const [key, value] of Object.entries(src)) { | ||
// if the value in src + dest is an array or object, then we need to merge it | ||
if ((isArray(value) && isArray(destAny[key])) || (isObject(value) && isObject(destAny[key]))) { | ||
// need to merge arrays or objects | ||
queue.push({ | ||
@@ -190,5 +143,2 @@ src: value, | ||
else { | ||
// primitive, so replace | ||
// or value is array but dest[key] is not, so replace | ||
// or value is object but dest[key] is not, so replace | ||
destAny[key] = value; | ||
@@ -195,0 +145,0 @@ } |
{ | ||
"name": "@prosopo/util", | ||
"version": "0.2.14", | ||
"version": "0.2.15", | ||
"author": "PROSOPO LIMITED <info@prosopo.io>", | ||
@@ -28,4 +28,6 @@ "license": "Apache-2.0", | ||
"./lodash": { | ||
"types": "./dist/lodash.d.ts", | ||
"import": "./dist/lodash.js", | ||
"require": "./dist/lodash.js" | ||
"require": "./dist/cjs/lodash.cjs", | ||
"default": "./dist/lodash.js" | ||
} | ||
@@ -32,0 +34,0 @@ }, |
export * from './util.js' | ||
export * from './ofLen.js' | ||
export * from './lodash.js' | ||
export * from './isMain.js' |
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
45
1132249
37840