Skip to content

Commit

Permalink
feat(internal): validateMap
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Feb 17, 2023
1 parent 045c4c6 commit 82c63b4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/internal/__tests__/validate-map.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<K, V>', () => {
// Arrange
type Expected = Map<string, unknown>

// Expect
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<Expected>()
})
})
36 changes: 36 additions & 0 deletions src/internal/__tests__/validate-map.spec.ts
Original file line number Diff line number Diff line change
@@ -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<TypeError>

// 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)
})
})
33 changes: 33 additions & 0 deletions src/internal/validate-map.ts
Original file line number Diff line number Diff line change
@@ -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<K, V>} `true` if `value` is a {@linkcode Map}
* @throws {NodeError<TypeError>} If `value` is not a {@linkcode Map}
*/
function validateMap<K extends string = string, V = unknown>(
value: unknown,
name: string
): value is Map<K, V> {
if (value instanceof Map) return true
throw new ERR_INVALID_ARG_TYPE(name, ['Map'], value)
}

export default validateMap

0 comments on commit 82c63b4

Please sign in to comment.