MCPcopy
hub / github.com/autoNumeric/autoNumeric / _formatValue

Method _formatValue

src/AutoNumeric.js:9288–9422  ·  view source on GitHub ↗

* Formatting of just processed value while keeping the cursor position * * @param {Event} e * @private

(e)

Source from the content-addressed store, hash-verified

9286 * @private
9287 */
9288 _formatValue(e) {
9289 //TODO Break apart and simplify this really long function
9290 const elementValue = AutoNumericHelper.getElementValue(this.domElement);
9291 let [left] = this._getUnformattedLeftAndRightPartAroundTheSelection();
9292
9293 // No grouping separator and no currency sign
9294 if ((this.settings.digitGroupSeparator === '' || (this.settings.digitGroupSeparator !== '' && !AutoNumericHelper.contains(elementValue, this.settings.digitGroupSeparator))) &&
9295 (this.settings.currencySymbol === '' || (this.settings.currencySymbol !== '' && !AutoNumericHelper.contains(elementValue, this.settings.currencySymbol)))) {
9296 let [subParts] = elementValue.split(this.settings.decimalCharacter);
9297 let negativeSign = '';
9298 if (AutoNumericHelper.isNegative(subParts, this.settings.negativeSignCharacter)) {
9299 negativeSign = this.settings.negativeSignCharacter;
9300 subParts = subParts.replace(this.settings.negativeSignCharacter, '');
9301 left = left.replace('-', ''); // Here we modify the unformatted value (with the 'normal' minus sign)
9302 }
9303
9304 // Strip leading zero on positive value if needed
9305 if (negativeSign === '' && subParts.length > this.settings.mIntPos && left.charAt(0) === '0') {
9306 left = left.slice(1);
9307 }
9308
9309 // Strip leading zero on negative value if needed
9310 if (negativeSign === this.settings.negativeSignCharacter && subParts.length > this.settings.mIntNeg && left.charAt(0) === '0') {
9311 left = left.slice(1);
9312 }
9313
9314 if (!this.isTrailingNegative) { // Only add the minus sign if it's needed on that side of the numbers
9315 left = `${negativeSign}${left}`;
9316 }
9317 }
9318
9319 const value = this.constructor._addGroupSeparators(elementValue, this.settings, this.isFocused, this.rawValue);
9320 let position = value.length;
9321 if (value) {
9322 // Prepare regexp which searches for cursor position from unformatted left part
9323 const leftAr = left.split('');
9324
9325 // Fixes caret position with trailing minus sign
9326 if ((this.settings.negativePositiveSignPlacement === AutoNumeric.options.negativePositiveSignPlacement.suffix ||
9327 (this.settings.negativePositiveSignPlacement !== AutoNumeric.options.negativePositiveSignPlacement.prefix && this.settings.currencySymbolPlacement === AutoNumeric.options.currencySymbolPlacement.suffix)) &&
9328 leftAr[0] === this.settings.negativeSignCharacter && !this.settings.isNegativeSignAllowed) {
9329 leftAr.shift(); // Remove the negative sign character
9330
9331 if ((this.eventKey === AutoNumericEnum.keyName.Backspace || this.eventKey === AutoNumericEnum.keyName.Delete) &&
9332 this.caretFix) {
9333 if ((this.settings.currencySymbolPlacement === AutoNumeric.options.currencySymbolPlacement.suffix && this.settings.negativePositiveSignPlacement === AutoNumeric.options.negativePositiveSignPlacement.left) ||
9334 (this.settings.currencySymbolPlacement === AutoNumeric.options.currencySymbolPlacement.prefix && this.settings.negativePositiveSignPlacement === AutoNumeric.options.negativePositiveSignPlacement.suffix)) {
9335 leftAr.push(this.settings.negativeSignCharacter);
9336 this.caretFix = e.type === 'keydown';
9337 }
9338
9339 if (this.settings.currencySymbolPlacement === AutoNumeric.options.currencySymbolPlacement.suffix &&
9340 this.settings.negativePositiveSignPlacement === AutoNumeric.options.negativePositiveSignPlacement.right) {
9341 const signParts = this.settings.currencySymbol.split('');
9342 const escapeChr = ['\\', '^', '$', '.', '|', '?', '*', '+', '(', ')', '['];
9343 const escapedParts = [];
9344 signParts.forEach((i, miniParts) => {
9345 miniParts = signParts[i];

Callers 3

_onKeydownMethod · 0.95
_onKeypressMethod · 0.95
_onKeyupMethod · 0.95

Calls 9

_setElementValueMethod · 0.95
_setCaretPositionMethod · 0.95
getElementValueMethod · 0.80
containsMethod · 0.80
isNegativeMethod · 0.80
_addGroupSeparatorsMethod · 0.80
isInArrayMethod · 0.80
isNegativeStrictMethod · 0.80

Tested by

no test coverage detected