From f04c587899d64c05cef3b88f647ee322fae0341a Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Sun, 13 Apr 2025 10:20:33 +0100 Subject: [PATCH 1/3] Add some notes to chemical formula component --- webapp/src/components/ChemicalFormula.vue | 33 +++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/webapp/src/components/ChemicalFormula.vue b/webapp/src/components/ChemicalFormula.vue index 2760657fc..255db8d05 100644 --- a/webapp/src/components/ChemicalFormula.vue +++ b/webapp/src/components/ChemicalFormula.vue @@ -1,11 +1,6 @@ @@ -19,13 +14,29 @@ export default { }, computed: { chemFormulaFormat() { + // Need to capture several groups, if the overall format doesn't apply, then + // there should be no additional formatting whatsoever + // + // Some rules: + // + // * numbers between element symbols need to be subscripted, including "." and variables like "x" + // - e.g., Na3P => Na3P, Na3+xP => Na3+xP + // * charges need to be handled separately and superscripted + // - e.g., Na+Cl- => Na+Cl- + // * empirical labels for formula units like [pyr] must be left alone + // * dots, when not used within numbers, must be treated as an interpunct "dot product" style dot + // - e.g., Cu2SO4.H2O => Cu2SO4 · H2O + if (!this.formula) { + return this.formula; + } + // Optimistically subscript all numbers in formula if it matches + // regexp for element symbols, brackets and numbers only + const chemicalFormulaRegex = /[A-Z][a-z]{0,2}(?:\d*(?:\.\d+)?)?|[([{]|[)]}]/g; + if (this.formula.match(chemicalFormulaRegex)) { + // Do stuff + } + return this.formula; - //if (!this.formula) { - // return " "; - //} - //const re = /([A-Z][a-z]?)(\d+\.?\d*)?/g; - //var all_matches = [...this.formula.matchAll(re)]; - //return all_matches; }, }, }; From d75ae0d77535135312f2f387c1c4a58b501ec565 Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Sun, 13 Apr 2025 10:36:49 +0100 Subject: [PATCH 2/3] Add a couple more tests, and try to handle the simplest formulae at least --- .../component/ChemicalFormulaTest.cy.jsx | 25 +++++++++++++++---- webapp/src/components/ChemicalFormula.vue | 21 ++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/webapp/cypress/component/ChemicalFormulaTest.cy.jsx b/webapp/cypress/component/ChemicalFormulaTest.cy.jsx index 409ecc66c..4d4ebb08e 100644 --- a/webapp/cypress/component/ChemicalFormulaTest.cy.jsx +++ b/webapp/cypress/component/ChemicalFormulaTest.cy.jsx @@ -11,10 +11,10 @@ describe("ChemFormulaInput", () => { cy.get("input").should("have.value", "Na"); }); - // it("renders single element with subscript correctly", () => { - // cy.get("input").type("Na3"); - // cy.get("input").should("have.value", "Na3P"); - // }); + it("renders single element with subscript correctly", () => { + cy.get("input").type("Na3P"); + cy.get("input").should("have.value", "Na3P"); + }); // it("renders formula with parentheses correctly", () => { // cy.get("input").type("Na3P"); @@ -31,8 +31,23 @@ describe("ChemFormulaInput", () => { // cy.get("input").should("have.value", "Na3P4"); // }); + // it("renders formula with multiple elements and subscripts correctly", () => { + // cy.get("input").type("Cu2SO4.H2O"); + // cy.get("input").should("have.value", "Cu2SO4·H2O"); + // }); + + // it("handles empirical shorthand units correctly", () => { + // cy.get("input").type("[pyr]"); + // cy.get("input").should("have.value", "[pyr]"); + // }); + + // it("handles charges and subscripts", () => { + // cy.get("input").type("[pyr]"); + // cy.get("input").should("have.value", "[pyr]"); + // }); + // it("handles invalid input gracefully", () => { // cy.get("input").type("Invalid@Formula"); - // cy.get("input").should("have.value", "InFo"); + // cy.get("input").should("have.value", "Invalid@Formula"); // }); }); diff --git a/webapp/src/components/ChemicalFormula.vue b/webapp/src/components/ChemicalFormula.vue index 255db8d05..5995a5be5 100644 --- a/webapp/src/components/ChemicalFormula.vue +++ b/webapp/src/components/ChemicalFormula.vue @@ -1,7 +1,5 @@