UNPKG

atriusmaps-node-sdk

Version:

This project provides an API to Atrius Personal Wayfinder maps within a Node environment. See the README.md for more information

56 lines (53 loc) 1.49 kB
'use strict'; function mulberry32(seed) { return function() { let t = seed += 1831565813; t = Math.imul(t ^ t >>> 15, t | 1); t ^= t + Math.imul(t ^ t >>> 7, t | 61); return ((t ^ t >>> 14) >>> 0) / 4294967296; }; } const random = undefined?.MODE === "test" ? mulberry32(12345678) : Math.random; const rand = (min, max) => { if (max == null) { max = min; min = 0; } return Math.floor(random() * (max - min) + min); }; function randomSet(size, max) { if (!max) { max = size; } if (size > max) { size = max; } const result = []; const SPARSITY_THRESHOLD = 0.3; const isSparse = size / max < SPARSITY_THRESHOLD; if (isSparse) { const seen = /* @__PURE__ */ new Set(); while (result.length < size) { const i = rand(max); if (!seen.has(i)) { seen.add(i); result.push(i); } } } else { const source = Array.from({ length: max }, (_, i) => i); for (let i = 0; i < size; i++) { const index = rand(max - i); result.push(source[index]); source[index] = source[max - i - 1]; } } return result; } const getMappedArray = (array, map) => map.map((i) => array[i]); const randomizeArray = (array) => getMappedArray(array, randomSet(array.length)); const arrayPick = (array, num = 1) => getMappedArray(array, randomSet(num, array.length)); exports.arrayPick = arrayPick; exports.rand = rand; exports.randomSet = randomSet; exports.randomizeArray = randomizeArray;