* Format the given number (or numeric string) with the given options. This returns the formatted value as a string. * This can also format the given DOM element value with the given options and returns the formatted value as a string. * Note : This function does *not* update that element v
(numericStringOrDomElement, ...options)
| 4403 | * @returns {string|null} |
| 4404 | */ |
| 4405 | static format(numericStringOrDomElement, ...options) { |
| 4406 | if (AutoNumericHelper.isUndefined(numericStringOrDomElement) || numericStringOrDomElement === null) { |
| 4407 | return null; |
| 4408 | } |
| 4409 | |
| 4410 | // Retrieve the value to format |
| 4411 | let value; |
| 4412 | if (AutoNumericHelper.isElement(numericStringOrDomElement)) { |
| 4413 | value = AutoNumericHelper.getElementValue(numericStringOrDomElement); |
| 4414 | } else { |
| 4415 | value = numericStringOrDomElement; |
| 4416 | } |
| 4417 | |
| 4418 | if (!AutoNumericHelper.isString(value) && !AutoNumericHelper.isNumber(value)) { |
| 4419 | AutoNumericHelper.throwError(`The value "${value}" being "set" is not numeric and therefore cannot be used appropriately.`); |
| 4420 | } |
| 4421 | |
| 4422 | // Manage options |
| 4423 | const optionsToUse = this._generateOptionsObjectFromOptionsArray(options); |
| 4424 | |
| 4425 | // Initiate a very basic settings object |
| 4426 | const settings = Object.assign({}, this.getDefaultConfig(), optionsToUse); |
| 4427 | settings.isNegativeSignAllowed = value < 0; |
| 4428 | settings.isPositiveSignAllowed = value >= 0; |
| 4429 | this._setBrackets(settings); |
| 4430 | |
| 4431 | const regex = {}; |
| 4432 | this._cachesUsualRegularExpressions(settings, regex); // This is needed by `_stripAllNonNumberCharactersExceptCustomDecimalChar` that uses those regex |
| 4433 | |
| 4434 | // Check the validity of the `value` parameter |
| 4435 | // Convert the value to a numeric string, stripping unnecessary characters in the process |
| 4436 | let valueString = this._toNumericValue(value, settings); |
| 4437 | if (isNaN(Number(valueString))) { |
| 4438 | AutoNumericHelper.throwError(`The value [${valueString}] that you are trying to format is not a recognized number.`); |
| 4439 | } |
| 4440 | |
| 4441 | // Check if the given valueString is valid |
| 4442 | if (!this._isWithinRangeWithOverrideOption(valueString, settings)) { |
| 4443 | // Throw a custom event |
| 4444 | AutoNumericHelper.triggerEvent(AutoNumeric.events.formatted, document, { |
| 4445 | oldValue : null, |
| 4446 | newValue : null, |
| 4447 | oldRawValue: null, |
| 4448 | newRawValue: null, |
| 4449 | isPristine : null, |
| 4450 | error : 'Range test failed', |
| 4451 | aNElement : null, |
| 4452 | }, true, true); |
| 4453 | AutoNumericHelper.throwError(`The value [${valueString}] being set falls outside of the minimumValue [${settings.minimumValue}] and maximumValue [${settings.maximumValue}] range set for this element`); |
| 4454 | } |
| 4455 | |
| 4456 | // Directly format any `valuesToStrings` values, if found |
| 4457 | if (settings.valuesToStrings && this._checkValuesToStringsSettings(value, settings)) { |
| 4458 | return settings.valuesToStrings[value]; |
| 4459 | } |
| 4460 | |
| 4461 | // Generate the `negativePositiveSignPlacement` option as needed |
| 4462 | this._correctNegativePositiveSignPlacementOption(settings); |
no test coverage detected