From 82c63b467615c0e799c4bb9d4bd1d35c5ac2ca1e Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Thu, 16 Feb 2023 20:40:46 -0500 Subject: [PATCH] feat(internal): `validateMap` Signed-off-by: Lexus Drumgold --- src/internal/__tests__/validate-map.spec-d.ts | 16 +++++++++ src/internal/__tests__/validate-map.spec.ts | 36 +++++++++++++++++++ src/internal/validate-map.ts | 33 +++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/internal/__tests__/validate-map.spec-d.ts create mode 100644 src/internal/__tests__/validate-map.spec.ts create mode 100644 src/internal/validate-map.ts diff --git a/src/internal/__tests__/validate-map.spec-d.ts b/src/internal/__tests__/validate-map.spec-d.ts new file mode 100644 index 00000000..75dfcf87 --- /dev/null +++ b/src/internal/__tests__/validate-map.spec-d.ts @@ -0,0 +1,16 @@ +/** + * @file Type Tests - validateMap + * @module mlly/internal/tests/unit-d/validateMap + */ + +import type testSubject from '../validate-map' + +describe('unit-d:internal/validateMap', () => { + it('should guard Map', () => { + // Arrange + type Expected = Map + + // Expect + expectTypeOf().guards.toEqualTypeOf() + }) +}) diff --git a/src/internal/__tests__/validate-map.spec.ts b/src/internal/__tests__/validate-map.spec.ts new file mode 100644 index 00000000..8856b29c --- /dev/null +++ b/src/internal/__tests__/validate-map.spec.ts @@ -0,0 +1,36 @@ +/** + * @file Unit Tests - validateMap + * @module mlly/internal/tests/unit/validateMap + */ + +import { ErrorCode, type NodeError } from '@flex-development/errnode' +import testSubject from '../validate-map' + +describe('unit:internal/validateMap', () => { + let name: string + + beforeEach(() => { + name = 'options.extension_format_map' + }) + + it('should return true if value is an instance of Map', () => { + expect(testSubject(new Map(), name)).to.be.true + }) + + it('should throw if value is not an instance of Map', () => { + // Arrange + const code: ErrorCode = ErrorCode.ERR_INVALID_ARG_TYPE + let error: NodeError + + // Act + try { + testSubject(new Set(), name) + } catch (e: unknown) { + error = e as typeof error + } + + // Expect + expect(error!).to.be.instanceof(TypeError) + expect(error!).to.have.property('code').equal(code) + }) +}) diff --git a/src/internal/validate-map.ts b/src/internal/validate-map.ts new file mode 100644 index 00000000..94fe854c --- /dev/null +++ b/src/internal/validate-map.ts @@ -0,0 +1,33 @@ +/** + * @file Internal - validateMap + * @module mlly/internal/validateMap + */ + +import { ERR_INVALID_ARG_TYPE, type NodeError } from '@flex-development/errnode' + +/** + * Checks if given `value` is a {@linkcode Map}. + * + * Throws [`ERR_INVALID_ARG_TYPE`][1] if the `value` is not a {@linkcode Map}. + * + * [1]: https://nodejs.org/api/errors.html#err_invalid_arg_value + * + * @see {@linkcode ERR_INVALID_ARG_TYPE} + * + * @template K - Map key type(s) + * @template V - Item type(s) + * + * @param {unknown} value - Value supplied by user + * @param {string} name - Name of invalid argument or property + * @return {value is Map} `true` if `value` is a {@linkcode Map} + * @throws {NodeError} If `value` is not a {@linkcode Map} + */ +function validateMap( + value: unknown, + name: string +): value is Map { + if (value instanceof Map) return true + throw new ERR_INVALID_ARG_TYPE(name, ['Map'], value) +} + +export default validateMap