typescanner
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -1,1 +0,1 @@ | ||
export declare const scan: <T>(value: unknown, condition: (value: unknown) => value is T, handleError?: ((value: unknown) => void) | undefined) => T; | ||
export declare const scan: <T>(value: unknown, condition: (value: unknown) => value is T) => T; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.scan = void 0; | ||
var scan = function (value, condition, handleError) { | ||
var scan = function (value, condition) { | ||
if (condition(value)) | ||
return value; | ||
if (handleError) | ||
handleError(value); | ||
throw new Error("type assertion is failed. (value: " + value + ")"); | ||
@@ -10,0 +8,0 @@ }; |
@@ -8,9 +8,15 @@ "use strict"; | ||
// check each value | ||
return (0, typeGuard_1.isObject)(value) && | ||
var isMeetCondition = (0, typeGuard_1.isObject)(value) && | ||
Object.entries(fields).every(function (_a) { | ||
var key = _a[0], condition = _a[1]; | ||
return Array.isArray(condition) | ||
var isMeet = Array.isArray(condition) | ||
? condition.some(function (cond) { return cond(value[key]); }) | ||
: condition(value[key]); | ||
if (isMeet) | ||
return true; | ||
throw new Error("value." + key + " does not meet the condition."); | ||
}); | ||
if (isMeetCondition) | ||
return true; | ||
throw new Error("value does not meet the condition."); | ||
}; | ||
@@ -17,0 +23,0 @@ }; |
{ | ||
"name": "typescanner", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "A simple library for implementing type guards in TypeScript.", | ||
@@ -5,0 +5,0 @@ "author": "yona3", |
@@ -65,18 +65,5 @@ import { | ||
expect(() => scan({} as unknown, isFoo)).toThrow( | ||
/type assertion is failed./ | ||
/does not meet the condition./ | ||
); | ||
}); | ||
it("throw custom error", () => { | ||
expect(() => | ||
scan("hello" as unknown, number, () => { | ||
throw new Error("custom error"); | ||
}) | ||
).toThrow(/custom error/); | ||
expect(() => | ||
scan({} as unknown, isFoo, () => { | ||
throw new Error("value is invalid."); | ||
}) | ||
).toThrow(/value is invalid./); | ||
}); | ||
}); |
export const scan = <T>( | ||
value: unknown, | ||
condition: (value: unknown) => value is T, | ||
handleError?: (value: unknown) => void | ||
condition: (value: unknown) => value is T | ||
): T => { | ||
if (condition(value)) return value; | ||
if (handleError) handleError(value); | ||
throw new Error(`type assertion is failed. (value: ${value})`); | ||
}; |
@@ -126,4 +126,4 @@ import { | ||
describe("to be false", () => { | ||
// to be false | ||
describe("throw error", () => { | ||
// throw error | ||
const post1 = (): Post => { | ||
@@ -134,3 +134,3 @@ const post = correctPost as any; | ||
}; | ||
// to be false | ||
// throw error | ||
const post2 = (): Post => { | ||
@@ -146,8 +146,10 @@ const post = correctPost as any; | ||
it("is post", () => { | ||
expect(isPost(post1)).toBe(false); | ||
expect(isPost(post2)).toBe(false); | ||
expect(() => isPost(post1)).toThrow(/does not meet the condition./); | ||
expect(() => isPost(post2)).toThrow(/does not meet the condition./); | ||
expect(isPost(post3)).toBe(true); | ||
}); | ||
it("is posts (include the correct type)", () => { | ||
expect(isPosts([post1, post2, post3])).toBe(false); | ||
expect(() => isPosts([post1, post2, post3])).toThrow( | ||
/does not meet the condition./ | ||
); | ||
}); | ||
@@ -165,3 +167,3 @@ | ||
it("is user", () => { | ||
expect(isUser(user1)).toBe(false); | ||
expect(() => isUser(user1)).toThrow(/does not meet the condition./); | ||
expect(isUser(user2)).toBe(true); | ||
@@ -171,5 +173,7 @@ }); | ||
it("is users (inclued the correct type)", () => { | ||
expect(isUsers([user1, user2])).toBe(false); | ||
expect(() => isUsers([user1, user2])).toThrow( | ||
/does not meet the condition./ | ||
); | ||
}); | ||
}); | ||
}); |
@@ -7,12 +7,22 @@ import { isObject } from "../typeGuard"; | ||
}): ((value: unknown) => value is T) => { | ||
return (value: unknown): value is T => | ||
return (value: unknown): value is T => { | ||
// check each value | ||
isObject<T>(value) && | ||
Object.entries<Condition<T[keyof T]> | Condition<T[keyof T]>[]>( | ||
fields | ||
).every(([key, condition]) => | ||
Array.isArray(condition) | ||
? condition.some((cond) => cond((value as any)[key])) | ||
: condition((value as any)[key]) | ||
); | ||
const isMeetCondition = | ||
isObject<T>(value) && | ||
Object.entries<Condition<T[keyof T]> | Condition<T[keyof T]>[]>( | ||
fields | ||
).every(([key, condition]) => { | ||
const isMeet = Array.isArray(condition) | ||
? condition.some((cond) => cond((value as any)[key])) | ||
: condition((value as any)[key]); | ||
if (isMeet) return true; | ||
throw new Error(`value.${key} does not meet the condition.`); | ||
}); | ||
if (isMeetCondition) return true; | ||
throw new Error("value does not meet the condition."); | ||
}; | ||
}; |
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
38058