Skip to content

Commit

Permalink
feat(guards): isJsonPrimitive
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Feb 1, 2023
1 parent e885589 commit 1c91304
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/guards/__tests__/is-json-primitive.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Tests - isJsonPrimitive
* @module tutils/guards/tests/unit-d/isJsonPrimitive
*/

import type { JsonPrimitive } from '#src/types'
import type testSubject from '../is-json-primitive'

describe('unit-d:guards/isJsonPrimitive', () => {
it('should guard JsonPrimitive', () => {
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<JsonPrimitive>()
})
})
25 changes: 25 additions & 0 deletions src/guards/__tests__/is-json-primitive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file Unit Tests - isJsonPrimitive
* @module tutils/guards/tests/unit/isJsonPrimitive
*/

import testSubject from '../is-json-primitive'

describe('unit:guards/isJsonPrimitive', () => {
it('should return false if value is not JSON primitive', () => {
expect(testSubject(faker.datatype.array())).to.be.false
})

it('should return true if value is JSON primitive', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
[faker.datatype.boolean()],
[faker.number.int()],
[faker.string.uuid()],
[null]
]

// Act + Expect
cases.forEach(([value]) => expect(testSubject(value)).to.be.true)
})
})
1 change: 1 addition & 0 deletions src/guards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export { default as isEmptyValue } from './is-empty-value'
export { default as isFloat } from './is-float'
export { default as isFunction } from './is-function'
export { default as isInt } from './is-int'
export { default as isJsonPrimitive } from './is-json-primitive'
export { default as isJwtType } from './is-jwt-type'
export { default as isNIL } from './is-nil'
export { default as isNodeEnv } from './is-node-env'
Expand Down
25 changes: 25 additions & 0 deletions src/guards/is-json-primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file Type Guards - isJsonPrimitive
* @module tutils/guards/isJsonPrimitive
*/

import type { JsonPrimitive } from '#src/types'
import isBoolean from './is-boolean'
import isNull from './is-null'
import isNumber from './is-number'
import isString from './is-string'

/**
* Checks if the given `value` is a [primitive][1] [`JSON` value][2].
*
* [1]: https://developer.mozilla.org/docs/Glossary/Primitive
* [2]: https://restfulapi.net/json-data-types
*
* @param {unknown} value - Value to evaluate
* @return {value is JsonPrimitive} `true` if `value` is primitive JSON value
*/
const isJsonPrimitive = (value: unknown): value is JsonPrimitive => {
return isBoolean(value) || isNumber(value) || isNull(value) || isString(value)
}

export default isJsonPrimitive

0 comments on commit 1c91304

Please sign in to comment.