generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export {maskitoTimeOptionsGenerator} from './time-mask'; | ||
export {MaskitoTimeOptions} from './time-options'; | ||
export {maskitoParseTime} from './utils'; |
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 @@ | ||
export * from './parse-time'; |
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 {DEFAULT_TIME_SEGMENT_MAX_VALUES} from '../../../constants'; | ||
import type {MaskitoTimeSegments} from '../../../types'; | ||
import {padTimeSegments, parseTimeString} from '../../../utils/time'; | ||
import type {MaskitoTimeOptions} from '../time-options'; | ||
|
||
/** | ||
* Converts a formatted time string to milliseconds based on the given `options.mode`. | ||
* | ||
* @param maskedTime a formatted time string by {@link maskitoTimeOptionsGenerator} or {@link maskitoStringifyTime} | ||
* @param options | ||
*/ | ||
export function maskitoParseTime( | ||
maskedTime: string, | ||
{mode, timeSegmentMaxValues = {}}: MaskitoTimeOptions, | ||
): number { | ||
const maxValues: MaskitoTimeSegments<number> = { | ||
...DEFAULT_TIME_SEGMENT_MAX_VALUES, | ||
...timeSegmentMaxValues, | ||
}; | ||
|
||
const msInSecond = maxValues.milliseconds + 1; | ||
const msInMinute = (maxValues.seconds + 1) * msInSecond; | ||
const msInHour = (maxValues.minutes + 1) * msInMinute; | ||
|
||
const parsedTime = padTimeSegments(parseTimeString(maskedTime, mode)); | ||
|
||
return ( | ||
Number(parsedTime.hours ?? '') * msInHour + | ||
Number(parsedTime.minutes ?? '') * msInMinute + | ||
Number(parsedTime.seconds ?? '') * msInSecond + | ||
Number(parsedTime.milliseconds ?? '') | ||
); | ||
} |
125 changes: 125 additions & 0 deletions
125
projects/kit/src/lib/masks/time/utils/tests/parse-time.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,125 @@ | ||
import {describe, expect, it} from '@jest/globals'; | ||
import type {MaskitoTimeMode, MaskitoTimeOptions} from '@maskito/kit'; | ||
import {maskitoParseTime} from '@maskito/kit'; | ||
|
||
const options: MaskitoTimeOptions = {mode: 'HH:MM:SS.MSS'}; | ||
|
||
describe('maskitoParseTime', () => { | ||
const testCases: Record<MaskitoTimeMode, Array<{ms: number; text: string}>> = { | ||
'HH:MM:SS.MSS': [ | ||
{ms: 0, text: ''}, | ||
{ms: 0, text: '00:00:00.000'}, | ||
|
||
{ms: 36000000, text: '1'}, | ||
{ms: 36000000, text: '10'}, | ||
{ms: 43200000, text: '12'}, | ||
{ms: 43200000, text: '12:'}, | ||
{ms: 45000000, text: '12:3'}, | ||
{ms: 45000000, text: '12:30'}, | ||
{ms: 45240000, text: '12:34'}, | ||
{ms: 45240000, text: '12:34:'}, | ||
{ms: 45290000, text: '12:34:5'}, | ||
{ms: 45290000, text: '12:34:50'}, | ||
{ms: 45296000, text: '12:34:56'}, | ||
{ms: 45296000, text: '12:34:56.'}, | ||
{ms: 45296700, text: '12:34:56.7'}, | ||
{ms: 45296700, text: '12:34:56.70'}, | ||
{ms: 45296700, text: '12:34:56.700'}, | ||
{ms: 45296780, text: '12:34:56.78'}, | ||
{ms: 45296780, text: '12:34:56.780'}, | ||
{ms: 45296789, text: '12:34:56.789'}, | ||
|
||
{ms: 86399999, text: '23:59:59.999'}, | ||
], | ||
'HH:MM:SS': [ | ||
{ms: 0, text: ''}, | ||
{ms: 0, text: '00:00:00'}, | ||
|
||
{ms: 36000000, text: '1'}, | ||
{ms: 36000000, text: '10'}, | ||
{ms: 43200000, text: '12'}, | ||
{ms: 43200000, text: '12:'}, | ||
{ms: 45000000, text: '12:3'}, | ||
{ms: 45000000, text: '12:30'}, | ||
{ms: 45240000, text: '12:34'}, | ||
{ms: 45240000, text: '12:34:'}, | ||
{ms: 45290000, text: '12:34:5'}, | ||
{ms: 45290000, text: '12:34:50'}, | ||
{ms: 45296000, text: '12:34:56'}, | ||
|
||
{ms: 86399000, text: '23:59:59'}, | ||
], | ||
'HH:MM': [ | ||
{ms: 0, text: ''}, | ||
{ms: 0, text: '00:00'}, | ||
|
||
{ms: 36000000, text: '1'}, | ||
{ms: 36000000, text: '10'}, | ||
{ms: 43200000, text: '12'}, | ||
{ms: 43200000, text: '12:'}, | ||
{ms: 45000000, text: '12:3'}, | ||
{ms: 45000000, text: '12:30'}, | ||
{ms: 45240000, text: '12:34'}, | ||
|
||
{ms: 86340000, text: '23:59'}, | ||
], | ||
HH: [ | ||
{ms: 0, text: ''}, | ||
{ms: 0, text: '00'}, | ||
|
||
{ms: 36000000, text: '1'}, | ||
{ms: 36000000, text: '10'}, | ||
{ms: 43200000, text: '12'}, | ||
|
||
{ms: 82800000, text: '23'}, | ||
], | ||
'MM.SS.MSS': [ | ||
{ms: 0, text: ''}, | ||
{ms: 0, text: '00:00.000'}, | ||
|
||
{ms: 36000000, text: '1'}, | ||
{ms: 36000000, text: '10'}, | ||
{ms: 43200000, text: '12'}, | ||
{ms: 43200000, text: '12:'}, | ||
{ms: 45000000, text: '12:3'}, | ||
{ms: 45000000, text: '12:30'}, | ||
{ms: 45240000, text: '12:34'}, | ||
{ms: 45240000, text: '12:34.'}, | ||
{ms: 45290000, text: '12:34.5'}, | ||
{ms: 45290000, text: '12:34.50'}, | ||
{ms: 45290000, text: '12:34.500'}, | ||
{ms: 45296000, text: '12:34.56'}, | ||
{ms: 45296000, text: '12:34.560'}, | ||
{ms: 45296700, text: '12:34.567'}, | ||
|
||
{ms: 216039900, text: '59:59.999'}, | ||
], | ||
'SS.MSS': [ | ||
{ms: 0, text: ''}, | ||
{ms: 0, text: '00.000'}, | ||
|
||
{ms: 36000000, text: '1'}, | ||
{ms: 36000000, text: '10'}, | ||
{ms: 43200000, text: '12'}, | ||
{ms: 43200000, text: '12.'}, | ||
{ms: 45000000, text: '12.3'}, | ||
{ms: 45000000, text: '12.30'}, | ||
{ms: 45000000, text: '12.300'}, | ||
{ms: 45240000, text: '12.34'}, | ||
{ms: 45240000, text: '12.340'}, | ||
{ms: 45290000, text: '12.345'}, | ||
|
||
{ms: 218430000, text: '59.999'}, | ||
], | ||
}; | ||
|
||
Object.entries(testCases).forEach(([mode, cases]) => { | ||
describe(`mode ${mode}`, () => { | ||
cases.forEach(({text, ms}) => { | ||
it(`'${text}' => ${ms}ms`, () => { | ||
expect(maskitoParseTime(text, options)).toBe(ms); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |