@xylabs/assert
Advanced tools
Comparing version 2.7.5 to 2.7.6
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.assertEx = void 0; | ||
/** | ||
* Error object thrown by assertEx | ||
*/ | ||
class AssertExError extends Error { | ||
} | ||
/** | ||
* | ||
* Intended for simple null/undefined checks for variables | ||
* | ||
* @param expr - Expression to be evaluated for truthiness | ||
* @param message - Message in Error if expression is false, may be a function that returns a string | ||
* @throws AssertExError | ||
* @returns Value of expression | ||
*/ | ||
const assertEx = (expr, message) => { | ||
function assertEx(expr, messageOrFunc) { | ||
if (expr) | ||
return expr; | ||
throw new AssertExError(typeof message === 'string' ? message : message === null || message === void 0 ? void 0 : message(expr)); | ||
}; | ||
if (typeof messageOrFunc === 'function') { | ||
const errorOrMessage = messageOrFunc(expr); | ||
throw typeof errorOrMessage === 'string' ? Error(errorOrMessage) : errorOrMessage; | ||
} | ||
// a string was sent | ||
throw Error(messageOrFunc); | ||
} | ||
exports.assertEx = assertEx; | ||
@@ -24,0 +15,0 @@ // eslint-disable-next-line import/no-default-export |
@@ -10,3 +10,3 @@ { | ||
{ | ||
"id": 11, | ||
"id": 26, | ||
"name": "default", | ||
@@ -19,10 +19,104 @@ "kind": 8388608, | ||
"fileName": "index.ts", | ||
"line": 23, | ||
"line": 27, | ||
"character": 0, | ||
"url": "https://github.com/xylabs/sdk-js/blob/09e8e07/packages/assert/src/index.ts#L23" | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L27" | ||
} | ||
], | ||
"target": 6 | ||
"target": 12 | ||
}, | ||
{ | ||
"id": 6, | ||
"name": "AssertExErrorFunc", | ||
"kind": 4194304, | ||
"kindString": "Type alias", | ||
"flags": {}, | ||
"sources": [ | ||
{ | ||
"fileName": "index.ts", | ||
"line": 2, | ||
"character": 12, | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L2" | ||
} | ||
], | ||
"typeParameters": [ | ||
{ | ||
"id": 10, | ||
"name": "T", | ||
"kind": 131072, | ||
"kindString": "Type parameter", | ||
"flags": {} | ||
}, | ||
{ | ||
"id": 11, | ||
"name": "R", | ||
"kind": 131072, | ||
"kindString": "Type parameter", | ||
"flags": {}, | ||
"type": { | ||
"type": "reference", | ||
"name": "Error", | ||
"qualifiedName": "Error", | ||
"package": "typescript" | ||
} | ||
} | ||
], | ||
"type": { | ||
"type": "reflection", | ||
"declaration": { | ||
"id": 7, | ||
"name": "__type", | ||
"kind": 65536, | ||
"kindString": "Type literal", | ||
"flags": {}, | ||
"sources": [ | ||
{ | ||
"fileName": "index.ts", | ||
"line": 2, | ||
"character": 52, | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L2" | ||
} | ||
], | ||
"signatures": [ | ||
{ | ||
"id": 8, | ||
"name": "__type", | ||
"kind": 4096, | ||
"kindString": "Call signature", | ||
"flags": {}, | ||
"parameters": [ | ||
{ | ||
"id": 9, | ||
"name": "value", | ||
"kind": 32768, | ||
"kindString": "Parameter", | ||
"flags": { | ||
"isOptional": true | ||
}, | ||
"type": { | ||
"type": "union", | ||
"types": [ | ||
{ | ||
"type": "reference", | ||
"id": 10, | ||
"name": "T" | ||
}, | ||
{ | ||
"type": "literal", | ||
"value": null | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"type": { | ||
"type": "reference", | ||
"id": 11, | ||
"name": "R" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"id": 1, | ||
@@ -36,7 +130,16 @@ "name": "AssertExMessageFunc", | ||
"fileName": "index.ts", | ||
"line": 6, | ||
"line": 1, | ||
"character": 12, | ||
"url": "https://github.com/xylabs/sdk-js/blob/09e8e07/packages/assert/src/index.ts#L6" | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L1" | ||
} | ||
], | ||
"typeParameters": [ | ||
{ | ||
"id": 5, | ||
"name": "T", | ||
"kind": 131072, | ||
"kindString": "Type parameter", | ||
"flags": {} | ||
} | ||
], | ||
"type": { | ||
@@ -53,5 +156,5 @@ "type": "reflection", | ||
"fileName": "index.ts", | ||
"line": 6, | ||
"character": 34, | ||
"url": "https://github.com/xylabs/sdk-js/blob/09e8e07/packages/assert/src/index.ts#L6" | ||
"line": 1, | ||
"character": 37, | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L1" | ||
} | ||
@@ -66,11 +169,2 @@ ], | ||
"flags": {}, | ||
"typeParameter": [ | ||
{ | ||
"id": 5, | ||
"name": "T", | ||
"kind": 131072, | ||
"kindString": "Type parameter", | ||
"flags": {} | ||
} | ||
], | ||
"parameters": [ | ||
@@ -82,7 +176,18 @@ { | ||
"kindString": "Parameter", | ||
"flags": {}, | ||
"flags": { | ||
"isOptional": true | ||
}, | ||
"type": { | ||
"type": "reference", | ||
"id": 5, | ||
"name": "T" | ||
"type": "union", | ||
"types": [ | ||
{ | ||
"type": "reference", | ||
"id": 5, | ||
"name": "T" | ||
}, | ||
{ | ||
"type": "literal", | ||
"value": null | ||
} | ||
] | ||
} | ||
@@ -101,3 +206,3 @@ } | ||
{ | ||
"id": 6, | ||
"id": 12, | ||
"name": "assertEx", | ||
@@ -110,5 +215,23 @@ "kind": 64, | ||
"fileName": "index.ts", | ||
"line": 17, | ||
"character": 6, | ||
"url": "https://github.com/xylabs/sdk-js/blob/09e8e07/packages/assert/src/index.ts#L17" | ||
"line": 13, | ||
"character": 9, | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L13" | ||
}, | ||
{ | ||
"fileName": "index.ts", | ||
"line": 14, | ||
"character": 9, | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L14" | ||
}, | ||
{ | ||
"fileName": "index.ts", | ||
"line": 15, | ||
"character": 9, | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L15" | ||
}, | ||
{ | ||
"fileName": "index.ts", | ||
"line": 16, | ||
"character": 9, | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L16" | ||
} | ||
@@ -118,3 +241,3 @@ ], | ||
{ | ||
"id": 7, | ||
"id": 13, | ||
"name": "assertEx", | ||
@@ -154,3 +277,3 @@ "kind": 4096, | ||
{ | ||
"id": 8, | ||
"id": 14, | ||
"name": "T", | ||
@@ -164,3 +287,3 @@ "kind": 131072, | ||
{ | ||
"id": 9, | ||
"id": 15, | ||
"name": "expr", | ||
@@ -189,3 +312,3 @@ "kind": 32768, | ||
"type": "reference", | ||
"id": 8, | ||
"id": 14, | ||
"name": "T" | ||
@@ -197,3 +320,3 @@ } | ||
{ | ||
"id": 10, | ||
"id": 16, | ||
"name": "message", | ||
@@ -214,15 +337,72 @@ "kind": 32768, | ||
"type": { | ||
"type": "intrinsic", | ||
"name": "string" | ||
} | ||
} | ||
], | ||
"type": { | ||
"type": "reference", | ||
"id": 14, | ||
"name": "T" | ||
} | ||
}, | ||
{ | ||
"id": 17, | ||
"name": "assertEx", | ||
"kind": 4096, | ||
"kindString": "Call signature", | ||
"flags": {}, | ||
"typeParameter": [ | ||
{ | ||
"id": 18, | ||
"name": "T", | ||
"kind": 131072, | ||
"kindString": "Type parameter", | ||
"flags": {} | ||
} | ||
], | ||
"parameters": [ | ||
{ | ||
"id": 19, | ||
"name": "expr", | ||
"kind": 32768, | ||
"kindString": "Parameter", | ||
"flags": { | ||
"isOptional": true | ||
}, | ||
"type": { | ||
"type": "union", | ||
"types": [ | ||
{ | ||
"type": "intrinsic", | ||
"name": "string" | ||
"type": "literal", | ||
"value": null | ||
}, | ||
{ | ||
"type": "reference", | ||
"id": 1, | ||
"name": "AssertExMessageFunc" | ||
"id": 18, | ||
"name": "T" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"id": 20, | ||
"name": "messageFunc", | ||
"kind": 32768, | ||
"kindString": "Parameter", | ||
"flags": { | ||
"isOptional": true | ||
}, | ||
"type": { | ||
"type": "reference", | ||
"id": 1, | ||
"typeArguments": [ | ||
{ | ||
"type": "reference", | ||
"id": 18, | ||
"name": "T" | ||
} | ||
], | ||
"name": "AssertExMessageFunc" | ||
} | ||
} | ||
@@ -232,5 +412,90 @@ ], | ||
"type": "reference", | ||
"id": 8, | ||
"id": 18, | ||
"name": "T" | ||
} | ||
}, | ||
{ | ||
"id": 21, | ||
"name": "assertEx", | ||
"kind": 4096, | ||
"kindString": "Call signature", | ||
"flags": {}, | ||
"typeParameter": [ | ||
{ | ||
"id": 22, | ||
"name": "T", | ||
"kind": 131072, | ||
"kindString": "Type parameter", | ||
"flags": {} | ||
}, | ||
{ | ||
"id": 23, | ||
"name": "R", | ||
"kind": 131072, | ||
"kindString": "Type parameter", | ||
"flags": {}, | ||
"type": { | ||
"type": "reference", | ||
"name": "Error", | ||
"qualifiedName": "Error", | ||
"package": "typescript" | ||
} | ||
} | ||
], | ||
"parameters": [ | ||
{ | ||
"id": 24, | ||
"name": "expr", | ||
"kind": 32768, | ||
"kindString": "Parameter", | ||
"flags": { | ||
"isOptional": true | ||
}, | ||
"type": { | ||
"type": "union", | ||
"types": [ | ||
{ | ||
"type": "literal", | ||
"value": null | ||
}, | ||
{ | ||
"type": "reference", | ||
"id": 22, | ||
"name": "T" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"id": 25, | ||
"name": "errorFunc", | ||
"kind": 32768, | ||
"kindString": "Parameter", | ||
"flags": { | ||
"isOptional": true | ||
}, | ||
"type": { | ||
"type": "reference", | ||
"id": 6, | ||
"typeArguments": [ | ||
{ | ||
"type": "reference", | ||
"id": 22, | ||
"name": "T" | ||
}, | ||
{ | ||
"type": "reference", | ||
"id": 23, | ||
"name": "R" | ||
} | ||
], | ||
"name": "AssertExErrorFunc" | ||
} | ||
} | ||
], | ||
"type": { | ||
"type": "reference", | ||
"id": 22, | ||
"name": "T" | ||
} | ||
} | ||
@@ -244,3 +509,3 @@ ] | ||
"children": [ | ||
11 | ||
26 | ||
] | ||
@@ -251,2 +516,3 @@ }, | ||
"children": [ | ||
6, | ||
1 | ||
@@ -258,3 +524,3 @@ ] | ||
"children": [ | ||
6 | ||
12 | ||
] | ||
@@ -266,7 +532,7 @@ } | ||
"fileName": "index.ts", | ||
"line": 4, | ||
"line": 1, | ||
"character": 0, | ||
"url": "https://github.com/xylabs/sdk-js/blob/09e8e07/packages/assert/src/index.ts#L4" | ||
"url": "https://github.com/xylabs/sdk-js/blob/8d1441a/packages/assert/src/index.ts#L1" | ||
} | ||
] | ||
} |
@@ -1,20 +0,11 @@ | ||
/** | ||
* Error object thrown by assertEx | ||
*/ | ||
class AssertExError extends Error { | ||
} | ||
/** | ||
* | ||
* Intended for simple null/undefined checks for variables | ||
* | ||
* @param expr - Expression to be evaluated for truthiness | ||
* @param message - Message in Error if expression is false, may be a function that returns a string | ||
* @throws AssertExError | ||
* @returns Value of expression | ||
*/ | ||
const assertEx = (expr, message) => { | ||
function assertEx(expr, messageOrFunc) { | ||
if (expr) | ||
return expr; | ||
throw new AssertExError(typeof message === 'string' ? message : message?.(expr)); | ||
}; | ||
if (typeof messageOrFunc === 'function') { | ||
const errorOrMessage = messageOrFunc(expr); | ||
throw typeof errorOrMessage === 'string' ? Error(errorOrMessage) : errorOrMessage; | ||
} | ||
// a string was sent | ||
throw Error(messageOrFunc); | ||
} | ||
// eslint-disable-next-line import/no-default-export | ||
@@ -21,0 +12,0 @@ export default assertEx; |
@@ -1,2 +0,3 @@ | ||
export type AssertExMessageFunc = <T>(value: T) => string; | ||
export type AssertExMessageFunc<T> = (value?: T | null) => string; | ||
export type AssertExErrorFunc<T, R extends Error> = (value?: T | null) => R; | ||
/** | ||
@@ -11,5 +12,7 @@ * | ||
*/ | ||
declare const assertEx: <T>(expr?: T | null | undefined, message?: string | AssertExMessageFunc) => T; | ||
declare function assertEx<T>(expr?: T | null, message?: string): T; | ||
declare function assertEx<T>(expr?: T | null, messageFunc?: AssertExMessageFunc<T>): T; | ||
declare function assertEx<T, R extends Error>(expr?: T | null, errorFunc?: AssertExErrorFunc<T, R>): T; | ||
export default assertEx; | ||
export { assertEx }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -54,3 +54,3 @@ { | ||
"sideEffects": false, | ||
"version": "2.7.5" | ||
"version": "2.7.6" | ||
} |
@@ -1,8 +0,4 @@ | ||
/** | ||
* Error object thrown by assertEx | ||
*/ | ||
class AssertExError extends Error {} | ||
export type AssertExMessageFunc<T> = (value?: T | null) => string | ||
export type AssertExErrorFunc<T, R extends Error> = (value?: T | null) => R | ||
export type AssertExMessageFunc = <T>(value: T) => string | ||
/** | ||
@@ -17,5 +13,13 @@ * | ||
*/ | ||
const assertEx = <T>(expr?: T | null, message?: string | AssertExMessageFunc): T => { | ||
function assertEx<T>(expr?: T | null, message?: string): T | ||
function assertEx<T>(expr?: T | null, messageFunc?: AssertExMessageFunc<T>): T | ||
function assertEx<T, R extends Error>(expr?: T | null, errorFunc?: AssertExErrorFunc<T, R>): T | ||
function assertEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(expr?: T | null, messageOrFunc?: P): T { | ||
if (expr) return expr | ||
throw new AssertExError(typeof message === 'string' ? message : message?.(expr)) | ||
if (typeof messageOrFunc === 'function') { | ||
const errorOrMessage = messageOrFunc(expr) | ||
throw typeof errorOrMessage === 'string' ? Error(errorOrMessage) : errorOrMessage | ||
} | ||
// a string was sent | ||
throw Error(messageOrFunc) | ||
} | ||
@@ -22,0 +26,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
25888
589