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): add full-width numbers support for
Time
, Date
, `DateTi…
…me`, `DateRange` (#1043)
- Loading branch information
1 parent
3984f63
commit 434c9c5
Showing
20 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
projects/demo-integrations/src/tests/kit/date-range/date-range-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,35 @@ | ||
import {DemoPath} from '@demo/constants'; | ||
|
||
describe('DateRange | Full width character parsing', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.DateRange}/API?mode=yyyy%2Fmm%2Fdd&dateSeparator=%2F`); | ||
cy.get('#demo-content input').should('be.visible').first().focus().as('input'); | ||
}); | ||
|
||
describe('basic typing', () => { | ||
const tests = [ | ||
// [Typed value, Masked value] | ||
['2', '2'], | ||
['20', '20'], | ||
['201', '201'], | ||
['2016', '2016'], | ||
['20162', '2016/02'], | ||
['2016228', '2016/02/28'], | ||
['20162282', '2016/02/28 – 2'], | ||
['201622820', '2016/02/28 – 20'], | ||
['20162282020', '2016/02/28 – 2020'], | ||
['201622820204', '2016/02/28 – 2020/04'], | ||
['2016228202044', '2016/02/28 – 2020/04/04'], | ||
] as const; | ||
|
||
tests.forEach(([typedValue, maskedValue]) => { | ||
it(`Type "${typedValue}" => "${maskedValue}"`, () => { | ||
cy.get('@input') | ||
.type(typedValue) | ||
.should('have.value', maskedValue) | ||
.should('have.prop', 'selectionStart', maskedValue.length) | ||
.should('have.prop', 'selectionEnd', maskedValue.length); | ||
}); | ||
}); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
projects/demo-integrations/src/tests/kit/date-time/date-time-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,36 @@ | ||
import {DemoPath} from '@demo/constants'; | ||
|
||
describe('DateTime | Full width character parsing', () => { | ||
beforeEach(() => { | ||
cy.visit( | ||
`/${DemoPath.DateTime}/API?dateMode=yyyy%2Fmm%2Fdd&timeMode=HH:MM:SS&dateSeparator=%2F`, | ||
); | ||
cy.get('#demo-content input').should('be.visible').first().focus().as('input'); | ||
}); | ||
|
||
describe('basic typing', () => { | ||
const tests = [ | ||
// [Typed value, Masked value] | ||
['2', '2'], | ||
['20', '20'], | ||
['201', '201'], | ||
['2016', '2016'], | ||
['20162', '2016/02'], | ||
['2016228', '2016/02/28'], | ||
['20162283', '2016/02/28, 03'], | ||
['2016228330', '2016/02/28, 03:30'], | ||
['20162283304', '2016/02/28, 03:30:4'], | ||
['201622833045', '2016/02/28, 03:30:45'], | ||
] as const; | ||
|
||
tests.forEach(([typedValue, maskedValue]) => { | ||
it(`Type "${typedValue}" => "${maskedValue}"`, () => { | ||
cy.get('@input') | ||
.type(typedValue) | ||
.should('have.value', maskedValue) | ||
.should('have.prop', 'selectionStart', maskedValue.length) | ||
.should('have.prop', 'selectionEnd', maskedValue.length); | ||
}); | ||
}); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
projects/demo-integrations/src/tests/kit/date/date-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,40 @@ | ||
import {DemoPath} from '@demo/constants'; | ||
|
||
/* NOTE: yyyy/mm/dd is very common in Japan */ | ||
describe('Date', () => { | ||
describe('Full width character parsing', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Date}/API?mode=yyyy%2Fmm%2Fdd&separator=%2F`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
describe('Accepts all of full width characters', () => { | ||
it('20191231 => 2019/12/31', () => { | ||
cy.get('@input') | ||
.type('20191231') | ||
.should('have.value', '2019/12/31') | ||
.should('have.prop', 'selectionStart', '2019/12/31'.length) | ||
.should('have.prop', 'selectionEnd', '2019/12/31'.length); | ||
}); | ||
}); | ||
|
||
describe('pads digits with zero if date segment exceeds its max possible value', () => { | ||
// NOTE: months can be > 12 => pads the first 2 with zero | ||
it('20102| => type 9 => 2010/02|', () => { | ||
cy.get('@input') | ||
.type('20102') | ||
.should('have.value', '2010/02') | ||
.should('have.prop', 'selectionStart', '2010/02'.length) | ||
.should('have.prop', 'selectionEnd', '2010/02'.length) | ||
.type('9') | ||
.should('have.value', '2010/02/09') | ||
.should('have.prop', 'selectionStart', '2010/02/09'.length) | ||
.should('have.prop', 'selectionEnd', '2010/02/09'.length); | ||
}); | ||
}); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
projects/demo-integrations/src/tests/kit/time/time-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,35 @@ | ||
import {DemoPath} from '@demo/constants'; | ||
|
||
describe('Time', () => { | ||
describe('Full width character parsing', () => { | ||
beforeEach(() => { | ||
cy.visit(`/${DemoPath.Time}/API?mode=HH:MM`); | ||
cy.get('#demo-content input') | ||
.should('be.visible') | ||
.first() | ||
.focus() | ||
.as('input'); | ||
}); | ||
|
||
describe('basic typing (1 character per keydown)', () => { | ||
const tests = [ | ||
// [Typed value, Masked value, caretIndex] | ||
['1', '1', 1], | ||
['12', '12', '12'.length], | ||
['12:', '12:', '12:'.length], | ||
['123', '12:3', '12:3'.length], | ||
['1234', '12:34', '12:34'.length], | ||
] as const; | ||
|
||
tests.forEach(([typedValue, maskedValue, caretIndex]) => { | ||
it(`Type "${typedValue}" => "${maskedValue}"`, () => { | ||
cy.get('@input') | ||
.type(typedValue) | ||
.should('have.value', maskedValue) | ||
.should('have.prop', 'selectionStart', caretIndex) | ||
.should('have.prop', 'selectionEnd', caretIndex); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
20 changes: 20 additions & 0 deletions
20
projects/kit/src/lib/processors/colon-convert-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 {toHalfWidthColon} from '../utils'; | ||
|
||
/** | ||
* Convert full width colon (:) to half width one (:) | ||
*/ | ||
export function createColonConvertPreprocessor(): MaskitoPreprocessor { | ||
return ({elementState, data}) => { | ||
const {value, selection} = elementState; | ||
|
||
return { | ||
elementState: { | ||
selection, | ||
value: toHalfWidthColon(value), | ||
}, | ||
data: toHalfWidthColon(data), | ||
}; | ||
}; | ||
} |
2 changes: 1 addition & 1 deletion
2
...rs/fullwidth-to-halfwidth-preprocessor.ts → ...rs/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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {toHalfWidthColon} from '../to-half-width-colon'; | ||
|
||
describe('`toHalfWidthColon` utility converts full width colon to half width colon', () => { | ||
it(': => :', () => { | ||
expect(toHalfWidthColon(':')).toBe(':'); | ||
}); | ||
}); |
File renamed without changes.
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 @@ | ||
import {CHAR_COLON, CHAR_JP_COLON} from '../constants'; | ||
|
||
/** | ||
* Replace fullwidth colon with half width colon | ||
* @param fullWidthColon full width colon | ||
* @returns processed half width colon | ||
*/ | ||
export function toHalfWidthColon(fullWidthColon: string): string { | ||
return fullWidthColon.replace(new RegExp(CHAR_JP_COLON, 'g'), CHAR_COLON); | ||
} |
File renamed without changes.