Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support readonly array in MaskitoMaskExpression #1761

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {ElementState, MaskitoMaskExpression} from '../../../types';
import {isArray} from '../../../utils';
import {guessValidValueByPattern} from './guess-valid-value-by-pattern';
import {guessValidValueByRegExp} from './guess-valid-value-by-reg-exp';
import {validateValueWithMask} from './validate-value-with-mask';
Expand All @@ -12,12 +13,12 @@ export function calibrateValueByMask(
return elementState;
}

const {value, selection} = Array.isArray(mask)
const {value, selection} = isArray(mask)
? guessValidValueByPattern(elementState, mask, initialElementState)
: guessValidValueByRegExp(elementState, mask);

return {
selection,
value: Array.isArray(mask) ? value.slice(0, mask.length) : value,
value: isArray(mask) ? value.slice(0, mask.length) : value,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {ElementState} from '../../../types';
import {isFixedCharacter} from './is-fixed-character';

export function getLeadingFixedCharacters(
mask: Array<RegExp | string>,
mask: ReadonlyArray<RegExp | string>,
validatedValuePart: string,
newCharacter: string,
initialElementState: ElementState | null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {validateValueWithMask} from './validate-value-with-mask';

export function guessValidValueByPattern(
elementState: ElementState,
mask: Array<RegExp | string>,
mask: ReadonlyArray<RegExp | string>,
initialElementState: ElementState | null,
): ElementState {
let maskedFrom: number | null = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type {ElementState, MaskitoMaskExpression} from '../../../types';
import {isArray} from '../../../utils';
import {isFixedCharacter} from './is-fixed-character';

export function removeFixedMaskCharacters(
initialElementState: ElementState,
mask: MaskitoMaskExpression,
): ElementState {
if (!Array.isArray(mask)) {
if (!isArray(mask)) {
return initialElementState;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type {MaskitoMaskExpression} from '../../../types';
import {isArray} from '../../../utils';
import {isFixedCharacter} from './is-fixed-character';

export function validateValueWithMask(
value: string,
maskExpression: MaskitoMaskExpression,
): boolean {
if (Array.isArray(maskExpression)) {
if (isArray(maskExpression)) {
return (
value.length === maskExpression.length &&
Array.from(value).every((char, i) => {
Expand Down
2 changes: 1 addition & 1 deletion projects/core/src/lib/types/mask.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ElementState} from './element-state';

export type MaskitoMaskExpression = Array<RegExp | string> | RegExp;
export type MaskitoMaskExpression = ReadonlyArray<RegExp | string> | RegExp;

export type MaskitoMask =
| MaskitoMaskExpression
Expand Down
1 change: 1 addition & 0 deletions projects/core/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export * from './element-states-equality';
export * from './get-line-selection';
export * from './get-not-empty-selection';
export * from './get-word-selection';
export * from './is-array';
export * from './pipe';
export * from './transform';
7 changes: 7 additions & 0 deletions projects/core/src/lib/utils/is-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @internal
*/
export function isArray(value: any): value is readonly unknown[] {
// See (Jul 7, 2017): https://github.com/microsoft/TypeScript/issues/17002
return Array.isArray(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('DateTime | dateTimeSeparator', () => {
const dateTimeSeparators = [':', ';_', '_-_', '_at_'];

dateTimeSeparators.forEach((dateTimeSeparator) => {
const testCases: Array<{
const testCases: ReadonlyArray<{
typedDigits: string;
formattedDate: string;
formattedValue: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {openNumberPage} from './utils';

describe('Number | minus sign', () => {
const pseudoMinuses: Array<{value: string; name: string}> = [
const pseudoMinuses: ReadonlyArray<{value: string; name: string}> = [
{value: CHAR_HYPHEN, name: 'hyphen'},
{value: CHAR_EN_DASH, name: 'en-dash'},
{value: CHAR_EM_DASH, name: 'em-dash'},
Expand All @@ -18,7 +18,7 @@ describe('Number | minus sign', () => {
];

describe('can use hyphen, all kind of dashes and minus interchangeably', () => {
const minuses: Array<{value: string; name: string}> = [
const minuses: ReadonlyArray<{value: string; name: string}> = [
{
value: CHAR_HYPHEN,
name: 'hyphen',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,13 @@ export default class DateRangeMaskDocComponent implements GeneratorOptions {
'2025-05-10',
] as const;

protected readonly minLengthOptions: Array<Partial<MaskitoDateSegments<number>>> = [
{day: 3},
{day: 15},
];

protected readonly maxLengthOptions: Array<Partial<MaskitoDateSegments<number>>> = [
{day: 5},
{month: 1},
{year: 1},
];
protected readonly minLengthOptions: ReadonlyArray<
Partial<MaskitoDateSegments<number>>
> = [{day: 3}, {day: 15}];

protected readonly maxLengthOptions: ReadonlyArray<
Partial<MaskitoDateSegments<number>>
> = [{day: 5}, {month: 1}, {year: 1}];

protected minStr: string = this.minMaxOptions[0];
protected maxStr: string = this.minMaxOptions[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('DateTime | dateTimeSeparator', () => {
let options = MASKITO_DEFAULT_OPTIONS;

dateTimeSeparators.forEach((dateTimeSeparator) => {
const testCases: Array<{
const testCases: ReadonlyArray<{
typedDigits: string;
formattedValue: string;
timeMode: MaskitoTimeMode;
Expand Down
200 changes: 101 additions & 99 deletions projects/kit/src/lib/masks/time/utils/tests/parse-time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,131 +3,133 @@ import type {MaskitoTimeMode} from '@maskito/kit';
import {maskitoParseTime} from '@maskito/kit';

describe('maskitoParseTime', () => {
const testCases = new Map<MaskitoTimeMode, Array<{text: string; ms: number}>>([
const testCases = new Map<MaskitoTimeMode, ReadonlyArray<{text: string; ms: number}>>(
[
'HH:MM:SS.MSS',
[
{text: '', ms: 0},
{text: '00:00:00.000', ms: 0},
'HH:MM:SS.MSS',
[
{text: '', ms: 0},
{text: '00:00:00.000', ms: 0},

{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},
{text: '12:', ms: 43200000},
{text: '12:3', ms: 45000000},
{text: '12:30', ms: 45000000},
{text: '12:34', ms: 45240000},
{text: '12:34:', ms: 45240000},
{text: '12:34:5', ms: 45290000},
{text: '12:34:50', ms: 45290000},
{text: '12:34:56', ms: 45296000},
{text: '12:34:56.', ms: 45296000},
{text: '12:34:56.7', ms: 45296700},
{text: '12:34:56.70', ms: 45296700},
{text: '12:34:56.700', ms: 45296700},
{text: '12:34:56.78', ms: 45296780},
{text: '12:34:56.780', ms: 45296780},
{text: '12:34:56.789', ms: 45296789},
{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},
{text: '12:', ms: 43200000},
{text: '12:3', ms: 45000000},
{text: '12:30', ms: 45000000},
{text: '12:34', ms: 45240000},
{text: '12:34:', ms: 45240000},
{text: '12:34:5', ms: 45290000},
{text: '12:34:50', ms: 45290000},
{text: '12:34:56', ms: 45296000},
{text: '12:34:56.', ms: 45296000},
{text: '12:34:56.7', ms: 45296700},
{text: '12:34:56.70', ms: 45296700},
{text: '12:34:56.700', ms: 45296700},
{text: '12:34:56.78', ms: 45296780},
{text: '12:34:56.780', ms: 45296780},
{text: '12:34:56.789', ms: 45296789},

{text: '23:59:59.999', ms: 86399999},
{text: '23:59:59.999', ms: 86399999},
],
],
],
[
'HH:MM:SS',
[
{text: '', ms: 0},
{text: '00:00:00', ms: 0},
'HH:MM:SS',
[
{text: '', ms: 0},
{text: '00:00:00', ms: 0},

{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},
{text: '12:', ms: 43200000},
{text: '12:3', ms: 45000000},
{text: '12:30', ms: 45000000},
{text: '12:34', ms: 45240000},
{text: '12:34:', ms: 45240000},
{text: '12:34:5', ms: 45290000},
{text: '12:34:50', ms: 45290000},
{text: '12:34:56', ms: 45296000},
{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},
{text: '12:', ms: 43200000},
{text: '12:3', ms: 45000000},
{text: '12:30', ms: 45000000},
{text: '12:34', ms: 45240000},
{text: '12:34:', ms: 45240000},
{text: '12:34:5', ms: 45290000},
{text: '12:34:50', ms: 45290000},
{text: '12:34:56', ms: 45296000},

{text: '23:59:59', ms: 86399000},
{text: '23:59:59', ms: 86399000},
],
],
],
[
'HH:MM',
[
{text: '', ms: 0},
{text: '00:00', ms: 0},
'HH:MM',
[
{text: '', ms: 0},
{text: '00:00', ms: 0},

{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},
{text: '12:', ms: 43200000},
{text: '12:3', ms: 45000000},
{text: '12:30', ms: 45000000},
{text: '12:34', ms: 45240000},
{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},
{text: '12:', ms: 43200000},
{text: '12:3', ms: 45000000},
{text: '12:30', ms: 45000000},
{text: '12:34', ms: 45240000},

{text: '23:59', ms: 86340000},
{text: '23:59', ms: 86340000},
],
],
],
[
'HH',
[
{text: '', ms: 0},
{text: '00', ms: 0},
'HH',
[
{text: '', ms: 0},
{text: '00', ms: 0},

{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},
{text: '1', ms: 36000000},
{text: '10', ms: 36000000},
{text: '12', ms: 43200000},

{text: '23', ms: 82800000},
{text: '23', ms: 82800000},
],
],
],
[
'MM:SS.MSS',
[
{text: '', ms: 0},
{text: '00:00.000', ms: 0},
'MM:SS.MSS',
[
{text: '', ms: 0},
{text: '00:00.000', ms: 0},

{text: '1', ms: 600000},
{text: '10', ms: 600000},
{text: '12', ms: 720000},
{text: '12.', ms: 720000},
{text: '12:3', ms: 750000},
{text: '12:30', ms: 750000},
{text: '12:34', ms: 754000},
{text: '12:34.', ms: 754000},
{text: '12:34.5', ms: 754500},
{text: '12:34.50', ms: 754500},
{text: '12:34.500', ms: 754500},
{text: '12:34.56', ms: 754560},
{text: '12:34.560', ms: 754560},
{text: '12:34.567', ms: 754567},
{text: '1', ms: 600000},
{text: '10', ms: 600000},
{text: '12', ms: 720000},
{text: '12.', ms: 720000},
{text: '12:3', ms: 750000},
{text: '12:30', ms: 750000},
{text: '12:34', ms: 754000},
{text: '12:34.', ms: 754000},
{text: '12:34.5', ms: 754500},
{text: '12:34.50', ms: 754500},
{text: '12:34.500', ms: 754500},
{text: '12:34.56', ms: 754560},
{text: '12:34.560', ms: 754560},
{text: '12:34.567', ms: 754567},

{text: '59:59.999', ms: 3599999},
{text: '59:59.999', ms: 3599999},
],
],
],
[
'SS.MSS',
[
{text: '', ms: 0},
{text: '00.000', ms: 0},
'SS.MSS',
[
{text: '', ms: 0},
{text: '00.000', ms: 0},

{text: '1', ms: 10000},
{text: '10', ms: 10000},
{text: '12', ms: 12000},
{text: '12.', ms: 12000},
{text: '12.3', ms: 12300},
{text: '12.30', ms: 12300},
{text: '12.300', ms: 12300},
{text: '12.34', ms: 12340},
{text: '12.340', ms: 12340},
{text: '12.345', ms: 12345},
{text: '1', ms: 10000},
{text: '10', ms: 10000},
{text: '12', ms: 12000},
{text: '12.', ms: 12000},
{text: '12.3', ms: 12300},
{text: '12.30', ms: 12300},
{text: '12.300', ms: 12300},
{text: '12.34', ms: 12340},
{text: '12.340', ms: 12340},
{text: '12.345', ms: 12345},

{text: '59.999', ms: 59999},
{text: '59.999', ms: 59999},
],
],
],
]);
);

testCases.forEach((cases, mode) => {
describe(`mode ${mode}`, () => {
Expand Down
Loading
Loading