autonumeric
Version:
autoNumeric is a standalone Javascript library that provides live *as-you-type* formatting for international numbers and currencies. It supports most international numeric formats and currencies including those used in Europe, Asia, and North and South Am
1,143 lines (1,023 loc) • 84.1 kB
TypeScript
/**
* Note that ES6 modules cannot directly export class objects.
* This file should be imported using the CommonJS-style:
* import AutoNumeric = require('autonumeric');
*
* Alternatively, if --allowSyntheticDefaultImports or
* --esModuleInterop is turned on, this file can also be
* imported as a default import:
* import AutoNumeric from 'autonumeric';
*
* Refer to the TypeScript documentation at
* https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
* to understand common workarounds for this limitation of ES6 modules.
*
* @packageDocumentation
*/
/**
* The class constructor function is the exported object from the file
*/
export = AutoNumeric;
import {
AutoNumericEventInitialized,
AutoNumericEventInvalidFormula,
AutoNumericEventRawValueModified,
AutoNumericEventFormatted,
AutoNumericEventValidFormula,
AutoNumericGlobal,
CallbackOptions,
GetValueCallback,
NameValuePair,
Options,
OptionsHandler,
OutputFormatOption,
PredefinedOptions
} from 'autonumeric';
declare global {
interface GlobalEventHandlersEventMap {
/** When an invalid value is corrected. */
"autoNumeric:correctedValue": CustomEvent<null>;
/** When the AutoNumeric element is initialized. */
"autoNumeric:initialized": CustomEvent<AutoNumericEventInitialized>;
/** When the user tries to validate an invalid math expression. */
"autoNumeric:invalidFormula": CustomEvent<AutoNumericEventInvalidFormula>;
/** When an invalid value is entered (ie. when the raw value is out of the min/max range). */
"autoNumeric:invalidValue": CustomEvent<null>;
/** When the `rawValue` is modified. */
"autoNumeric:rawValueModified": CustomEvent<AutoNumericEventRawValueModified>;
/** When all the formatting is done and the formatted string is modified. */
"autoNumeric:formatted": CustomEvent<AutoNumericEventFormatted>;
/** When the `minimumValue` is not respected. */
"autoNumeric:minExceeded": CustomEvent<null>;
/** When the `maximumValue` is not respected. */
"autoNumeric:maxExceeded": CustomEvent<null>;
/** When the user validates a valid math expression. */
"autoNumeric:validFormula": CustomEvent<AutoNumericEventValidFormula>;
}
}
declare class AutoNumeric {
/**
* Enables the AutoNumeric feature for the given element.
*
* The DOM element must be one of the allowed elements. Allowed elements are `<input>`, as well as some
* other text-containing elements such `<div>`. See the documentation for the full list.
*
* When not an `input` element, the element may have the `contenteditable` set. If it does, all
* entered values are formatted according to the given options. Otherwise, the formatted value is
* set once and no further edits are possible.
*
* @param element Either one of the allowed elements, or a CSS selector string for a single element.
* @param initialValue Initial value, when `null`, the value of the DOM element is used.
* @param options Settings for AutoNumeric.
*/
constructor(
element: string | HTMLElement,
initialValue?: string | number | null,
options?: CallbackOptions | string | (CallbackOptions | string)[] | null
);
/**
* Enables the AutoNumeric feature for the given element.
*
* The DOM element must be one of the allowed elements. Allowed elements are `<input>`, as well as some
* other text-containing elements such `<div>`. See the documentation for the full list.
*
* When not an `input` element, the element may have the `contenteditable` set. If it does, all
* entered values are formatted according to the given options. Otherwise, the formatted value is
* set once and no further edits are possible.
*
* @param element Either one of the allowed elements, or a CSS selector string for a single element.
* @param options Settings for AutoNumeric.
*/
constructor(
element: string | HTMLElement,
options: CallbackOptions | (CallbackOptions | string)[]
);
/**
* Enables the AutoNumeric feature for the given elements.
*
* The DOM element must be one of the allowed elements. Allowed elements are `<input>`, as well as some
* other text-containing elements such `<div>`. See the documentation for the full list.
*
* When not an `input` element, the element may have the `contenteditable` set. If it does, all
* entered values are formatted according to the given options. Otherwise, the formatted value is
* set once and no further edits are possible.
* @param elements A list of elements, which may be a CSS selector string.
* @param initialValue Initial value to set. Can be an array to set a different value for each element. When `null`, the
* value of the DOM element is used.
* @param options AutoNumeric options. Can be an array to use a different set of options for each element.
*/
static multiple(
elements: string | HTMLElement[] | { rootElement: HTMLElement; exclude?: HTMLInputElement[] },
initialValue?: number | string | (number | string | null)[] | null,
options?: CallbackOptions | string | (CallbackOptions | string)[] | null
): AutoNumeric[];
/**
* Enables the AutoNumeric feature for the given elements.
*
* The DOM element must be one of the allowed elements. Allowed elements are `<input>`, as well as some
* other text-containing elements such `<div>`. See the documentation for the full list.
*
* When not an `input` element, the element may have the `contenteditable` set. If it does, all
* entered values are formatted according to the given options. Otherwise, the formatted value is
* set once and no further edits are possible.
* @param elements A list of elements, which may be a CSS selector string.
* @param options AutoNumeric options. Can be an array to use a different set of options for each element.
*/
static multiple(
elements: string | HTMLElement[] | { rootElement: HTMLElement; exclude?: HTMLInputElement[] },
options: CallbackOptions | (CallbackOptions | string)[]
): AutoNumeric[];
/**
* Return true in the settings are valid
*/
static areSettingsValid(options: Options): boolean;
/**
* Format the given number with the given options. This returns the formatted value as a string.
* @param domElement An element with a value to format.
* @param options Options to use instead of the default options.
* @return The formatted string.
*/
static format(value: number | string | HTMLElement, ...options: Options[]): string;
/**
* Format the value of the DOM element with the given options and returns the formatted value as a string.
* @param domElement An element with a value to format.
* @param options Options to use instead of the default options.
* @return The formatted string.
*/
static formatAndSet(domElement: HTMLElement, options?: Options | null): string;
/**
* Return the AutoNumeric object that manages the given DOM element
*/
static getAutoNumericElement(domElement: HTMLElement): AutoNumeric;
/**
* Return the default AutoNumeric settings
*/
static getDefaultConfig(): Required<Options>;
/**
* Return all the predefined options in one object
*/
static getPredefinedOptions(): PredefinedOptions;
/**
* Returns the unformatted value following the `outputFormat` setting, from the given DOM element or query selector.
*
* See the non-static `getLocalized` method documentation for more details.
*
* @param value A string to localize, or a DOM element with a value to localize.
* @param forcedOutputFormat Override for the `outputFormat` option.
* @param callback Callback to invoke with the localized value.
* @returns The localized value.
*/
static getLocalized(value: string | HTMLElement, forcedOutputFormat?: OutputFormatOption | null, callback?: ((value: string | number) => void) | null): string | number;
/**
* Returns the unformatted value following the `outputFormat` setting, from the given DOM element or query selector.
*
* See the non-static `getLocalized` method documentation for more details.
*
* @param value A string to localize, or a DOM element with a value to localize.
* @param callback Callback to invoke with the localized value.
* @returns The localized value.
*/
static getLocalized(value: string | HTMLElement, callback: (value: string | number) => void): string | number;
/**
* Return true if the given DOM element has an AutoNumeric object that manages it.
*/
static isManagedByAutoNumeric(domElement: HTMLElement): boolean;
/**
* Unformats and localizes the given formatted string with the given options.
*
* This basically allows to get the localized value without first having to initialize an AutoNumeric object.
*
* The returned value may be either a string or a number, depending on the `outputFormat` option.
*
* @param value A string to unformat and localize, or an input element with a value to unformat and localize.
* @param options Optional options to use instead of the current options of this instance.
* @returns The localized value.
*/
static localize(value: number | string | HTMLElement, options?: Options | null): number | string;
/**
* Unformats and localizes the value of the given element with the given options, then sets the localized
* value on the element.
*
* This basically allows to set the localized value without first having to initialize an AutoNumeric object.
*
* The returned value may be either a string or a number, depending on the `outputFormat` option.
*
* @param value A string to unformat and localize, or an input element with a value to unformat and localize.
* @param options Optional options to use instead of the current options of this instance.
* @returns The localized value.
*/
static localizeAndSet(domElement: HTMLElement, options?: Options | null): number | string;
/**
* Merge the current options with the given list of options.
*
* If a `string` is given, then we try to get the related pre-defined option using that string as its name.
*
* When merging the options, the latest option overwrite any previously set. This allows to fine tune a pre-defined option for instance.
*
* @param options List of options to set.
* @returns The merged options.
*/
static mergeOptions(options: (Options | string)[]): Options;
/**
* Test if the given DOM element, or the element selected by the given selector string is already managed by AutoNumeric
* (if it has been initialized on the current page).
*
* @param domElementOrSelector The DOM element to test. A string is interpreted as a CSS selector that should return one element, if any.
* @returns Whether the element is managed by AutoNumeric.
*/
static test(domElement: HTMLElement | string): boolean;
/**
* Unformats the given formatted string with the given options. This returns a numeric string.
*
* It can also unformat the given DOM element value with the given options and returns the unformatted numeric string.
*
* Note: This does *not* update that element value.
*
* This basically allows to get the unformatted value without first having to initialize an AutoNumeric object.
*
* The returned value might be a string or a number, depending on the `outputFormat` option.
* @param value A number, or a string that represent a JavaScript number, or a DOM element with a value.
* @param options Optional to use instead of the default options.
* @returns The unformatted value.
*/
static unformat(value: string | number | HTMLElement, ...options: Options[]): number | string;
/**
* Unformats the given DOM element value, and set the resulting value back as the element value.
*
* The returned value might be a string or a number, depending on the `outputFormat` option.
* @param domElement DOM element with a value to unformat and set.
* @param options Optional to use instead of the default options.
* @returns The unformatted value.
*/
static unformatAndSet(value: HTMLElement, options?: Options | null): number | string;
/**
* Set the given value on the AutoNumeric object that manages the given DOM element, if any.
* @param element DOM element with a value to unformat and set. Can be a CSS selector string.
* @param newValue The new value to set. Can be `null` when `emptyInputBehavior` is set to `null`.
* @param options A settings object that will override the current settings. Note: the update is done only if the `newValue` is defined.
* @param saveChangeToHistory If set to `true`, then the change is recorded in the history table. Defaults to `true`.
* @returns The AutoNumeric instance of the given element, or `null` if no such instance was found.
*/
static set(element: HTMLElement | string, newValue: number | string | null, options?: CallbackOptions | null, saveChangeToHistory?: boolean): AutoNumeric | null;
/**
* Validate the given option object.
*
* If the options are valid, this function returns nothing, otherwise if the options are invalid, this function throws an error.
*
* This tests if the options are not conflicting and are well formatted.
*
* This function is lenient since it only tests the settings properties ; it ignores any other properties the options object could have.
*
* @param options Options to validate.
* @param shouldExtendDefaultOptions If `true`, then this function will extend the `userOptions` passed by the user, with the default options.
* @param originalOptions The user can pass the original options (and not the one that are generated from the default settings
* and the various usability corrections), in order to add compatibility and conflicts checks.
* @throws If the given options are not valid.
*/
static validate(options: Options, shouldExtendDefaultOptions?: boolean, originalOptions?: Options | null): void;
/**
* Returns the AutoNumeric version number (for debugging purpose).
*
* @returns The current AutoNumeric version.
*/
static version(): string;
/**
* Contains convenience methods to update individual options, and also allows the options to be reset.
*/
readonly options: OptionsHandler;
/**
* Whenever {@link init} is used to initialize other DOM elements, a shared local `init` list of those elements is stored in
* the AutoNumeric instances.
*
* This allows for neat things like modifying all those linked AutoNumeric elements globally, with only one call.
*
* Use this property to access the handler that provides methods to modify all linked elements.
*/
readonly global: AutoNumericGlobal;
/**
* Set the given element value, and format it immediately.
* Additionally, this `set()` method can accept options that will be merged into the current AutoNumeric element, taking precedence over any previous settings.
*
* @example anElement.set('12345.67') // Formats the value
* @example anElement.set(12345.67) // Formats the value
* @example anElement.set(12345.67, { decimalCharacter : ',' }) // Update the settings and formats the value in one go
* @example anElement.northAmerican().set('$12,345.67') // Set an already formatted value (this does not _exactly_ respect the currency symbol/negative placements, but only remove all non-numbers characters, according to the ones given in the settings)
* @example anElement.set(null) // Set the rawValue and element value to `null`
*
* @param newValue The value must be a Number, a numeric string or `null` (if `emptyInputBehavior` is set to `null`)
* @param options A settings object that will override the current settings. Note: the update is done only if the `newValue` is defined.
* @param saveChangeToHistory If set to `true`, then the change is recorded in the history table
* @returns This instance for chaining method calls.
*/
set(
newValue: number | string | null,
options?: CallbackOptions,
saveChangeToHistory?: boolean
): AutoNumeric;
/**
* Set the given value directly as the DOM element value, without formatting it beforehand.
*
* You can also set the value and update the setting in one go (the value will again not be formatted immediately).
* @param value New value to set.
* @param options New options to set.
* @returns This instance for chaining method calls.
*/
setUnformatted(value: number | string | null, options?: CallbackOptions): AutoNumeric;
// The get() function is deprecated and should not be used. Omitted from TS def for that reason.
/**
* Return the current formatted value of the AutoNumeric element as a string.
* @param callback Optional callback that, when given, is called with the value and the AutoNumeric instance.
* @returns The formatted value.
*/
getFormatted(
callback?: GetValueCallback<string> | null
): string;
/**
* Return the element unformatted value as a real JavaScript number.
* @param callback Optional callback that, when given, is called with the value and the AutoNumeric instance.
* @returns The number value.
*/
getNumber(
callback?: GetValueCallback<number | null> | null
): number | null;
/**
* Return the unformatted value as a string.
* This can also return `null` if `rawValue` is null.
* @param callback Optional callback that, when given, is called with the value and the AutoNumeric instance.
* @returns The numerical value.
*/
getNumericString(
callback?: GetValueCallback<string | null> | null
): string | null;
/**
* Returns the unformatted value, but following the `outputFormat` setting, which means the output can either be:
*
* - a string (that could or could not represent a number, ie. "12345,67-"), or
* - a plain number (if the setting `'number'` is used).
*
* By default the returned values are an ISO numeric string "1234.56" or "-1234.56" where the decimal character is a period.
*
* Check the `outputFormat` option definition for more details.
* @param forcedOutputFormat Override for the `outputFormat` option.
* @param callback Optional callback that, when given, is called with the value and the AutoNumeric instance.
* @returns The localized value.
*/
getLocalized(forcedOutputFormat?: OutputFormatOption | null, callback?: GetValueCallback<string | number> | null): string | number;
/**
* Returns the unformatted value, but following the `outputFormat` setting, which means the output can either be:
*
* - a string (that could or could not represent a number, ie. "12345,67-"), or
* - a plain number (if the setting `'number'` is used).
*
* By default the returned values are an ISO numeric string "1234.56" or "-1234.56" where the decimal character is a period.
*
* Check the `outputFormat` option definition for more details.
* @param callback Optional callback that, when given, is called with the value and the AutoNumeric instance.
* @returns The localized value.
*/
getLocalized(callback: GetValueCallback<string | number>): string | number;
/**
* Returns the options object containing all the current autoNumeric settings in effect.
* You can then directly access each option by using its name:
*
* > anElement.getSettings().optionNameAutoCompleted
* @returns The current settings for this instance.
*/
getSettings(): Required<Options>;
/**
* Force each element of the local AutoNumeric element list to reformat its value
* @returns This instance for chaining method calls.
*/
reformat(): AutoNumeric;
/**
* Remove the formatting and keep only the raw unformatted value (as a numericString) in each element of the local AutoNumeric element list
* @returns This instance for chaining method calls.
*/
unformat(): AutoNumeric;
/**
* Remove the formatting and keep only the localized unformatted value in the element, with the option to override the default outputFormat if needed
*
* @param forcedOutputFormat If set to something different from `null`, then this is used as an overriding outputFormat option
* @returns This instance for chaining method calls.
*/
unformatLocalized(forcedOutputFormat?: OutputFormatOption): AutoNumeric;
/**
* Return `true` if *all* the autoNumeric-managed elements are pristine, if their raw value hasn't changed.
*
* By default, this returns `true` if the raw unformatted value is still the same even if the formatted one has changed (due to a configuration update for instance).
*
* @param checkOnlyRawValue If set to `true`, the pristine value is done on the raw unformatted value, not the formatted one. If set to `false`, this also checks that the formatted value hasn't changed.
* @returns If this instance is pristine.
*/
isPristine(checkOnlyRawValue?: boolean): boolean;
/**
* Select the formatted element content, based on the `selectNumberOnly` option
*
* @returns This instance for chaining method calls.
*/
select(): AutoNumeric;
/**
* Select only the numbers in the formatted element content, leaving out the currency symbol, whatever the value of the `selectNumberOnly` option
*
* @returns This instance for chaining method calls.
*/
selectNumber(): AutoNumeric;
/**
* Select only the integer part in the formatted element content, whatever the value of `selectNumberOnly`
*
* @returns This instance for chaining method calls.
*/
selectInteger(): AutoNumeric;
/**
* Select only the decimal part in the formatted element content, whatever the value of `selectNumberOnly`
* Multiple cases are possible:
*
* - +1.234,57suffixText
* - € +1.234,57suffixText
* - +€ 1.234,57suffixText
* - € 1.234,57+suffixText
* - 1.234,57+ €suffixText
* - 1.234,57 €+suffixText
* - +1.234,57 €suffixText
* @returns This instance for chaining method calls.
*/
selectDecimal(): AutoNumeric;
/**
* Reset the element value either to the empty string '', or the currency sign, depending on the `emptyInputBehavior` option value.
* If you set the `forceClearAll` argument to `true`, then the `emptyInputBehavior` option is overridden
* and the whole input is cleared, including any currency sign.
*
* @param forceClearAll `true` to clear the the entire input, including the currency sign.
* @returns This instance for chaining method calls.
*/
clear(forceClearAll?: boolean): AutoNumeric;
/**
* Updates the AutoNumeric settings, and immediately format the element accordingly.
* @param options New options to set. When multiple options are specified, later options override previous options.
* @returns This instance for chaining method calls.
*/
update(...options: CallbackOptions[]): AutoNumeric;
/**
* Remove the autoNumeric data and event listeners from the element, but keep the element content intact.
*
* This also clears the value from sessionStorage (or cookie, depending on browser supports).
*
* Note: this does not remove the formatting.
*/
remove(): void;
/**
* Remove the autoNumeric data and event listeners from the element, and reset its value to the empty string.
* This also clears the value from sessionStorage (or cookie, depending on browser supports).
*/
wipe(): void;
/**
* Remove the autoNumeric data and event listeners from the element, and delete the DOM element altogether
*/
nuke(): void;
/**
* Return the DOM element reference of the autoNumeric-managed element. The exact type depends
* on the element that on which AutoNumeric was initialized - AutoNumeric supports input elements
* as well as other content editable elements such as div elements.
*/
node(): HTMLElement;
/**
* Return the DOM element reference of the parent node of the AutoNumeric managed element
*
* @returns The parent of the AutoNumeric element.
*/
parent(): HTMLElement;
/**
* Detach the current AutoNumeric element from the shared local `init` list.
*
* This means any changes made on that local shared list will not be transmitted to that element anymore.
*
* Note: The user can provide another AutoNumeric element, and detach this one instead of the current one.
*
* @param otherAnElement Element to detach.
* @returns This instance for chaining method calls.
*/
detach(otherAnElement?: AutoNumeric | null): AutoNumeric;
/**
* Attach the given AutoNumeric element to the shared local `init` list.
*
* When doing that, by default the DOM content is left untouched.
*
* The user can force a reformat with the new shared list options by passing a second argument to `true`.
*
* @param otherAnElement Element to attach.
* @param reFormat Whether to reformat the value after the element was attached. Defaults to `true`.
* @returns This instance for chaining method calls.
*/
attach(otherAnElement: AutoNumeric, reFormat?: boolean): AutoNumeric;
/**
* Use the current AutoNumeric element settings to initialize the DOM element(s) given as a parameter.
*
* Doing so will *link* the AutoNumeric elements together since they will share the same local AutoNumeric element list.
*
* (cf. prototype pattern : https://en.wikipedia.org/wiki/Prototype_pattern)
*
* You can `init` either a single DOM element (in that case an AutoNumeric object will be returned), or an array of DOM elements or a string that will be used as a CSS selector. In the latter cases, an array of AutoNumeric objects will then be returned (or an empty array if nothing gets selected by the CSS selector).
*
* Use case : Once you have an AutoNumeric element already setup correctly with the right options, you can use it as many times you want to initialize as many other DOM elements as needed.
*
* Note: this works only on elements that can be managed by autoNumeric.
*
* @param domElement A single element to initialize.
* @param attached If set to `false`, then the newly generated AutoNumeric element will not share the same local element list.
* @returns The initialized AutoNumeric instance.
*/
init(domElement: HTMLElement, attached?: boolean): AutoNumeric;
/**
* Use the current AutoNumeric element settings to initialize the DOM element(s) given as a parameter.
*
* Doing so will *link* the AutoNumeric elements together since they will share the same local AutoNumeric element list.
*
* (cf. prototype pattern : https://en.wikipedia.org/wiki/Prototype_pattern)
*
* You can `init` either a single DOM element (in that case an AutoNumeric object will be returned), or an array of DOM elements or a string that will be used as a CSS selector. In the latter cases, an array of AutoNumeric objects will then be returned (or an empty array if nothing gets selected by the CSS selector).
*
* Use case : Once you have an AutoNumeric element already setup correctly with the right options, you can use it as many times you want to initialize as many other DOM elements as needed.
*
* Note: this works only on elements that can be managed by autoNumeric.
*
* @param domElement A list of elements, or a string representing a CSS selector.
* @param attached If set to `false`, then the newly generated AutoNumeric element will not share the same local element list.
* @returns The initialized AutoNumeric instances.
*/
init(domElement: HTMLElement[] | string, attached?: boolean): AutoNumeric[];
/**
* Return a reference to the parent <form> element if it exists, otherwise return `null`.
*
* If the parent form element as already been found, this directly return a reference to it.
*
* However, you can force AutoNumeric to search again for its reference by passing `true` as a parameter to this method.
*
* This method updates the `this.parentForm` attribute.
*
* In either case, whenever a new parent form is set for the current AutoNumeric element, we make sure to update the anCount and anFormHandler attributes on both the old form and the new one (for instance in case the user moved the input elements with `appendChild()` since AutoNumeric cannot not detect that).
*
* @param forceSearch Whether to force a new search for the parent `<form>` element, discarding any previously found one. Defaults to `false`.
*
* @returns The form element containing this AutoNumeric element, if any.
*/
form(forceSearch?: boolean): HTMLFormElement | null;
/**
* Return a string in standard URL-encoded notation with the form input values being unformatted.
*
* This string can be used as a query for instance.
*
* @returns The formatted string.
*/
formNumericString(): string;
/**
* Return a string in standard URL-encoded notation with the form input values being formatted.
*
* @returns The formatted string.
*/
formFormatted(): string;
/**
* Return a string in standard URL-encoded notation with the form input values, with localized values.
*
* The default output format can be overridden by passing the option as a parameter.
*
* @param forcedOutputFormat If set to something different from `null`, then this is used as an override for the `outputFormat` option
* @returns The localized string.
*/
formLocalized(forcedOutputFormat?: OutputFormatOption | null): string;
/**
* Return an array containing an object for each form <input> element. The name of each pair is the name of the DOM elements.
* The value is is stringified numeric value of each AutoNumeric input.
* @returns The numerical values.
*/
formArrayNumericString(): NameValuePair<number | string>[];
/**
* Return an array containing an object for each form <input> element. The name of each pair is the name of the DOM elements.
* The value is is formatted value of each AutoNumeric input.
* @returns The formatted values.
*/
formArrayFormatted(): NameValuePair<string>[];
/**
* Return an array containing an object for each form <input> element. The name of each pair is the name of the DOM elements.
*
* The value is is localized value of each AutoNumeric input.
*
* Values might be a string or a number, depending on the `outputFormat` option.
*
* @param forcedOutputFormat If set to something different from `null`, then this is used as an override for the `outputFormat` option
* @returns The localized values.
*/
formArrayLocalized(forcedOutputFormat?: OutputFormatOption | null): NameValuePair<number | string>[];
/**
* Return an array containing an object for each form <input> element, stringified as a JSON string.
* The name of each pair is the name of the DOM elements. The value is is localized value of each AutoNumeric input.
* @returns The numerical values.
*/
formJsonNumericString(): string;
/**
* Return an array containing an object for each form <input> element, stringified as a JSON string.
* The name of each pair is the name of the DOM elements. The value is is formatted value of each AutoNumeric input.
* @returns The formatted values.
*/
formJsonFormatted(): string;
/**
* Return an array containing an object for each form <input> element, stringified as a JSON string.
* The name of each pair is the name of the DOM elements. The value is is localized value of each AutoNumeric input.
* @param forcedOutputFormat If set to something different from `null`, then this is used as an override for the `outputFormat` option.
* @returns The localized values.
*/
formJsonLocalized(forcedOutputFormat?: OutputFormatOption | null): string;
/**
* Unformat all the autoNumeric-managed elements that are a child of the parent <form> element of this DOM element, to numeric strings
*
* @returns This instance for chaining method calls.
*/
formUnformat(): AutoNumeric;
/**
* Unformat all the autoNumeric-managed elements that are a child of the parent `<form>` element of
* this DOM element, to localized strings
*
* @returns This instance for chaining method calls.
*/
formUnformatLocalized(): AutoNumeric;
/**
* Reformat all the autoNumeric-managed elements that are a child of the parent <form> element of this DOM element
*
* @returns This instance for chaining method calls.
*/
formReformat(): AutoNumeric;
/**
* Generate an array of numeric strings from the `<input>` elements, and pass it to the given callback.
* Under the hood, the array is generated via a call to `formArrayNumericString()`.
*
* @param callback Callback to invoke with the values.
* @returns This instance for chaining method calls.
*/
formSubmitArrayNumericString(callback: (pairs: NameValuePair<number | string>[]) => void): AutoNumeric;
/**
* Generate an array of the current formatted values from the `<input>` elements, and pass it to the given callback.
* Under the hood, the array is generated via a call to `formArrayFormatted()`.
*
* @param callback Callback to invoke with the values.
* @returns This instance for chaining method calls.
*/
formSubmitArrayFormatted(callback: (pairs: NameValuePair<string>[]) => void): AutoNumeric;
/**
* Generate an array of localized strings from the `<input>` elements, and pass it to the given callback.
*
* Under the hood, the array is generated via a call to `formArrayLocalized()`.
*
* Values might be a string or a number, depending on the `outputFormat` option.
* @param callback Callback to invoke with the values.
* @param forcedOutputFormat If set to something different from `null`, then this is used as an override for the `outputFormat` option.
* @returns This instance for chaining method calls.
*/
formSubmitArrayLocalized(callback: (pairs: NameValuePair<number | string>[]) => void, forcedOutputFormat?: OutputFormatOption | null): AutoNumeric;
/**
* Generate a JSON string with the numeric strings values from the `<input>` elements, and pass it to the given callback.
* Under the hood, the array is generated via a call to `formJsonNumericString()`.
*
* @param callback Callback to invoke with the values.
* @returns This instance for chaining method calls.
*/
formSubmitJsonNumericString(callback: (values: string) => void): AutoNumeric;
/**
* Generate a JSON string with the current formatted values from the `<input>` elements, and pass it to the given callback.
* Under the hood, the array is generated via a call to `formJsonFormatted()`.
*
* @param callback Callback to invoke with the values.
* @returns This instance for chaining method calls.
*/
formSubmitJsonFormatted(callback: (values: string) => void): AutoNumeric;
/**
* Generate a JSON string with the localized strings values from the `<input>` elements, and pass it to the given callback.
* Under the hood, the array is generated via a call to `formJsonLocalized()`.
*
* @param callback Callback to invoke with the values.
* @param forcedOutputFormat If set to something different from `null`, then this is used as an override for the `outputFormat` option.
* @returns This instance for chaining method calls.
*/
formSubmitJsonLocalized(callback: (values: string) => void, forcedOutputFormat?: null | string): AutoNumeric;
/**
* Update the settings to use the Brazilian pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
brazilian(optionOverride?: CallbackOptions | null): AutoNumeric;
/**
* Update the settings to use the British pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
british(optionOverride?: CallbackOptions | null): AutoNumeric;
/**
* Update the settings to use the Chinese pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
chinese(optionOverride?: CallbackOptions | null): AutoNumeric;
/**
* Update the settings to use the French pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
french(optionOverride?: CallbackOptions | null): AutoNumeric;
/**
* Update the settings to use the Japanese pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
japanese(optionOverride?: CallbackOptions | null): AutoNumeric;
/**
* Update the settings to use the North American pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
northAmerican(optionOverride?: CallbackOptions | null): AutoNumeric;
/**
* Update the settings to use the Spanish pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
spanish(optionOverride?: CallbackOptions | null): AutoNumeric;
/**
* Update the settings to use the Swiss pre-defined language options.
* Those pre-defined options can be overridden by passing an option object as a parameter.
*
* @param optionOverride Optional options to override the pre-defined options.
* @returns This instance for chaining method calls.
*/
swiss(optionOverride?: CallbackOptions | null): AutoNumeric;
}
/**
* Exposes the types used in that definitions file
*/
declare namespace AutoNumeric {
/**
* Callback that can optionally be passed to the various `get*` methods. Receives
* the value as well as the current AutoNumeric instance.
* @typeParam T Type of the value returned by the get method.
*/
export type GetValueCallback<T> = (value: T, instance: AutoNumeric) => void;
export type OutputFormatOption =
| "string"
| "number"
| "."
| "-."
| ","
| "-,"
| ".-"
| ",-"
| null;
export type CaretPositionOption =
| "start"
| "end"
| "decimalLeft"
| "decimalRight"
| "doNoForceCaretPosition";
export type CurrencySymbolPlacementOption = "p" | "s";
export type EmptyInputBehaviorOption =
| "null"
| "focus"
| "press"
| "always"
| "min"
| "max"
| "zero"
| number
| string /* representing a number */;
export type LeadingZeroOption = "allow" | "deny" | "keep";
export type NegativePositiveSignPlacementOption =
| "p"
| "s"
| "l"
| "r"
| null;
export type OnInvalidPasteOption =
| "error"
| "ignore"
| "clamp"
| "truncate"
| "replace";
export type OverrideMinMaxLimitsOption =
| "ceiling"
| "floor"
| "ignore"
| "invalid"
| null;
export type RoundingMethodOption =
| "S"
| "A"
| "s"
| "a"
| "B"
| "U"
| "D"
| "C"
| "F"
| "N05"
| "CHF"
| "U05"
| "D05";
export type DigitalGroupSpacingOption =
| "2"
| "2t"
| "2s"
| "3"
| "4"
export type NegativeBracketsTypeOnBlurOption =
| "(,)"
| "[,]"
| "<,>"
| "{,}"
| "〈,〉"
| "「,」"
| "⸤,⸥"
| "⟦,⟧"
| "‹,›"
| "«,»";
export type SerializeSpacesOption = "+" | "%20";
export type ValueOrCallback<T> = T | ((instance: AutoNumeric, key: string) => T);
export type OptionsHandler = {
[K in keyof Options]-?: (value: Required<Options[K]>) => AutoNumeric
} & {
/**
* Reset any options set previously, by overwriting them with the default settings
* @returns This AutoNumeric instance for chaining method calls.
*/
reset: () => AutoNumeric;
};
/**
* A pair with a name and a string value.
* @typeParam T Type of the value.
*/
export interface NameValuePair<T> {
/** The name of this pair. */
name: string;
/** The value of this pair. */
value: T;
}
export interface Options {
/**
* Allow padding the decimal places with zeros.
* @default true
*/
allowDecimalPadding?: boolean | number | string | "floats";
/**
* Determine where should be positioned the caret on focus
* @default null
*/
caretPositionOnFocus?: CaretPositionOption | null;
/**
* Defines if the decimal character or decimal character alternative should be accepted when there is already a
* decimal character shown in the element.
*
* If set to `true`, any decimal character input will be accepted and will subsequently modify the decimal character
* position, as well as the `rawValue`.
*
* If set to `false`, the decimal character and its alternative key will be dropped as before. This is the default setting.
* @default false
*/
alwaysAllowDecimalCharacter?: boolean;
/**
* Determine if a local list of AutoNumeric objects must be kept when initializing the elements and others
* @default true
*/
createLocalList?: boolean;
/**
* Currency symbol
* @default ''
*/
currencySymbol?: string;
/**
* Placement of the currency sign, relative to the number (as a prefix or a suffix)
* @default 'p'
*/
currencySymbolPlacement?: CurrencySymbolPlacementOption;
/**
* Decimal separator character
* @default '.'
*/
decimalCharacter?: string;
/**
* Allow to declare alternative decimal separator which is automatically replaced by the real decimal character
* @default null
*/
decimalCharacterAlternative?: string | null;
/**
* Defines the default number of decimal places to show on the formatted value, and to keep as the precision for the rawValue
* 0 or positive integer
* @default 2
*/
decimalPlaces?: number | string;
/**
* Defines how many decimal places should be kept for the raw value.
* @default null
*/
decimalPlacesRawValue?: number | string | null;
/**
* The number of decimal places to show when unfocused
* @default null
*/
decimalPlacesShownOnBlur?: number | string | null;
/**
* The number of decimal places to show when focused
* @default null
*/
decimalPlacesShownOnFocus?: number | string | null;
/**
* Helper option for ASP.NET postback
*
* This should be set as the value of the unformatted default value
*
* Examples:
* - no default value="" {defaultValueOverride: ""}
* - value=1234.56 {defaultValueOverride: '1234.56'}
* @default null
*/
defaultValueOverride?: string | { doNotOverride: null } | null;
/* Defines how many numbers should be grouped together (usually for the thousand separator)
* - `2`, results in 99,99,99,99 Group by two
* - `2t`, results in 99,99,99,999 India's lakhs
* - `2s`, results in 99,999,99,99,999 India's lakhs scaled
* - `3`, results in 999,999,999 (default)
* - `4`, results in 9999,9999,9999 used in some Asian countries
* Note: This option does not accept other grouping choice.
* @default '3'
*/
digitalGroupSpacing?: DigitalGroupSpacingOption;
/**
* Thousand separator character
* @default ','
*/
digitGroupSeparator?: string;
/**
* Define the number that will divide the current value shown when unfocused
* @default null
*/
divisorWhenUnfocused?: number | string | null;
/**
* Defines what should be displayed in the element if the raw value is an empty string.
* - `focus`: The currency sign is displayed when the input receives focus (default)
* - `press`: The currency sign is displayed whenever a key is being pressed
* - `always`: The currency sign is always displayed
* - `zero`: A zero is displayed (`rounded` with or without a currency sign) if the input has no value on focus out
* - `min`: The minimum value is displayed if the input has no value on focus out
* - `max`: The maximum value is displayed if the input has no value on focus out
* - `null`: When the element is empty, the `rawValue` and the element value/text is set to `null`. This also allows to set the value to `null` using `anElement.set(null)`.
*/
emptyInputBehavior?: EmptyInputBehaviorOption;
/**
* Defines if the custom and native events triggered by AutoNumeric should bubble up or not.
*/
eventBubbles?: boolean;
/**
* Defines if the custom and native events triggered by AutoNumeric should be cancelable.
* @default true
*/
eventIsCancelable?: boolean;
/**
* This option is the `strict mode` (aka `debug` mode), which allows autoNumeric to strictly analyze the options passed, and fails if an unknown options is used in the settings object.
*
* You should set that to `true` if you want to make sure you are only using pure autoNumeric settings objects in your code.
*
* If you see uncaught errors in the console and your code starts to fail, this means somehow those options gets polluted by another program (which usually happens when using frameworks).
*/
failOnUnknownOption?: boolean;
/**
* Determine if the default value will be formatted on initialization.
*/
formatOnPageLoad?: boolean;
/**
* Defines if the `formula mode` can be activated by the user.
*
* If set to `true`, then the user can enter the formula mode by entering the `=` character.
*
* He will then be allowed to enter any simple math formula using numeric characters as well as the following operators +, -, *, /, ( and ).
*
* The formula mode is closed when the user either validate their math expression using the `Enter` key, or when the element is blurred.
*
* If the formula is invalid, the previous valid `rawValue` is set back, and the `autoNumeric:invalidFormula` event is sent.
*
* When a valid formula is accepted, then its result is {@link AutoNumeric.prototype.set set}, and the `autoNumeric:validFormula` event is sent.
*
* By default, this mode is disabled.
*/
formulaMode?: boolean;
/**
* Set the undo/redo history table size.
*
* Each record keeps the ra