generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kit):
Number
with decimalZeroPadding=true
should erase everyt…
…hing on `.00` (#1207)
- Loading branch information
1 parent
08bb9b3
commit d72f225
Showing
7 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
projects/kit/src/lib/masks/number/processors/empty-postprocessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type {MaskitoPostprocessor} from '@maskito/core'; | ||
|
||
import {extractAffixes} from '../../../utils'; | ||
import {toNumberParts} from '../utils'; | ||
|
||
/** | ||
* Make textfield empty if there is no integer part and all decimal digits are zeroes. | ||
* @example 0|,00 => Backspace => Empty. | ||
* @example -0|,00 => Backspace => -. | ||
* @example ,42| => Backspace x2 => ,|00 => Backspace => Empty | ||
*/ | ||
export function emptyPostprocessor({ | ||
prefix, | ||
postfix, | ||
decimalSeparator, | ||
}: { | ||
prefix: string; | ||
postfix: string; | ||
decimalSeparator: string; | ||
}): MaskitoPostprocessor { | ||
return ({value, selection}) => { | ||
const [caretIndex] = selection; | ||
const {cleanValue, extractedPrefix, extractedPostfix} = extractAffixes(value, { | ||
prefix, | ||
postfix, | ||
}); | ||
const {minus, integerPart, decimalPart} = toNumberParts( | ||
cleanValue, | ||
decimalSeparator, | ||
); | ||
|
||
if ( | ||
!integerPart && | ||
!Number(decimalPart) && | ||
caretIndex === (minus + extractedPrefix).length | ||
) { | ||
return { | ||
selection, | ||
value: extractedPrefix + minus + extractedPostfix, | ||
}; | ||
} | ||
|
||
return {value, selection}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './generate-mask-expression'; | ||
export * from './parse-number'; | ||
export * from './stringify-number-without-exp'; | ||
export * from './to-number-parts'; | ||
export * from './validate-decimal-pseudo-separators'; |
96 changes: 96 additions & 0 deletions
96
projects/kit/src/lib/masks/number/utils/tests/to-number-parts.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
CHAR_EM_DASH, | ||
CHAR_EN_DASH, | ||
CHAR_HYPHEN, | ||
CHAR_JP_HYPHEN, | ||
CHAR_MINUS, | ||
} from '../../../../constants'; | ||
import {toNumberParts} from '../to-number-parts'; | ||
|
||
describe('toNumberParts', () => { | ||
[',', '.'].forEach(decimalSeparator => { | ||
describe(`decimalSeparator = ${decimalSeparator}`, () => { | ||
it('empty string => empty parts', () => { | ||
expect(toNumberParts('', decimalSeparator)).toEqual({ | ||
minus: '', | ||
integerPart: '', | ||
decimalPart: '', | ||
}); | ||
}); | ||
|
||
it(`123${decimalSeparator}45 => {minus: "", integerPart: "123", decimalPart: "45"}`, () => { | ||
expect( | ||
toNumberParts(`123${decimalSeparator}45`, decimalSeparator), | ||
).toEqual({ | ||
minus: '', | ||
integerPart: '123', | ||
decimalPart: '45', | ||
}); | ||
}); | ||
|
||
it(`-123${decimalSeparator}45 => {minus: "-", integerPart: "123", decimalPart: "45"}`, () => { | ||
expect( | ||
toNumberParts(`-123${decimalSeparator}45`, decimalSeparator), | ||
).toEqual({ | ||
minus: '-', | ||
integerPart: '123', | ||
decimalPart: '45', | ||
}); | ||
}); | ||
|
||
it('123 => {minus: "", integerPart: "123", decimalPart: ""}', () => { | ||
expect(toNumberParts('123', decimalSeparator)).toEqual({ | ||
minus: '', | ||
integerPart: '123', | ||
decimalPart: '', | ||
}); | ||
}); | ||
|
||
it('-123 => {minus: "-", integerPart: "123", decimalPart: ""}', () => { | ||
expect(toNumberParts('-123', decimalSeparator)).toEqual({ | ||
minus: '-', | ||
integerPart: '123', | ||
decimalPart: '', | ||
}); | ||
}); | ||
|
||
it(`${decimalSeparator}45 => {minus: "", integerPart: "", decimalPart: "45"}`, () => { | ||
expect(toNumberParts(`${decimalSeparator}45`, decimalSeparator)).toEqual({ | ||
minus: '', | ||
integerPart: '', | ||
decimalPart: '45', | ||
}); | ||
}); | ||
|
||
it(`-${decimalSeparator}45 => {minus: "-", integerPart: "", decimalPart: "45"}`, () => { | ||
expect(toNumberParts(`-${decimalSeparator}45`, decimalSeparator)).toEqual( | ||
{ | ||
minus: '-', | ||
integerPart: '', | ||
decimalPart: '45', | ||
}, | ||
); | ||
}); | ||
|
||
it('- => {minus: "-", integerPart: "", decimalPart: ""}', () => { | ||
expect(toNumberParts('-', decimalSeparator)).toEqual({ | ||
minus: '-', | ||
integerPart: '', | ||
decimalPart: '', | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it('different minus signs', () => { | ||
[CHAR_MINUS, CHAR_HYPHEN, CHAR_EN_DASH, CHAR_EM_DASH, CHAR_JP_HYPHEN].forEach( | ||
minus => { | ||
expect(toNumberParts(`${minus}123.45`, '.')).toEqual({ | ||
minus, | ||
integerPart: '123', | ||
decimalPart: '45', | ||
}); | ||
}, | ||
); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
projects/kit/src/lib/masks/number/utils/to-number-parts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const EXTRACT_MINUS_RE = /(\D)?(.*)/; | ||
|
||
export function toNumberParts( | ||
value: string, | ||
decimalSeparator: string, | ||
): {minus: string; integerPart: string; decimalPart: string} { | ||
const [integerWithMinus = '', decimalPart = ''] = value.split(decimalSeparator); | ||
const [, minus = '', integerPart = ''] = | ||
integerWithMinus.match(EXTRACT_MINUS_RE) || []; | ||
|
||
return {minus, integerPart, decimalPart}; | ||
} |