@richardmcquiston01/gcode 0.0.0-stage → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Richard McQuiston
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,26 @@
1
- # Temporary Holding Version
1
+ # @richardmcquiston01/gcode
2
2
 
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.
3
+ G-code generation and parsing for laser, CNC and other maker machines: convert vector paths or raster images to G-code, and parse G-code back into moves with bounds.
4
+
5
+ Part of [maker-image-tools](https://github.com/RichardMcQuiston01/maker-image-tools).
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm i @richardmcquiston01/gcode
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { computeGcodeBounds, parseGcode, pathsToGcode } from "@richardmcquiston01/gcode";
17
+
18
+ const gcode: string = pathsToGcode(paths);
19
+ const bounds = computeGcodeBounds(parseGcode(gcode));
20
+ ```
21
+
22
+ ESM only.
23
+
24
+ ## License
25
+
26
+ MIT
@@ -0,0 +1,36 @@
1
+ import type { VectorPath } from "@richardmcquiston01/core-vector";
2
+ export interface GcodeGenerationOptions {
3
+ /** mm/min feed rate for cutting/engraving moves. Default 1000. */
4
+ feedRate?: number;
5
+ /** Reserved for future rapid-move tuning; G0 moves carry no F word today. */
6
+ travelFeedRate?: number;
7
+ /** Laser S value (0-1000) used for M4. Default 1000. */
8
+ power?: number;
9
+ /** Number of times to repeat each path before moving to the next. Default 1. */
10
+ passes?: number;
11
+ /** Tolerance passed to flattenPath when converting curves to line segments. Default 0.1. */
12
+ flattenTolerance?: number;
13
+ /** Added to every emitted X coordinate. Default 0. */
14
+ originX?: number;
15
+ /** Added to every emitted Y coordinate. Default 0. */
16
+ originY?: number;
17
+ }
18
+ export interface RasterGcodeOptions {
19
+ /** mm/min feed rate for engraving moves. Default 1000. */
20
+ feedRate?: number;
21
+ /** Laser S value at full black (darkest pixel). Default 1000. */
22
+ maxPower?: number;
23
+ /** Laser S value at full white that still fires. Default 0. */
24
+ minPower?: number;
25
+ /** Physical size of one pixel, in mm. Default 0.1. */
26
+ pixelSizeMm?: number;
27
+ /** Added to every emitted X coordinate. Default 0. */
28
+ originX?: number;
29
+ /** Added to every emitted Y coordinate. Default 0. */
30
+ originY?: number;
31
+ /** Grayscale value (0-255) at or above which a pixel is skipped entirely. Default 250. */
32
+ whiteThreshold?: number;
33
+ }
34
+ export declare function pathsToGcode(paths: VectorPath[], options?: GcodeGenerationOptions): string;
35
+ export declare function rasterToGcode(image: ImageData, options?: RasterGcodeOptions): string;
36
+ //# sourceMappingURL=generate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAGlE,MAAM,WAAW,sBAAsB;IACrC,kEAAkE;IAClE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6EAA6E;IAC7E,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4FAA4F;IAC5F,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,kBAAkB;IACjC,0DAA0D;IAC1D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,sDAAsD;IACtD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0FAA0F;IAC1F,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAaD,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,OAAO,GAAE,sBAA2B,GAAG,MAAM,CAoC9F;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAqFxF"}
@@ -0,0 +1,117 @@
1
+ import { flattenPath } from "@richardmcquiston01/core-vector";
2
+ const PREAMBLE = ["; Generated by maker-image-tools", "G21", "G90"];
3
+ const POSTAMBLE = ["G0 X0 Y0", "M5"];
4
+ /** Rounds to 3 decimal places and strips trailing zeros / a trailing decimal point. */
5
+ function formatCoord(n) {
6
+ let rounded = Math.round(n * 1000) / 1000;
7
+ if (rounded === 0)
8
+ rounded = 0; // normalize -0 to 0
9
+ const fixed = rounded.toFixed(3);
10
+ return fixed.replace(/\.?0+$/, "") || "0";
11
+ }
12
+ export function pathsToGcode(paths, options = {}) {
13
+ const feedRate = options.feedRate ?? 1000;
14
+ const power = Math.round(options.power ?? 1000);
15
+ const passes = options.passes ?? 1;
16
+ const flattenTolerance = options.flattenTolerance ?? 0.1;
17
+ const originX = options.originX ?? 0;
18
+ const originY = options.originY ?? 0;
19
+ const lines = [...PREAMBLE];
20
+ for (const path of paths) {
21
+ const points = flattenPath(path, flattenTolerance);
22
+ if (points.length < 2)
23
+ continue;
24
+ for (let pass = 0; pass < passes; pass++) {
25
+ const first = points[0];
26
+ lines.push(`G0 X${formatCoord(first.x + originX)} Y${formatCoord(first.y + originY)}`);
27
+ lines.push(`M4 S${power}`);
28
+ // F is modal in real G-code, so it's only emitted on this first G1 of the path-pass.
29
+ const second = points[1];
30
+ lines.push(`G1 X${formatCoord(second.x + originX)} Y${formatCoord(second.y + originY)} F${formatCoord(feedRate)}`);
31
+ for (let i = 2; i < points.length; i++) {
32
+ const p = points[i];
33
+ lines.push(`G1 X${formatCoord(p.x + originX)} Y${formatCoord(p.y + originY)}`);
34
+ }
35
+ lines.push("M5");
36
+ }
37
+ }
38
+ lines.push(...POSTAMBLE);
39
+ return lines.join("\n") + "\n";
40
+ }
41
+ export function rasterToGcode(image, options = {}) {
42
+ const feedRate = options.feedRate ?? 1000;
43
+ const maxPower = options.maxPower ?? 1000;
44
+ const minPower = options.minPower ?? 0;
45
+ const pixelSizeMm = options.pixelSizeMm ?? 0.1;
46
+ const originX = options.originX ?? 0;
47
+ const originY = options.originY ?? 0;
48
+ const whiteThreshold = options.whiteThreshold ?? 250;
49
+ const lines = [...PREAMBLE];
50
+ const { width, height, data } = image;
51
+ if (width > 0 && height > 0) {
52
+ let laserOn = false;
53
+ let currentPower = -1;
54
+ for (let y = 0; y < height; y++) {
55
+ // Row 0 (the image's top row) maps to the largest world Y.
56
+ const worldY = originY + (height - 1 - y) * pixelSizeMm;
57
+ const leftToRight = y % 2 === 0;
58
+ const cols = [];
59
+ if (leftToRight) {
60
+ for (let x = 0; x < width; x++)
61
+ cols.push(x);
62
+ }
63
+ else {
64
+ for (let x = width - 1; x >= 0; x--)
65
+ cols.push(x);
66
+ }
67
+ const powers = cols.map((x) => {
68
+ const idx = (y * width + x) * 4;
69
+ const r = data[idx] ?? 0;
70
+ const g = data[idx + 1] ?? 0;
71
+ const b = data[idx + 2] ?? 0;
72
+ const luma = 0.299 * r + 0.587 * g + 0.114 * b;
73
+ if (luma >= whiteThreshold)
74
+ return 0;
75
+ const raw = minPower + ((maxPower - minPower) * (255 - luma)) / 255;
76
+ return Math.round(Math.min(Math.max(raw, minPower), maxPower));
77
+ });
78
+ let i = 0;
79
+ let positioned = false;
80
+ while (i < cols.length) {
81
+ const runPower = powers[i];
82
+ let j = i;
83
+ while (j < cols.length && powers[j] === runPower)
84
+ j++;
85
+ if (runPower === 0) {
86
+ if (j < cols.length) {
87
+ const afterCol = cols[j];
88
+ const worldX = originX + afterCol * pixelSizeMm;
89
+ lines.push(`G0 X${formatCoord(worldX)} Y${formatCoord(worldY)}`);
90
+ }
91
+ }
92
+ else {
93
+ const firstCol = cols[i];
94
+ const lastCol = cols[j - 1];
95
+ if (!positioned) {
96
+ const worldXFirst = originX + firstCol * pixelSizeMm;
97
+ lines.push(`G0 X${formatCoord(worldXFirst)} Y${formatCoord(worldY)}`);
98
+ }
99
+ if (!laserOn || currentPower !== runPower) {
100
+ lines.push(`M4 S${runPower}`);
101
+ laserOn = true;
102
+ currentPower = runPower;
103
+ }
104
+ const worldXLast = originX + lastCol * pixelSizeMm;
105
+ lines.push(`G1 X${formatCoord(worldXLast)} Y${formatCoord(worldY)} F${formatCoord(feedRate)}`);
106
+ }
107
+ positioned = true;
108
+ i = j;
109
+ }
110
+ lines.push("M5");
111
+ laserOn = false;
112
+ }
113
+ }
114
+ lines.push(...POSTAMBLE);
115
+ return lines.join("\n") + "\n";
116
+ }
117
+ //# sourceMappingURL=generate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate.js","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAoC9D,MAAM,QAAQ,GAAG,CAAC,kCAAkC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AACpE,MAAM,SAAS,GAAG,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;AAErC,uFAAuF;AACvF,SAAS,WAAW,CAAC,CAAS;IAC5B,IAAI,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC1C,IAAI,OAAO,KAAK,CAAC;QAAE,OAAO,GAAG,CAAC,CAAC,CAAC,oBAAoB;IACpD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IACjC,OAAO,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,KAAmB,EAAE,UAAkC,EAAE;IACpF,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC;IAC1C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;IAChD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC,CAAC;IACnC,MAAM,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,GAAG,CAAC;IACzD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,CAAC,CAAC;IACrC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,CAAC,CAAC;IAErC,MAAM,KAAK,GAAa,CAAC,GAAG,QAAQ,CAA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CAAC,UAAU,CAAC,KAAK,WAAW,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC,QAAQ,CAAC,EAAE,CACnF,CAAC;gBACJ,CAAC;gBAED,UAAU,GAAG,IAAI,CAAC;gBAClB,CAAC,GAAG,CAAC,CAAC;YACR,CAAC;YAED,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACjB,OAAO,GAAG,KAAK,CAAC;QAClB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC;IACzB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AACjC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./generate.js";
2
+ export * from "./parse.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./generate.js";
2
+ export * from "./parse.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { BBox, Point } from "@richardmcquiston01/core-vector";
2
+ export interface GcodeMove {
3
+ type: "rapid" | "cut";
4
+ from: Point;
5
+ to: Point;
6
+ feedRate?: number;
7
+ power?: number;
8
+ laserOn: boolean;
9
+ }
10
+ export declare function parseGcode(gcodeText: string): GcodeMove[];
11
+ export declare function computeGcodeBounds(moves: GcodeMove[]): BBox;
12
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAC;AAGnE,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,OAAO,GAAG,KAAK,CAAC;IACtB,IAAI,EAAE,KAAK,CAAC;IACZ,EAAE,EAAE,KAAK,CAAC;IACV,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;CAClB;AAyCD,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,CAuDzD;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,IAAI,CAM3D"}
package/dist/parse.js ADDED
@@ -0,0 +1,91 @@
1
+ import { boundingBoxOfPoints } from "@richardmcquiston01/core-vector";
2
+ const WORD_RE = /^([A-Za-z])(-?\d+(?:\.\d+)?)$/;
3
+ /** Strip `;` line comments and `(...)` parenthetical comments, then trim. */
4
+ function stripComments(line) {
5
+ const withoutSemicolon = line.split(";")[0] ?? "";
6
+ return withoutSemicolon.replace(/\([^)]*\)/g, "").trim();
7
+ }
8
+ function tokenize(line) {
9
+ const words = [];
10
+ for (const token of line.split(/\s+/)) {
11
+ if (token.length === 0)
12
+ continue;
13
+ const match = WORD_RE.exec(token);
14
+ if (!match)
15
+ continue;
16
+ const letter = match[1]?.toUpperCase();
17
+ const value = Number(match[2]);
18
+ if (!letter || Number.isNaN(value))
19
+ continue;
20
+ words.push({ letter, value });
21
+ }
22
+ return words;
23
+ }
24
+ function findWord(words, letter) {
25
+ return words.find((w) => w.letter === letter);
26
+ }
27
+ function hasGCode(words, code) {
28
+ return words.some((w) => w.letter === "G" && w.value === code);
29
+ }
30
+ function hasMCode(words, code) {
31
+ return words.some((w) => w.letter === "M" && w.value === code);
32
+ }
33
+ export function parseGcode(gcodeText) {
34
+ const moves = [];
35
+ let currentX = 0;
36
+ let currentY = 0;
37
+ let currentFeedRate;
38
+ let currentPower;
39
+ let laserOn = false;
40
+ const lines = gcodeText.split(/\r\n|\n/);
41
+ for (const rawLine of lines) {
42
+ const line = stripComments(rawLine);
43
+ if (line.length === 0)
44
+ continue;
45
+ const words = tokenize(line);
46
+ if (words.length === 0)
47
+ continue;
48
+ const fWord = findWord(words, "F");
49
+ if (fWord)
50
+ currentFeedRate = fWord.value;
51
+ const sWord = findWord(words, "S");
52
+ if (sWord)
53
+ currentPower = sWord.value;
54
+ if (hasMCode(words, 3) || hasMCode(words, 4))
55
+ laserOn = true;
56
+ if (hasMCode(words, 5))
57
+ laserOn = false;
58
+ const isRapid = hasGCode(words, 0);
59
+ const isCut = hasGCode(words, 1) || hasGCode(words, 2) || hasGCode(words, 3);
60
+ // G2/G3 (arcs) are best-effort only: treated as a straight chord to the
61
+ // target X/Y, ignoring I/J/R — real arc interpolation is out of scope.
62
+ if (!isRapid && !isCut)
63
+ continue;
64
+ const xWord = findWord(words, "X");
65
+ const yWord = findWord(words, "Y");
66
+ const targetX = xWord ? xWord.value : currentX;
67
+ const targetY = yWord ? yWord.value : currentY;
68
+ if (targetX !== currentX || targetY !== currentY) {
69
+ const move = {
70
+ type: isRapid ? "rapid" : "cut",
71
+ from: { x: currentX, y: currentY },
72
+ to: { x: targetX, y: targetY },
73
+ laserOn,
74
+ ...(currentFeedRate !== undefined ? { feedRate: currentFeedRate } : {}),
75
+ ...(currentPower !== undefined ? { power: currentPower } : {}),
76
+ };
77
+ moves.push(move);
78
+ }
79
+ currentX = targetX;
80
+ currentY = targetY;
81
+ }
82
+ return moves;
83
+ }
84
+ export function computeGcodeBounds(moves) {
85
+ const points = [];
86
+ for (const move of moves) {
87
+ points.push(move.from, move.to);
88
+ }
89
+ return boundingBoxOfPoints(points);
90
+ }
91
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,i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,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAC/D,CAAC;YACF,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,CAAC;QAED,QAAQ,GAAG,OAAO,CAAC;QACnB,QAAQ,GAAG,OAAO,CAAC;IACrB,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,KAAkB;IACnD,MAAM,MAAM,GAAY,EAAE,CAAC;IAC3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@richardmcquiston01/gcode",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "G-code generation and parsing for laser/CNC/maker machines",
5
+ "keywords": [
6
+ "gcode",
7
+ "g-code",
8
+ "laser",
9
+ "cnc",
10
+ "engraving",
11
+ "grbl",
12
+ "maker",
13
+ "typescript"
14
+ ],
15
+ "license": "MIT",
16
+ "homepage": "https://github.com/RichardMcQuiston01/maker-image-tools#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/RichardMcQuiston01/maker-image-tools/issues"
19
+ },
20
+ "type": "module",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/RichardMcQuiston01/maker-image-tools.git",
24
+ "directory": "packages/gcode"
25
+ },
26
+ "main": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "README.md",
37
+ "LICENSE"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "scripts": {
43
+ "build": "tsc -p tsconfig.json",
44
+ "test": "vitest run",
45
+ "typecheck": "tsc -p tsconfig.json --noEmit"
46
+ },
47
+ "dependencies": {
48
+ "@richardmcquiston01/core-vector": "0.1.1"
49
+ },
50
+ "devDependencies": {
51
+ "jsdom": "^25.0.1",
52
+ "typescript": "^5.7.3",
53
+ "vitest": "^3.0.4"
54
+ }
55
+ }