Skip to content

Commit 892575a

Browse files
BryanBryan
authored andcommitted
Add legacy tests, correct case leading to "parseInt('123-')"
1 parent dd26157 commit 892575a

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/utilities/parse/price.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,27 @@ module.exports = (() => {
9191

9292
const fractionDigits = unitCodeItem.getFractionDigits(specialFractions);
9393

94-
let integerCharacters = value.substring(0, value.length - fractionDigits - fractionSeparator.length);
95-
let fractionCharacters = value.slice(-fractionDigits);
94+
let integerCharacters;
95+
let fractionCharacters;
9696

97-
if (integerCharacters === '') {
98-
integerCharacters = '0';
97+
if (fractionSeparator.length === 1) {
98+
const characterGroups = value.split(fractionSeparator);
99+
100+
integerCharacters = characterGroups[0];
101+
fractionCharacters = characterGroups[1];
102+
} else {
103+
integerCharacters = value.substring(0, value.length - fractionDigits - fractionSeparator.length);
104+
fractionCharacters = value.slice(-fractionDigits);
99105
}
100106

101107
if (fractionCharacters.length !== fractionDigits) {
102108
return Number.NaN;
103109
}
104110

111+
if (integerCharacters === '') {
112+
integerCharacters = '0';
113+
}
114+
105115
const integerPart = parseInt(integerCharacters);
106116
const fractionPart = parseInt(fractionCharacters);
107117

test/specs/utilities/parse/priceSpec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,28 @@ describe('when valid prices are parsed', () => {
456456
});
457457
});
458458
});
459+
460+
describe('with ad hoc settings from previous unit tests', () => {
461+
it('parses "125-5" as 125.625 (with unit code 2)', () => {
462+
expect(parsePrice('125-5', '2')).toEqual(125.625);
463+
});
464+
465+
it('parses "-125-5" as -125.625 (with unit code 2)', () => {
466+
expect(parsePrice('-125-5', '2')).toEqual(-125.625);
467+
});
468+
469+
it('parses "125-240" as 125.75 (with unit code 5, using special fractions)', () => {
470+
expect(parsePrice('125-240', '5', '-', true)).toEqual(125.75);
471+
});
472+
473+
it('parses "-125-240" as -125.75 (with unit code 5, using special fractions)', () => {
474+
expect(parsePrice('-125-240', '5', '-', true)).toEqual(-125.75);
475+
});
476+
});
477+
478+
describe('with insufficient data to infer correct settings', () => {
479+
it('parses "125-240" as Number.NaN (with unit code 5 where "special fractions" cannot be inferred)', () => {
480+
expect(parsePrice('125-240', '5')).toEqual(Number.NaN);
481+
});
482+
});
459483
});

0 commit comments

Comments
 (0)