Skip to content

Commit

Permalink
fix: add colon convert preprocessor, add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
rikusen0335 committed Feb 17, 2024
1 parent 8a91586 commit 7ef851b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 1 deletion.
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-time/date-time-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 {TIME_FIXED_CHARACTERS} from '../../constants';
import {
createColonConvertPreprocessor,
createDateSegmentsZeroPaddingPostprocessor,
createFirstDateEndSeparatorPreprocessor,
createFullWidthToHalfWidthPreprocessor,
Expand Down Expand Up @@ -43,6 +44,7 @@ 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/time/time-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 {DEFAULT_TIME_SEGMENT_MAX_VALUES, TIME_FIXED_CHARACTERS} from '../../constants';
import {
createColonConvertPreprocessor,
createFullWidthToHalfWidthPreprocessor,
createZeroPlaceholdersPreprocessor,
} from '../../processors';
Expand All @@ -27,6 +28,7 @@ export function maskitoTimeOptionsGenerator({
),
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 '../masks/number/utils/to-half-width-number';
import {toHalfWidthNumber} from '../utils';

/**
* Convert full width numbers like 1, 2 to half width numbers 1, 2
Expand Down
1 change: 1 addition & 0 deletions projects/kit/src/lib/processors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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';
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 7ef851b

Please sign in to comment.