Skip to content

Commit

Permalink
feat(guards): isUnixTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Feb 6, 2022
1 parent cc80fc9 commit 4401dc8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = {
'duid',
'enum',
'enums',
'uid'
'uid',
'unix'
]
}
]
Expand Down
24 changes: 24 additions & 0 deletions src/guards/__tests__/is-unix-timestamp.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Testcase } from '@tests/utils/types'
import testSubject from '../is-number-string.guard'

/**
* @file Unit Tests - isUnixTimestamp
* @module tutils/guards/tests/unit/isUnixTimestamp
*/

describe('unit:guards/isUnixTimestamp', () => {
type Case = Testcase<boolean> & { state: string; value: any }

const cases: Case[] = [
{ expected: false, state: 'non-number value', value: null },
{ expected: true, state: 'unix timestamp', value: Date.now() }
]

it.each<Case>(cases)('should return $expected given $state', testcase => {
// Arrange
const { expected, value } = testcase

// Act + Expect
expect(testSubject(value)).toBe(expected)
})
})
1 change: 1 addition & 0 deletions src/guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { default as isEmptyValue } from './is-empty-value.guard'
export { default as isNIL } from './is-nil.guard'
export { default as isNodeEnv } from './is-node-env.guard'
export { default as isNumberString } from './is-number-string.guard'
export { default as isUnixTimestamp } from './is-unix-timestamp.guard'
18 changes: 18 additions & 0 deletions src/guards/is-unix-timestamp.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Type Guards - isUnixTimestamp
* @module tutils/guards/isUnixTimestamp
*/

/**
* Checks if `timestamp` is a valid [unix timestamp][1].
*
* [1]: https://unixtimestamp.com
*
* @param {any} timestamp - Value to check
* @return {boolean} `true` if unix timestamp, `false` otherwise
*/
const isUnixTimestamp = (timestamp: any): boolean => {
return typeof timestamp === 'number' && new Date(timestamp).getTime() > 0
}

export default isUnixTimestamp

0 comments on commit 4401dc8

Please sign in to comment.