@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
39 lines • 1.66 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const expressionRgx = /^(([A-Z]\w*:)?([A-Z]\w*|\[([A-Z]\w*:)?[A-Z]\w*\])(\(-?\d+\))?(\*(?!$)|$))+$/i;
const tokenRgx = /(?:(\[)?((?:[A-Z]\w*:)?[A-Z]\w*)\]?)(?:\((-?\d+)\))?/i;
const sp = "*";
/** @internal */
var Tokens;
(function (Tokens) {
Tokens[Tokens["Bracket"] = 1] = "Bracket";
Tokens[Tokens["Word"] = 2] = "Word";
Tokens[Tokens["Exponent"] = 3] = "Exponent";
})(Tokens || (Tokens = {}));
/** @internal */
export function parseDefinition(definition) {
const unitMap = new Map();
if (expressionRgx.test(definition)) {
for (const unit of definition.split(sp)) {
const tokens = unit.split(tokenRgx);
const name = tokens[Tokens.Word];
const exponent = tokens[Tokens.Exponent] ? Number(tokens[Tokens.Exponent]) : 1;
const constant = tokens[Tokens.Bracket] !== undefined;
if (unitMap.has(name)) {
const currentDefinition = unitMap.get(name);
currentDefinition.exponent += exponent;
unitMap.set(name, currentDefinition);
}
else {
unitMap.set(name, { name, exponent, constant });
}
}
return unitMap;
}
else {
throw new Error("Invalid definition expression.");
}
}
//# sourceMappingURL=Parser.js.map