MCPcopy
hub / github.com/autoNumeric/autoNumeric / _calculateDecimalPlacesOnUpdate

Method _calculateDecimalPlacesOnUpdate

src/AutoNumeric.js:8202–8267  ·  view source on GitHub ↗

* Recalculate the number de decimal places to be used by the AutoNumeric object, for each of its state, and for its formatted and raw value. * By default, the `rawValue` precision is the same as the formatted value one. * * This method is close to the one called during initialization,

(settings, currentSettings = null)

Source from the content-addressed store, hash-verified

8200 * @private
8201 */
8202 static _calculateDecimalPlacesOnUpdate(settings, currentSettings = null) {
8203 // Check the `decimalPlaces*` options and output any warnings as needed, before modifying those options
8204 this._validateDecimalPlacesRawValue(settings);
8205
8206 // Update phase
8207 if (AutoNumericHelper.isNull(currentSettings)) {
8208 AutoNumericHelper.throwError(`When updating the settings, the previous ones should be passed as an argument.`);
8209 }
8210
8211 const decimalPlacesInOptions = 'decimalPlaces' in settings;
8212 if (!(decimalPlacesInOptions ||
8213 'decimalPlacesRawValue' in settings ||
8214 'decimalPlacesShownOnFocus' in settings ||
8215 'decimalPlacesShownOnBlur' in settings ||
8216 'rawValueDivisor' in settings)) {
8217 // Do Nothing if no decimal places-related options are modified
8218 return;
8219 }
8220
8221 // Overwrite the `decimalPlaces*` values if the `decimalPlaces*` options are not set in the `settings`
8222 if (decimalPlacesInOptions) {
8223 if (!('decimalPlacesShownOnFocus' in settings) ||
8224 settings.decimalPlacesShownOnFocus === AutoNumeric.options.decimalPlacesShownOnFocus.useDefault) {
8225 settings.decimalPlacesShownOnFocus = settings.decimalPlaces;
8226 }
8227
8228 if (!('decimalPlacesShownOnBlur' in settings) ||
8229 settings.decimalPlacesShownOnBlur === AutoNumeric.options.decimalPlacesShownOnBlur.useDefault) {
8230 settings.decimalPlacesShownOnBlur = settings.decimalPlaces;
8231 }
8232
8233 if (!('decimalPlacesRawValue' in settings) ||
8234 settings.decimalPlacesRawValue === AutoNumeric.options.decimalPlacesRawValue.useDefault) {
8235 settings.decimalPlacesRawValue = settings.decimalPlaces;
8236 }
8237 } else {
8238 if (AutoNumericHelper.isUndefined(settings.decimalPlacesShownOnFocus)) {
8239 settings.decimalPlacesShownOnFocus = currentSettings.decimalPlacesShownOnFocus;
8240 }
8241
8242 if (AutoNumericHelper.isUndefined(settings.decimalPlacesShownOnBlur)) {
8243 settings.decimalPlacesShownOnBlur = currentSettings.decimalPlacesShownOnBlur;
8244 }
8245 }
8246
8247 // Add the additional decimal places to the raw value
8248 let additionalDecimalPlacesRawValue = 0;
8249 if (settings.rawValueDivisor && settings.rawValueDivisor !== AutoNumeric.options.rawValueDivisor.none) {
8250 additionalDecimalPlacesRawValue = String(settings.rawValueDivisor).length - 1; // i.e. Dividing by '100' adds 2 decimal places to the needed precision
8251 if (additionalDecimalPlacesRawValue < 0) {
8252 additionalDecimalPlacesRawValue = 0;
8253 }
8254 }
8255
8256 if (!settings.decimalPlaces && !settings.decimalPlacesRawValue) {
8257 settings.decimalPlacesRawValue = Math.max(
8258 Math.max(settings.decimalPlacesShownOnBlur, settings.decimalPlacesShownOnFocus) + additionalDecimalPlacesRawValue,
8259 Number(currentSettings.originalDecimalPlacesRawValue) + additionalDecimalPlacesRawValue,

Callers 1

_setSettingsMethod · 0.80

Calls 4

isNullMethod · 0.80
throwErrorMethod · 0.80
isUndefinedMethod · 0.80

Tested by

no test coverage detected