generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kit): new utility
maskitoParseNumber
(#178)
- Loading branch information
1 parent
20316f1
commit fc58141
Showing
11 changed files
with
85 additions
and
23 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
projects/demo/src/pages/kit/number/examples/maskito-parse-number-demo.md
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,8 @@ | ||
```ts | ||
import {maskitoParseNumber} from '@maskito/kit'; | ||
|
||
maskitoParseNumber( | ||
'10 000,42', // document.querySelector('input').value | ||
',', // decimal separator (dot is default) | ||
); // 10000.42 | ||
``` |
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
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
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 +1,2 @@ | ||
export {maskitoNumberOptionsGenerator} from './number-mask'; | ||
export {maskitoParseNumber} from './utils'; |
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
12 changes: 3 additions & 9 deletions
12
projects/kit/src/lib/masks/number/processors/max-validation-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
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,2 +1,3 @@ | ||
export * from './generate-mask-expression'; | ||
export * from './get-default-pseudo-separators'; | ||
export * from './parse-number'; |
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,10 @@ | ||
export function maskitoParseNumber( | ||
maskedNumber: string, | ||
decimalSeparator: string = '.', | ||
): number { | ||
return Number( | ||
maskedNumber | ||
.replace(new RegExp(`[^\\d\\${decimalSeparator}]`, 'g'), '') | ||
.replace(decimalSeparator, '.'), | ||
); | ||
} |
39 changes: 39 additions & 0 deletions
39
projects/kit/src/lib/masks/number/utils/tests/parse-number.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,39 @@ | ||
import {maskitoParseNumber} from '../parse-number'; | ||
|
||
describe('maskitoParseNumber', () => { | ||
describe('decimal separator is dot (default one)', () => { | ||
it('thousand separator is space', () => { | ||
expect(maskitoParseNumber('1 000 000.42')).toBe(1000000.42); | ||
}); | ||
|
||
it('thousand separator is hyphen', () => { | ||
expect(maskitoParseNumber('1-000-000.42')).toBe(1000000.42); | ||
}); | ||
|
||
it('thousand separator is empty string', () => { | ||
expect(maskitoParseNumber('1000000.42')).toBe(1000000.42); | ||
}); | ||
|
||
it('empty decimal part & thousand separator is comma', () => { | ||
expect(maskitoParseNumber('1,000,000')).toBe(1000000); | ||
}); | ||
}); | ||
|
||
describe('decimal separator is comma', () => { | ||
it('thousand separator is space', () => { | ||
expect(maskitoParseNumber('42 111,42', ',')).toBe(42111.42); | ||
}); | ||
|
||
it('thousand separator is hyphen', () => { | ||
expect(maskitoParseNumber('42-111,42', ',')).toBe(42111.42); | ||
}); | ||
|
||
it('thousand separator is empty string', () => { | ||
expect(maskitoParseNumber('42111,42', ',')).toBe(42111.42); | ||
}); | ||
|
||
it('empty decimal part & thousand separator is dot', () => { | ||
expect(maskitoParseNumber('42.111', ',')).toBe(42111); | ||
}); | ||
}); | ||
}); |