MCPcopy
hub / github.com/autoNumeric/autoNumeric / _addGroupSeparators

Method _addGroupSeparators

src/AutoNumeric.js:5341–5436  ·  view source on GitHub ↗

* Modify the input value by adding the group separators, as defined in the settings, and the negative brackets if needed. * * @param {string} inputValue The formatted value (i.e. with the `decimalCharacter` defined in the settings, not the raw value) * @param {object} settings *

(inputValue, settings, isFocused, currentRawValue, forcedRawValue = null)

Source from the content-addressed store, hash-verified

5339 * @returns {*}
5340 */
5341 static _addGroupSeparators(inputValue, settings, isFocused, currentRawValue, forcedRawValue = null) {
5342 //TODO Test if `inputValue` === '', and return '' directly if that's the case,
5343 //XXX Note; this function is static since we need to pass a `settings` object when calling the static `AutoNumeric.format()` method
5344 let isValueNegative;
5345 if (!AutoNumericHelper.isNull(forcedRawValue)) {
5346 // Prefer directly testing if the raw value is negative in order for the test to be more performant than manipulating the formatted value
5347 isValueNegative = forcedRawValue < 0;
5348 } else {
5349 isValueNegative = AutoNumericHelper.isNegative(inputValue, settings.negativeSignCharacter) || AutoNumericHelper.isNegativeWithBrackets(inputValue, settings.firstBracket, settings.lastBracket); // Test if the value is negative before removing the negative sign
5350 }
5351
5352 inputValue = this._stripAllNonNumberCharactersExceptCustomDecimalChar(inputValue, settings, false, isFocused);
5353
5354 if (this._isElementValueEmptyOrOnlyTheNegativeSign(inputValue, settings)) {
5355 return this._orderValueCurrencySymbolAndSuffixText(inputValue, settings, true);
5356 }
5357
5358 const isZeroOrHasNoValue = AutoNumericHelper.isZeroOrHasNoValue(inputValue);
5359
5360 // Temporarily remove the negative sign if present
5361 if (isValueNegative) {
5362 inputValue = inputValue.replace('-', ''); // At this point the `inputValue` has been normalized with a 'normal' negative sign `'-'` //TODO Check that comment validity, since `_stripAllNonNumberCharactersExceptCustomDecimalChar` *does not* convert the negative sign
5363 }
5364
5365 // Splits the string at the decimal string
5366 let [integerPart, decimalPart] = inputValue.split(settings.decimalCharacter);
5367 if (settings.decimalCharacterAlternative && AutoNumericHelper.isUndefined(decimalPart)) {
5368 [integerPart, decimalPart] = inputValue.split(settings.decimalCharacterAlternative);
5369 }
5370
5371 if (settings.digitGroupSeparator !== '') {
5372 settings.digitalGroupSpacing = settings.digitalGroupSpacing.toString();
5373 let digitalGroup;
5374 switch (settings.digitalGroupSpacing) {
5375 case AutoNumeric.options.digitalGroupSpacing.twoThree:
5376 digitalGroup = /(\d)((\d)(\d{2}?)+)$/;
5377 break;
5378 case AutoNumeric.options.digitalGroupSpacing.twoScaled:
5379 digitalGroup = /(\d)((?:\d{2}){0,2}\d{3}(?:(?:\d{2}){2}\d{3})*?)$/;
5380 break;
5381 case AutoNumeric.options.digitalGroupSpacing.two:
5382 case AutoNumeric.options.digitalGroupSpacing.three:
5383 case AutoNumeric.options.digitalGroupSpacing.four:
5384 default :
5385 digitalGroup = new RegExp(`(\\d)((\\d{${settings.digitalGroupSpacing}}?)+)$`);
5386 }
5387
5388 // Re-inserts the thousand separator via a regular expression
5389 while (digitalGroup.test(integerPart)) {
5390 integerPart = integerPart.replace(digitalGroup, `$1${settings.digitGroupSeparator}$2`);
5391 }
5392 }
5393
5394 // Find out how many decimal places should be kept, depending on the object state (isFocused)
5395 let decimalPlacesToRoundTo;
5396 if (isFocused) {
5397 decimalPlacesToRoundTo = settings.decimalPlacesShownOnFocus;
5398 } else {

Callers 5

formatMethod · 0.95
setMethod · 0.80
_formatValueMethod · 0.80

Tested by

no test coverage detected