MCPcopy
hub / github.com/autoNumeric/autoNumeric / _processCharacterInsertion

Method _processCharacterInsertion

src/AutoNumeric.js:9184–9280  ·  view source on GitHub ↗

* Return `true` if the key is allowed. * This function decides if the key pressed should be dropped or accepted, and modify the value 'on-the-fly' accordingly. * //TODO This should use another function in order to separate the test and the modification * * @returns {boolean}

()

Source from the content-addressed store, hash-verified

9182 * @returns {boolean}
9183 */
9184 _processCharacterInsertion() {
9185 let [left, right] = this._getUnformattedLeftAndRightPartAroundTheSelection();
9186 if (this.eventKey !== AutoNumericEnum.keyName.AndroidDefault) {
9187 this.throwInput = true;
9188 }
9189
9190 // Start rules when the decimal character key is pressed always use numeric pad dot to insert decimal separator
9191 // Do not allow decimal character if no decimal part allowed
9192 if (this.eventKey === this.settings.decimalCharacter ||
9193 (this.settings.decimalCharacterAlternative && this.eventKey === this.settings.decimalCharacterAlternative)) {
9194 if (!this._isDecimalCharacterInsertionAllowed() || !this.settings.decimalCharacter) {
9195 return false;
9196 }
9197
9198 if (this.settings.alwaysAllowDecimalCharacter) {
9199 // Remove any previous decimal character
9200 left = left.replace(this.settings.decimalCharacter, '');
9201 right = right.replace(this.settings.decimalCharacter, '');
9202 } else {
9203 // Do not allow a decimal character if another decimal character is already present
9204 if (AutoNumericHelper.contains(left, this.settings.decimalCharacter)) {
9205 return true;
9206 }
9207
9208 // Prevent adding a decimal character at the far right of the number
9209 if (right.indexOf(this.settings.decimalCharacter) > 0) {
9210 return true;
9211 }
9212
9213 // Remove the decimal character if found on the far left of the right part
9214 if (right.indexOf(this.settings.decimalCharacter) === 0) {
9215 right = right.substring(1);
9216 }
9217 }
9218
9219 // If the user is trying to add a decimal character on the far left of the number, we allow it
9220 if (this.settings.negativeSignCharacter && AutoNumericHelper.contains(right, this.settings.negativeSignCharacter)) {
9221 // We need however to move the negative sign from the right to the left part
9222 left = `${this.settings.negativeSignCharacter}${left}`;
9223 right = right.replace(this.settings.negativeSignCharacter, '');
9224 }
9225
9226 this._setValueParts(left + this.settings.decimalCharacter, right);
9227
9228 return true;
9229 }
9230
9231 // Prevent entering the minus sign if it's not allowed (Note: `this.settings.isNegativeSignAllowed` is only set to `true` if the minimumValue is lower than zero, allowing negative numbers to be entered)
9232 if ((this.eventKey === '-' || this.eventKey === '+') && this.settings.isNegativeSignAllowed) {
9233 // Here, the left and right parts have been normalized already, hence the minus sign usage
9234 if (left === '' && AutoNumericHelper.contains(right, '-')) {
9235 // The value is originally negative (with a trailing negative sign)
9236 if (this.settings.negativePositiveSignBehavior || (!this.settings.negativePositiveSignBehavior && this.eventKey === '+')) {
9237 right = right.replace('-', '');
9238 }
9239 } else if (AutoNumericHelper.isNegativeStrict(left, '-')) {
9240 // The value is originally negative (with a leading negative sign)
9241 // Remove the negative sign, effectively converting the value to a positive one

Callers 1

_onKeypressMethod · 0.95

Tested by

no test coverage detected