MCPcopy
hub / github.com/autoNumeric/autoNumeric / update

Method update

src/AutoNumeric.js:1989–2034  ·  view source on GitHub ↗

* Method that updates the AutoNumeric settings, and immediately format the element accordingly. * The options passed as parameter(s) is either one or many objects that each contains some settings, i.e. : * { * digitGroupSeparator: ".", * decimalCharacter: ",", *

(...newOptions)

Source from the content-addressed store, hash-verified

1987 * @returns {AutoNumeric}
1988 */
1989 update(...newOptions) {
1990 if (Array.isArray(newOptions) && Array.isArray(newOptions[0])) {
1991 // Allows to pass a single array of options
1992 newOptions = newOptions[0];
1993 }
1994
1995 // Keep a copy of the original settings before changing them, in case they do not validate correctly, so we can switch back to them
1996 const originalSettings = AutoNumericHelper.cloneObject(this.settings); //TODO Check that the `styleRules` option is correctly cloned (due to depth cloning limitation)
1997
1998 // Store the current unformatted input value
1999 const numericString = this.rawValue;
2000
2001 // Generate a single option object with the settings from the latter overwriting those from the former
2002 let optionsToUse = {};
2003 if (AutoNumericHelper.isUndefinedOrNullOrEmpty(newOptions) || newOptions.length === 0) {
2004 optionsToUse = null;
2005 } else if (newOptions.length >= 1) {
2006 newOptions.forEach(optionObject => {
2007 if (this.constructor._isPreDefinedOptionValid(optionObject)) {
2008 // The option object is a predefined option name (i.e. 'euro')
2009 optionObject = this.constructor._getOptionObject(optionObject);
2010 }
2011
2012 Object.assign(optionsToUse, optionObject);
2013 });
2014 }
2015
2016 // Update the settings
2017 try {
2018 this._setSettings(optionsToUse, true);
2019 this._setWritePermissions(); // Update the read/write permissions
2020 this._updateEventListeners();
2021
2022 // Reformat the input value with the new settings
2023 // Note: we always `set`, even when `numericString` is the empty string '', since `emptyInputBehavior` (set to `always` or `zero`) can change how the empty input is formatted
2024 this.set(numericString);
2025 } catch (error) {
2026 // If the settings validation fails, then we switch back to the previous valid settings
2027 this._setSettings(originalSettings, true); // `_setSettings()` is used here instead of directly doing `this.settings = originalSettings;` since lots of side variables are calculated from the settings, and we need to get those back to their previous state. Note: `_setSettings()` is called in the 'update' mode in order to correctly set back the `originalDecimalPlacesRawValue` value.
2028 AutoNumericHelper.throwError(`Unable to update the settings, those are invalid: [${error}]`);
2029
2030 return this;
2031 }
2032
2033 return this;
2034 }
2035
2036 /**
2037 * Return the options object containing all the current autoNumeric settings in effect.

Callers 5

constructorMethod · 0.95
attachMethod · 0.80

Calls 10

_setSettingsMethod · 0.95
_setWritePermissionsMethod · 0.95
_updateEventListenersMethod · 0.95
setMethod · 0.95
isArrayMethod · 0.80
cloneObjectMethod · 0.80
_getOptionObjectMethod · 0.80
throwErrorMethod · 0.80

Tested by

no test coverage detected