diff options
| author | Ruben Beltran del Rio <jj@r.bdr.sh> | 2025-12-12 15:22:14 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <jj@r.bdr.sh> | 2025-12-12 15:24:22 +0100 |
| commit | aee5dffd8013f6f6a6b77e5d00edd981bcfcad48 (patch) | |
| tree | 5139b74aed9b4ba49e57d688039b0eb4fab29392 | |
| parent | 68a999ed21907ef8f14c1aa0bf760d3c9b4b4c8c (diff) | |
Use component/s instead of vert(ex/ices) and circle as default shape
| -rw-r--r-- | src/index.js | 29 | ||||
| -rw-r--r-- | test/parser.test.js | 24 |
2 files changed, 29 insertions, 24 deletions
diff --git a/src/index.js b/src/index.js index 653de18..24a2c2e 100644 --- a/src/index.js +++ b/src/index.js @@ -10,12 +10,17 @@ */ /** + * Any of the potential shapes in a component. + * @typedef {"x"|"square"|"triangle"|"circle"} Shape + */ + +/** * A component in the map. * @typedef {Object} Component * @property {'component'} type - Entity type * @property {string} label - Component label * @property {[number, number]} coordinates - X and Y coordinates - * @property {string|null} shape - Shape type (x, square, triangle, circle) or null + * @property {Shape} shape - Shape of the component */ /** @@ -47,21 +52,21 @@ * A group of components. * @typedef {Object} Group * @property {'group'} type - Entity type - * @property {string[]} vertices - Array of component labels in the group + * @property {string[]} components - Array of component labels in the group */ /** * Inertia associated with a component. * @typedef {Object} Inertia * @property {'inertia'} type - Entity type - * @property {string} vertex - Component label with inertia + * @property {string} component - Component label with inertia */ /** * Evolution associated with a component. * @typedef {Object} Evolution * @property {'evolution'} type - Entity type - * @property {string} vertex - Component label + * @property {string} component - Component label * @property {boolean} isPositive - Whether evolution is positive (+) or negative (-) * @property {number} value - Evolution value */ @@ -199,18 +204,18 @@ function parseKeywordEntity(line, start, length) { // Group: [Group] label1, label2, ... if (keyword === "group") { - const vertices = line + const components = line .substring(i) .split(",") .map(v => v.trim()) .filter(v => v); - return vertices.length > 0 ? { type: "group", vertices } : null; + return components.length > 0 ? { type: "group", components } : null; } // Inertia: [Inertia] label if (keyword === "inertia") { - const vertex = line.substring(i).trim(); - return vertex ? { type: "inertia", vertex } : null; + const component = line.substring(i).trim(); + return component ? { type: "inertia", component } : null; } // Evolution: [Evolution] label +/- number @@ -232,8 +237,8 @@ function parseKeywordEntity(line, start, length) { if (indexOfSign === -1 || indexOfSign === i) return null; - const vertex = line.substring(i, indexOfSign).trim(); - if (!vertex) return null; + const component = line.substring(i, indexOfSign).trim(); + if (!component) return null; // Read number after sign let indexOfNumber = indexOfSign + 1; @@ -244,7 +249,7 @@ function parseKeywordEntity(line, start, length) { return { type: "evolution", - vertex, + component, isPositive: signCharacter === "+", value: parseFloat(line.substring(numStart, indexOfNumber)), }; @@ -334,7 +339,7 @@ function parseComponent(line, indexOfParenthesis, length) { i++; // Optional shape: [shape] - let shape = null; + let shape = "circle"; while (i < length && isWhitespace(line[i])) i++; if (i < length && line[i] === "[") { i++; diff --git a/test/parser.test.js b/test/parser.test.js index 055e756..975c6f0 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -12,7 +12,7 @@ describe("wmap parser", () => { assert.strictEqual(result.entities[0].type, "component"); assert.strictEqual(result.entities[0].label, "Tea"); assert.deepStrictEqual(result.entities[0].coordinates, [0.9, 0.5]); - assert.strictEqual(result.entities[0].shape, null); + assert.strictEqual(result.entities[0].shape, "circle"); }); it("should parse a component with shape", async () => { @@ -210,14 +210,14 @@ describe("wmap parser", () => { assert.strictEqual(result.entities.length, 1); assert.strictEqual(result.entities[0].type, "group"); - assert.deepStrictEqual(result.entities[0].vertices, ["Tea"]); + assert.deepStrictEqual(result.entities[0].components, ["Tea"]); }); it("should parse a group with multiple components", async () => { const source = "[Group] Tea, Cup, Kettle"; const result = await parse(source); - assert.deepStrictEqual(result.entities[0].vertices, [ + assert.deepStrictEqual(result.entities[0].components, [ "Tea", "Cup", "Kettle", @@ -235,7 +235,7 @@ describe("wmap parser", () => { const source = "[GROUP] MyTea, YourCup"; const result = await parse(source); - assert.deepStrictEqual(result.entities[0].vertices, [ + assert.deepStrictEqual(result.entities[0].components, [ "MyTea", "YourCup", ]); @@ -245,7 +245,7 @@ describe("wmap parser", () => { const source = "[Group] Tea , Cup , Kettle"; const result = await parse(source); - assert.deepStrictEqual(result.entities[0].vertices, [ + assert.deepStrictEqual(result.entities[0].components, [ "Tea", "Cup", "Kettle", @@ -260,7 +260,7 @@ describe("wmap parser", () => { assert.strictEqual(result.entities.length, 1); assert.strictEqual(result.entities[0].type, "inertia"); - assert.strictEqual(result.entities[0].vertex, "Tea"); + assert.strictEqual(result.entities[0].component, "Tea"); }); it("should handle case insensitive inertia keyword", async () => { @@ -270,11 +270,11 @@ describe("wmap parser", () => { assert.strictEqual(result.entities[0].type, "inertia"); }); - it("should preserve case in inertia vertex label", async () => { + it("should preserve case in inertia component label", async () => { const source = "[INERTIA] MyTea"; const result = await parse(source); - assert.strictEqual(result.entities[0].vertex, "MyTea"); + assert.strictEqual(result.entities[0].component, "MyTea"); }); }); @@ -285,7 +285,7 @@ describe("wmap parser", () => { assert.strictEqual(result.entities.length, 1); assert.strictEqual(result.entities[0].type, "evolution"); - assert.strictEqual(result.entities[0].vertex, "Tea"); + assert.strictEqual(result.entities[0].component, "Tea"); assert.strictEqual(result.entities[0].isPositive, true); assert.strictEqual(result.entities[0].value, 0.5); }); @@ -305,11 +305,11 @@ describe("wmap parser", () => { assert.strictEqual(result.entities[0].type, "evolution"); }); - it("should preserve case in evolution vertex label", async () => { + it("should preserve case in evolution component label", async () => { const source = "[EVOLUTION] MyTea - 0.2"; const result = await parse(source); - assert.strictEqual(result.entities[0].vertex, "MyTea"); + assert.strictEqual(result.entities[0].component, "MyTea"); }); it("should handle decimal evolution values", async () => { @@ -454,7 +454,7 @@ Kettle -> Hot Water assert.strictEqual(result.entities[11].type, "note"); assert.strictEqual(result.entities[12].type, "group"); - assert.strictEqual(result.entities[12].vertices.length, 3); + assert.strictEqual(result.entities[12].components.length, 3); assert.strictEqual(result.entities[13].type, "inertia"); |