Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handling comas and dots input in parseToken of Parser #7514

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-quantity",
"comment": "minor fix in parseToken",
"type": "none"
}
],
"packageName": "@itwin/core-quantity"
}
13 changes: 12 additions & 1 deletion core/quantity/src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class ParseToken {

public get isString(): boolean { return !this.isOperator && typeof this.value === "string"; }
public get isNumber(): boolean { return typeof this.value === "number"; }
public get isSpecialCharacter(): boolean {
const format = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+$/;
return this.isString && this.value.toString().match(format) !== null;
}
}

/** A ScientificToken holds an index and string representing the exponent.
Expand Down Expand Up @@ -352,7 +356,7 @@ export class Parser {

// handle case where end of input string is reached.
if (wipToken.length > 0) {
if (processingNumber) {
if (processingNumber && !isNaN(Number(wipToken))) {
if (signToken.length > 0) {
wipToken = signToken + wipToken;
}
Expand Down Expand Up @@ -582,6 +586,12 @@ export class Parser {
let sign: 1 | -1 = 1;

let compositeUnitIndex = 0;

// if every token is a special character then return error.
if(tokens.every((token) => token.isSpecialCharacter)){
return { ok: false, error: ParseError.NoValueOrUnitFoundInString };
}

for (let i = 0; i < tokens.length; i = i + increment) {
tokenPair = this.getNextTokenPair(i, tokens);
if(!tokenPair || tokenPair.length === 0){
Expand Down Expand Up @@ -634,6 +644,7 @@ export class Parser {
}
}
}

return { ok: true, value: mag };
}

Expand Down
64 changes: 63 additions & 1 deletion core/quantity/src/test/Parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,45 @@ describe("Parsing tests:", () => {
}
});

it("should return parseError when parsing only special characters", async () => {
const formatData = {
formatTraits: ["keepSingleZero", "applyRounding", "showUnitLabel"],
precision: 4,
type: "Decimal",
uomSeparator: "",
composite: {
units: [
{
label: "m",
name: "Units.M",
},
],
},
allowMathematicOperations: true,
};

const testData = [
".",
",",
",,",
"..",
",,,",
"...",
".,",
",.",
"!@#$%^&*()_", // special characters
];

const unitsProvider = new TestUnitsProvider();
const format = new Format("test");
await format.fromJSON(unitsProvider, formatData).catch(() => { });

for (const testEntry of testData) {
const parseResult = await Parser.parseIntoQuantity(testEntry, format, unitsProvider);
expect(parseResult.isValid).to.eql(false);
}
});

});

describe("Synchronous Parsing tests:", async () => {
Expand Down Expand Up @@ -927,4 +966,27 @@ describe("Synchronous Parsing tests:", async () => {
}
});

});
it("should return parseError when parsing only special characters", async () => {
const testData = [
".",
",",
",,",
"..",
",,,",
"...",
".,",
",.",
"!@#$%^&*()_",
];

for (const testEntry of testData) {
const parseResult = Parser.parseQuantityString(testEntry, parserSpec);
if (Parser.isParseError(parseResult)){
expect(parseResult.error).to.eql(ParseError.NoValueOrUnitFoundInString);
} else {
assert.fail(`Expected a ParseError with input: ${testEntry}`);
}
}
});

});
Loading