File tree Expand file tree Collapse file tree 2 files changed +38
-4
lines changed
test/specs/utilities/parse Expand file tree Collapse file tree 2 files changed +38
-4
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments