Skip to content

Commit

Permalink
feat(kit): maskitoStringifyTime utility
Browse files Browse the repository at this point in the history
  • Loading branch information
demensky committed Jun 14, 2024
1 parent 83bfb31 commit 362a606
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions projects/kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {maskitoDateTimeOptionsGenerator} from './lib/masks/date-time';
export {maskitoNumberOptionsGenerator, maskitoParseNumber} from './lib/masks/number';
export {
maskitoParseTime,
maskitoStringifyTime,
MaskitoTimeOptions,
maskitoTimeOptionsGenerator,
} from './lib/masks/time';
Expand Down
2 changes: 1 addition & 1 deletion projects/kit/src/lib/masks/time/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {maskitoTimeOptionsGenerator} from './time-mask';
export {MaskitoTimeOptions} from './time-options';
export {maskitoParseTime} from './utils';
export {maskitoParseTime, maskitoStringifyTime} from './utils';
1 change: 1 addition & 0 deletions projects/kit/src/lib/masks/time/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './parse-time';
export * from './stringify-time';
44 changes: 44 additions & 0 deletions projects/kit/src/lib/masks/time/utils/stringify-time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {DEFAULT_TIME_SEGMENT_MAX_VALUES} from '../../../constants';
import type {MaskitoTimeSegments} from '../../../types';
import {padTimeSegments} from '../../../utils/time';
import type {MaskitoTimeOptions} from '../time-options';

/**
* Converts milliseconds to a formatted time string based on the given `options.mode`.
*
* @param milliseconds unsigned integer milliseconds
* @param options
*/
export function maskitoStringifyTime(
milliseconds: number,
{mode, timeSegmentMaxValues = {}}: MaskitoTimeOptions,
): string {
const maxValues: MaskitoTimeSegments<number> = {
...DEFAULT_TIME_SEGMENT_MAX_VALUES,
...timeSegmentMaxValues,
};

const millisecondsInSecond = maxValues.milliseconds + 1;
const millisecondsInMinute = (maxValues.seconds + 1) * millisecondsInSecond;
const millisecondsInHour = (maxValues.minutes + 1) * millisecondsInMinute;

const hours = Math.trunc(milliseconds / millisecondsInHour);

milliseconds -= hours * millisecondsInHour;

const minutes = Math.trunc(milliseconds / millisecondsInMinute);

milliseconds -= minutes * millisecondsInMinute;

const seconds = Math.trunc(milliseconds / millisecondsInSecond);

milliseconds -= seconds * millisecondsInSecond;

const result = padTimeSegments({hours, minutes, seconds, milliseconds});

return mode
.replaceAll(/H+/g, result.hours)
.replaceAll('MSS', result.milliseconds)
.replaceAll(/M+/g, result.minutes)
.replaceAll(/S+/g, result.seconds);
}
66 changes: 66 additions & 0 deletions projects/kit/src/lib/masks/time/utils/tests/stringify-time.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {describe, expect, it} from '@jest/globals';
import type {MaskitoTimeMode} from '@maskito/kit';
import {maskitoStringifyTime} from '@maskito/kit';

describe('maskitoStringifyTime', () => {
const testCases = new Map<MaskitoTimeMode, Array<{text: string; ms: number}>>([
[
'HH:MM:SS.MSS',
[
{text: '00:00:00.000', ms: 0},
{text: '12:34:56.789', ms: 45296789},
{text: '23:59:59.999', ms: 86399999},
],
],
[
'HH:MM:SS',
[
{text: '00:00:00', ms: 0},
{text: '12:34:56', ms: 45296000},
{text: '23:59:59', ms: 86399000},
],
],
[
'HH:MM',
[
{text: '00:00', ms: 0},
{text: '12:34', ms: 45240000},
{text: '23:59', ms: 86340000},
],
],
[
'HH',
[
{text: '00', ms: 0},
{text: '12', ms: 43200000},
{text: '23', ms: 82800000},
],
],
[
'MM.SS.MSS',
[
{text: '00.00.000', ms: 0},
{text: '12.34.560', ms: 754560},
{text: '59.59.999', ms: 3599999},
],
],
[
'SS.MSS',
[
{text: '00.000', ms: 0},
{text: '12.345', ms: 12345},
{text: '59.999', ms: 59999},
],
],
]);

testCases.forEach((cases, mode) => {
describe(`mode ${mode}`, () => {
cases.forEach(({text, ms}) => {
it(`${ms}ms => '${text}'`, () => {
expect(maskitoStringifyTime(ms, {mode})).toBe(text);
});
});
});
});
});

0 comments on commit 362a606

Please sign in to comment.