UNPKG

gamekit-utils

Version:

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

184 lines (126 loc) โ€ข 4.52 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 - [ ] ๐Ÿ“˜ Documented with practical examples - [ ] ๐Ÿ”ฌ Covered with unit tests (Vitest) - [ ] ๐Ÿงช 100% test coverage - [ ] ๐Ÿง  Typed from day one - [ ] ๐Ÿ”„ 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 | --- ## โšก 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 ``` ### ๐Ÿ“ 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] ] */ ``` --- ## ๐Ÿ“ฆ 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 | --- ## ๐Ÿ’ก Author **Oleh Levchenko** GitHub: [@leva13007](https://github.com/leva13007) Support: [buymeacoffee.com/zloyleva](https://buymeacoffee.com/zloyleva) --- ## ๐Ÿ“„ License MIT ยฉ Oleh Levchenko