-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
045c4c6
commit 82c63b4
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |