Skip to content

Commit

Permalink
allow syntax of units (m)^2
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed May 29, 2024
1 parent 95e2841 commit f679201
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Programming platform for Quantitative Systems Pharmacology modeling in NodeJS",
"main": "src/index.js",
"scripts": {
"test:dev": "mocha test/check-units/calc-unit --config=./test/.mocharc.json",
"test:dev": "mocha test/unit/parse-and-stringify --config=./test/.mocharc.json",
"test": "mocha test --config=./test/.mocharc.json",
"jsdoc": "jsdoc -r -c .jsdoc.json --readme api-references.md -d docs/dev src",
"test:cov": "nyc --reporter=lcov npm run test",
Expand Down
4 changes: 2 additions & 2 deletions src/core/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class Unit extends Array {
* @param {String} unitString - string of format 'mM^2*L/mg/h2' or (1e-2 mg)^3/L
* @return {Unit} A Unit object.
*/
static parse(unitString){
static parse(unitString) {
let unit = new Unit();

// create string from number
Expand All @@ -250,7 +250,7 @@ class Unit extends Array {
// checking "/xxx^12.23" or "1" or "/1"
let shortFormat = /^(\/|\*)?[_A-Za-z1][_A-Za-z0-9]*\^?(\d+(\.?\d*)?)?$/;
// checking "/(1e-2xxx)^12.23"
let longFormat = /^(\/|\*)?\(\d+(\.\d*)?([eE][+-]?\d+)?([_A-Za-z][_A-Za-z0-9]*)?\)\^?(\d+(\.?\d*)?)?$/;
let longFormat = /^(\/|\*)?\((\d+(\.\d*)?)?([eE][+-]?\d+)?([_A-Za-z][_A-Za-z0-9]*)?\)\^?(\d+(\.?\d*)?)?$/;

if (!shortFormat.test(item) && !longFormat.test(item))
throw new SyntaxError(`Wrong syntax of unit's item: "${unitString}"`);
Expand Down
2 changes: 2 additions & 0 deletions test/unit/parse-and-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const correctUnits = [
{str0: 'xxx_yyy', str: 'xxx_yyy', hash: '_xxx_yyy', tex: 'xxx\\_yyy', html: 'xxx_yyy'},
{str0: 'xxx_yyy*aaa_bbb^2', str: 'xxx_yyy*aaa_bbb^2', hash: '_xxx_yyy_aaa_bbb2', tex: 'xxx\\_yyy \\cdot aaa\\_bbb^{2}', html: 'xxx_yyy&times;aaa_bbb<sup>2</sup>'},
{str0: 'u12x', str: 'u12x', hash: '_u12x', tex: 'u12x', html: 'u12x'},
{str0: '(L)', str: 'L', hash: '_L', tex: 'L', html: 'L'},
{str0: '(metre)^2', str: 'metre^2', hash: '_metre2', tex: 'metre^{2}', html: 'metre<sup>2</sup>'},
];

describe('Testing correct units.', () => {
Expand Down

0 comments on commit f679201

Please sign in to comment.