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):
Number
allows to enter full width numbers (#864)
- Loading branch information
1 parent
6b5704a
commit b25db10
Showing
7 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
projects/demo-integrations/src/tests/kit/number/number-fullwidth-to-halfwidth.cy.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,33 @@ | ||
import {openNumberPage} from './utils'; | ||
|
||
describe('Number | Accepts full width numbers used by JP, CN or others', () => { | ||
beforeEach(() => { | ||
openNumberPage('thousandSeparator=_&precision=2'); | ||
}); | ||
|
||
describe('Invalid characters', () => { | ||
it('accepts full width numbers', () => { | ||
cy.get('@input') | ||
.type('1 2 3 4 5') | ||
.should('have.value', '12_345') | ||
.should('have.prop', 'selectionStart', '12_345'.length) | ||
.should('have.prop', 'selectionEnd', '12_345'.length); | ||
}); | ||
|
||
it('accepts full width characters with minus', () => { | ||
cy.get('@input') | ||
.type('ー123456') | ||
.should('have.value', '−123_456') | ||
.should('have.prop', 'selectionStart', '−123_456'.length) | ||
.should('have.prop', 'selectionEnd', '−123_456'.length); | ||
}); | ||
|
||
it('rejects full width characters, not numbers', () => { | ||
cy.get('@input') | ||
.type('あいうえお12345こんにちは') | ||
.should('have.value', '12_345') | ||
.should('have.prop', 'selectionStart', '12_345'.length) | ||
.should('have.prop', 'selectionEnd', '12_345'.length); | ||
}); | ||
}); | ||
}); |
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
20 changes: 20 additions & 0 deletions
20
projects/kit/src/lib/masks/number/processors/fullwidth-to-halfwidth-preprocessor.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,20 @@ | ||
import {MaskitoPreprocessor} from '@maskito/core'; | ||
|
||
import {toHalfWidthNumber} from '../utils/to-half-width-number'; | ||
|
||
/** | ||
* Convert full width numbers like 1, 2 to half width numbers 1, 2 | ||
*/ | ||
export function createFullWidthToHalfWidthPreprocessor(): MaskitoPreprocessor { | ||
return ({elementState, data}) => { | ||
const {value, selection} = elementState; | ||
|
||
return { | ||
elementState: { | ||
selection, | ||
value: toHalfWidthNumber(value), | ||
}, | ||
data: toHalfWidthNumber(data), | ||
}; | ||
}; | ||
} |
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
30 changes: 30 additions & 0 deletions
30
projects/kit/src/lib/masks/number/utils/tests/to-half-width-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,30 @@ | ||
import {toHalfWidthNumber} from '../to-half-width-number'; | ||
|
||
describe('`toHalfWidthNumber` utility converts full width numbers to half width numbers', () => { | ||
const tests = [ | ||
// [full width value, half width value] | ||
['1', '1'], | ||
['2', '2'], | ||
['3', '3'], | ||
['4', '4'], | ||
['5', '5'], | ||
['6', '6'], | ||
['7', '7'], | ||
['8', '8'], | ||
['9', '9'], | ||
] as const; | ||
|
||
tests.forEach(([fullWidthValue, halfWidthValue]) => { | ||
it(`${fullWidthValue} => ${halfWidthValue}`, () => { | ||
expect(toHalfWidthNumber(fullWidthValue)).toBe(halfWidthValue); | ||
}); | ||
}); | ||
|
||
it('123456 => 123456', () => { | ||
expect(toHalfWidthNumber('123456')).toBe('123456'); | ||
}); | ||
|
||
it('123456 (full width + half width mix) => 123456', () => { | ||
expect(toHalfWidthNumber('123456')).toBe('123456'); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
projects/kit/src/lib/masks/number/utils/to-half-width-number.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,10 @@ | ||
/** | ||
* Replace fullwidth numbers with half width number | ||
* @param fullWidthNumber full width number | ||
* @returns processed half width number | ||
*/ | ||
export function toHalfWidthNumber(fullWidthNumber: string): string { | ||
return fullWidthNumber.replace(/[0-9]/g, s => | ||
String.fromCharCode(s.charCodeAt(0) - 0xfee0), | ||
); | ||
} |