Skip to content

Commit

Permalink
feat(types): TimestampFormat, Timestamp
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 15, 2023
1 parent 3632179 commit 35fdbd3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/types/__tests__/timestamp-format.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Type Tests - TimestampFormat
* @module tutils/types/tests/unit-d/TimestampFormat
*/

import type TestSubject from '../timestamp-format'

describe('unit-d:types/TimestampFormat', () => {
it('should extract "iso"', () => {
expectTypeOf<TestSubject>().extract<'iso'>().not.toBeNever()
})

it('should extract "unix"', () => {
expectTypeOf<TestSubject>().extract<'unix'>().not.toBeNever()
})
})
24 changes: 24 additions & 0 deletions src/types/__tests__/timestamp.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file Type Tests - Timestamp
* @module tutils/types/tests/unit-d/Timestamp
*/

import type { tag } from '../opaque'
import type TestSubject from '../timestamp'
import type TimestampFormat from '../timestamp-format'

describe('unit-d:types/Timestamp', () => {
it('should extend number if Format extends "unix"', () => {
expectTypeOf<TestSubject<'unix'>>().toMatchTypeOf<number>()
})

it('should extend string if Format extends "iso"', () => {
expectTypeOf<TestSubject<'iso'>>().toMatchTypeOf<string>()
})

it('should match [readonly [tag]: "$timestamp"]', () => {
expectTypeOf<TestSubject<TimestampFormat>>().toMatchTypeOf<{
readonly [tag]: '$timestamp'
}>()
})
})
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,7 @@ export type { default as Promisable } from './promisable'
export type { default as Simplify } from './simplify'
export type { default as Split } from './split'
export type { default as Stringafiable } from './stringafiable'
export type { default as Timestamp } from './timestamp'
export type { default as TimestampFormat } from './timestamp-format'
export type { default as TupleToUnion } from './tuple-to-union'
export type { default as TypedArray } from './typed-array'
14 changes: 14 additions & 0 deletions src/types/timestamp-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file Type Definitions - TimestampFormat
* @module tutils/types/TimestampFormat
*/

/**
* Timestamp formats.
*
* @see https://en.wikipedia.org/wiki/ISO_8601
* @see https://unixtimestamp.com
*/
type TimestampFormat = 'iso' | 'unix'

export type { TimestampFormat as default }
26 changes: 26 additions & 0 deletions src/types/timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file Type Definitions - Timestamp
* @module tutils/types/Timestamp
*/

import type NumberString from './number-string'
import type Opaque from './opaque'
import type TimestampFormat from './timestamp-format'

/**
* An [ISO 8601][1] or [unix][2] timestamp.
*
* [1]: https://en.wikipedia.org/wiki/ISO_8601
* [2]: https://unixtimestamp.com
*
* @see {@linkcode TimestampFormat}
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date
*
* @template Format - Timestamp format
*/
type Timestamp<Format extends TimestampFormat> = Opaque<
Format extends 'unix' ? number : Format extends 'iso' ? string : NumberString,
'$timestamp'
>

export type { Timestamp as default }

0 comments on commit 35fdbd3

Please sign in to comment.