1import * as num from "./num.mjs"; 2 3// Re-export anyKey from num.mjs (moved there to break circular dependency) 4export { anyKey } from "./num.mjs"; 5 6const { floor } = Math; 7 8// Randomly returns one of the arguments. 9export function choose() { 10 return arguments[num.randInt(arguments.length - 1)]; 11} 12 13// Flip a coin, returning true or false. 14export function flip() { 15 return choose(true, false); 16} 17 18// Set every property of an object to a certain value. 19export function every(obj, value) { 20 Object.keys(obj).forEach((k) => (obj[k] = value)); 21} 22 23// Returns a random index value from an array. 24export function anyIndex(array) { 25 return num.randInt(array.length - 1); 26} 27 28// Returns a random value from an object, or array. 29export function any(objOrArray) { 30 if (Array.isArray(objOrArray)) { 31 return objOrArray[anyIndex(objOrArray)]; 32 } else { 33 const keys = Object.keys(objOrArray); 34 return objOrArray[keys[(keys.length * Math.random()) << 0]]; 35 } 36} 37 38// Shuffles an array: https://stackoverflow.com/a/2450976 39export function shuffleInPlace(array) { 40 let currentIndex = array.length, 41 randomIndex; 42 43 // While there remain elements to shuffle. 44 while (currentIndex != 0) { 45 // Pick a remaining element. 46 randomIndex = Math.floor(Math.random() * currentIndex); 47 currentIndex--; 48 49 // And swap it with the current element. 50 [array[currentIndex], array[randomIndex]] = [ 51 array[randomIndex], 52 array[currentIndex], 53 ]; 54 } 55 return array; 56} 57 58// Run a function on every value in an object. 59// Ex. each(obj, (value, key) => console.log(value, key)); 60export function each(obj, fn) { 61 Object.entries(obj).forEach(([key, obj]) => fn(obj, key)); 62} 63 64// Run a function `n` times, passing in `i` on each iteration 65// and returning an array of the results (like map). 66export function repeat(n, fn) { 67 const reps = []; 68 for (let i = 0; i < floor(n); i += 1) reps.push(fn(i)); 69 return reps; 70} 71 72// Find a key value pair match inside an object of objects, 0 levels deep. 73export function findKeyAndValue(obj, k, v) { 74 return obj[Object.keys(obj).find((key) => obj[key][k] === v)]; 75} 76 77// Determine if an input value is nothing. 78export function nonvalue(i) { 79 return i === undefined || i === null || isNaN(i); 80} 81 82// Resize an array by taking samples at equal intervals, with no interpolation. 83export function resampleArray(inputArray, newLength) { 84 const inputLength = inputArray.length; 85 const outputArray = []; 86 for (let i = 0; i < newLength; i++) { 87 const index = floor((i / newLength) * inputLength); 88 outputArray.push(inputArray[index]); 89 } 90 return outputArray; 91}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.