From 1c91304c90438c44357a50ae7994ec97a63d9cc2 Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Wed, 1 Feb 2023 15:51:15 -0500 Subject: [PATCH] feat(guards): `isJsonPrimitive` Signed-off-by: Lexus Drumgold --- .../__tests__/is-json-primitive.spec-d.ts | 13 ++++++++++ .../__tests__/is-json-primitive.spec.ts | 25 +++++++++++++++++++ src/guards/index.ts | 1 + src/guards/is-json-primitive.ts | 25 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 src/guards/__tests__/is-json-primitive.spec-d.ts create mode 100644 src/guards/__tests__/is-json-primitive.spec.ts create mode 100644 src/guards/is-json-primitive.ts diff --git a/src/guards/__tests__/is-json-primitive.spec-d.ts b/src/guards/__tests__/is-json-primitive.spec-d.ts new file mode 100644 index 00000000..c3136005 --- /dev/null +++ b/src/guards/__tests__/is-json-primitive.spec-d.ts @@ -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().guards.toEqualTypeOf() + }) +}) diff --git a/src/guards/__tests__/is-json-primitive.spec.ts b/src/guards/__tests__/is-json-primitive.spec.ts new file mode 100644 index 00000000..3cabaccd --- /dev/null +++ b/src/guards/__tests__/is-json-primitive.spec.ts @@ -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[] = [ + [faker.datatype.boolean()], + [faker.number.int()], + [faker.string.uuid()], + [null] + ] + + // Act + Expect + cases.forEach(([value]) => expect(testSubject(value)).to.be.true) + }) +}) diff --git a/src/guards/index.ts b/src/guards/index.ts index 0d84c9a5..4d4951a7 100644 --- a/src/guards/index.ts +++ b/src/guards/index.ts @@ -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' diff --git a/src/guards/is-json-primitive.ts b/src/guards/is-json-primitive.ts new file mode 100644 index 00000000..e77e4a28 --- /dev/null +++ b/src/guards/is-json-primitive.ts @@ -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