UNPKG

gamekit-utils

Version:

Minimal, fast and useful utilities for randomness, array manipulation and math โ€” built for games, UI logic and generative design.

229 lines (158 loc) โ€ข 5.61 kB
# ๐ŸŽฎ gamekit-utils Minimal, fast and useful utilities for randomness, array manipulation and math โ€” built for games, UI logic and generative design. > Lightweight toolkit for game developers, creative coders, and UI experimenters. --- ## โœ… What's the promise? We're building a tiny, powerful toolkit for developers who create: - ๐ŸŽฒ casual games - ๐Ÿง  game logic systems - โœจ creative procedural content - ๐Ÿงฉ UI components with randomized behavior Everything will be: - [x] ๐ŸŸข Written in TypeScript - [x] ๐ŸŒณ Fully tree-shakable (ESM only) - [x] ๐Ÿ“ฆ Zero-dependency - [x] ๐Ÿ“˜ Documented with practical examples - [x] ๐Ÿ”ฌ Covered with unit tests (Vitest) - [x] ๐Ÿงช 100% test coverage - [x] ๐Ÿ”„ Publish-ready as v1.0.0 --- ## ๐Ÿงช Core Modules | Status | Function | Description | |--------|--------------------------|--------------------------------------------| | โœ… | `random(arr)` | Pick a random element from a non-empty array | | โœ… | `shuffle(arr)` | Shuffle an array using Fisherโ€“Yates | | โœ… | `pickN(arr, n)` | Pick N unique random elements | | โœ… | `chance(percent)` | Return true with a probability | | โœ… | `clamp(val, min, max)` | Clamp number within bounds | | โœ… | `lerp(a, b, t)` | Linear interpolation | | โœ… | `normalize(a, b, t)` | Normalize a value to a 0-1 range | | โœ… | `create2D(rows, cols, fn)` | Create a 2D array filled with a default value | | โœ… | `getNeighbors(row, col, grid, diagonal)` | Get neighbors of a cell in a 2D grid | | โœ… | `pulse(from, to, phase, cycle)` | Check if a phase is within a cyclic range | --- ## โšก Usage Examples ### ๐ŸŽฒ random Picks a random element from a non-empty array. ```ts import { random } from "gamekit-utils"; const enemies = ["orc", "troll", "goblin"]; const chosen: string = random(enemies); console.log(chosen); // โ†’ e.g., "troll" ``` ### ๐Ÿ”€ shuffle Returns a shuffled copy of the original array (non-mutating). ```ts import { shuffle } from "gamekit-utils"; const deck = [1, 2, 3, 4, 5]; const shuffled: number[] = shuffle(deck); console.log(shuffled); // โ†’ e.g., [4, 1, 5, 3, 2] console.log(deck); // โ†’ original remains: [1, 2, 3, 4, 5] ``` ### ๐ŸŽฏ pickN Returns N unique random elements from the array (non-mutating). ```ts import { pickN } from "gamekit-utils"; const names = ["Alice", "Bob", "Charlie", "Dave"]; const group = pickN(names, 2); // โ†’ e.g., ["Charlie", "Alice"] ``` ### ๐ŸŽฐ chance Returns true with a given probability (0โ€“100%). ```ts import { chance } from "gamekit-utils"; if (chance(30)) { console.log("You got lucky!"); // ~30% chance } ``` ### ๐Ÿ“‰ clamp Clamps a number between min and max bounds. ```ts import { clamp } from "gamekit-utils"; const hp = clamp(player.hp, 0, 100); console.log(hp); // โ†’ between 0 and 100 ``` ### ๐Ÿ“ lerp Returns a value interpolated between two numbers based on a ratio. ```ts import { lerp } from "gamekit-utils"; const value = lerp(10, 20, 0.5); console.log(value); // โ†’ 15 ``` ### ๐Ÿ“ normalize Normalizes a value t from the range [a, b] to [0, 1]. ```ts import { normalize } from "gamekit-utils"; const n1 = normalize(10, 20, 15); // โ†’ 0.5 const n2 = normalize(0, 100, 25); // โ†’ 0.25 ``` ## Grid Utilities ### ๐Ÿ“ create2D Creates a 2D array of given size, filled with a default value. ```ts import { create2D } from "gamekit-utils"; const grid = create2D(3, 4, 0); /* [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ] */ ``` ### ๐Ÿงญ getNeighbors Returns the existing neighbors of a cell in a 2D grid. ```ts import { getNeighbors } from "gamekit-utils"; const grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; getNeighbors(1, 1, grid); // โ†’ [ // { row: 0, col: 1, value: 2 }, // { row: 2, col: 1, value: 8 }, // { row: 1, col: 0, value: 4 }, // { row: 1, col: 2, value: 6 } // ] ``` ### โšก pulse Checks if the given phase is within the specified range in a cyclic manner. ```ts import { pulse } from "gamekit-utils"; const isActive = pulse(0, 5, 3, 10); console.log(isActive); // โ†’ 1 for (let i = 0; i < 11; i++) { console.log(i, pulse(0, 3, i, 5)); } i: 0 1 2 3 4 5 6 7 8 9 10 output: 1 1 1 0 0 1 1 1 0 0 1 ``` ## ๐Ÿ“ฆ Installation ```bash npm install gamekit-utils # or yarn add gamekit-utils ``` --- ## ๐Ÿงช Core Modules (roadmap) | Function | Since | Tested | Docs | Status | |---------------------|---------|--------|------|---------| | `random(arr)` | 0.0.1 | โœ… | โœ… | โœ… Done | | `shuffle(arr)` | 0.0.5 | โœ… | โœ… | โœ… Done | | `pickN(arr, n)` | 0.0.6 | โœ… | โœ… | โœ… Done | | `chance(percent)` | 0.0.7 | โœ… | โœ… | โœ… Done | | `clamp(val, min, max)` | 0.0.8 | โœ… | โœ… | โœ… Done | | `lerp(a, b, t)` | 0.0.9 | โœ… | โœ… | โœ… Done | | `normalize(a, b, t)` | 0.1.0 | โœ… | โœ… | โœ… Done | | `create2D(rows, cols, fn)` | 0.1.1 | โœ… | โœ… | โœ… Done | | `getNeighbors(row, col, grid, diagonal)` | 0.1.2 | โœ… | โœ… | โœ… Done | | `pulse(from, to, phase, cycle)` | 0.1.4 | โœ… | โœ… | โœ… Done | --- ## ๐Ÿ’ก Author **Oleh Levchenko** GitHub: [@leva13007](https://github.com/leva13007) Support: [buymeacoffee.com/zloyleva](https://buymeacoffee.com/zloyleva) --- ## ๐Ÿ“„ License MIT ยฉ Oleh Levchenko