Merge pull request #12882 from keymanapp/fix/common/web/types/1288-handle-invalid-order-and-tertiary-arguments-to-ElementString-fromString

fix(common/web): handle invalid order and tertiary arguments to ElementString.fromString()
This commit is contained in:
Dr Mark C. Sinclair 2025-01-18 15:58:55 +00:00 committed by GitHub
commit d5c3e43f20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 6 deletions

View file

@ -104,8 +104,8 @@ export class ElementString extends Array<ElemElement> {
typeFlag |= constants.elem_flags_type_str;
}
}
elem.order = orders.length ? parseInt(orders[i], 10) : 0;
elem.tertiary = tertiaries.length ? parseInt(tertiaries[i], 10) : 0;
elem.order = orders.length ? this.parseIntOrZero(orders[i]) : 0;
elem.tertiary = tertiaries.length ? this.parseIntOrZero(tertiaries[i]) : 0;
elem.flags = ElemElementFlags.none |
(ElemElementFlags.type & typeFlag) |
(tertiary_bases?.[i] == '1' /* TODO-LDML: or 'true'? */ ? ElemElementFlags.tertiary_base : 0) |
@ -125,5 +125,9 @@ export class ElementString extends Array<ElemElement> {
}
return true;
}
private static parseIntOrZero(str: string): number {
const num = parseInt(str, 10);
return !Number.isNaN(num) ? num : 0;
}
}
;

View file

@ -142,7 +142,7 @@ describe('Test of ElementString file', () => {
];
assert.deepEqual(actual, expected);
});
it.skip('can handle order string that is too short', () => {
it('can handle order string that is too short', () => {
sections.strs.allocString = stubStrsAllocString_Char;
const actual = ElementString.fromStrings(
sections,
@ -156,7 +156,7 @@ describe('Test of ElementString file', () => {
];
assert.deepEqual(actual, expected);
});
it.skip('can handle non-number in order string', () => {
it('can handle non-number in order string', () => {
sections.strs.allocString = stubStrsAllocString_Char;
const actual = ElementString.fromStrings(
sections,
@ -200,7 +200,7 @@ describe('Test of ElementString file', () => {
];
assert.deepEqual(actual, expected);
});
it.skip('can handle tertiary string that is too short', () => {
it('can handle tertiary string that is too short', () => {
sections.strs.allocString = stubStrsAllocString_Char;
const actual = ElementString.fromStrings(
sections,
@ -215,7 +215,7 @@ describe('Test of ElementString file', () => {
];
assert.deepEqual(actual, expected);
});
it.skip('can handle non-number in tertiary string', () => {
it('can handle non-number in tertiary string', () => {
sections.strs.allocString = stubStrsAllocString_Char;
const actual = ElementString.fromStrings(
sections,
@ -428,6 +428,24 @@ describe('Test of ElementString file', () => {
assert.isFalse(one.isEqual(two));
});
});
describe('Test of parseIntOrZero()', () => {
it('returns a number for a valid string', () => {
const num = ElementString['parseIntOrZero']('1');
assert.equal(num, 1);
});
it('returns zero for an invalid string', () => {
const num = ElementString['parseIntOrZero']('A');
assert.equal(num, 0);
});
it('returns zero for undefined', () => {
const num = ElementString['parseIntOrZero'](undefined);
assert.equal(num, 0);
});
it('returns zero for a null string', () => {
const num = ElementString['parseIntOrZero'](null);
assert.equal(num, 0);
});
});
});
});