typescanner
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -10,3 +10,3 @@ import type { Condition } from "../types"; | ||
export declare const date: Condition<Date>; | ||
export declare const union: <T>(...conditions: Condition<T>[]) => Condition<T>[]; | ||
export declare const union: <T>(...conditions: Condition<T>[]) => Condition<T>; | ||
export declare const array: <T>(...conditions: Condition<T>[]) => Condition<T[]>; | ||
@@ -13,0 +13,0 @@ export declare const optional: <T>(...conditions: Condition<T>[]) => Condition<T | undefined>; |
@@ -27,3 +27,5 @@ "use strict"; | ||
} | ||
return conditions; | ||
return function (value) { | ||
return typeGuard_1.isUnion.apply(void 0, __spreadArray([value], conditions, false)); | ||
}; | ||
}; | ||
@@ -30,0 +32,0 @@ exports.union = union; |
export { scan } from "./scan"; | ||
export { scanner } from "./scanner"; | ||
export { array, bigint, boolean, date, instanceOf, list, Null, number, optional, string, symbol, Undefined, union, } from "./fields"; | ||
export { isArray, isBigint, isBoolean, isDate, isInstanceOf, isList, isNull, isNumber, isObject, isOptional, isString, isSymbol, isUndefined, } from "./typeGuard"; | ||
export { isArray, isBigint, isBoolean, isDate, isInstanceOf, isList, isNull, isNumber, isObject, isOptional, isString, isSymbol, isUndefined, isUnion, } from "./typeGuard"; |
@@ -10,3 +10,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.isUndefined = exports.isSymbol = exports.isString = exports.isOptional = exports.isObject = exports.isNumber = exports.isNull = exports.isList = exports.isInstanceOf = exports.isDate = exports.isBoolean = exports.isBigint = exports.isArray = exports.union = exports.Undefined = exports.symbol = exports.string = exports.optional = exports.number = exports.Null = exports.list = exports.instanceOf = exports.date = exports.boolean = exports.bigint = exports.array = exports.scanner = exports.scan = void 0; | ||
exports.isUnion = exports.isUndefined = exports.isSymbol = exports.isString = exports.isOptional = exports.isObject = exports.isNumber = exports.isNull = exports.isList = exports.isInstanceOf = exports.isDate = exports.isBoolean = exports.isBigint = exports.isArray = exports.union = exports.Undefined = exports.symbol = exports.string = exports.optional = exports.number = exports.Null = exports.list = exports.instanceOf = exports.date = exports.boolean = exports.bigint = exports.array = exports.scanner = exports.scan = void 0; | ||
// scan | ||
@@ -48,2 +48,3 @@ var scan_1 = require("./scan"); | ||
__createBinding(exports, typeGuard_1, "isUndefined"); | ||
__createBinding(exports, typeGuard_1, "isUnion"); | ||
//# sourceMappingURL=index.js.map |
import type { Condition } from "../types"; | ||
export declare const scanner: <T extends Record<string, unknown>>(fields: { [K in keyof Required<T>]: Condition<T[K]> | Condition<T[K]>[]; }, option?: { | ||
isUseWithUnion: boolean; | ||
export declare const scanner: <T extends Record<string, unknown>>(fields: { [K in keyof Required<T>]: Condition<T[K]>; }, option?: { | ||
outputLog: boolean; | ||
} | undefined) => Condition<T>; |
@@ -7,23 +7,15 @@ "use strict"; | ||
return function (value) { | ||
if (!(0, typeGuard_1.isObject)(value)) { | ||
if (option === null || option === void 0 ? void 0 : option.outputLog) | ||
console.error("value is not object"); | ||
return false; | ||
} | ||
// check each value | ||
var isMeetCondition = (0, typeGuard_1.isObject)(value) && | ||
Object.entries(fields).every(function (_a) { | ||
var key = _a[0], condition = _a[1]; | ||
// if union type or not | ||
var isMeet = Array.isArray(condition) | ||
? condition.some(function (cond) { return cond(value[key]); }) | ||
: condition(value[key]); | ||
if (isMeet) | ||
return true; | ||
// if use with union, return false | ||
if (option === null || option === void 0 ? void 0 : option.isUseWithUnion) | ||
return false; | ||
throw new Error("value." + key + " does not meet the condition."); | ||
}); | ||
if (isMeetCondition) | ||
return true; | ||
// if use with union, return false | ||
if (option === null || option === void 0 ? void 0 : option.isUseWithUnion) | ||
return false; | ||
throw new Error("value does not meet the condition."); | ||
return Object.entries(fields).every(function (_a) { | ||
var key = _a[0], condition = _a[1]; | ||
var isMeet = condition(value[key]); | ||
if (!isMeet && (option === null || option === void 0 ? void 0 : option.outputLog)) | ||
console.error("value." + key + " does not meet the condition."); | ||
return isMeet; | ||
}); | ||
}; | ||
@@ -30,0 +22,0 @@ }; |
@@ -1,10 +0,11 @@ | ||
import type { WouldBe } from "../types"; | ||
export declare const isString: (value: unknown) => value is string; | ||
export declare const isNumber: (value: unknown) => value is number; | ||
export declare const isBoolean: (value: unknown) => value is boolean; | ||
export declare const isSymbol: (value: unknown) => value is symbol; | ||
export declare const isBigint: (value: unknown) => value is bigint; | ||
export declare const isUndefined: (value: unknown) => value is undefined; | ||
export declare const isNull: (value: unknown) => value is null; | ||
export declare const isDate: (value: unknown) => value is Date; | ||
import type { Condition, WouldBe } from "../types"; | ||
export declare const isString: Condition<string>; | ||
export declare const isNumber: Condition<number>; | ||
export declare const isBoolean: Condition<boolean>; | ||
export declare const isSymbol: Condition<symbol>; | ||
export declare const isBigint: Condition<bigint>; | ||
export declare const isUndefined: Condition<undefined>; | ||
export declare const isNull: Condition<null>; | ||
export declare const isDate: Condition<Date>; | ||
export declare const isUnion: <T>(value: unknown, ...conditions: Condition<T>[]) => value is T; | ||
export declare const isArray: <T>(array: unknown, ...conditions: ((value: unknown) => value is T)[]) => array is T[]; | ||
@@ -11,0 +12,0 @@ export declare const isObject: <T extends Record<string, unknown>>(value: unknown) => value is WouldBe<T>; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.isInstanceOf = exports.isList = exports.isOptional = exports.isObject = exports.isArray = exports.isDate = exports.isNull = exports.isUndefined = exports.isBigint = exports.isSymbol = exports.isBoolean = exports.isNumber = exports.isString = void 0; | ||
exports.isInstanceOf = exports.isList = exports.isOptional = exports.isObject = exports.isArray = exports.isUnion = exports.isDate = exports.isNull = exports.isUndefined = exports.isBigint = exports.isSymbol = exports.isBoolean = exports.isNumber = exports.isString = void 0; | ||
var isString = function (value) { | ||
@@ -12,5 +12,3 @@ return typeof value === "string"; | ||
exports.isNumber = isNumber; | ||
var isBoolean = function (value) { | ||
return typeof value === "boolean"; | ||
}; | ||
var isBoolean = function (value) { return typeof value === "boolean"; }; | ||
exports.isBoolean = isBoolean; | ||
@@ -25,10 +23,20 @@ var isSymbol = function (value) { | ||
exports.isBigint = isBigint; | ||
var isUndefined = function (value) { | ||
return typeof value === "undefined"; | ||
var isUndefined = function (value) { return typeof value === "undefined"; }; | ||
exports.isUndefined = isUndefined; | ||
var isNull = function (value) { | ||
return value === null; | ||
}; | ||
exports.isUndefined = isUndefined; | ||
var isNull = function (value) { return value === null; }; | ||
exports.isNull = isNull; | ||
var isDate = function (value) { return value instanceof Date; }; | ||
var isDate = function (value) { | ||
return value instanceof Date; | ||
}; | ||
exports.isDate = isDate; | ||
var isUnion = function (value) { | ||
var conditions = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
conditions[_i - 1] = arguments[_i]; | ||
} | ||
return conditions.some(function (condition) { return condition(value); }); | ||
}; | ||
exports.isUnion = isUnion; | ||
var isArray = function (array) { | ||
@@ -35,0 +43,0 @@ var conditions = []; |
{ | ||
"name": "typescanner", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A simple library for implementing type guard in TypeScript.", | ||
@@ -5,0 +5,0 @@ "author": "yona3", |
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
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
23790
245