Thomas worked for a company based in Germany which was looking to expand internationally. Once they started servicing other locales, things started to break. It didn't take long to track the problem down to a very "percise" numeric parser.

handleInput( value ){
   let value_ = value;
   if( value.substring( 0, 1 ) === '+' ){
      value_ = value.substring( 1 );
   }

   value_ = value_.split( '.' ).join( '' );

   if( this.usePercisionIfPercentage && value_.indexOf( ',' ) >= 0 ) {
      const parsedPreValue = value_.split( ',' )[ 0 ];
      const parsedCommaValue = parseInt( value_.split( ',' )[ 1 ], 10 ) < 10 ?
         parseInt( value_.split( ',' )[ 1 ], 10 ) * 10 : value_.split( ',' )[ 1 ].substring( 0, 2 );

      if( parsedCommaValue === 0 ) {
         value_ = parseInt( parsedPreValue, 10 );
      }
      else {
         const parsedValue = parseInt( parsedPreValue + parsedCommaValue, 10 );
         value_ = parseInt( parsedValue, 10 ) / 100;
      }
   }
   
   // do stuff with value_
}

We start by checking if the first character of our input value is a "+", and if it is, we strip it off, storing the result in value_. Then, we split on "."- the thousands separator in their locale- and join it all back together.

Then we attempt to parse the number, first by checking if this.usePercisionIfPercentage is true, and if the value_ contains a ","- our decimal separator.

If it does, we split the string, taking the whole numbered portion in one variable, and doing a song and dance to ensure we only grab two characters of the decimal version. The song and dance involves splitting the string multiple times, parsing it into an int multiple times, and a spare ternary for good measure.

Finally, we put the halves of the number back together… by adding them together, taking advantage of string munging to do it. We add parsedPreValue to parsedCommaValue which, because this is JavaScript and parsedPreValue is still a string (despite parsedCommaValue being an integer), we're concatenating, not adding. We concatenate the values together and divide by 100 to get the "percision" we want.

Notably: if usePercisionIfPercentage is set, and the input has a fractional portion, we end up populating value_ with an integer. But if that's not true, by the time we hit // do stuff with value_, it's still a string.

It wasn't a huge amount of effort for Thomas to strip this out and replace it with a call to a locale-aware number parser. It was much more effort to understand how this code happened in the first place.

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.