MCPcopy
hub / github.com/autoNumeric/autoNumeric / _normalizeParts

Method _normalizeParts

src/AutoNumeric.js:8784–8823  ·  view source on GitHub ↗

* Strip parts from excess characters and leading zeros. * * @param {string} left * @param {string} right * @returns {[*,*,*]} * @private

(left, right)

Source from the content-addressed store, hash-verified

8782 * @private
8783 */
8784 _normalizeParts(left, right) {
8785 //TODO Refactor with `_getUnformattedLeftAndRightPartAroundTheSelection` which share a lot of similar code
8786 // If changing the sign and left is equal to the number zero - prevents stripping the leading zeros
8787 let stripZeros = true;
8788 if ((this.eventKey === AutoNumericEnum.keyName.Hyphen || this.eventKey === AutoNumericEnum.keyName.Minus) && Number(left) === 0) {
8789 stripZeros = false;
8790 }
8791
8792 if (this.isTrailingNegative &&
8793 AutoNumericHelper.isNegative(right, this.settings.negativeSignCharacter) &&
8794 !AutoNumericHelper.isNegative(left, this.settings.negativeSignCharacter)) {
8795 // Only set the negative sign if the value is negative
8796 left = `-${left}`;
8797 right = right.replace(this.settings.negativeSignCharacter, '');
8798 }
8799
8800 left = AutoNumeric._stripAllNonNumberCharactersExceptCustomDecimalChar(left, this.settings, stripZeros, this.isFocused);
8801 right = AutoNumeric._stripAllNonNumberCharactersExceptCustomDecimalChar(right, this.settings, false, this.isFocused);
8802
8803 // Prevents multiple leading zeros from being entered
8804 if (this.settings.leadingZero === AutoNumeric.options.leadingZero.deny &&
8805 (this.eventKey === AutoNumericEnum.keyName.num0 || this.eventKey === AutoNumericEnum.keyName.numpad0) &&
8806 Number(left) === 0 &&
8807 // If `right` is not empty and the first character is not `decimalCharacter`
8808 !AutoNumericHelper.contains(left, this.settings.decimalCharacter) && right !== '') {
8809 left = left.substring(0, left.length - 1);
8810 }
8811
8812 // Insert zero there is a leading dot
8813 let newValue = left + right;
8814 if (this.settings.decimalCharacter) {
8815 const m = newValue.match(new RegExp(`^${this.regex.aNegRegAutoStrip}\\${this.settings.decimalCharacter}`));
8816 if (m) {
8817 left = left.replace(m[1], m[1] + '0');
8818 newValue = left + right;
8819 }
8820 }
8821
8822 return [left, right, newValue];
8823 }
8824
8825 /**
8826 * Set the formatted element value as well as the `rawValue`.

Callers 1

_setValuePartsMethod · 0.95

Calls 3

isNegativeMethod · 0.80
containsMethod · 0.80

Tested by

no test coverage detected