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
Markdown
# ๐ฎ 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