Skip to content

Commit

Permalink
feat(kit): add full-width numbers support for Time, Date, `DateTi…
Browse files Browse the repository at this point in the history
…me`, `DateRange` (#1043)
  • Loading branch information
rikusen0335 authored Feb 19, 2024
1 parent 3984f63 commit 434c9c5
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 6 deletions.
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);
});
});
});
});
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);
});
});
});
});
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);
});
});
});
});
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);
});
});
});
});
});
14 changes: 14 additions & 0 deletions projects/kit/src/lib/constants/unicode-characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ export const CHAR_MINUS = '\u2212';
* is used as prolonged sounds in Japanese.
*/
export const CHAR_JP_HYPHEN = '\u30FC';

/**
* {@link https://symbl.cc/en/003A/ Colon}
* is a punctuation mark that connects parts of a text logically.
* ---
* is also used as separator in time.
*/
export const CHAR_COLON = '\u003A';

/**
* {@link https://symbl.cc/en/FF1A/ Full-width colon}
* is a full-width punctuation mark used to separate parts of a text commonly in Japanese.
*/
export const CHAR_JP_COLON = '\uFF1A';
2 changes: 2 additions & 0 deletions projects/kit/src/lib/masks/date-range/date-range-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {CHAR_EN_DASH, CHAR_NO_BREAK_SPACE} from '../../constants';
import {
createDateSegmentsZeroPaddingPostprocessor,
createFirstDateEndSeparatorPreprocessor,
createFullWidthToHalfWidthPreprocessor,
createMinMaxDatePostprocessor,
createValidDatePreprocessor,
createZeroPlaceholdersPreprocessor,
Expand Down Expand Up @@ -42,6 +43,7 @@ export function maskitoDateRangeOptionsGenerator({
mask: [...dateMask, ...Array.from(rangeSeparator), ...dateMask],
overwriteMode: 'replace',
preprocessors: [
createFullWidthToHalfWidthPreprocessor(),
createFirstDateEndSeparatorPreprocessor({
dateModeTemplate,
dateSegmentSeparator: dateSeparator,
Expand Down
4 changes: 4 additions & 0 deletions projects/kit/src/lib/masks/date-time/date-time-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions} from '@maskito/core';

import {TIME_FIXED_CHARACTERS} from '../../constants';
import {
createColonConvertPreprocessor,
createDateSegmentsZeroPaddingPostprocessor,
createFirstDateEndSeparatorPreprocessor,
createFullWidthToHalfWidthPreprocessor,
createZeroPlaceholdersPreprocessor,
normalizeDatePreprocessor,
} from '../../processors';
Expand Down Expand Up @@ -41,6 +43,8 @@ export function maskitoDateTimeOptionsGenerator({
],
overwriteMode: 'replace',
preprocessors: [
createFullWidthToHalfWidthPreprocessor(),
createColonConvertPreprocessor(),
createFirstDateEndSeparatorPreprocessor({
dateModeTemplate,
dateSegmentSeparator: dateSeparator,
Expand Down
2 changes: 2 additions & 0 deletions projects/kit/src/lib/masks/date/date-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions} from '@maskito/core';

import {
createDateSegmentsZeroPaddingPostprocessor,
createFullWidthToHalfWidthPreprocessor,
createMinMaxDatePostprocessor,
createValidDatePreprocessor,
createZeroPlaceholdersPreprocessor,
Expand Down Expand Up @@ -29,6 +30,7 @@ export function maskitoDateOptionsGenerator({
),
overwriteMode: 'replace',
preprocessors: [
createFullWidthToHalfWidthPreprocessor(),
createZeroPlaceholdersPreprocessor(),
normalizeDatePreprocessor({
dateModeTemplate,
Expand Down
3 changes: 1 addition & 2 deletions projects/kit/src/lib/masks/date/tests/date-mask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ describe('Date (maskitoTransform)', () => {
});
});

// TODO: https://github.com/taiga-family/maskito/pull/907
xit('accepts full width characters', () => {
it('accepts full width characters', () => {
expect(maskitoTransform('12345', options)).toBe('1234/05');
expect(maskitoTransform('12341226', options)).toBe('1234/12/26');
});
Expand Down
2 changes: 1 addition & 1 deletion projects/kit/src/lib/masks/number/number-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CHAR_ZERO_WIDTH_SPACE,
} from '../../constants';
import {
createFullWidthToHalfWidthPreprocessor,
maskitoPostfixPostprocessorGenerator,
maskitoPrefixPostprocessorGenerator,
} from '../../processors';
Expand All @@ -21,7 +22,6 @@ import {
import {
createAffixesFilterPreprocessor,
createDecimalZeroPaddingPostprocessor,
createFullWidthToHalfWidthPreprocessor,
createInitializationOnlyPreprocessor,
createMinMaxPostprocessor,
createNonRemovableCharsDeletionPreprocessor,
Expand Down
1 change: 0 additions & 1 deletion projects/kit/src/lib/masks/number/processors/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './affixes-filter-preprocessor';
export * from './decimal-zero-padding-postprocessor';
export * from './fullwidth-to-halfwidth-preprocessor';
export * from './initialization-only-preprocessor';
export * from './leading-zeroes-validation-postprocessor';
export * from './min-max-postprocessor';
Expand Down
8 changes: 7 additions & 1 deletion projects/kit/src/lib/masks/time/time-mask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions} from '@maskito/core';

import {DEFAULT_TIME_SEGMENT_MAX_VALUES, TIME_FIXED_CHARACTERS} from '../../constants';
import {createZeroPlaceholdersPreprocessor} from '../../processors';
import {
createColonConvertPreprocessor,
createFullWidthToHalfWidthPreprocessor,
createZeroPlaceholdersPreprocessor,
} from '../../processors';
import {MaskitoTimeMode, MaskitoTimeSegments} from '../../types';
import {createMaxValidationPreprocessor} from './processors';

Expand All @@ -23,6 +27,8 @@ export function maskitoTimeOptionsGenerator({
TIME_FIXED_CHARACTERS.includes(char) ? char : /\d/,
),
preprocessors: [
createFullWidthToHalfWidthPreprocessor(),
createColonConvertPreprocessor(),
createZeroPlaceholdersPreprocessor(),
createMaxValidationPreprocessor(enrichedTimeSegmentMaxValues),
],
Expand Down
20 changes: 20 additions & 0 deletions projects/kit/src/lib/processors/colon-convert-preprocessor.ts
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),
};
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {MaskitoPreprocessor} from '@maskito/core';

import {toHalfWidthNumber} from '../utils/to-half-width-number';
import {toHalfWidthNumber} from '../utils';

/**
* Convert full width numbers like 1, 2 to half width numbers 1, 2
Expand Down
2 changes: 2 additions & 0 deletions projects/kit/src/lib/processors/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export {createColonConvertPreprocessor} from './colon-convert-preprocessor';
export {createDateSegmentsZeroPaddingPostprocessor} from './date-segments-zero-padding-postprocessor';
export {createFirstDateEndSeparatorPreprocessor} from './first-date-end-separator-preprocessor';
export {createFullWidthToHalfWidthPreprocessor} from './fullwidth-to-halfwidth-preprocessor';
export {createMinMaxDatePostprocessor} from './min-max-date-postprocessor';
export {normalizeDatePreprocessor} from './normalize-date-preprocessor';
export {maskitoPostfixPostprocessorGenerator} from './postfix-postprocessor';
Expand Down
2 changes: 2 additions & 0 deletions projects/kit/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export * from './find-common-beginning-substr';
export * from './identity';
export * from './is-empty';
export * from './pad-with-zeroes-until-valid';
export * from './to-half-width-colon';
export * from './to-half-width-number';
7 changes: 7 additions & 0 deletions projects/kit/src/lib/utils/tests/to-half-width-colon.spec.ts
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(':');
});
});
10 changes: 10 additions & 0 deletions projects/kit/src/lib/utils/to-half-width-colon.ts
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);
}

0 comments on commit 434c9c5

Please sign in to comment.