@peacestay/arcade-core 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +84 -2
- package/dist/2048/board.d.ts +3 -0
- package/dist/2048/board.d.ts.map +1 -0
- package/dist/2048/board.js +8 -0
- package/dist/2048/board.js.map +1 -0
- package/dist/2048/move.d.ts +9 -0
- package/dist/2048/move.d.ts.map +1 -0
- package/dist/2048/move.js +102 -0
- package/dist/2048/move.js.map +1 -0
- package/dist/2048/rules.d.ts +4 -0
- package/dist/2048/rules.d.ts.map +1 -0
- package/dist/2048/rules.js +13 -0
- package/dist/2048/rules.js.map +1 -0
- package/dist/2048/spawn.d.ts +3 -0
- package/dist/2048/spawn.d.ts.map +1 -0
- package/dist/2048/spawn.js +33 -0
- package/dist/2048/spawn.js.map +1 -0
- package/dist/2048/types.d.ts +19 -0
- package/dist/2048/types.d.ts.map +1 -0
- package/dist/2048/types.js +2 -0
- package/dist/2048/types.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/package.json +24 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2026-10-22
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `createBoard()`, create an empty board
|
|
7
|
+
- `spawn(board, rng?)`, spawn a tile 2 or 4 on the board
|
|
8
|
+
- `move(board, direction)`, move in one chosen direction
|
|
9
|
+
- `score(board)`, collect the score
|
|
10
|
+
- `hasMoves(board)`, return true if any possible move left
|
|
11
|
+
- `Board`, represents a board with size of 4 and a list of tiles
|
|
12
|
+
- `Tile`, represents a tile with attributes
|
|
13
|
+
- `Direction`, union type of left, right, up and down
|
|
14
|
+
- `Position`, wrapper for row, col coord
|
package/README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @peacestay/arcade-core
|
|
2
|
+
|
|
3
|
+
Arcade core package for 2048 game logics, for Student Zhiyuan Zhao in CS5610 course
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @peacestay/arcade-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Development
|
|
12
|
+
```bash
|
|
13
|
+
npm install
|
|
14
|
+
npm run build && npm test
|
|
15
|
+
npm publish --access public
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Get started
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createBoard, spawn, move, score, hasMoves } from '@peacestay/arcade-core';
|
|
22
|
+
const board = createBoard();
|
|
23
|
+
const firstSpawn = spawn(board);
|
|
24
|
+
const secondSpawn = spawn(firstSpawn);
|
|
25
|
+
const movedBoard = move(secondSpawn, 'left') // movedBoard is the same if no moved nor merged
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
create a board with no tiles -> spawn twice to get started
|
|
29
|
+
choose a direction to move -> check if the board changed, either merged or moved
|
|
30
|
+
if changed -> add the score() is merged -> spawn again
|
|
31
|
+
if not changed -> not a valid move, return the same board
|
|
32
|
+
if hasMoves() is false -> game ends
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
### `createBoard(): Board`
|
|
37
|
+
create a Board object with empty cells
|
|
38
|
+
|
|
39
|
+
### `move(board, direction): Board`
|
|
40
|
+
move a board given the direction, merge or just move the cells if possible, return the same board if neither
|
|
41
|
+
|
|
42
|
+
### `spawn(board, rng?): Board`
|
|
43
|
+
spawn a cell with value 2 or 4, rng is optional, math.random by default
|
|
44
|
+
call rng first to pick a cell, and call it again for 2 or 4 (<0.9 -> 2, else 4)
|
|
45
|
+
|
|
46
|
+
### `score(board): number`
|
|
47
|
+
points earned by the move
|
|
48
|
+
|
|
49
|
+
### `hasMoves(board): boolean`
|
|
50
|
+
use move in four directions too see if any move is possible
|
|
51
|
+
|
|
52
|
+
## Key Types
|
|
53
|
+
|
|
54
|
+
### Board
|
|
55
|
+
|
|
56
|
+
Represents a board with size=4 by default, a list of Tiles and the id number of next cell
|
|
57
|
+
id starts at 1 incremented whenever there's a new tile spawned or a valid merge.
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
export interface Board {
|
|
61
|
+
size: 4;
|
|
62
|
+
tiles: Tile[];
|
|
63
|
+
nextId: number;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Tile
|
|
68
|
+
|
|
69
|
+
Represents a cell with a value
|
|
70
|
+
id is number passed from the nextId in the board, row, col are the coords
|
|
71
|
+
merged is only true when a new tile generated from a merge
|
|
72
|
+
isNew is ture only when the tile was first added by a spawn (merged tile also has false flag)
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
export interface Tile {
|
|
76
|
+
id: number;
|
|
77
|
+
value: number;
|
|
78
|
+
row: number;
|
|
79
|
+
col: number;
|
|
80
|
+
merged: boolean;
|
|
81
|
+
isNew: boolean;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
2
85
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"board.d.ts","sourceRoot":"","sources":["../../src/2048/board.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,wBAAgB,WAAW,IAAI,KAAK,CAMnC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"board.js","sourceRoot":"","sources":["../../src/2048/board.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,WAAW;IACvB,OAAO;QACH,IAAI,EAAE,CAAC;QACP,KAAK,EAAE,EAAE;QACT,MAAM,EAAE,CAAC;KACZ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Board, Direction, Tile } from "./types.js";
|
|
2
|
+
export interface MergeResult {
|
|
3
|
+
tiles: Tile[];
|
|
4
|
+
points: number;
|
|
5
|
+
nextId: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function mergeLine(tiles: Tile[], nextId: number): MergeResult;
|
|
8
|
+
export declare function move(board: Board, direction: Direction): Board;
|
|
9
|
+
//# sourceMappingURL=move.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"move.d.ts","sourceRoot":"","sources":["../../src/2048/move.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAY,MAAM,YAAY,CAAC;AAEnE,MAAM,WAAW,WAAW;IACxB,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,CAmCpE;AAED,wBAAgB,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,GAAG,KAAK,CAsC9D"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export function mergeLine(tiles, nextId) {
|
|
2
|
+
let points = 0;
|
|
3
|
+
const mergedTiles = [];
|
|
4
|
+
let currentTile = null;
|
|
5
|
+
for (const tile of tiles) {
|
|
6
|
+
if (currentTile === null) {
|
|
7
|
+
currentTile = tile;
|
|
8
|
+
}
|
|
9
|
+
else if (currentTile.value === tile.value) {
|
|
10
|
+
points += currentTile.value * 2;
|
|
11
|
+
const newTile = {
|
|
12
|
+
id: nextId++,
|
|
13
|
+
value: currentTile.value * 2,
|
|
14
|
+
row: currentTile.row,
|
|
15
|
+
col: currentTile.col,
|
|
16
|
+
merged: true,
|
|
17
|
+
isNew: false
|
|
18
|
+
};
|
|
19
|
+
mergedTiles.push(newTile);
|
|
20
|
+
currentTile = null;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
mergedTiles.push({ ...currentTile, merged: false, isNew: false });
|
|
24
|
+
currentTile = tile;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (currentTile !== null) {
|
|
28
|
+
mergedTiles.push({ ...currentTile, merged: false, isNew: false });
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
tiles: mergedTiles,
|
|
32
|
+
points,
|
|
33
|
+
nextId
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function move(board, direction) {
|
|
37
|
+
let nextId = board.nextId;
|
|
38
|
+
const newTiles = [];
|
|
39
|
+
let changed = false;
|
|
40
|
+
const distance = (tile) => distanceFromWall(tile, direction, board.size);
|
|
41
|
+
for (let i = 0; i < board.size; i++) {
|
|
42
|
+
const lineTiles = board.tiles.filter(tile => lineOf(tile, direction) === i)
|
|
43
|
+
.sort((a, b) => distance(a) - distance(b));
|
|
44
|
+
const result = mergeLine(lineTiles, nextId);
|
|
45
|
+
nextId = result.nextId;
|
|
46
|
+
if (result.points > 0) {
|
|
47
|
+
changed = true;
|
|
48
|
+
}
|
|
49
|
+
for (const [k, tile] of result.tiles.entries()) {
|
|
50
|
+
const position = positionFor(direction, i, k, board.size);
|
|
51
|
+
if (tile.row !== position.row || tile.col !== position.col) {
|
|
52
|
+
changed = true;
|
|
53
|
+
}
|
|
54
|
+
newTiles.push({
|
|
55
|
+
...tile,
|
|
56
|
+
row: position.row,
|
|
57
|
+
col: position.col
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!changed) {
|
|
62
|
+
return board; // No change in the board
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
...board,
|
|
66
|
+
tiles: newTiles,
|
|
67
|
+
nextId
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function lineOf(tile, direction) {
|
|
71
|
+
if (direction === 'up' || direction === 'down') {
|
|
72
|
+
return tile.col;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
return tile.row;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function distanceFromWall(tile, direction, size) {
|
|
79
|
+
switch (direction) {
|
|
80
|
+
case 'up':
|
|
81
|
+
return tile.row;
|
|
82
|
+
case 'down':
|
|
83
|
+
return size - 1 - tile.row;
|
|
84
|
+
case 'left':
|
|
85
|
+
return tile.col;
|
|
86
|
+
case 'right':
|
|
87
|
+
return size - 1 - tile.col;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function positionFor(direction, line, index, size) {
|
|
91
|
+
switch (direction) {
|
|
92
|
+
case 'up':
|
|
93
|
+
return { row: index, col: line };
|
|
94
|
+
case 'down':
|
|
95
|
+
return { row: size - 1 - index, col: line };
|
|
96
|
+
case 'left':
|
|
97
|
+
return { row: line, col: index };
|
|
98
|
+
case 'right':
|
|
99
|
+
return { row: line, col: size - 1 - index };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=move.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"move.js","sourceRoot":"","sources":["../../src/2048/move.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,SAAS,CAAC,KAAa,EAAE,MAAc;IACnD,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,MAAM,WAAW,GAAW,EAAE,CAAC;IAC/B,IAAI,WAAW,GAAgB,IAAI,CAAC;IAEpC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACvB,IAAI,WAAW,KAAK,IAAI,EAAE,CAAC;YACvB,WAAW,GAAG,IAAI,CAAC;QACvB,CAAC;aAAM,IAAI,WAAW,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YAC1C,MAAM,IAAI,WAAW,CAAC,KAAK,GAAG,CAAC,CAAC;YAChC,MAAM,OAAO,GAAS;gB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KAAK,GAAG,OAAO,CAO9C"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { move } from "./move.js";
|
|
2
|
+
export function score(board) {
|
|
3
|
+
return board.tiles.reduce((sum, tile) => sum + (tile.merged ? tile.value : 0), 0);
|
|
4
|
+
}
|
|
5
|
+
export function hasMoves(board) {
|
|
6
|
+
for (const direction of ['up', 'down', 'left', 'right']) {
|
|
7
|
+
if (move(board, direction) !== board) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../../src/2048/rules.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,UAAU,KAAK,CAAC,KAAY;IAC9B,OAAO,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACtF,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,KAAY;IACjC,KAAK,MAAM,SAAS,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAgB,EAAE,CAAC;QACrE,IAAI,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,KAAK,KAAK,EAAE,CAAC;YACnC,OAAO,IAAI,CAAC;QAChB,CAAC;IACL,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spawn.d.ts","sourceRoot":"","sources":["../../src/2048/spawn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAkB,MAAM,YAAY,CAAC;AAExD,wBAAgB,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,GAAE,MAAM,MAAoB,GAAG,KAAK,CAsB1E"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function spawn(board, rng = Math.random) {
|
|
2
|
+
const empty = emptyCells(board);
|
|
3
|
+
const emptyCell = empty[Math.floor(rng() * empty.length)];
|
|
4
|
+
if (!emptyCell) {
|
|
5
|
+
return board; // No empty cells, return the board unchanged
|
|
6
|
+
}
|
|
7
|
+
const { row, col } = emptyCell;
|
|
8
|
+
const newTile = {
|
|
9
|
+
id: board.nextId,
|
|
10
|
+
value: rng() < 0.9 ? 2 : 4,
|
|
11
|
+
row: row,
|
|
12
|
+
col: col,
|
|
13
|
+
merged: false,
|
|
14
|
+
isNew: true
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
...board,
|
|
18
|
+
tiles: [...board.tiles, newTile],
|
|
19
|
+
nextId: board.nextId + 1
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function emptyCells(board) {
|
|
23
|
+
const empty = [];
|
|
24
|
+
for (let row = 0; row < board.size; row++) {
|
|
25
|
+
for (let col = 0; col < board.size; col++) {
|
|
26
|
+
if (!board.tiles.some(tile => tile.row === row && tile.col === col)) {
|
|
27
|
+
empty.push({ row, col });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return empty;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=spawn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spawn.js","sourceRoot":"","sources":["../../src/2048/spawn.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,KAAK,CAAC,KAAY,EAAE,GAAG,GAAiB,IAAI,CAAC,MAAM;IAC/D,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;IAEhC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,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
|
|
|
1
|
+
export type Direction = 'up' | 'down' | 'left' | 'right';
|
|
2
|
+
export interface Tile {
|
|
3
|
+
id: number;
|
|
4
|
+
value: number;
|
|
5
|
+
row: number;
|
|
6
|
+
col: number;
|
|
7
|
+
merged: boolean;
|
|
8
|
+
isNew: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface Board {
|
|
11
|
+
size: 4;
|
|
12
|
+
tiles: Tile[];
|
|
13
|
+
nextId: number;
|
|
14
|
+
}
|
|
15
|
+
export interface Position {
|
|
16
|
+
row: number;
|
|
17
|
+
col: number;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/2048/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAEzD,MAAM,WAAW,IAAI;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,KAAK;IACrB,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,QAAQ;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACZ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/2048/types.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { Direction, Tile, Board, Position } from "./2048/types.js";
|
|
2
|
+
export { createBoard } from "./2048/board.js";
|
|
3
|
+
export { move } from "./2048/move.js";
|
|
4
|
+
export { spawn } from "./2048/spawn.js";
|
|
5
|
+
export { score, hasMoves } from "./2048/rules.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peacestay/arcade-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Arcade core package for 2048 game logic",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"CHANGELOG.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"test": "vitest run"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [],
|
|
20
|
+
"author": "Zhiyuan Zhao",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^7.0.2",
|
|
24
|
+
"vitest": "^5.0.5"
|
|
25
|
+
}
|
|
26
|
+
}
|