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 13, 2024
1 parent 4e3f591 commit c843bc5
Show file tree
Hide file tree
Showing 5 changed files with 66 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);
}
19 changes: 19 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,19 @@
import {describe, expect, it} from '@jest/globals';
import type {MaskitoTimeOptions} from '@maskito/kit';
import {maskitoStringifyTime} from '@maskito/kit';

describe('maskitoStringifyTime', () => {
const options: MaskitoTimeOptions = {mode: 'HH:MM:SS.MSS'};

it('smallest value', () => {
expect(maskitoStringifyTime(0, options)).toBe('00:00:00.000');
});

it('unique numbers', () => {
expect(maskitoStringifyTime(45296789, options)).toBe('12:34:56.789');
});

it('biggest value', () => {
expect(maskitoStringifyTime(86399999, options)).toBe('23:59:59.999');
});
});

0 comments on commit c843bc5

Please sign in to comment.