@santi100/assertion-lib
Advanced tools
Comparing version 1.0.6 to 1.0.7
# Changelog | ||
# Version 1.0.6 | ||
- Added `assertTypeOf`, `assertOneOf`, `assertInteger`, `assertMin`, `assertMax` and `assertRange`. | ||
## Version 1.0.6 | ||
- Added `assertTypeOf`, `assertOneOf`, `assertInteger`, `assertMin`, `assertMax` and `assertRange`. | ||
## Version 1.0.7 | ||
- Modified `assertMax`, `assertMin` and `assertRange` to throw `RangeError`s instead of `TypeError`s. | ||
- Added `assertArray`. |
@@ -63,3 +63,3 @@ export declare class AssertionError<E = unknown, A = unknown> extends Error implements AssertOptionalParams<E, A> { | ||
*/ | ||
export declare function assertTypeOf(arg: any, expectedType: Type, name: string): void; | ||
export declare function assertTypeOf(arg: any, expectedType: Type, name?: string): void; | ||
/** | ||
@@ -80,5 +80,5 @@ * Asserts `arg` is one of `choices`. Throws a `TypeError` otherwise. | ||
*/ | ||
export declare function assertInteger(arg: number, name: string): void; | ||
export declare function assertInteger(arg: number, name?: string): void; | ||
/** | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -91,3 +91,3 @@ * @param arg Any value. | ||
/** | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -100,3 +100,3 @@ * @param arg Any value. | ||
/** | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `TypeError` otherwise. | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `RangeError` otherwise. | ||
* | ||
@@ -109,2 +109,9 @@ * @param arg Any value. | ||
export declare function assertRange(arg: any, name: string, min: any, max: any): void; | ||
/** | ||
* Asserts `arg` is an Array. Throws a `TypeError` otherwise. | ||
* | ||
* @param arg Any value. | ||
* @param name The name of the expression for `arg`. | ||
*/ | ||
export declare function assertArray(arg: any, name?: string): void; | ||
export {}; |
@@ -18,3 +18,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.assertRange = exports.assertMax = exports.assertMin = exports.assertInteger = exports.assertOneOf = exports.assertTypeOf = exports.assertType = exports.assert = exports.AssertionError = void 0; | ||
exports.assertArray = exports.assertRange = exports.assertMax = exports.assertMin = exports.assertInteger = exports.assertOneOf = exports.assertTypeOf = exports.assertType = exports.assert = exports.AssertionError = void 0; | ||
var equal_lib_1 = require("@santi100/equal-lib"); | ||
@@ -85,2 +85,3 @@ function indexOf(arr, item) { | ||
function assertTypeOf(arg, expectedType, name) { | ||
if (name === void 0) { name = 'arg'; } | ||
var TYPES = [ | ||
@@ -96,4 +97,3 @@ 'string', | ||
]; | ||
if (TYPES.indexOf(expectedType) === -1) | ||
throw new TypeError("".concat(name, " must be one of ").concat(TYPES.join(', '), ". Got \"").concat(arg, " of type \"").concat(typeof arg, "\".")); | ||
assertOneOf(expectedType, 'expectedType', TYPES); | ||
if (typeof arg !== expectedType) | ||
@@ -126,2 +126,3 @@ throw new TypeError("\"".concat(name, "\" must be of type \"").concat(expectedType, "\". Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
function assertInteger(arg, name) { | ||
if (name === void 0) { name = 'arg'; } | ||
if (!__isInteger(arg)) | ||
@@ -132,3 +133,3 @@ throw new TypeError("\"".concat(name, "\" must be an integer. Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
/** | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -141,7 +142,7 @@ * @param arg Any value. | ||
if (arg < min) | ||
throw new TypeError("\"".concat(name, "\" must be bigger than ").concat(min, ". Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
throw new RangeError("\"".concat(name, "\" must be bigger than ").concat(min, ". Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
} | ||
exports.assertMin = assertMin; | ||
/** | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -154,7 +155,7 @@ * @param arg Any value. | ||
if (arg > max) | ||
throw new TypeError("\"".concat(name, "\" must be smaller than ").concat(max, ". Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
throw new RangeError("\"".concat(name, "\" must be smaller than ").concat(max, ". Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
} | ||
exports.assertMax = assertMax; | ||
/** | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `TypeError` otherwise. | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `RangeError` otherwise. | ||
* | ||
@@ -168,4 +169,16 @@ * @param arg Any value. | ||
if (arg > max || arg < min) | ||
throw new TypeError("\"".concat(name, "\" must be smaller than ").concat(max, " and bigger than ").concat(min, ". Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
throw new RangeError("\"".concat(name, "\" must be smaller than ").concat(max, " and bigger than ").concat(min, ". Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
} | ||
exports.assertRange = assertRange; | ||
/** | ||
* Asserts `arg` is an Array. Throws a `TypeError` otherwise. | ||
* | ||
* @param arg Any value. | ||
* @param name The name of the expression for `arg`. | ||
*/ | ||
function assertArray(arg, name) { | ||
if (name === void 0) { name = 'arg'; } | ||
if (!(arg instanceof Array)) | ||
throw new TypeError("\"".concat(name, "\" must be an Array. Got \"").concat(arg, "\" of type \"").concat(typeof arg, "\".")); | ||
} | ||
exports.assertArray = assertArray; |
@@ -63,3 +63,3 @@ export declare class AssertionError<E = unknown, A = unknown> extends Error implements AssertOptionalParams<E, A> { | ||
*/ | ||
export declare function assertTypeOf(arg: any, expectedType: Type, name: string): void; | ||
export declare function assertTypeOf(arg: any, expectedType: Type, name?: string): void; | ||
/** | ||
@@ -80,5 +80,5 @@ * Asserts `arg` is one of `choices`. Throws a `TypeError` otherwise. | ||
*/ | ||
export declare function assertInteger(arg: number, name: string): void; | ||
export declare function assertInteger(arg: number, name?: string): void; | ||
/** | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -91,3 +91,3 @@ * @param arg Any value. | ||
/** | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -100,3 +100,3 @@ * @param arg Any value. | ||
/** | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `TypeError` otherwise. | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `RangeError` otherwise. | ||
* | ||
@@ -109,2 +109,9 @@ * @param arg Any value. | ||
export declare function assertRange(arg: any, name: string, min: any, max: any): void; | ||
/** | ||
* Asserts `arg` is an Array. Throws a `TypeError` otherwise. | ||
* | ||
* @param arg Any value. | ||
* @param name The name of the expression for `arg`. | ||
*/ | ||
export declare function assertArray(arg: any, name?: string): void; | ||
export {}; |
{ | ||
"name": "@santi100/assertion-lib", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"main": "cjs/index.js", | ||
"module": "./index.js", | ||
"keywords": ["assertion", "type-assertion", "lightweight", "es3"], | ||
"description": "Santi's Assertion Library: Quick and reliable assertions!", | ||
"license": "MIT", | ||
"author": "santi100a <santyrojasprieto9@gmail.com>", | ||
"repository": { | ||
@@ -11,6 +14,8 @@ "url": "https://github.com/santi100a/assertion-lib.git", | ||
}, | ||
"author": "santi100a <santyrojasprieto9@gmail.com>", | ||
"dependencies": { | ||
"@santi100/equal-lib": "^1.0.8" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.4.0", | ||
"jest": "^29.4.2", | ||
"@types/jest": "^29.5.0", | ||
"jest": "^29.5.0", | ||
"typescript": "4.8.4" | ||
@@ -23,6 +28,3 @@ }, | ||
"test:watch": "jest --watchAll" | ||
}, | ||
"dependencies": { | ||
"@santi100/equal-lib": "^1.0.8" | ||
} | ||
} |
@@ -30,25 +30,28 @@ # Santi's Assertion Library | ||
- `assert(condition: boolean, { expected, actual, operator }?: AssertOptionalParams): void;` | ||
- `function assert(condition: boolean, { expected, actual, operator }?: AssertOptionalParams): void;` | ||
Asserts that `condition` is truthy. Throws an `AssertionError` otherwise. | ||
- `assertType(val: unknown, expectedType: string): void;` | ||
- `function assertType(val: unknown, expectedType: string): void;` | ||
Asserts that the type of `val` is `expectedType`. Throws an `AssertionError` otherwise. | ||
- `function assertTypeOf(arg: any, expectedType: Type, name: string): void;` | ||
- `function assertTypeOf(arg: any, expectedType: Type, name: string): void;` (since 1.0.6) | ||
Asserts that the type of `arg` is `expectedType`. Throws a `TypeError` otherwise. | ||
- `function assertOneOf(arg: any, name: string, choices: any[]): void;` | ||
- `function assertOneOf(arg: any, name: string, choices: any[]): void;` (since 1.0.6) | ||
Asserts `arg` is one of `choices`. Throws a `TypeError` otherwise. | ||
- `function assertInteger(arg: number, name: string): void;`: | ||
- `function assertInteger(arg: number, name: string): void;` (since 1.0.6) | ||
Asserts `arg` is an integer. Throws a `TypeError` otherwise. | ||
- `function assertMin(arg: any, name: string, min: any): void;`: | ||
- `function assertMin(arg: any, name: string, min: any): void;` (since 1.0.6) | ||
Asserts `arg` is bigger or equal than `min`. Throws a `TypeError` otherwise. | ||
- `function assertMax(arg: any, name: string, max: any): void;` | ||
- `function assertMax(arg: any, name: string, max: any): void;` (since 1.0.6) | ||
Asserts `arg` is smaller or equal than `max`. Throws a `TypeError` otherwise. | ||
- `function assertRange(arg: any, name: string, min: any, max: any): void;`: | ||
Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `TypeError` otherwise. | ||
- `function assertRange(arg: any, name: string, min: any, max: any): void;` (since 1.0.6) | ||
Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `TypeError` | ||
(`RangeError` since 1.0.7) otherwise. | ||
- `function assertArray(arg: any, name?: string): void;` (since 1.0.7) | ||
Asserts `arg` is an Array. Throws a `TypeError` otherwise. | ||
@@ -55,0 +58,0 @@ ## Usage example |
@@ -104,3 +104,3 @@ import { deepEquality } from '@santi100/equal-lib'; | ||
*/ | ||
export function assertTypeOf(arg: any, expectedType: Type, name: string) { | ||
export function assertTypeOf(arg: any, expectedType: Type, name = 'arg') { | ||
const TYPES = [ | ||
@@ -116,8 +116,3 @@ 'string', | ||
]; | ||
if (TYPES.indexOf(expectedType) === -1) | ||
throw new TypeError( | ||
`${name} must be one of ${TYPES.join( | ||
', ' | ||
)}. Got "${arg} of type "${typeof arg}".` | ||
); | ||
assertOneOf(expectedType, 'expectedType', TYPES); | ||
if (typeof arg !== expectedType) | ||
@@ -135,3 +130,3 @@ throw new TypeError( | ||
* to be thrown. | ||
*/ | ||
*/ | ||
export function assertOneOf(arg: any, name: string, choices: any[]) { | ||
@@ -154,3 +149,3 @@ if (indexOf(choices, arg) === -1) | ||
*/ | ||
export function assertInteger(arg: number, name: string) { | ||
export function assertInteger(arg: number, name = 'arg') { | ||
if (!__isInteger(arg)) | ||
@@ -162,3 +157,3 @@ throw new TypeError( | ||
/** | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is bigger or equal than `min`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -171,3 +166,3 @@ * @param arg Any value. | ||
if (arg < min) | ||
throw new TypeError( | ||
throw new RangeError( | ||
`"${name}" must be bigger than ${min}. Got "${arg}" of type "${typeof arg}".` | ||
@@ -178,3 +173,3 @@ ); | ||
/** | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `TypeError` otherwise. | ||
* Asserts `arg` is smaller or equal than `max`. Throws a `RangeError` otherwise. | ||
* | ||
@@ -187,3 +182,3 @@ * @param arg Any value. | ||
if (arg > max) | ||
throw new TypeError( | ||
throw new RangeError( | ||
`"${name}" must be smaller than ${max}. Got "${arg}" of type "${typeof arg}".` | ||
@@ -193,3 +188,3 @@ ); | ||
/** | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `TypeError` otherwise. | ||
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `RangeError` otherwise. | ||
* | ||
@@ -203,5 +198,17 @@ * @param arg Any value. | ||
if (arg > max || arg < min) | ||
throw new TypeError( | ||
throw new RangeError( | ||
`"${name}" must be smaller than ${max} and bigger than ${min}. Got "${arg}" of type "${typeof arg}".` | ||
); | ||
} | ||
/** | ||
* Asserts `arg` is an Array. Throws a `TypeError` otherwise. | ||
* | ||
* @param arg Any value. | ||
* @param name The name of the expression for `arg`. | ||
*/ | ||
export function assertArray(arg: any, name = 'arg') { | ||
if (!(arg instanceof Array)) | ||
throw new TypeError( | ||
`"${name}" must be an Array. Got "${arg}" of type "${typeof arg}".` | ||
); | ||
} |
@@ -10,3 +10,4 @@ describe('assertion-lib test suites', () => { | ||
assertOneOf, | ||
assertRange | ||
assertRange, | ||
assertArray | ||
} = require('..'); | ||
@@ -19,3 +20,3 @@ | ||
test(`"assert" doesn't throw an error if the predicate is "true"`, () => { | ||
expect(assert(true)).toBe(undefined); | ||
expect(() => assert(true)).not.toThrow(); | ||
}); | ||
@@ -31,5 +32,5 @@ test('"assert" throws an error if the predicate is "false"', () => { | ||
test(`"assert" doesn't throw an error if the type is correct`, () => { | ||
expect(assertType(true, 'boolean')).toBe(undefined); | ||
expect(() => assertType(true, 'boolean')).not.toThrow(); | ||
}); | ||
test('"assertType" throws an error if the type is correct', () => { | ||
test('"assertType" throws an error if the type is incorrect', () => { | ||
expect(() => assertType({}, 'number')).toThrowError(); | ||
@@ -43,6 +44,6 @@ }); | ||
test(`"assertTypeOf" doesn't throw an error if the type is correct`, () => { | ||
expect(assertTypeOf(true, 'boolean', 'bool')).toBe(undefined); | ||
expect(() => assertTypeOf(true, 'boolean', 'bool')).not.toThrow(); | ||
}); | ||
test('"assertTypeOf" throws an error if the type is incorrect', () => { | ||
expect(() => assertTypeOf(false, 'object', 'bool')).toThrowError(); | ||
expect(() => assertTypeOf(false, 'object', 'bool')).toThrow(TypeError); | ||
}); | ||
@@ -55,6 +56,6 @@ }); | ||
test(`"assertInteger" doesn't throw an error if the first argument is an int`, () => { | ||
expect(assertInteger(54, 'num')).toBe(undefined); | ||
expect(() => assertInteger(54, 'num')).not.toThrow(); | ||
}); | ||
test('"assertInteger" throws an error if the first argument is a float', () => { | ||
expect(() => assertInteger(Math.PI, 'Math.PI')).toThrowError(); | ||
expect(() => assertInteger(Math.PI, 'Math.PI')).toThrow(TypeError); | ||
}); | ||
@@ -67,6 +68,6 @@ }); | ||
test(`"assertMax" doesn't throw an error if the type is correct`, () => { | ||
expect(assertMax(50, 'num', 100)).toBe(undefined); | ||
expect(() => assertMax(50, 'num', 100)).not.toThrow(); | ||
}); | ||
test('"assertMax" throws an error if the type is incorrect', () => { | ||
expect(() => assertMax(30, 'num', 15)).toThrowError(); | ||
expect(() => assertMax(30, 'num', 15)).toThrow(RangeError); | ||
}); | ||
@@ -78,7 +79,12 @@ }); | ||
}); | ||
test(`"assertMin" doesn't throw an error if the type is correct`, () => { | ||
expect(assertMin(50, 'num', 20)).toBe(undefined); | ||
test(`"assertMin" doesn't throw an error if the argument is big enough`, () => { | ||
expect(() => assertMin(50, 'num', 20)).not.toThrow(); | ||
expect(() => assertMin(20, 'num', 20)).not.toThrow(); | ||
}); | ||
test('"assertMin" throws an error if the type is incorrect', () => { | ||
expect(() => assertMin(20, 'num', 50)).toThrowError(); | ||
test('"assertMin" throws an error if the argument is too small', () => { | ||
expect(() => assertMin(20, 'num', 50)).toThrow(RangeError); | ||
expect(() => assertMin(30, 'num', 50)).toThrow(RangeError); | ||
expect(() => assertMin(40, 'num', 50)).toThrow(RangeError); | ||
expect(() => assertMin(45, 'num', 50)).toThrow(RangeError); | ||
expect(() => assertMin(49, 'num', 50)).toThrow(RangeError); | ||
}); | ||
@@ -91,9 +97,9 @@ }); | ||
test(`"assertRange" doesn't throw an error if the argument fits the range`, () => { | ||
expect(assertRange(30, 'num', 15, 33)).toBe(undefined); | ||
expect(assertRange(30, 'num', 15, 33)); | ||
}); | ||
test(`"assertRange" throws an error if the argument is smaller than the lower limit`, () => { | ||
expect(() => assertRange(45, 'num', 50, 100)).toThrowError(); | ||
expect(() => assertRange(45, 'num', 50, 100)).toThrow(RangeError); | ||
}); | ||
test(`"assertRange" throws an error if the argument is bigger than the upper limit`, () => { | ||
expect(() => assertRange(450, 'num', 50, 100)).toThrowError(); | ||
expect(() => assertRange(450, 'num', 50, 100)).toThrow(RangeError); | ||
}); | ||
@@ -106,5 +112,5 @@ }); | ||
test(`"assertOneOf" doesn't throw an error if the argument is in the list`, () => { | ||
expect(assertOneOf(30, 'num', [50, 20, 30])).toBe(undefined); | ||
expect(assertOneOf(30, 'num', [50, 20, 30])); | ||
expect( | ||
assertOneOf('hello world', 'num', [ | ||
assertOneOf('hello world', 'str', [ | ||
'everything', | ||
@@ -114,9 +120,23 @@ 'oh shucks', | ||
]) | ||
).toBe(undefined); | ||
expect(assertOneOf(true, 'num', [false, true])).toBe(undefined); | ||
); | ||
expect(assertOneOf(true, 'num', [false, true])); | ||
}); | ||
test(`"assertOneOf" throws an error if the argument is not in the list`, () => { | ||
expect(() => assertOneOf(45, 'num', [50, 100])).toThrowError(); | ||
expect(() => assertOneOf(45, 'num', [50, 100])).toThrow(TypeError); | ||
}); | ||
}); | ||
}); | ||
describe('assertArray', () => { | ||
test(`"assertArray" doesn't throw an error if the argument is an array`, () => { | ||
expect(assertArray([], 'array')); | ||
}); | ||
test(`"assertArray" throws an error if the argument isn't an array`, () => { | ||
expect(() => assertArray(30, 'array')).toThrow(TypeError); | ||
expect(() => assertArray('string', 'array')).toThrow(TypeError); | ||
expect(() => assertArray(false, 'array')).toThrow(TypeError); | ||
expect(() => assertArray({}, 'array')).toThrow(TypeError); | ||
expect(() => assertArray(new Date, 'array')).toThrow(TypeError); | ||
expect(() => assertArray(/regex/, 'array')).toThrow(TypeError); | ||
expect(() => assertArray(() => {}, 'array')).toThrow(TypeError); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
48584
762
187
0