https://github.com/partman7/generic-astar
Generic JS/TS A* implementation.
https://github.com/partman7/generic-astar
Last synced: 8 months ago
JSON representation
Generic JS/TS A* implementation.
- Host: GitHub
- URL: https://github.com/partman7/generic-astar
- Owner: PartMan7
- Created: 2025-07-31T10:03:11.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-07-31T10:23:36.000Z (9 months ago)
- Last Synced: 2025-08-09T06:54:26.464Z (9 months ago)
- Language: TypeScript
- Size: 21.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# generic-astar
Generic JS/TS A\* implementation. Comes with helper utils!
## Installation
```bash
# Any of the following
npm install generic-astar
yarn add generic-astar
bun add generic-astar
```
## Usage
#### TypeScript
```ts
import AStar from 'generic-astar';
import { GridAdapter } from 'generic-astar/adapters';
const adapter = new GridAdapter(
{ x: 5, y: 5 },
{
blocked: [[1, 1]],
target: [[4, 4]],
diagonals: true,
}
);
const result = AStar({ from: [0, 0], adapter });
```
```ts
import AStar from 'generic-astar';
const result = AStar<[number, number]>({
from: [0, 0],
to: [[4, 4]],
hash: node => node.join(','),
heuristic: node => Math.sqrt((4 - node[0]) ** 2 + (4 - node[1]) ** 2),
neighbors: node => {
if (node[0] === 0 && node[1] === 0)
return [
{ node: [3, 0], cost: 3 },
{ node: [0, 2], cost: 2 },
];
if (node[0] === 4) return [{ node: [4, 4], cost: Math.abs(node[1] - 4) }];
return [{ node: [node[0] + 1, node[1] + 1], cost: Math.sqrt(2) }];
},
});
// ({
// distance: 4 + 4 * SQRT2,
// path: [
// [0, 0],
// [0, 2],
// [1, 3],
// [2, 4],
// [3, 5],
// [4, 6],
// [4, 4],
// ],
// });
```
#### JavaScript (ESM)
```js
import AStar from 'generic-astar';
```
#### JavaScript (CJS)
```js
const AStar = require('generic-astar');
```