gamekit-utils
Version:
Minimal, fast and useful utilities for randomness, array manipulation and math — built for games, UI logic and generative design.
1 lines • 6.27 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../src/array/random.ts","../src/array/shuffle.ts","../src/array/pickN.ts","../src/math/chance.ts","../src/math/clamp.ts","../src/math/lerp.ts","../src/math/normalize.ts","../src/grid/create2D.ts"],"sourcesContent":["/**\n * Returns a random element from an array.\n * Throws an error if the array is empty.\n * \n * @example\n * random([1, 2, 3]); // → 2\n */\nexport const random = <T>(arr: T[]): T => {\n if (!Array.isArray(arr)) {\n throw new TypeError(\"Expected an array\");\n }\n if (arr.length === 0) {\n throw new RangeError(\"Cannot select a random element from an empty array\");\n }\n const index = Math.floor(Math.random() * arr.length);\n return arr[index];\n}","/**\n * Returns a new array with the same elements in random order (Fisher–Yates).\n * \n * Note: Only the array structure is copied. Objects inside are not cloned.\n *\n * @example\n * shuffle([1, 2, 3]); // → [2, 3, 1]\n */\nexport const shuffle = <T>(arr: T[]): T[] => {\n const result = [...arr]; // shallow copy\n for (let i = result.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [result[i], result[j]] = [result[j], result[i]];\n }\n return result;\n}","import { shuffle } from \"./shuffle\";\n\n/**\n * Picks `n` unique random elements from the array.\n * \n * @throws if n > arr.length\n */\nexport function pickN<T>(arr: T[], n: number): T[] {\n if (!Array.isArray(arr)) {\n throw new TypeError(\"Expected an array\");\n }\n if (n > arr.length) {\n throw new RangeError(\"Cannot pick more elements than array contains\");\n }\n return shuffle(arr).slice(0, n);\n}","/**\n * Returns true with the given percentage chance (0–100).\n *\n * @example\n * chance(25) // ~25% chance to return true\n */\nexport function chance(percent: number): boolean {\n if (typeof percent !== \"number\" || isNaN(percent)) {\n throw new TypeError(\"Chance percent must be a valid number\");\n }\n\n if (percent < 0 || percent > 100) {\n throw new RangeError(\"Chance percent must be between 0 and 100\");\n }\n\n return Math.random() < percent / 100;\n}","/**\n * Clamps a number between min and max bounds.\n *\n * @example\n * clamp(5, 1, 10) // → 5\n * clamp(-2, 0, 10) // → 0\n * clamp(12, 0, 10) // → 10\n */\nexport function clamp(val: number, min: number, max: number): number {\n if (min > max) {\n throw new RangeError(\"clamp: min cannot be greater than max\");\n }\n return Math.min(Math.max(val, min), max);\n}","/**\n * Linearly interpolate between two values.\n * @param a The start value.\n * @param b The end value.\n * @param t The interpolation factor, clamped between 0 and 1.\n * @returns The interpolated value.\n */\n\nexport function lerp(a: number, b: number, t: number): number {\n return a + (b - a) * t;\n}","/**\n * Normalizes a value t from the range [a, b] to [0, 1].\n *\n * @param a The start of the range.\n * @param b The end of the range.\n * @param t The value to normalize.\n * @returns The normalized value in [0, 1].\n *\n * @example\n * normalize(10, 20, 15) // → 0.5\n * normalize(0, 100, 25) // → 0.25\n */\nexport function normalize(a: number, b: number, t: number): number {\n if (a === b) throw new RangeError('normalize: a and b cannot be equal');\n return (t - a) / (b - a);\n} ","/**\n * Creates a 2D array.\n * @param rows The number of rows.\n * @param cols The number of columns.\n * @param value The initial value or a function to generate values.\n * @returns A 2D array filled with the specified value.\n */\nexport function create2D<T>(\n rows: number,\n cols: number,\n value: T | ((r: number, c: number) => T)\n): T[][] {\n const isFn = typeof value === \"function\";\n\n return Array.from({ length: rows }, (_, r) =>\n Array.from({ length: cols }, (_, c) =>\n isFn ? (value as (r: number, c: number) => T)(r, c) : value\n )\n );\n}"],"names":[],"mappings":";;AAAA;;;;;;AAMG;AACU,MAAA,MAAM,GAAG,CAAI,GAAQ,KAAO;IACvC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;AACvB,QAAA,MAAM,IAAI,SAAS,CAAC,mBAAmB,CAAC;;AAE1C,IAAA,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE;AACpB,QAAA,MAAM,IAAI,UAAU,CAAC,oDAAoD,CAAC;;AAE5E,IAAA,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EA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