numora 3.4.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/NumoraInput.d.ts +39 -6
- package/dist/config.d.ts +1 -0
- package/dist/features/decimals.d.ts +0 -4
- package/dist/features/formatting/caret-position-utils.d.ts +28 -34
- package/dist/features/formatting/cursor-position.d.ts +2 -2
- package/dist/features/formatting/dom-writes.d.ts +26 -0
- package/dist/features/formatting/index.d.ts +5 -4
- package/dist/features/formatting/thousand-grouping.d.ts +0 -11
- package/dist/features/fullwidth-digits.d.ts +13 -0
- package/dist/features/max-length.d.ts +8 -0
- package/dist/features/prepend-leading-zero.d.ts +6 -0
- package/dist/features/sanitization.d.ts +16 -29
- package/dist/index.d.ts +14 -5
- package/dist/index.js +1 -1
- package/dist/index.mjs +874 -818
- package/dist/types.d.ts +48 -1
- package/dist/utils/event-handlers.d.ts +49 -20
- package/dist/utils/regex-cache.d.ts +0 -27
- package/dist/utils/set-numora-input-value.d.ts +32 -0
- package/dist/validation.d.ts +3 -0
- package/package.json +2 -2
package/dist/NumoraInput.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FormatOn, ThousandStyle } from './types';
|
|
2
|
-
export interface NumoraInputOptions extends Partial<Omit<HTMLInputElement, 'value' | 'defaultValue' | 'onChange'>> {
|
|
2
|
+
export interface NumoraInputOptions extends Partial<Omit<HTMLInputElement, 'value' | 'defaultValue' | 'onChange' | 'maxLength'>> {
|
|
3
3
|
formatOn?: FormatOn;
|
|
4
4
|
thousandSeparator?: string;
|
|
5
5
|
thousandStyle?: ThousandStyle;
|
|
@@ -10,7 +10,12 @@ export interface NumoraInputOptions extends Partial<Omit<HTMLInputElement, 'valu
|
|
|
10
10
|
enableCompactNotation?: boolean;
|
|
11
11
|
enableNegative?: boolean;
|
|
12
12
|
enableLeadingZeros?: boolean;
|
|
13
|
+
autoAddLeadingZero?: boolean;
|
|
13
14
|
rawValueMode?: boolean;
|
|
15
|
+
/** Max raw length (digits + decimal sep + leading `-`). Separators not counted. */
|
|
16
|
+
maxLength?: number;
|
|
17
|
+
/** Reject keystroke/paste if this returns false. Called with post-sanitization raw value. */
|
|
18
|
+
isAllowed?: (rawValue: string) => boolean;
|
|
14
19
|
onChange?: (value: string) => void;
|
|
15
20
|
value?: string;
|
|
16
21
|
defaultValue?: string;
|
|
@@ -19,21 +24,43 @@ export declare class NumoraInput {
|
|
|
19
24
|
private element;
|
|
20
25
|
private resolvedOptions;
|
|
21
26
|
private rawValue;
|
|
27
|
+
private suppressNextInputEvent;
|
|
28
|
+
private pendingMouseStrip;
|
|
22
29
|
private caretPositionBeforeChange?;
|
|
23
|
-
constructor(container: HTMLElement,
|
|
30
|
+
constructor(container: HTMLElement, options: NumoraInputOptions);
|
|
31
|
+
private toRaw;
|
|
32
|
+
private setDefaultValue;
|
|
24
33
|
private createInputElement;
|
|
25
34
|
private setupEventListeners;
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Runs `fn` with `suppressNextInputEvent` set so the synchronous `input` event fired
|
|
37
|
+
* by `setRangeText` is short-circuited in `handleChange`. Exactly one set/clear pair
|
|
38
|
+
* per write; try/finally guarantees the flag clears even if `fn` throws.
|
|
39
|
+
*/
|
|
40
|
+
private withInternalWrite;
|
|
28
41
|
private handleValueChange;
|
|
29
|
-
private
|
|
42
|
+
private applyDisplaySeparators;
|
|
43
|
+
private handleBeforeInput;
|
|
30
44
|
private handleChange;
|
|
31
45
|
private handleKeyDown;
|
|
32
46
|
private handlePaste;
|
|
47
|
+
private stripSeparatorsAndMapCaret;
|
|
33
48
|
private handleFocus;
|
|
49
|
+
private handleMouseDown;
|
|
50
|
+
private handleClick;
|
|
34
51
|
private handleBlur;
|
|
35
52
|
getValue(): string;
|
|
36
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Sets the input's value programmatically.
|
|
55
|
+
*
|
|
56
|
+
* @param value - The new value. In `rawValueMode`, this is treated as raw and re-formatted for display.
|
|
57
|
+
* @param options.undoable - Defaults to `true`: routes the write through `setRangeText`
|
|
58
|
+
* so the browser's undo stack stays intact and `Ctrl+Z` can revert this call. Pass
|
|
59
|
+
* `false` only when you intentionally want to wipe the undo history (e.g. form reset).
|
|
60
|
+
*/
|
|
61
|
+
setValue(value: string, options?: {
|
|
62
|
+
undoable?: boolean;
|
|
63
|
+
}): void;
|
|
37
64
|
disable(): void;
|
|
38
65
|
enable(): void;
|
|
39
66
|
addEventListener(event: string, callback: EventListenerOrEventListenerObject): void;
|
|
@@ -58,6 +85,12 @@ export declare class NumoraInput {
|
|
|
58
85
|
/**
|
|
59
86
|
* Gets the value as a number, similar to HTMLInputElement.valueAsNumber.
|
|
60
87
|
* Returns NaN if the value cannot be converted to a number.
|
|
88
|
+
*
|
|
89
|
+
* **Precision warning**: this getter returns a JavaScript `number` (IEEE 754 double).
|
|
90
|
+
* Values beyond ~15 significant digits will lose precision. Numora is designed around
|
|
91
|
+
* string values for this reason - prefer {@link getValue} when exact precision matters.
|
|
92
|
+
* Treat `valueAsNumber` strictly as an escape hatch for arithmetic that you already know
|
|
93
|
+
* is safe at float precision.
|
|
61
94
|
*/
|
|
62
95
|
get valueAsNumber(): number;
|
|
63
96
|
/**
|
package/dist/config.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ export declare const DEFAULT_DECIMAL_SEPARATOR = ".";
|
|
|
8
8
|
export declare const DEFAULT_ENABLE_COMPACT_NOTATION = false;
|
|
9
9
|
export declare const DEFAULT_ENABLE_NEGATIVE = false;
|
|
10
10
|
export declare const DEFAULT_ENABLE_LEADING_ZEROS = false;
|
|
11
|
+
export declare const DEFAULT_AUTO_ADD_LEADING_ZERO = false;
|
|
11
12
|
export declare const DEFAULT_RAW_VALUE_MODE = false;
|
|
@@ -3,10 +3,6 @@ import type { SeparatorOptions, Separators, FormattingOptions } from '@/types';
|
|
|
3
3
|
* Normalizes separator configuration with defaults.
|
|
4
4
|
*/
|
|
5
5
|
export declare function getSeparators(options: SeparatorOptions | FormattingOptions | undefined): Separators;
|
|
6
|
-
/**
|
|
7
|
-
* Handles keyboard events for decimal separators, converting comma/dot and preventing duplicates.
|
|
8
|
-
*/
|
|
9
|
-
export declare function handleDecimalSeparatorKey(e: KeyboardEvent, inputElement: HTMLInputElement, decimalSeparator: string): boolean;
|
|
10
6
|
/**
|
|
11
7
|
* Trims decimals to a maximum length.
|
|
12
8
|
*/
|
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Pure caret-position math: where the caret should land after a value change, and
|
|
3
|
+
* helpers for the focus-strip path. DOM writes live in `./dom-writes.ts`.
|
|
4
4
|
*/
|
|
5
5
|
import type { FormattingOptions, CaretPositionInfo, Separators } from '@/types';
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* @param el - The input element
|
|
11
|
-
* @param caretPos - The desired caret position
|
|
12
|
-
* @returns True if successful, false otherwise
|
|
7
|
+
* Result of computing a focus-strip: the raw value with separators removed and the
|
|
8
|
+
* caret positions mapped from the formatted display indices to the raw indices.
|
|
13
9
|
*/
|
|
14
|
-
export
|
|
10
|
+
export interface StripSeparatorsResult {
|
|
11
|
+
raw: string;
|
|
12
|
+
rawStart: number;
|
|
13
|
+
rawEnd: number;
|
|
14
|
+
}
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* Pure compute for the focus-strip path: takes the current formatted value plus the
|
|
17
|
+
* user's display caret/selection, returns the stripped value and the equivalent caret
|
|
18
|
+
* positions in the raw value. Returns `null` when no strip is needed (the value already
|
|
19
|
+
* contains no separators).
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* @returns Timeout ID that can be cleared if needed
|
|
24
|
-
*/
|
|
25
|
-
export declare function setCaretPositionWithRetry(el: HTMLInputElement, caretPos: number, currentValue: string): ReturnType<typeof setTimeout> | null;
|
|
26
|
-
/**
|
|
27
|
-
* Gets the current caret position from an input element.
|
|
28
|
-
* Uses max of selectionStart and selectionEnd to handle mobile browser quirks.
|
|
21
|
+
* The mapping rule: the new caret index equals the count of non-separator characters
|
|
22
|
+
* in the formatted prefix up to the display caret. The transformation is bijective on
|
|
23
|
+
* digits, so this exactly preserves which digit the user clicked on.
|
|
29
24
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
25
|
+
* Does NOT mutate the DOM. Callers own the write and any internal-write/broadcast wrapping.
|
|
26
|
+
*
|
|
27
|
+
* @param currentValue - The current (formatted) input value
|
|
28
|
+
* @param displayStart - selectionStart in the formatted value
|
|
29
|
+
* @param displayEnd - selectionEnd in the formatted value
|
|
30
|
+
* @param separator - The thousand separator character to strip
|
|
31
|
+
* @returns Strip result, or null if no separators present
|
|
32
32
|
*/
|
|
33
|
-
export declare function
|
|
33
|
+
export declare function computeStripSeparatorsResult(currentValue: string, displayStart: number, displayEnd: number, separator: string): StripSeparatorsResult | null;
|
|
34
34
|
/**
|
|
35
35
|
* Skips cursor over thousand separator when deleting/backspacing in 'change' mode.
|
|
36
36
|
* This prevents the cursor from stopping on the separator, making deletion smoother.
|
|
@@ -41,14 +41,8 @@ export declare function getInputCaretPosition(el: HTMLInputElement): number;
|
|
|
41
41
|
*/
|
|
42
42
|
export declare function skipOverThousandSeparatorOnDelete(e: KeyboardEvent, inputElement: HTMLInputElement, formattingOptions?: FormattingOptions): void;
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* @param oldValue - The value before the change
|
|
48
|
-
* @param newValue - The value after the change
|
|
49
|
-
* @param oldCursorPosition - The cursor position before the change
|
|
50
|
-
* @param caretPositionBeforeChange - Optional caret position info from keydown handler
|
|
51
|
-
* @param separators - Separator configuration
|
|
52
|
-
* @param formattingOptions - Optional formatting options
|
|
44
|
+
* Pure compute version of cursor-position resolution. Returns the cursor position the
|
|
45
|
+
* caret should land at after a value change, or null when the inputs don't provide enough
|
|
46
|
+
* signal to compute one. Does NOT mutate the DOM. Callers own the setSelectionRange call.
|
|
53
47
|
*/
|
|
54
|
-
export declare function
|
|
48
|
+
export declare function computeCursorPosition(oldValue: string, newValue: string, oldCursorPosition: number, caretPositionBeforeChange: CaretPositionInfo | undefined, separators: Separators, formattingOptions?: FormattingOptions): number | null;
|
|
@@ -50,14 +50,13 @@ import { ThousandStyle } from '@/types';
|
|
|
50
50
|
* Type for character equivalence checking.
|
|
51
51
|
* Returns true if two characters should be considered equivalent for cursor mapping.
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
type IsCharacterEquivalent = (char1: string, char2: string, context: {
|
|
54
54
|
oldValue: string;
|
|
55
55
|
newValue: string;
|
|
56
56
|
typedRange?: ChangeRange;
|
|
57
57
|
oldIndex: number;
|
|
58
58
|
newIndex: number;
|
|
59
59
|
}) => boolean;
|
|
60
|
-
export declare const defaultIsCharacterEquivalent: IsCharacterEquivalent;
|
|
61
60
|
/**
|
|
62
61
|
* Options for cursor position calculation.
|
|
63
62
|
*/
|
|
@@ -91,3 +90,4 @@ export interface CursorPositionOptions {
|
|
|
91
90
|
* // Returns: 5 (cursor after last zero)
|
|
92
91
|
*/
|
|
93
92
|
export declare function calculateCursorPositionAfterFormatting(oldFormattedValue: string, newFormattedValue: string, oldCursorPosition: number, separator: string, _groupStyle: ThousandStyle, changeRange?: ChangeRange, decimalSeparator?: string, options?: CursorPositionOptions): number;
|
|
93
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOM write helpers that preserve the browser's undo/redo stack.
|
|
3
|
+
*
|
|
4
|
+
* Direct `element.value = x` wipes the undo history. `setRangeText` registers an undo
|
|
5
|
+
* step instead, so Ctrl+Z keeps working. Every value mutation in NumoraInput should
|
|
6
|
+
* route through one of these helpers (the public `setValue({ undoable: false })`
|
|
7
|
+
* escape hatch is the only intentional exception).
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Writes a new value to the input and sets the caret in one operation, preserving the
|
|
11
|
+
* undo stack. Skips the write if the value and caret are already what we want - that
|
|
12
|
+
* avoids adding a noisy no-op undo step.
|
|
13
|
+
*
|
|
14
|
+
* @param el - The input element
|
|
15
|
+
* @param newValue - The value to write
|
|
16
|
+
* @param cursorPos - The caret position to set after the write
|
|
17
|
+
*/
|
|
18
|
+
export declare function writeValuePreservingUndo(el: HTMLInputElement, newValue: string, cursorPos: number): void;
|
|
19
|
+
/**
|
|
20
|
+
* Writes a focus-strip result: replaces the formatted value with raw digits and maps
|
|
21
|
+
* the caret/selection in one shot. Uses `preserve` (not `end`) so setRangeText does not
|
|
22
|
+
* snap the caret to the end of the string before setSelectionRange runs - that snap is
|
|
23
|
+
* what causes the visible left-then-right flicker when external `input` listeners paint
|
|
24
|
+
* between the two calls.
|
|
25
|
+
*/
|
|
26
|
+
export declare function writeStripPreservingUndo(el: HTMLInputElement, raw: string, rawStart: number, rawEnd: number): void;
|
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
*/
|
|
9
9
|
export type { ChangeRange } from './constants';
|
|
10
10
|
export { GROUPING_CONFIG } from './constants';
|
|
11
|
-
export { formatWithSeparators
|
|
12
|
-
export { calculateCursorPositionAfterFormatting, type CursorPositionOptions,
|
|
11
|
+
export { formatWithSeparators } from './thousand-grouping';
|
|
12
|
+
export { calculateCursorPositionAfterFormatting, type CursorPositionOptions, } from './cursor-position';
|
|
13
13
|
export { findChangedRangeFromCaretPositions, findChangeRange } from './change-detection';
|
|
14
14
|
export { getCaretBoundary, getCaretPosInBoundary } from './cursor-boundary';
|
|
15
|
-
export {
|
|
16
|
-
export {
|
|
15
|
+
export { computeCursorPosition, computeStripSeparatorsResult, skipOverThousandSeparatorOnDelete, } from './caret-position-utils';
|
|
16
|
+
export type { StripSeparatorsResult } from './caret-position-utils';
|
|
17
|
+
export { writeValuePreservingUndo, writeStripPreservingUndo, } from './dom-writes';
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* Number formatting utilities with thousand separators.
|
|
3
3
|
* Supports multiple grouping styles: thousand (Western), lakh (Indian), wan (Chinese)
|
|
4
4
|
*/
|
|
5
|
-
import type { FormattingOptions, Separators } from '@/types';
|
|
6
5
|
import { ThousandStyle } from '@/types';
|
|
7
6
|
/**
|
|
8
7
|
* Formats a numeric string with thousand separators based on the specified group style.
|
|
@@ -22,13 +21,3 @@ import { ThousandStyle } from '@/types';
|
|
|
22
21
|
* formatWithSeparators("1234,56", ",", "thousand", false, ',') // "1,234,56"
|
|
23
22
|
*/
|
|
24
23
|
export declare function formatWithSeparators(value: string, separator: string, groupStyle?: ThousandStyle, enableLeadingZeros?: boolean, decimalSeparator?: string): string;
|
|
25
|
-
/**
|
|
26
|
-
* Applies formatting to the input element if formatting is enabled.
|
|
27
|
-
*
|
|
28
|
-
* @param target - The input element
|
|
29
|
-
* @param sanitizedAndTrimmedValue - The sanitized value to format
|
|
30
|
-
* @param formattingOptions - Optional formatting options
|
|
31
|
-
* @param separators - Optional separator configuration
|
|
32
|
-
* @returns The formatted value, or the original value if formatting is not needed
|
|
33
|
-
*/
|
|
34
|
-
export declare function formatNumoraInput(sanitizedAndTrimmedValue: string, formattingOptions?: FormattingOptions, separators?: Separators): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes fullwidth digits (U+FF10–U+FF19) to ASCII digits (0–9).
|
|
3
|
+
* Japanese and Chinese IME keyboards commonly input fullwidth digits
|
|
4
|
+
* that look identical to ASCII digits but are stripped by numeric filtering.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* @param value - Input value potentially containing fullwidth digits
|
|
8
|
+
* @returns Value with fullwidth digits converted to ASCII
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* normalizeFullWidthDigits("123") // Returns: "123"
|
|
12
|
+
*/
|
|
13
|
+
export declare function normalizeFullWidthDigits(value: string): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates the raw value to `maxLength` characters. Counts every character of the raw
|
|
3
|
+
* (post-sanitization, pre-format) string - digits, decimal separator, and a leading `-`.
|
|
4
|
+
*
|
|
5
|
+
* If truncation would leave a trailing decimal separator or a bare `-`, those are stripped
|
|
6
|
+
* so the result is never a malformed intermediate state purely from the truncation itself.
|
|
7
|
+
*/
|
|
8
|
+
export declare function truncateToMaxLength(value: string, maxLength: number, decimalSeparator?: string): string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prepends `0` before a bare leading decimal separator so `.5` → `0.5` (and `-.5` → `-0.5`).
|
|
3
|
+
* Intended to run inside the sanitization pipeline AFTER `removeLeadingZeros`, since stripping
|
|
4
|
+
* `0.5` → `.5` is exactly the case this reverses.
|
|
5
|
+
*/
|
|
6
|
+
export declare function prependLeadingZero(value: string, decimalSeparator?: string): string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FormattingOptions
|
|
1
|
+
import type { FormattingOptions } from '@/types';
|
|
2
2
|
/**
|
|
3
3
|
* Removes all occurrences of thousand separator from a string.
|
|
4
4
|
* Uses cached regex for performance optimization.
|
|
@@ -8,37 +8,24 @@ import type { FormattingOptions, Separators } from '@/types';
|
|
|
8
8
|
* @returns The string with all thousand separators removed
|
|
9
9
|
*/
|
|
10
10
|
export declare function removeThousandSeparators(value: string, thousandSeparator: string): string;
|
|
11
|
-
export interface SanitizationOptions {
|
|
12
|
-
enableCompactNotation?: boolean;
|
|
13
|
-
enableNegative?: boolean;
|
|
14
|
-
enableLeadingZeros?: boolean;
|
|
15
|
-
decimalSeparator?: string;
|
|
16
|
-
thousandSeparator?: string;
|
|
17
|
-
}
|
|
18
11
|
/**
|
|
19
12
|
* Sanitizes numeric input by:
|
|
20
13
|
* 0. Filter mobile keyboard artifacts (non-breaking spaces, Unicode whitespace)
|
|
21
|
-
* 1. Remove thousand separators (formatting, not data)
|
|
22
|
-
* 2.
|
|
23
|
-
* 3. Expanding
|
|
24
|
-
* 4.
|
|
25
|
-
* 5. Removing
|
|
26
|
-
* 6.
|
|
14
|
+
* 1. Remove thousand separators (formatting, not data) - only when `thousandSeparator` is set
|
|
15
|
+
* 2. Normalizing fullwidth digits (e.g., 123 → 123)
|
|
16
|
+
* 3. (Optional) Expanding compact notation (e.g., 1k → 1000)
|
|
17
|
+
* 4. Expanding scientific notation (e.g., 1.5e-5 → 0.000015)
|
|
18
|
+
* 5. Removing non-numeric characters
|
|
19
|
+
* 6. Removing extra decimal points
|
|
20
|
+
* 7. (Optional) Removing leading zeros
|
|
21
|
+
* 8. (Optional) Prepend `0` before a bare leading decimal separator (.5 → 0.5)
|
|
22
|
+
* 9. (Optional) Truncate raw to `maxLength` characters
|
|
27
23
|
*
|
|
28
|
-
* Note: Decimal separator conversion (comma ↔ dot) is handled in the
|
|
29
|
-
* (
|
|
24
|
+
* Note: Decimal separator conversion (comma ↔ dot) is handled in the beforeinput event
|
|
25
|
+
* (handleOnBeforeInputNumoraInput), not here, to avoid converting thousand separators.
|
|
30
26
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
27
|
+
* `formattingOptions.thousandSeparator` doubles as a flag: pass it to strip separators,
|
|
28
|
+
* leave it undefined to keep them. Callers that want to short-circuit removal (e.g.
|
|
29
|
+
* formatOn === 'blur' on the typing path) should set thousandSeparator to undefined.
|
|
34
30
|
*/
|
|
35
|
-
export declare const sanitizeNumoraInput: (value: string,
|
|
36
|
-
/**
|
|
37
|
-
* Builds sanitization options from formatting options and separators.
|
|
38
|
-
*
|
|
39
|
-
* @param formattingOptions - Optional formatting options
|
|
40
|
-
* @param separators - Separator configuration
|
|
41
|
-
* @param shouldRemoveThousandSeparators - Whether to remove thousand separators
|
|
42
|
-
* @returns Sanitization options
|
|
43
|
-
*/
|
|
44
|
-
export declare function buildSanitizationOptions(formattingOptions: FormattingOptions | undefined, separators: Separators, shouldRemoveThousandSeparators: boolean): SanitizationOptions;
|
|
31
|
+
export declare const sanitizeNumoraInput: (value: string, formattingOptions?: FormattingOptions) => string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
export * from './NumoraInput';
|
|
2
|
-
export { ThousandStyle, FormatOn } from './types';
|
|
2
|
+
export { ThousandStyle, FormatOn, InputType } from './types';
|
|
3
3
|
export { getSeparatorsFromLocale, applyLocale } from './utils/locale';
|
|
4
|
-
export {
|
|
5
|
-
export { formatValueForDisplay,
|
|
4
|
+
export { handleOnBeforeInputNumoraInput, handleOnPasteNumoraInput, handleOnKeyDownNumoraInput, } from './utils/event-handlers';
|
|
5
|
+
export { formatValueForDisplay, } from './utils/format-utils';
|
|
6
6
|
export type { FormattingOptions, CaretPositionInfo } from './types';
|
|
7
|
-
export { removeThousandSeparators } from './features/sanitization';
|
|
7
|
+
export { sanitizeNumoraInput, removeThousandSeparators } from './features/sanitization';
|
|
8
|
+
export { filterMobileKeyboardArtifacts } from './features/mobile-keyboard-filtering';
|
|
9
|
+
export { normalizeFullWidthDigits } from './features/fullwidth-digits';
|
|
10
|
+
export { expandCompactNotation } from './features/compact-notation';
|
|
11
|
+
export { expandScientificNotation } from './features/scientific-notation';
|
|
12
|
+
export { removeNonNumericCharacters } from './features/non-numeric-characters';
|
|
13
|
+
export { removeExtraDecimalSeparators } from './features/decimals';
|
|
14
|
+
export { removeLeadingZeros } from './features/leading-zeros';
|
|
15
|
+
export { prependLeadingZero } from './features/prepend-leading-zero';
|
|
16
|
+
export { truncateToMaxLength } from './features/max-length';
|
|
8
17
|
export { validateNumoraInputOptions } from './validation';
|
|
9
|
-
export {
|
|
18
|
+
export { writeValuePreservingUndo, writeStripPreservingUndo, computeStripSeparatorsResult, type StripSeparatorsResult, } from './features/formatting';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var le=Object.defineProperty;var he=(e,t,n)=>t in e?le(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var w=(e,t,n)=>he(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var N=(e=>(e.Blur="blur",e.Change="change",e))(N||{}),m=(e=>(e.None="none",e.Thousand="thousand",e.Lakh="lakh",e.Wan="wan",e))(m||{});const G=2,B=0,Z=N.Blur,ue=",",k=m.None,C=".",W=!1,U=!1,P=!1,z=!1;function y(e){return e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}const J=new Map;function O(e,t="g"){const n=`${e}:${t}`;let r=J.get(n);return r||(r=new RegExp(e,t),J.set(n,r)),r}function de(e,t="g"){const n=y(e);return O(n,t)}const fe=/[.,]/g;function _(e){return{decimalSeparator:(e==null?void 0:e.decimalSeparator)??C,thousandSeparator:e==null?void 0:e.thousandSeparator}}function Y(e,t){const n=e.startsWith("-"),r=n?e.slice(1):e,[i="",s=""]=r.split(t);return{sign:n?"-":"",integer:i,decimal:s}}function ge(e,t){if(!e.value.includes(t))return!1;const{selectionStart:n,selectionEnd:r,value:i}=e;return!i.slice(n??0,r??0).includes(t)}function Se(e,t,n){const{key:r}=e;if(r!==","&&r!==".")return!1;if(ge(t,n))return!0;if(r!==n){const{selectionStart:i,selectionEnd:s,value:a}=t,c=i??0,l=s??c;t.value=a.slice(0,c)+n+a.slice(l);const o=c+1;return t.setSelectionRange(o,o),!0}return!1}const Ee=(e,t,n=C)=>{const{sign:r,integer:i,decimal:s}=Y(e,n);if(!s)return e;const a=s.slice(0,t);return`${r}${i}${n}${a}`},pe=(e,t=C)=>{const n=e.indexOf(t);if(n===-1||n===e.length-1)return e;const r=e.slice(0,n+1),i=e.slice(n+1),a=t==="."||t===","?fe:O("[,\\."+y(t)+"]","g");return r+i.replace(a,"")},me=(e,t=0,n=C)=>{if(t<=0)return e;const{sign:r,integer:i,decimal:s}=Y(e,n);if(s.length>=t)return e;const a=s.padEnd(t,"0");return`${r}${i}${n}${a}`},D={thousand:{size:3},lakh:{firstGroup:3,restGroup:2},wan:{size:4}},be=/^(0+)/;function Q(e,t,n=m.Thousand,r=!1,i="."){if(!e||e==="0"||e===i||e==="-"||e===`-${i}`)return e;const s=e.includes(i),a=e.startsWith("-"),c=a?e.slice(1):e,[l,o]=c.split(i);if(!l){const u=o?`${i}${o}`:c;return a?`-${u}`:u}if(r&&l.startsWith("0")&&l.length>1){const u=l.match(be);if(u){const S=u[1],E=l.slice(S.length);if(E){const p=X(E,t,n),g=S+p,d=a?"-":"";return s?o?`${d}${g}${i}${o}`:`${d}${g}${i}`:`${d}${g}`}}}const h=X(l,t,n),f=a?"-":"";return s?o?`${f}${h}${i}${o}`:`${f}${h}${i}`:`${f}${h}`}function X(e,t,n){if(e==="0"||e==="")return e;switch(n){case m.None:return e;case m.Thousand:return j(e,t,D.thousand.size);case m.Lakh:return Ne(e,t);case m.Wan:return j(e,t,D.wan.size);default:return e}}function Ne(e,t){if(e.length<=D.lakh.firstGroup)return e;const n=[],r=e.length-D.lakh.firstGroup;n.unshift(e.slice(r));for(let i=r;i>0;i-=D.lakh.restGroup)n.unshift(e.slice(Math.max(0,i-D.lakh.restGroup),i));return n.join(t)}function j(e,t,n){const r=[];for(let i=e.length;i>0;i-=n)r.unshift(e.slice(Math.max(0,i-n),i));return r.join(t)}function Le(e,t,n){return(t==null?void 0:t.formatOn)===N.Change&&t.thousandSeparator?Q(e,t.thousandSeparator,t.ThousandStyle??m.None,t.enableLeadingZeros,(n==null?void 0:n.decimalSeparator)??C):e}function $(e,t,n){let r=0;for(let i=0;i<t&&i<e.length;i++)e[i]!==n&&r++;return r}function ve(e,t,n){if(t===0)return 0;let r=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(r===t-1)return i+1;r++}return e.length}function T(e,t,n){if(t===0)return 0;let r=0;for(let i=0;i<e.length;i++)if(e[i]!==n){if(r++,r===t)return i+1;if(r>t)return i}return e.length}function V(e,t,n){return t<0||t>=e.length?!1:e[t]===n}const $e=/\d/;function De(e,t={}){const{thousandSeparator:n,decimalSeparator:r=".",prefix:i="",suffix:s=""}=t,a=new Array(e.length+1).fill(!0);if(i&&a.fill(!1,0,i.length),s){const c=e.length-s.length;a.fill(!1,c+1,e.length+1)}for(let c=0;c<e.length;c++){const l=e[c];(n&&l===n||l===r)&&(a[c]=!1,c+1<e.length&&!$e.test(e[c+1])&&(a[c+1]=!1))}return a.some(c=>c)||a.fill(!0),a}function x(e,t,n,r){const i=e.length;if(t=Math.max(0,Math.min(t,i)),r==="left"){for(;t>=0&&!n[t];)t--;t===-1&&(t=n.indexOf(!0))}else if(r==="right"){for(;t<=i&&!n[t];)t++;t>i&&(t=n.lastIndexOf(!0))}else if(!n[t]){let s=t;for(;s<=i&&!n[s];)s++;let a=t;for(;a>=0&&!n[a];)a--;s<=i&&a>=0?t=t-a<s-t?a:s:s<=i?t=s:a>=0&&(t=a)}return(t===-1||t>i)&&(t=i),t}const K=/(\d+\.?\d*)\s*[kmbt]$/i,Ce=(e,t)=>e===t;function we(e,t,n,r,i,s,a=".",c={}){if(n<0)return 0;if(n>e.length||e===""||t===""||K.test(e)&&!K.test(t)&&t.length>e.length&&n>=e.length-1)return t.length;const l=t.length<e.length,o=V(e,n,r),h=e.indexOf(a),f=t.indexOf(a);if(c.isCharacterEquivalent&&e!==t){const g={thousandSeparator:r||void 0,...c},d=ye(e,t,n,c.isCharacterEquivalent||Ce,s,g);if(d!==void 0)return d}const S=new Map,E=(g,d,L)=>{const b=`${g===e?"o":"n"}:${d}`;return S.has(b)||S.set(b,$(g,d,r)),S.get(b)};if(l)return Ie(e,t,n,r,o,h,f,s,c,E);const p=Te(e,t,n,r,o,E);return c.boundary?x(t,p,c.boundary):p}function ye(e,t,n,r,i,s){const a=e.length,c=t.length;let l=0,o=0,h=c;for(let d=0;d<a;d++){let L=-1;for(let b=l;b<c;b++)if(r(e[d],t[b],{oldValue:e,newValue:t,typedRange:i,oldIndex:d,newIndex:b})){L=b;break}L!==-1&&(l=L+1,d<n?o=L+1:h===c&&/\d/.test(e[d])&&(h=L))}if(o>h)return h;const f=t.length<e.length||i!==void 0&&i.isDelete===!1;let u,S=!1,E=!1;if(f){u=o;const d=s.thousandSeparator,L=s.decimalSeparator??".",b=u<t.length?t[u]:"";d&&b===d?(n>0?e[n-1]:"")===d?(u=h,S=!0):E=!0:b===L&&(E=!0)}else u=n-o<h-n?o:h;const p=f&&!S?"left":"right";if(s.boundary)return E?u:x(t,u,s.boundary,p);const g=s.thousandSeparator;if(!E&&g&&u>=0&&u<t.length&&t[u]===g)if(f&&!S)for(;u>0&&t[u]===g;)u--;else for(;u<t.length&&t[u]===g;)u++;return u}function Ie(e,t,n,r,i,s,a,c,l={},o=$){if(i)return Me(e,t,n,r,a,o);const h=o(e,n,r),f=o(e,e.length,r),u=o(t,t.length,r),S=f-u,E=Ae(e,n,r,h,S,s,c,o),p=s===-1||n<=s,g=Re(t,E,r,S,c,p,a,o);return l.boundary?x(t,g,l.boundary):g}function Me(e,t,n,r,i,s=$){const a=n+1;if(a<e.length){const c=s(e,a,r),l=ve(t,c,r);return l<t.length&&t[l]!==r?l+1:l}return n}function Ae(e,t,n,r,i,s,a,c=$){if(a){const{start:o,isDelete:h}=a;return h?c(e,o,n):Math.max(0,c(e,o,n))}return t>0&&e[t-1]===n&&i>0?r+1:r}function q(e,t,n,r,i,s,a=$){if(t>0&&t<e.length&&a(e,t,r)===n){if(e[t]===r&&s&&i>0&&t<e.length-1)return t+1;if(!s&&i>0&&t<e.length-1)return Math.min(t+1,e.length)}return t}function Re(e,t,n,r,i,s,a,c=$){if(s&&a!==-1){const o=e.substring(0,a),h=c(o,o.length,n);if(t<=h){const f=T(o,t,n);return q(o,f,t,n,r,i,c)}}const l=T(e,t,n);return q(e,l,t,n,r,i,c)}function Te(e,t,n,r,i,s=$){const a=n>=e.length,c=s(e,n,r),l=s(e,e.length,r),o=s(t,t.length,r);if(a||c===l)return t.length;const h=o-l;let f=c;h>0&&!a&&c<l&&(f=c+1);const u=T(t,f,r);return i&&!V(t,u,r)?Math.max(0,u-1):u}function Oe(e,t,n){const{selectionStart:r,selectionEnd:i,endOffset:s=0}=e;if(r!==i){const h=i-r;return{start:r,end:i,deletedLength:h,isDelete:!1}}if(s>0){const h=s;return{start:r,end:r+h,deletedLength:h,isDelete:!0}}const c=t.length,l=n.length,o=c-l;if(!(o<=0))return{start:r,end:r+o,deletedLength:o,isDelete:!1}}function _e(e,t){if(e===t)return;let n=0;for(;n<e.length&&n<t.length&&e[n]===t[n];)n++;let r=e.length-1,i=t.length-1;for(;r>=n&&i>=n&&e[r]===t[i];)r--,i--;const s=r-n+1,a=i-n+1;if(!(s===0&&a===0))return{start:n,end:r+1,deletedLength:s,isDelete:s>a}}function H(e,t){if(e.value=e.value,e===null)return!1;if(e.createTextRange){const n=e.createTextRange();return n.move("character",t),n.select(),!0}return e.selectionStart!==null||e.selectionStart===0?(e.focus(),e.setSelectionRange(t,t),!0):(e.focus(),!1)}function xe(e,t,n){return e.selectionStart===0&&e.selectionEnd===e.value.length?null:(H(e,t),setTimeout(()=>{e.value===n&&e.selectionStart!==t&&H(e,t)},0))}function Ge(e){return Math.max(e.selectionStart,e.selectionEnd)}function Be(e,t,n){if((n==null?void 0:n.formatOn)!==N.Change||!n.thousandSeparator)return;const{selectionStart:r,selectionEnd:i,value:s}=t;if(r===null||i===null||r!==i)return;const{key:a}=e,c=n.thousandSeparator;a==="Backspace"&&r>0&&s[r-1]===c&&t.setSelectionRange(r-1,r-1),a==="Delete"&&s[r]===c&&t.setSelectionRange(r+1,r+1)}function Ze(e,t,n,r,i,s,a){if(!i)return;const{selectionStart:c=0,selectionEnd:l=0,endOffset:o=0}=i;let h=Oe({selectionStart:c,selectionEnd:l,endOffset:o},t,n);if(h||(h=_e(t,n)),!h)return;const f=De(n,{thousandSeparator:(a==null?void 0:a.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator}),u={thousandSeparator:(a==null?void 0:a.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator,isCharacterEquivalent:(g,d)=>{const L=(a==null?void 0:a.thousandSeparator)??s.thousandSeparator;return L&&g===L?!1:g===d},boundary:f},S=(a==null?void 0:a.thousandSeparator)??s.thousandSeparator??",",E=(a==null?void 0:a.ThousandStyle)??m.None,p=we(t,n,r,S,E,h,s.decimalSeparator,u);xe(e,p,n)}const ke=(e,t=!1,n=".")=>{const r=y(n),i=O(`[^0-9${r}]`,"g");if(!t)return e.replace(i,"");const s=e.startsWith("-"),a=e.replace(i,"");return s&&(a.length>0||e==="-")?"-"+a:a},We=/([+-]?\d+\.?\d*)[eE]([+-]?\d+)/g,F=/^0+$/,A=/\.?0+$/;function Ue(e){return!e.includes("e")&&!e.includes("E")?e:e.replace(We,(t,n,r)=>{const i=parseInt(r,10);if(i===0)return n;const s=n.startsWith("-"),a=s?n.slice(1):n,[c,l=""]=a.split("."),o=i>0?Pe(c,l,i):ze(c,l,Math.abs(i));return s?"-"+o:o})}function Pe(e,t,n){const r=e+t;if(r==="0"||F.test(r))return"0";const i=t.length;if(n<=i){const a=r.slice(0,e.length+n),c=r.slice(e.length+n);return c?`${a}.${c}`:a}const s=n-i;return r+"0".repeat(s)}function ze(e,t,n){const r=e+t;if(r==="0"||F.test(r))return"0";const s=e.length-n;if(s<=0){const a=Math.abs(s),c=`0.${"0".repeat(a)}${r}`;return R(c)}if(s<e.length){const a=r.slice(0,s),c=r.slice(s),l=`${a}.${c}`;return R(l)}return R(r)}function R(e){if(!e.includes("."))return e;if(e==="0.")return"0";if(e.startsWith("0.")){const t=e.replace(A,"");return t==="0"?"0":t||"0"}if(e.startsWith("-0.")){const t=e.replace(A,"");return t==="-0"||t==="0"?"0":t||"0"}return e.replace(A,"")||e}const Je=/(\d+\.?\d*)\s*([kmbt])/gi,Xe=/^0+/,je=/^(-?)0+([1-9])/,Ke=/^-?0+$/,qe=/\.?0+$/,He={k:3,m:6,b:9,t:12};function Ye(e){return e.replace(Je,(t,n,r)=>{const i=r.toLowerCase(),s=He[i];if(!s)return t;const[a,c=""]=n.split("."),l=a.replace(Xe,"")||"0";let o;if(c.length===0)o=l+"0".repeat(s);else if(c.length<=s){const h=s-c.length;o=l+c+"0".repeat(h)}else{const h=c.slice(0,s),f=c.slice(s);o=l+h+"."+f}return o=o.replace(je,"$1$2"),Ke.test(o)&&(o="0"),o.includes(".")&&(o=o.replace(qe,"")),o})}function Qe(e){if(!e||e==="0"||e==="-0"||e==="-"||e===".")return e;const t=e.startsWith("-"),n=t?e.slice(1):e;if(n===".")return e;if(n.includes(".")){const[i,s]=n.split(".");if(i){const c=(i.replace(/^0+/,"")||"0")+"."+s;return t?"-"+c:c}return e}if(n.startsWith("0")){const i=n.replace(/^0+/,"")||"0";return t?"-"+i:i}return e}function Ve(e){return e.replace(/[\s\u200B]/g,"")}function v(e,t){const n=de(t);return e.replace(n,"")}const Fe=(e,t)=>{let n=Ve(e);return t!=null&&t.thousandSeparator&&(n=v(n,t.thousandSeparator)),t!=null&&t.enableCompactNotation&&(n=Ye(n)),n=Ue(n),n=ke(n,t==null?void 0:t.enableNegative,t==null?void 0:t.decimalSeparator),n=pe(n,t==null?void 0:t.decimalSeparator),t!=null&&t.enableLeadingZeros||(n=Qe(n)),n};function et(e,t,n){return{enableCompactNotation:e==null?void 0:e.enableCompactNotation,enableNegative:e==null?void 0:e.enableNegative,enableLeadingZeros:e==null?void 0:e.enableLeadingZeros,decimalSeparator:t.decimalSeparator,thousandSeparator:n?t.thousandSeparator:void 0}}function M(e,t,n,r){const i=_(n),s=r??(n==null?void 0:n.formatOn)===N.Change,a=Fe(e,et(n,i,s)),c=Ee(a,t,i.decimalSeparator),l=(n==null?void 0:n.decimalMinLength)??0,o=me(c,l,i.decimalSeparator);return{formatted:Le(o,n,i),raw:o}}function tt(e,t,n){const r=!!(n!=null&&n.thousandSeparator);return M(e,t,n,r)}function nt(e,t,n){if(e==="Backspace"||e==="Delete")return e==="Delete"&&t===n?{endOffset:1}:{endOffset:0}}function ee(e,t){const{decimalSeparator:n}=_(t),r=e.target;if(Se(e,r,n)){e.preventDefault();return}return Be(e,r,t),nt(e.key,r.selectionStart,r.selectionEnd)}function te(e,t,n,r){const i=e.target,s=i.value,a=Ge(i),c=_(r),l=(r==null?void 0:r.formatOn)===N.Change,{formatted:o,raw:h}=M(s,t,r,l);return i.value=o,s!==o&&Ze(i,s,o,a,n,c,r),{formatted:o,raw:h}}function rt(e,t,n,r){const i=r-n;return e+t+i}function ne(e,t,n){var u;e.preventDefault();const r=e.target,{value:i,selectionStart:s,selectionEnd:a}=r,c=((u=e.clipboardData)==null?void 0:u.getData("text/plain"))||"",l=i.slice(0,s||0)+c+i.slice(a||0),{formatted:o,raw:h}=M(l,t,n,!0);r.value=o;const f=rt(s||0,c.length,l.length,o.length);return r.setSelectionRange(f,f),{formatted:o,raw:h}}function re(e,t){const n=y(e);return t?`^-?[0-9]*[${n}]?[0-9]*$`:`^[0-9]*[${n}]?[0-9]*$`}function ie(e){var t,n;try{const i=new Intl.NumberFormat(e).formatToParts(123456789e-2);return{decimalSeparator:((t=i.find(s=>s.type==="decimal"))==null?void 0:t.value)??".",thousandSeparator:((n=i.find(s=>s.type==="group"))==null?void 0:n.value)??","}}catch{return{thousandSeparator:",",decimalSeparator:"."}}}function se(e,t){if(!e)return t;const r=ie(e===!0?void 0:e);return{thousandSeparator:t.thousandSeparator??r.thousandSeparator,decimalSeparator:t.decimalSeparator??r.decimalSeparator}}function ae(e){it(e.decimalMaxLength),st(e.decimalMinLength),at(e.decimalMinLength,e.decimalMaxLength),ct(e.formatOn),ot(e.thousandSeparator),lt(e.thousandStyle),ht(e.decimalSeparator),ut(e.thousandSeparator,e.decimalSeparator),I("enableCompactNotation",e.enableCompactNotation),I("enableNegative",e.enableNegative),I("enableLeadingZeros",e.enableLeadingZeros),I("rawValueMode",e.rawValueMode),dt(e.onChange)}function it(e){if(e!==void 0){if(typeof e!="number")throw new Error(`decimalMaxLength must be a number. Received: ${typeof e} (${JSON.stringify(e)})`);if(!Number.isInteger(e))throw new Error(`decimalMaxLength must be an integer. Received: ${e}`);if(e<0)throw new Error(`decimalMaxLength must be non-negative. Received: ${e}`)}}function st(e){if(e!==void 0){if(typeof e!="number")throw new Error(`decimalMinLength must be a number. Received: ${typeof e} (${JSON.stringify(e)})`);if(!Number.isInteger(e))throw new Error(`decimalMinLength must be an integer. Received: ${e}`);if(e<0)throw new Error(`decimalMinLength must be non-negative. Received: ${e}`)}}function at(e,t){if(!(e===void 0||t===void 0)&&e>t)throw new Error(`decimalMinLength (${e}) cannot be greater than decimalMaxLength (${t}).`)}function ct(e){if(e!==void 0&&e!==N.Blur&&e!==N.Change)throw new Error(`formatOn must be either ${N.Blur} or ${N.Change}. Received: ${JSON.stringify(e)}`)}function ot(e){if(e!==void 0){if(typeof e!="string")throw new Error(`thousandSeparator must be a string. Received: ${typeof e} (${JSON.stringify(e)})`);if(e.length===0)throw new Error(`thousandSeparator cannot be empty. Received: ${JSON.stringify(e)}`);if(e.length>1)throw new Error(`thousandSeparator must be a single character. Received: "${e}" (length: ${e.length})`)}}function lt(e){if(e!==void 0&&!Object.values(m).includes(e))throw new Error(`ThousandStyle must be one of: ${Object.values(m).map(t=>`'${t}'`).join(", ")}. Received: ${JSON.stringify(e)}`)}function ht(e){if(e!==void 0){if(typeof e!="string")throw new Error(`decimalSeparator must be a string. Received: ${typeof e} (${JSON.stringify(e)})`);if(e.length===0)throw new Error(`decimalSeparator cannot be empty. Received: ${JSON.stringify(e)}`);if(e.length>1)throw new Error(`decimalSeparator must be a single character. Received: "${e}" (length: ${e.length})`)}}function ut(e,t){if(!(e===void 0||t===void 0)&&e===t)throw new Error(`Decimal separator can't be same as thousand separator. thousandSeparator: ${e}, decimalSeparator: ${t}`)}function I(e,t){if(t!==void 0&&typeof t!="boolean")throw new Error(`${e} must be a boolean. Received: ${typeof t} (${JSON.stringify(t)})`)}function dt(e){if(e!==void 0&&typeof e!="function")throw new Error(`onChange must be a function or undefined. Received: ${typeof e} (${JSON.stringify(e)})`)}class ft{constructor(t,{decimalMaxLength:n=G,decimalMinLength:r=B,formatOn:i=Z,thousandSeparator:s,thousandStyle:a=k,decimalSeparator:c,locale:l,enableCompactNotation:o=W,enableNegative:h=U,enableLeadingZeros:f=P,rawValueMode:u=z,onChange:S,...E}){w(this,"element");w(this,"resolvedOptions");w(this,"rawValue","");w(this,"caretPositionBeforeChange");ae({decimalMaxLength:n,decimalMinLength:r,formatOn:i,thousandSeparator:s,thousandStyle:a,decimalSeparator:c,enableCompactNotation:o,enableNegative:h,enableLeadingZeros:f,rawValueMode:u,onChange:S});const p={decimalMaxLength:n,decimalMinLength:r,onChange:S,formatOn:i,thousandSeparator:s,thousandStyle:a,decimalSeparator:c,locale:l,enableCompactNotation:o,enableNegative:h,enableLeadingZeros:f,rawValueMode:u,...E};if(this.resolvedOptions=this.getResolvedOptions(p),this.createInputElement(t,p),this.setupEventListeners(),this.resolvedOptions.rawValueMode&&this.element.value){const g=this.element.value,d=this.resolvedOptions.thousandSeparator?v(g,this.resolvedOptions.thousandSeparator):g;this.rawValue=d,this.element.value=this.formatValueForDisplay(d)}else if(this.element.value){const g=this.element.value;this.element.value=this.formatValueForDisplay(g)}}createInputElement(t,n){this.element=document.createElement("input"),this.element.setAttribute("type","text"),this.element.setAttribute("inputmode","decimal"),this.element.setAttribute("spellcheck","false"),this.element.setAttribute("autocomplete","off");const r=re(this.resolvedOptions.decimalSeparator,this.resolvedOptions.enableNegative);this.element.setAttribute("pattern",r);const{decimalMaxLength:i,decimalMinLength:s,formatOn:a,thousandSeparator:c,thousandStyle:l,decimalSeparator:o,locale:h,enableCompactNotation:f,enableNegative:u,enableLeadingZeros:S,rawValueMode:E,onChange:p,value:g,defaultValue:d,type:L,inputMode:b,spellcheck:ce,autocomplete:gt,...oe}=n;Object.assign(this.element,oe),g!==void 0?this.element.value=g:d!==void 0&&(this.element.defaultValue=d,this.element.value=d),t.appendChild(this.element)}setupEventListeners(){this.element.addEventListener("input",this.handleChange.bind(this)),this.element.addEventListener("keydown",this.handleKeyDown.bind(this)),this.element.addEventListener("paste",this.handlePaste.bind(this)),this.resolvedOptions.formatOn===N.Blur&&this.resolvedOptions.thousandSeparator&&(this.element.addEventListener("focus",this.handleFocus.bind(this)),this.element.addEventListener("blur",this.handleBlur.bind(this)))}getResolvedOptions(t){const n=se(t.locale,{thousandSeparator:t.thousandSeparator,decimalSeparator:t.decimalSeparator});return{decimalMaxLength:t.decimalMaxLength??G,decimalMinLength:t.decimalMinLength??B,formatOn:t.formatOn??Z,thousandSeparator:n.thousandSeparator??ue,thousandStyle:t.thousandStyle??k,decimalSeparator:n.decimalSeparator??C,enableCompactNotation:t.enableCompactNotation??W,enableNegative:t.enableNegative??U,enableLeadingZeros:t.enableLeadingZeros??P,rawValueMode:t.rawValueMode??z,onChange:t.onChange}}buildFormattingOptions(){return{formatOn:this.resolvedOptions.formatOn,thousandSeparator:this.resolvedOptions.thousandSeparator,ThousandStyle:this.resolvedOptions.thousandStyle,enableCompactNotation:this.resolvedOptions.enableCompactNotation,enableNegative:this.resolvedOptions.enableNegative,enableLeadingZeros:this.resolvedOptions.enableLeadingZeros,decimalSeparator:this.resolvedOptions.decimalSeparator,decimalMinLength:this.resolvedOptions.decimalMinLength,rawValueMode:this.resolvedOptions.rawValueMode}}handleValueChange(t,n){if(this.resolvedOptions.rawValueMode&&n!==void 0&&(this.rawValue=n),this.resolvedOptions.onChange){const r=this.resolvedOptions.rawValueMode?this.rawValue:t;this.resolvedOptions.onChange(r)}}formatValueForDisplay(t){if(!t)return t;const{thousandSeparator:n,thousandStyle:r,enableLeadingZeros:i,decimalSeparator:s}=this.resolvedOptions;return n&&r!==m.None?Q(t,n,r,i,s):t}handleChange(t){const{formatted:n,raw:r}=te(t,this.resolvedOptions.decimalMaxLength,this.caretPositionBeforeChange,this.buildFormattingOptions());this.caretPositionBeforeChange=void 0,this.handleValueChange(n,r)}handleKeyDown(t){const n=t.target,{selectionStart:r,selectionEnd:i}=n,s=this.buildFormattingOptions(),a=ee(t,{formatOn:s.formatOn,thousandSeparator:s.thousandSeparator,ThousandStyle:s.ThousandStyle,decimalSeparator:s.decimalSeparator});a?this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0,endOffset:a.endOffset}:this.caretPositionBeforeChange={selectionStart:r??0,selectionEnd:i??0}}handlePaste(t){const{formatted:n,raw:r}=ne(t,this.resolvedOptions.decimalMaxLength,this.buildFormattingOptions());this.handleValueChange(n,r);const i=new Event("input",{bubbles:!0,cancelable:!0});this.element.dispatchEvent(i)}handleFocus(t){if(this.resolvedOptions.formatOn===N.Blur&&this.resolvedOptions.thousandSeparator){const n=t.target;n.value=v(n.value,this.resolvedOptions.thousandSeparator)}}handleBlur(t){const n=t.target,{thousandSeparator:r,thousandStyle:i}=this.resolvedOptions;if(r&&i!==m.None&&n.value){const s=this.formatValueForDisplay(n.value);n.value=s;const a=this.resolvedOptions.rawValueMode?v(s,r):void 0;this.handleValueChange(s,a)}}getValue(){return this.resolvedOptions.rawValueMode?this.rawValue:this.element.value}setValue(t){if(this.resolvedOptions.rawValueMode){const n=this.resolvedOptions.thousandSeparator?v(t,this.resolvedOptions.thousandSeparator):t;this.rawValue=n,this.element.value=this.formatValueForDisplay(n)}else this.element.value=t}disable(){this.element.disabled=!0}enable(){this.element.disabled=!1}addEventListener(t,n){this.element.addEventListener(t,n)}removeEventListener(t,n){this.element.removeEventListener(t,n)}getElement(){return this.element}get value(){return this.getValue()}set value(t){this.setValue(t)}get valueAsNumber(){const t=this.getValue();if(!t)return NaN;const n=this.resolvedOptions.thousandSeparator?v(t,this.resolvedOptions.thousandSeparator):t,r=this.resolvedOptions.decimalSeparator&&this.resolvedOptions.decimalSeparator!=="."?n.replace(new RegExp(y(this.resolvedOptions.decimalSeparator),"g"),"."):n;return parseFloat(r)}set valueAsNumber(t){if(isNaN(t)){this.setValue("");return}const n=t.toString();this.setValue(n)}}exports.FormatOn=N;exports.NumoraInput=ft;exports.ThousandStyle=m;exports.applyLocale=se;exports.formatInputValue=M;exports.formatValueForDisplay=tt;exports.getNumoraPattern=re;exports.getSeparatorsFromLocale=ie;exports.handleOnChangeNumoraInput=te;exports.handleOnKeyDownNumoraInput=ee;exports.handleOnPasteNumoraInput=ne;exports.removeThousandSeparators=v;exports.validateNumoraInputOptions=ae;
|
|
1
|
+
"use strict";var Le=Object.defineProperty;var De=(t,e,n)=>e in t?Le(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var N=(t,e,n)=>De(t,typeof e!="symbol"?e+"":e,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var I=(t=>(t.Blur="blur",t.Change="change",t))(I||{}),y=(t=>(t.None="none",t.Thousand="thousand",t.Lakh="lakh",t.Wan="wan",t))(y||{});const L={InsertText:"insertText",InsertFromPaste:"insertFromPaste",InsertFromDrop:"insertFromDrop",DeleteContentBackward:"deleteContentBackward",DeleteContentForward:"deleteContentForward",DeleteByCut:"deleteByCut",DeleteByDrag:"deleteByDrag",DeleteSoftLineBackward:"deleteSoftLineBackward",DeleteHardLineBackward:"deleteHardLineBackward",DeleteSoftLineForward:"deleteSoftLineForward",DeleteHardLineForward:"deleteHardLineForward",HistoryUndo:"historyUndo",HistoryRedo:"historyRedo"},we=2,ye=0,Ie=I.Blur,Ne=",",Ae=y.None,x=".",Ce=!1,be=!1,ve=!1,Te=!1,Me=!1,U=new Map;function B(t,e="g"){const n=`${t}:${e}`;let r=U.get(n);return r||(r=new RegExp(t,e),U.set(n,r)),r}function M(t){return t.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")}const xe=/[.,]/g;function Z(t){return{decimalSeparator:(t==null?void 0:t.decimalSeparator)??x,thousandSeparator:t==null?void 0:t.thousandSeparator}}function q(t,e){const n=t.startsWith("-"),r=n?t.slice(1):t,[s="",i=""]=r.split(e);return{sign:n?"-":"",integer:s,decimal:i}}const $e=(t,e,n=x)=>{const{sign:r,integer:s,decimal:i}=q(t,n);if(!i)return t;const a=i.slice(0,e);return`${r}${s}${n}${a}`},Y=(t,e=x)=>{const n=t.indexOf(e);if(n===-1||n===t.length-1)return t;const r=t.slice(0,n+1),s=t.slice(n+1),a=e==="."||e===","?xe:B("[,\\."+M(e)+"]","g");return r+s.replace(a,"")},Re=(t,e=0,n=x)=>{if(e<=0)return t;const{sign:r,integer:s,decimal:i}=q(t,n);if(i.length>=e)return t;const a=i.padEnd(e,"0");return`${r}${s}${n}${a}`},Q=(t,e=!1,n=".")=>{const r=M(n),s=B(`[^0-9${r}]`,"g");if(!e)return t.replace(s,"");const i=t.startsWith("-"),a=t.replace(s,"");return i&&(a.length>0||t==="-")?"-"+a:a},_e=/([+-]?\d+\.?\d*)[eE]([+-]?\d+)/g,O=/^0+$/,R=/\.?0+$/;function V(t){return!t.includes("e")&&!t.includes("E")?t:t.replace(_e,(e,n,r)=>{const s=parseInt(r,10);if(s===0)return n;const i=n.startsWith("-"),a=i?n.slice(1):n,[c,h=""]=a.split("."),l=s>0?me(c,h,s):ke(c,h,Math.abs(s));return i?"-"+l:l})}function me(t,e,n){const r=t+e;if(r==="0"||O.test(r))return"0";const s=e.length;if(n<=s){const a=r.slice(0,t.length+n),c=r.slice(t.length+n);return c?`${a}.${c}`:a}const i=n-s;return r+"0".repeat(i)}function ke(t,e,n){const r=t+e;if(r==="0"||O.test(r))return"0";const i=t.length-n;if(i<=0){const a=Math.abs(i),c=`0.${"0".repeat(a)}${r}`;return _(c)}if(i<t.length){const a=r.slice(0,i),c=r.slice(i),h=`${a}.${c}`;return _(h)}return _(r)}function _(t){if(!t.includes("."))return t;if(t==="0.")return"0";if(t.startsWith("0.")){const e=t.replace(R,"");return e==="0"?"0":e||"0"}if(t.startsWith("-0.")){const e=t.replace(R,"");return e==="-0"||e==="0"?"0":e||"0"}return t.replace(R,"")||t}const Be=/(\d+\.?\d*)\s*([kmbt])/gi,Ze=/^0+/,Ge=/^(-?)0+([1-9])/,We=/^-?0+$/,Ue=/\.?0+$/,Pe={k:3,m:6,b:9,t:12};function F(t){return t.replace(Be,(e,n,r)=>{const s=r.toLowerCase(),i=Pe[s];if(!i)return e;const[a,c=""]=n.split("."),h=a.replace(Ze,"")||"0";let l;if(c.length===0)l=h+"0".repeat(i);else if(c.length<=i){const o=i-c.length;l=h+c+"0".repeat(o)}else{const o=c.slice(0,i),f=c.slice(i);l=h+o+"."+f}return l=l.replace(Ge,"$1$2"),We.test(l)&&(l="0"),l.includes(".")&&(l=l.replace(Ue,"")),l})}function ee(t){if(!t||t==="0"||t==="-0"||t==="-"||t===".")return t;const e=t.startsWith("-"),n=e?t.slice(1):t;if(n===".")return t;if(n.includes(".")){const[s,i]=n.split(".");if(s){const c=(s.replace(/^0+/,"")||"0")+"."+i;return e?"-"+c:c}return t}if(n.startsWith("0")){const s=n.replace(/^0+/,"")||"0";return e?"-"+s:s}return t}function te(t,e="."){return t&&(t===e?"0"+e:t==="-"+e?"-0"+e:t.startsWith("-"+e)?"-0"+t.slice(1):t.startsWith(e)?"0"+t:t)}function ne(t,e,n="."){if(e<0||t.length<=e)return t;let r=t.slice(0,e);return r.endsWith(n)&&(r=r.slice(0,-1)),r==="-"?"":r}function re(t){return t.replace(/[\s\u200B]/g,"")}function se(t){return t.replace(/[\uFF10-\uFF19]/g,e=>String.fromCharCode(e.charCodeAt(0)-65248))}function A(t,e){return t.replace(B(M(e)),"")}const ie=(t,e)=>{let n=re(t);return e!=null&&e.thousandSeparator&&(n=A(n,e.thousandSeparator)),n=se(n),e!=null&&e.enableCompactNotation&&(n=F(n)),n=V(n),n=Q(n,e==null?void 0:e.enableNegative,e==null?void 0:e.decimalSeparator),n=Y(n,e==null?void 0:e.decimalSeparator),e!=null&&e.enableLeadingZeros||(n=ee(n)),e!=null&&e.autoAddLeadingZero&&(n=te(n,e==null?void 0:e.decimalSeparator)),(e==null?void 0:e.maxLength)!==void 0&&(n=ne(n,e.maxLength,e==null?void 0:e.decimalSeparator)),n},v={thousand:{size:3},lakh:{firstGroup:3,restGroup:2},wan:{size:4}},je=/^(0+)/;function ae(t,e,n=y.Thousand,r=!1,s="."){if(!t||t==="0"||t===s||t==="-"||t===`-${s}`)return t;const i=t.includes(s),a=t.startsWith("-"),c=a?t.slice(1):t,[h,l]=c.split(s);if(!h){const u=l?`${s}${l}`:c;return a?`-${u}`:u}if(r&&h.startsWith("0")&&h.length>1){const u=h.match(je);if(u){const E=u[1],p=h.slice(E.length);if(p){const D=P(p,e,n),S=E+D,d=a?"-":"";return i?l?`${d}${S}${s}${l}`:`${d}${S}${s}`:`${d}${S}`}}}const o=P(h,e,n),f=a?"-":"";return i?l?`${f}${o}${s}${l}`:`${f}${o}${s}`:`${f}${o}`}function P(t,e,n){if(t==="0"||t==="")return t;switch(n){case y.None:return t;case y.Thousand:return j(t,e,v.thousand.size);case y.Lakh:return ze(t,e);case y.Wan:return j(t,e,v.wan.size);default:return t}}function ze(t,e){if(t.length<=v.lakh.firstGroup)return t;const n=[],r=t.length-v.lakh.firstGroup;n.unshift(t.slice(r));for(let s=r;s>0;s-=v.lakh.restGroup)n.unshift(t.slice(Math.max(0,s-v.lakh.restGroup),s));return n.join(e)}function j(t,e,n){const r=[];for(let s=t.length;s>0;s-=n)r.unshift(t.slice(Math.max(0,s-n),s));return r.join(e)}function C(t,e,n){let r=0;for(let s=0;s<e&&s<t.length;s++)t[s]!==n&&r++;return r}function He(t,e,n){if(e===0)return 0;let r=0;for(let s=0;s<t.length;s++)if(t[s]!==n){if(r===e-1)return s+1;r++}return t.length}function k(t,e,n){if(e===0)return 0;let r=0;for(let s=0;s<t.length;s++)if(t[s]!==n){if(r++,r===e)return s+1;if(r>e)return s}return t.length}function ce(t,e,n){return e<0||e>=t.length?!1:t[e]===n}const Xe=/\d/;function Je(t,e={}){const{thousandSeparator:n,decimalSeparator:r=".",prefix:s="",suffix:i=""}=e,a=new Array(t.length+1).fill(!0);if(s&&a.fill(!1,0,s.length),i){const c=t.length-i.length;a.fill(!1,c+1,t.length+1)}for(let c=0;c<t.length;c++){const h=t[c];(n&&h===n||h===r)&&(a[c]=!1,c+1<t.length&&!Xe.test(t[c+1])&&(a[c+1]=!1))}return a.some(c=>c)||a.fill(!0),a}function G(t,e,n,r){const s=t.length;if(e=Math.max(0,Math.min(e,s)),r==="left"){for(;e>=0&&!n[e];)e--;e===-1&&(e=n.indexOf(!0))}else if(r==="right"){for(;e<=s&&!n[e];)e++;e>s&&(e=n.lastIndexOf(!0))}else if(!n[e]){let i=e;for(;i<=s&&!n[i];)i++;let a=e;for(;a>=0&&!n[a];)a--;i<=s&&a>=0?e=e-a<i-e?a:i:i<=s?e=i:a>=0&&(e=a)}return(e===-1||e>s)&&(e=s),e}const z=/(\d+\.?\d*)\s*[kmbt]$/i,Ke=(t,e)=>t===e;function qe(t,e,n,r,s,i,a=".",c={}){if(n<0)return 0;if(n>t.length||t===""||e===""||z.test(t)&&!z.test(e)&&e.length>t.length&&n>=t.length-1)return e.length;const h=e.length<t.length,l=ce(t,n,r),o=t.indexOf(a),f=e.indexOf(a);if(c.isCharacterEquivalent&&t!==e){const S={thousandSeparator:r||void 0,...c},d=Ye(t,e,n,c.isCharacterEquivalent||Ke,i,S);if(d!==void 0)return d}const E=new Map,p=(S,d,g)=>{const w=`${S===t?"o":"n"}:${d}`;return E.has(w)||E.set(w,C(S,d,r)),E.get(w)};if(h)return Qe(t,e,n,r,l,o,f,i,c,p);const D=et(t,e,n,r,l,p);return c.boundary?G(e,D,c.boundary):D}function Ye(t,e,n,r,s,i){const a=t.length,c=e.length;let h=0,l=0,o=c;for(let d=0;d<a;d++){let g=-1;for(let w=h;w<c;w++)if(r(t[d],e[w],{oldValue:t,newValue:e,typedRange:s,oldIndex:d,newIndex:w})){g=w;break}g!==-1&&(h=g+1,d<n?l=g+1:o===c&&/\d/.test(t[d])&&(o=g))}if(l>o)return o;const f=e.length<t.length||s!==void 0&&s.isDelete===!1;let u,E=!1,p=!1;if(f){u=l;const d=i.thousandSeparator,g=i.decimalSeparator??".",w=u<e.length?e[u]:"";d&&w===d?(n>0?t[n-1]:"")===d?(u=o,E=!0):p=!0:w===g&&(p=!0)}else u=n-l<o-n?l:o;const D=f&&!E?"left":"right";if(i.boundary)return p?u:G(e,u,i.boundary,D);const S=i.thousandSeparator;if(!p&&S&&u>=0&&u<e.length&&e[u]===S)if(f&&!E)for(;u>0&&e[u]===S;)u--;else for(;u<e.length&&e[u]===S;)u++;return u}function Qe(t,e,n,r,s,i,a,c,h={},l=C){if(s)return Oe(t,e,n,r,a,l);const o=l(t,n,r),f=l(t,t.length,r),u=l(e,e.length,r),E=f-u,p=Ve(t,n,r,o,E,i,c,l),D=i===-1||n<=i,S=Fe(e,p,r,E,c,D,a,l);return h.boundary?G(e,S,h.boundary):S}function Oe(t,e,n,r,s,i=C){const a=n+1;if(a<t.length){const c=i(t,a,r),h=He(e,c,r);return h<e.length&&e[h]!==r?h+1:h}return n}function Ve(t,e,n,r,s,i,a,c=C){if(a){const{start:l,isDelete:o}=a;return o?c(t,l,n):Math.max(0,c(t,l,n))}return e>0&&t[e-1]===n&&s>0?r+1:r}function H(t,e,n,r,s,i,a=C){if(e>0&&e<t.length&&a(t,e,r)===n){if(t[e]===r&&i&&s>0&&e<t.length-1)return e+1;if(!i&&s>0&&e<t.length-1)return Math.min(e+1,t.length)}return e}function Fe(t,e,n,r,s,i,a,c=C){if(i&&a!==-1){const l=t.substring(0,a),o=c(l,l.length,n);if(e<=o){const f=k(l,e,n);return H(l,f,e,n,r,s,c)}}const h=k(t,e,n);return H(t,h,e,n,r,s,c)}function et(t,e,n,r,s,i=C){const a=n>=t.length,c=i(t,n,r),h=i(t,t.length,r),l=i(e,e.length,r);if(a||c===h)return e.length;const o=l-h;let f=c;o>0&&!a&&c<h&&(f=c+1);const u=k(e,f,r);return s&&!ce(e,u,r)?Math.max(0,u-1):u}function tt(t,e,n){const{selectionStart:r,selectionEnd:s,endOffset:i=0}=t;if(r!==s){const o=s-r;return{start:r,end:s,deletedLength:o,isDelete:!1}}if(i>0){const o=i;return{start:r,end:r+o,deletedLength:o,isDelete:!0}}const c=e.length,h=n.length,l=c-h;if(!(l<=0))return{start:r,end:r+l,deletedLength:l,isDelete:!1}}function nt(t,e){if(t===e)return;let n=0;for(;n<t.length&&n<e.length&&t[n]===e[n];)n++;let r=t.length-1,s=e.length-1;for(;r>=n&&s>=n&&t[r]===e[s];)r--,s--;const i=r-n+1,a=s-n+1;if(!(i===0&&a===0))return{start:n,end:r+1,deletedLength:i,isDelete:i>a}}function le(t,e,n,r){const s=A(t,r);return s===t?null:{raw:s,rawStart:A(t.slice(0,e),r).length,rawEnd:A(t.slice(0,n),r).length}}function rt(t,e,n){if((n==null?void 0:n.formatOn)!==I.Change||!n.thousandSeparator)return;const{selectionStart:r,selectionEnd:s,value:i}=e;if(r===null||s===null||r!==s)return;const{key:a}=t,c=n.thousandSeparator;a==="Backspace"&&r>0&&i[r-1]===c&&e.setSelectionRange(r-1,r-1),a==="Delete"&&i[r]===c&&e.setSelectionRange(r+1,r+1)}function he(t,e,n,r,s,i){if(!r)return null;const{selectionStart:a=0,selectionEnd:c=0,endOffset:h=0}=r;let l=tt({selectionStart:a,selectionEnd:c,endOffset:h},t,e);if(l||(l=nt(t,e)),!l)return null;const o=Je(e,{thousandSeparator:(i==null?void 0:i.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator}),f={thousandSeparator:(i==null?void 0:i.thousandSeparator)??s.thousandSeparator,decimalSeparator:s.decimalSeparator,isCharacterEquivalent:(p,D)=>{const S=(i==null?void 0:i.thousandSeparator)??s.thousandSeparator;return S&&p===S?!1:p===D},boundary:o},u=(i==null?void 0:i.thousandSeparator)??s.thousandSeparator??",",E=(i==null?void 0:i.thousandStyle)??y.None;return qe(t,e,n,u,E,l,s.decimalSeparator,f)}function oe(t,e,n,r){setTimeout(()=>{t.value===e&&(t.selectionStart!==n||t.selectionEnd!==r)&&t.setSelectionRange(n,r)},0)}function b(t,e,n){const r=t.value;r===e&&t.selectionStart===n&&t.selectionEnd===n||(t.setRangeText(e,0,r.length,"end"),t.setSelectionRange(n,n),oe(t,e,n,n))}function ue(t,e,n,r){t.value===e&&t.selectionStart===n&&t.selectionEnd===r||(t.setRangeText(e,0,t.value.length,"preserve"),t.setSelectionRange(n,r),oe(t,e,n,r))}function $(t,e,n,r){const s=Z(n),a=r??(n==null?void 0:n.formatOn)===I.Change?n:{...n,thousandSeparator:void 0},c=ie(t,a),h=$e(c,e,s.decimalSeparator),l=(n==null?void 0:n.decimalMinLength)??0,o=Re(h,l,s.decimalSeparator);return{formatted:(n==null?void 0:n.formatOn)===I.Change&&!!n.thousandSeparator?ae(o,n.thousandSeparator,n.thousandStyle??y.None,n.enableLeadingZeros,s.decimalSeparator):o,raw:o}}function st(t,e,n){const r=!!(n!=null&&n.thousandSeparator);return $(t,e,n,r)}function de(t,e,n){if(t.inputType===L.InsertFromPaste||t.inputType===L.InsertFromDrop)return{type:"skip"};const r=t.target,s=r.value,i=r.selectionStart??0,a=r.selectionEnd??0,c=Z(n);let h=t.data??"";if(t.inputType===L.InsertText&&(t.data===","||t.data===".")){const d=c.decimalSeparator;if((s.slice(0,i)+s.slice(a)).includes(d))return{type:"reject"};h=d}if(t.inputType===L.InsertText&&/^\d$/.test(h)){const d=c.decimalSeparator,g=s.indexOf(d);if(g!==-1&&i>g){const w=s.length-g-1,W=a-i;if(w-W+1>e)return{type:"reject"}}}let l,o,f=0;const u=i!==a;switch(t.inputType){case L.InsertText:{l=s.slice(0,i)+h+s.slice(a),o=i+h.length;break}case L.DeleteContentBackward:{const d=u?i:Math.max(0,i-1),g=u?a:i;l=s.slice(0,d)+s.slice(g),o=d;break}case L.DeleteContentForward:{const d=u?a:i+1;l=s.slice(0,i)+s.slice(d),o=i,u||(f=1);break}case L.DeleteByCut:case L.DeleteByDrag:{l=s.slice(0,i)+s.slice(a),o=i;break}case L.DeleteSoftLineBackward:case L.DeleteHardLineBackward:{const d=u?i:0;l=s.slice(0,d)+s.slice(a),o=d;break}case L.DeleteSoftLineForward:case L.DeleteHardLineForward:{const d=u?a:s.length;l=s.slice(0,i)+s.slice(d),o=i,u||(f=s.length-i);break}default:return{type:"skip"}}if((n==null?void 0:n.maxLength)!==void 0&&t.inputType===L.InsertText){const d=n.thousandSeparator;if((d?A(l,d).length:l.length)>n.maxLength)return{type:"reject"}}const E=(n==null?void 0:n.formatOn)===I.Change,{formatted:p,raw:D}=$(l,e,n,E);if(n!=null&&n.isAllowed&&!n.isAllowed(D))return{type:"reject"};let S;return l!==p?S=he(l,p,o,{selectionStart:i,selectionEnd:a,endOffset:f},c,n)??o:S=o,{type:"handled",formatted:p,raw:D,cursorPos:S}}function fe(t,e){const n=t.target;if(rt(t,n,e),t.key!=="Backspace"&&t.key!=="Delete")return;const r=n.selectionStart??0,s=n.selectionEnd??0;return{selectionStart:r,selectionEnd:s,endOffset:t.key==="Delete"&&r===s?1:0}}function it(t,e,n,r){const s=r-n;return t+e+s}function Se(t,e,n){var u;const r=t.target,{value:s,selectionStart:i,selectionEnd:a}=r,c=((u=t.clipboardData)==null?void 0:u.getData("text/plain"))||"",h=s.slice(0,i||0)+c+s.slice(a||0),{formatted:l,raw:o}=$(h,e,n,!0);if(n!=null&&n.isAllowed&&!n.isAllowed(o))return{type:"reject"};const f=it(i||0,c.length,h.length,l.length);return{type:"handled",formatted:l,raw:o,cursorPos:f}}function pe(t){var e,n;try{const s=new Intl.NumberFormat(t).formatToParts(123456789e-2);return{decimalSeparator:((e=s.find(i=>i.type==="decimal"))==null?void 0:e.value)??".",thousandSeparator:((n=s.find(i=>i.type==="group"))==null?void 0:n.value)??","}}catch{return{thousandSeparator:",",decimalSeparator:"."}}}function ge(t,e){if(!t)return e;const r=pe(t===!0?void 0:t);return{thousandSeparator:e.thousandSeparator??r.thousandSeparator,decimalSeparator:e.decimalSeparator??r.decimalSeparator}}function Ee(t){m("decimalMaxLength",t.decimalMaxLength),m("decimalMinLength",t.decimalMinLength),at(t.decimalMinLength,t.decimalMaxLength),K("formatOn",t.formatOn,[I.Blur,I.Change]),X("thousandSeparator",t.thousandSeparator),K("thousandStyle",t.thousandStyle,Object.values(y)),X("decimalSeparator",t.decimalSeparator),ct(t.thousandSeparator,t.decimalSeparator),T("enableCompactNotation",t.enableCompactNotation),T("enableNegative",t.enableNegative),T("enableLeadingZeros",t.enableLeadingZeros),T("autoAddLeadingZero",t.autoAddLeadingZero),T("rawValueMode",t.rawValueMode),m("maxLength",t.maxLength),J("isAllowed",t.isAllowed),J("onChange",t.onChange)}function m(t,e){if(e!==void 0&&(typeof e!="number"||!Number.isInteger(e)||e<0))throw new Error(`${t} must be a non-negative integer. Received: ${JSON.stringify(e)}`)}function X(t,e){if(e!==void 0&&(typeof e!="string"||e.length!==1))throw new Error(`${t} must be a single character. Received: ${JSON.stringify(e)}`)}function T(t,e){if(e!==void 0&&typeof e!="boolean")throw new Error(`${t} must be a boolean. Received: ${JSON.stringify(e)}`)}function J(t,e){if(e!==void 0&&typeof e!="function")throw new Error(`${t} must be a function. Received: ${JSON.stringify(e)}`)}function K(t,e,n){if(e!==void 0&&!n.includes(e))throw new Error(`${t} must be one of: ${n.map(r=>`'${r}'`).join(", ")}. Received: ${JSON.stringify(e)}`)}function at(t,e){if(!(t===void 0||e===void 0)&&t>e)throw new Error(`decimalMinLength (${t}) cannot be greater than decimalMaxLength (${e}).`)}function ct(t,e){if(!(t===void 0||e===void 0)&&t===e)throw new Error(`decimalSeparator cannot equal thousandSeparator. Both were ${JSON.stringify(t)}.`)}const lt=new Set(["decimalMaxLength","decimalMinLength","formatOn","thousandSeparator","thousandStyle","decimalSeparator","locale","enableCompactNotation","enableNegative","enableLeadingZeros","autoAddLeadingZero","rawValueMode","maxLength","isAllowed","onChange","value","defaultValue","type","inputMode","spellcheck","autocomplete"]);class ht{constructor(e,n){N(this,"element");N(this,"resolvedOptions");N(this,"rawValue","");N(this,"suppressNextInputEvent",!1);N(this,"pendingMouseStrip",!1);N(this,"caretPositionBeforeChange");Ee(n);const{decimalMaxLength:r=we,decimalMinLength:s=ye,formatOn:i=Ie,thousandSeparator:a,thousandStyle:c=Ae,decimalSeparator:h,locale:l,enableCompactNotation:o=Ce,enableNegative:f=be,enableLeadingZeros:u=ve,autoAddLeadingZero:E=Te,rawValueMode:p=Me,maxLength:D,isAllowed:S,onChange:d}=n,g=ge(l,{thousandSeparator:a,decimalSeparator:h});this.resolvedOptions={decimalMaxLength:r,decimalMinLength:s,formatOn:i,thousandSeparator:g.thousandSeparator??Ne,thousandStyle:c,decimalSeparator:g.decimalSeparator??x,enableCompactNotation:o,enableNegative:f,enableLeadingZeros:u,autoAddLeadingZero:E,rawValueMode:p,maxLength:D,isAllowed:S,onChange:d},this.createInputElement(e,n),this.setupEventListeners(),this.setDefaultValue()}toRaw(e){const n=this.resolvedOptions.thousandSeparator;return n?A(e,n):e}setDefaultValue(){if(!this.element.value)return;const e=this.toRaw(this.element.value);this.resolvedOptions.rawValueMode&&(this.rawValue=e),this.element.value=this.applyDisplaySeparators(e)}createInputElement(e,n){const r=e instanceof HTMLInputElement;this.element=r?e:document.createElement("input"),this.element.setAttribute("type","text"),this.element.setAttribute("inputmode","decimal"),this.element.setAttribute("spellcheck","false"),this.element.setAttribute("autocomplete","off");const s=M(this.resolvedOptions.decimalSeparator),i=this.resolvedOptions.enableNegative?"-?":"";this.element.setAttribute("pattern",`^${i}[0-9]*[${s}]?[0-9]*$`);const a=Object.fromEntries(Object.entries(n).filter(([c])=>!lt.has(c)));Object.assign(this.element,a),n.value!==void 0?this.element.value=n.value:n.defaultValue!==void 0&&(this.element.defaultValue=n.defaultValue,this.element.value=n.defaultValue),r||e.appendChild(this.element)}setupEventListeners(){this.element.addEventListener("beforeinput",this.handleBeforeInput.bind(this)),this.element.addEventListener("input",this.handleChange.bind(this)),this.element.addEventListener("keydown",this.handleKeyDown.bind(this)),this.element.addEventListener("paste",this.handlePaste.bind(this)),this.resolvedOptions.formatOn===I.Blur&&this.resolvedOptions.thousandSeparator&&(this.element.addEventListener("focus",this.handleFocus.bind(this)),this.element.addEventListener("blur",this.handleBlur.bind(this)),this.element.addEventListener("mousedown",this.handleMouseDown.bind(this)),this.element.addEventListener("click",this.handleClick.bind(this)))}withInternalWrite(e){this.suppressNextInputEvent=!0;try{e()}finally{this.suppressNextInputEvent=!1}}handleValueChange(e,n){if(this.resolvedOptions.rawValueMode&&n!==void 0&&(this.rawValue=n),this.resolvedOptions.onChange){const r=this.resolvedOptions.rawValueMode?this.rawValue:e;this.resolvedOptions.onChange(r)}}applyDisplaySeparators(e){if(!e)return e;const{thousandSeparator:n,thousandStyle:r,enableLeadingZeros:s,decimalSeparator:i}=this.resolvedOptions;return n&&r!==y.None?ae(e,n,r,s,i):e}handleBeforeInput(e){const n=de(e,this.resolvedOptions.decimalMaxLength,this.resolvedOptions);switch(n.type){case"handled":{e.preventDefault(),this.withInternalWrite(()=>b(this.element,n.formatted,n.cursorPos)),this.handleValueChange(n.formatted,n.raw);break}case"reject":e.preventDefault();break}}handleChange(e){if(this.suppressNextInputEvent)return;const n=e.target;if(e instanceof InputEvent&&(e.inputType===L.HistoryUndo||e.inputType===L.HistoryRedo)){const l=this.toRaw(n.value);this.caretPositionBeforeChange=void 0,this.handleValueChange(n.value,l);return}const r=n.value,s=Math.max(n.selectionStart??0,n.selectionEnd??0),i=Z(this.resolvedOptions),a=this.resolvedOptions.formatOn===I.Change,{formatted:c,raw:h}=$(r,this.resolvedOptions.decimalMaxLength,this.resolvedOptions,a);if(r!==c){const l=he(r,c,s,this.caretPositionBeforeChange,i,this.resolvedOptions);this.withInternalWrite(()=>b(n,c,l??c.length))}this.caretPositionBeforeChange=void 0,this.handleValueChange(c,h)}handleKeyDown(e){const n=e.target;this.caretPositionBeforeChange=fe(e,this.resolvedOptions)??{selectionStart:n.selectionStart??0,selectionEnd:n.selectionEnd??0}}handlePaste(e){e.preventDefault();const n=Se(e,this.resolvedOptions.decimalMaxLength,this.resolvedOptions);n.type!=="reject"&&(this.withInternalWrite(()=>b(this.element,n.formatted,n.cursorPos)),this.handleValueChange(n.formatted,n.raw))}stripSeparatorsAndMapCaret(e){const n=this.resolvedOptions.thousandSeparator;if(!n||this.resolvedOptions.thousandStyle===y.None)return;const r=e.selectionStart??0,s=e.selectionEnd??e.value.length,i=le(e.value,r,s,n);i&&(this.withInternalWrite(()=>ue(e,i.raw,i.rawStart,i.rawEnd)),this.handleValueChange(i.raw,i.raw))}handleFocus(e){this.pendingMouseStrip||this.stripSeparatorsAndMapCaret(e.target)}handleMouseDown(){this.pendingMouseStrip=!0}handleClick(e){this.pendingMouseStrip&&(this.pendingMouseStrip=!1,this.stripSeparatorsAndMapCaret(e.target))}handleBlur(e){this.pendingMouseStrip=!1;const n=e.target,{thousandSeparator:r,thousandStyle:s}=this.resolvedOptions;if(!r||s===y.None||!n.value)return;const i=this.applyDisplaySeparators(n.value);i!==n.value&&this.withInternalWrite(()=>b(n,i,i.length));const a=this.resolvedOptions.rawValueMode?this.toRaw(i):void 0;this.handleValueChange(i,a)}getValue(){return this.resolvedOptions.rawValueMode?this.rawValue:this.element.value}setValue(e,n){let r;if(this.resolvedOptions.rawValueMode){const s=this.toRaw(e);this.rawValue=s,r=this.applyDisplaySeparators(s)}else r=e;if((n==null?void 0:n.undoable)===!1){this.element.value=r;return}this.withInternalWrite(()=>b(this.element,r,r.length))}disable(){this.element.disabled=!0}enable(){this.element.disabled=!1}addEventListener(e,n){this.element.addEventListener(e,n)}removeEventListener(e,n){this.element.removeEventListener(e,n)}getElement(){return this.element}get value(){return this.getValue()}set value(e){this.setValue(e)}get valueAsNumber(){const e=this.getValue();if(!e)return NaN;const n=this.toRaw(e),r=this.resolvedOptions.decimalSeparator&&this.resolvedOptions.decimalSeparator!=="."?n.replace(new RegExp(M(this.resolvedOptions.decimalSeparator),"g"),"."):n;return parseFloat(r)}set valueAsNumber(e){if(isNaN(e)){this.setValue("");return}const n=e.toString();this.setValue(n)}}exports.FormatOn=I;exports.InputType=L;exports.NumoraInput=ht;exports.ThousandStyle=y;exports.applyLocale=ge;exports.computeStripSeparatorsResult=le;exports.expandCompactNotation=F;exports.expandScientificNotation=V;exports.filterMobileKeyboardArtifacts=re;exports.formatValueForDisplay=st;exports.getSeparatorsFromLocale=pe;exports.handleOnBeforeInputNumoraInput=de;exports.handleOnKeyDownNumoraInput=fe;exports.handleOnPasteNumoraInput=Se;exports.normalizeFullWidthDigits=se;exports.prependLeadingZero=te;exports.removeExtraDecimalSeparators=Y;exports.removeLeadingZeros=ee;exports.removeNonNumericCharacters=Q;exports.removeThousandSeparators=A;exports.sanitizeNumoraInput=ie;exports.truncateToMaxLength=ne;exports.validateNumoraInputOptions=Ee;exports.writeStripPreservingUndo=ue;exports.writeValuePreservingUndo=b;
|