diff --git a/projects/kit/src/index.ts b/projects/kit/src/index.ts index 8bace04e9..1d306426e 100644 --- a/projects/kit/src/index.ts +++ b/projects/kit/src/index.ts @@ -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'; diff --git a/projects/kit/src/lib/masks/time/index.ts b/projects/kit/src/lib/masks/time/index.ts index 0f58eaa5c..fdaafd0f7 100644 --- a/projects/kit/src/lib/masks/time/index.ts +++ b/projects/kit/src/lib/masks/time/index.ts @@ -1,3 +1,3 @@ export {maskitoTimeOptionsGenerator} from './time-mask'; export {MaskitoTimeOptions} from './time-options'; -export {maskitoParseTime} from './utils'; +export {maskitoParseTime, maskitoStringifyTime} from './utils'; diff --git a/projects/kit/src/lib/masks/time/utils/index.ts b/projects/kit/src/lib/masks/time/utils/index.ts index 5f953b5cb..d95c80596 100644 --- a/projects/kit/src/lib/masks/time/utils/index.ts +++ b/projects/kit/src/lib/masks/time/utils/index.ts @@ -1 +1,2 @@ export * from './parse-time'; +export * from './stringify-time'; diff --git a/projects/kit/src/lib/masks/time/utils/stringify-time.ts b/projects/kit/src/lib/masks/time/utils/stringify-time.ts new file mode 100644 index 000000000..ffcb20194 --- /dev/null +++ b/projects/kit/src/lib/masks/time/utils/stringify-time.ts @@ -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 = { + ...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); +} diff --git a/projects/kit/src/lib/masks/time/utils/tests/stringify-time.spec.ts b/projects/kit/src/lib/masks/time/utils/tests/stringify-time.spec.ts new file mode 100644 index 000000000..605105c6e --- /dev/null +++ b/projects/kit/src/lib/masks/time/utils/tests/stringify-time.spec.ts @@ -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'); + }); +});