-
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.
- Loading branch information
1 parent
aac40ce
commit 36feb3b
Showing
10 changed files
with
160 additions
and
23 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
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
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
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 |
---|---|---|
|
@@ -10,8 +10,77 @@ type Types = { | |
}; | ||
|
||
declare module 'nodejs-vitals/validation' { | ||
/** | ||
* Checks whether or not all the items in the | ||
* given array are type of the given type | ||
* @param type Type to check the items in the array for | ||
* @param arr The array to check | ||
* @returns Whether or not all the items are | ||
* type of the given type | ||
* @example | ||
* ```javascript | ||
* const arr = [ 2, 4, 'Hello World!', 8 ]; | ||
* isArrayOf('number', arr); // false | ||
* ``` | ||
* @example | ||
* ```javascript | ||
* const arr = [ 2, 4, 8, 12 ]; | ||
* isArrayOf('number', arr); // true | ||
* ``` | ||
* @since `[email protected]` | ||
*/ | ||
export function isArrayOf<T extends keyof Types>( | ||
type: T, | ||
arr: unknown | ||
): arr is Types[T][]; | ||
/** | ||
* @enum Primitives in NodeJS | ||
* @since `[email protected]` | ||
*/ | ||
export enum Primitives { | ||
BIGINT = 'bigint', | ||
BOOLEAN = 'boolean', | ||
FUNCTION = 'function', | ||
NUMBER = 'number', | ||
OBJECT = 'object', | ||
STRING = 'string', | ||
SYMBOL = 'symbol', | ||
UNDEFINED = 'undefined', | ||
} | ||
/** | ||
* Checks if the given string is | ||
* the name of a primitive | ||
* @param value Value to check for | ||
* @returns Whether or not the given string is the name of a primitive | ||
* @example | ||
* ```javascript | ||
* isPrimitive('bigint'); // true | ||
* isPrimitive('number'); // true | ||
* isPrimitive('Hello World!'); // false | ||
* ``` | ||
* @since `[email protected]` | ||
*/ | ||
export function isPrimitive(value: unknown): value is keyof Types; | ||
/** | ||
* Throws an error if the type of given value does | ||
* not match the name of the given primitive | ||
* @param value Value to check | ||
* @param type Name of the primitive | ||
* @param argumentName Optional, the argument name, displayed in the error message | ||
* @example | ||
* ```javascript | ||
* function add(a, b) { | ||
* validatePrimitive(a, 'number', 'a'); // Throws an error if a | ||
* validatePrimitive(b, 'number', 'b'); // or b is not a number | ||
* | ||
* return a + b; | ||
* } | ||
* ``` | ||
* @since `[email protected]` | ||
*/ | ||
export function validatePrimitive( | ||
value: unknown, | ||
type: keyof Types, | ||
argumentName?: string | ||
): void; | ||
} |
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
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
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
const { validatePrimitive } = require('../validation'); | ||
|
||
module.exports = async function wait(ms) { | ||
validatePrimitive(ms, 'number', 'ms'); | ||
|
||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module.exports = { | ||
isArrayOf: require('./isArrayOf'), | ||
...require('./primitives'), | ||
validatePrimitive: require('./validatePrimitive'), | ||
}; |
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,18 @@ | ||
const Primitives = Object.freeze({ | ||
BIGINT: 'bigint', | ||
BOOLEAN: 'boolean', | ||
FUNCTION: 'function', | ||
NUMBER: 'number', | ||
OBJECT: 'object', | ||
STRING: 'string', | ||
SYMBOL: 'symbol', | ||
UNDEFINED: 'undefined', | ||
}); | ||
|
||
const primitives = Object.values(Primitives); | ||
|
||
function isPrimitive(value) { | ||
return primitives.includes(value); | ||
} | ||
|
||
module.exports = { Primitives, isPrimitive }; |
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,30 @@ | ||
const { isPrimitive } = require('./primitives'); | ||
|
||
module.exports = function validatePrimitive(value, type, argumentName) { | ||
if (!isPrimitive(type)) | ||
throw new TypeError( | ||
`The "type" argument must be a string representing a primitive. Received ${type}` | ||
); | ||
|
||
if (typeof value === type) return; | ||
|
||
argumentName = argumentName | ||
? `The "${argumentName}" argument` | ||
: 'The argument'; | ||
|
||
let actualType = typeof value; | ||
let primitive = true; | ||
if (actualType === 'object') | ||
try { | ||
actualType = Object.getPrototypeOf(value).constructor.name; | ||
primitive = false; | ||
} catch (error) {} | ||
|
||
const error = new TypeError( | ||
`${argumentName} must be of type ${type}. Received ${ | ||
primitive ? 'a type of' : 'an instance of' | ||
} ${actualType}` | ||
); | ||
error.code = 'ERR_INVALID_ARG_TYPE'; | ||
throw error; | ||
}; |