@paulpopat/safe-type
Advanced tools
Comparing version 2.1.4 to 2.2.0
@@ -29,2 +29,3 @@ export declare type IsType<T> = T extends (arg: any) => arg is infer T ? T : never; | ||
export declare function IsObject<T extends CheckerObject>(checker: T): ObjectChecker<T>; | ||
export declare function IsRecord<TKey extends string | symbol, T>(keys: Checker<TKey>, c: Checker<T>): Checker<Record<TKey, T>>; | ||
export declare function IsDictionary<T>(c: Checker<T>): Checker<{ | ||
@@ -31,0 +32,0 @@ [key: string]: T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PatternMatch = exports.Assert = exports.DoNotCare = exports.Optional = exports.IsDictionary = exports.IsObject = exports.IsIntersection = exports.IsUnion = exports.IsTuple = exports.IsArray = exports.IsLiteral = exports.IsDate = exports.IsFunction = exports.IsBoolean = exports.IsSymbol = exports.IsBigInt = exports.IsNumber = exports.IsString = void 0; | ||
exports.PatternMatch = exports.Assert = exports.DoNotCare = exports.Optional = exports.IsDictionary = exports.IsRecord = exports.IsObject = exports.IsIntersection = exports.IsUnion = exports.IsTuple = exports.IsArray = exports.IsLiteral = exports.IsDate = exports.IsFunction = exports.IsBoolean = exports.IsSymbol = exports.IsBigInt = exports.IsNumber = exports.IsString = void 0; | ||
function IsString(arg) { | ||
@@ -111,27 +111,24 @@ return typeof arg === "string"; | ||
exports.IsObject = IsObject; | ||
function IsDictionary(c) { | ||
function IsRecord(keys, c) { | ||
return function (arg, strict) { | ||
if (strict === void 0) { strict = true; } | ||
if (strict) { | ||
return (Object.keys(arg).find(function (k) { | ||
if (!arg.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
if (!IsString(k)) { | ||
return false; | ||
} | ||
return arg[k] == null || !c(arg[k], true); | ||
}) == null); | ||
} | ||
return (Object.keys(arg).find(function (k) { | ||
var checker = strict | ||
? function (a) { return a == null || !c(a, true); } | ||
: function (a) { return a != null && c(a, true); }; | ||
var res = Object.keys(arg).find(function (k) { | ||
if (!arg.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
if (!IsString(k)) { | ||
return false; | ||
if (!keys(k)) { | ||
return true; | ||
} | ||
return arg[k] != null && c(arg[k], true); | ||
}) != null); | ||
return checker(arg[k]); | ||
}); | ||
return strict ? res == null : res != null; | ||
}; | ||
} | ||
exports.IsRecord = IsRecord; | ||
function IsDictionary(c) { | ||
return IsRecord(IsString, c); | ||
} | ||
exports.IsDictionary = IsDictionary; | ||
@@ -138,0 +135,0 @@ function Optional(c) { |
{ | ||
"name": "@paulpopat/safe-type", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"description": "Typesafety with strong inference", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -19,2 +19,3 @@ import { | ||
PatternMatch, | ||
IsRecord, | ||
} from "./index"; | ||
@@ -79,2 +80,13 @@ | ||
["An empty string in a dictionary", { test: "" }, IsDictionary(IsString)], | ||
["An empty record", {}, IsRecord(IsString, IsString)], | ||
[ | ||
"A record of strings", | ||
{ "1": "test", "2": "test" }, | ||
IsRecord(IsString, IsString), | ||
], | ||
[ | ||
"A record of literals", | ||
{ t1: "test", t2: "test" }, | ||
IsRecord(IsUnion(IsLiteral("t1"), IsLiteral("t2")), IsString), | ||
], | ||
]) { | ||
@@ -129,2 +141,7 @@ it(`Correctly assignes to true for ${name}`, () => { | ||
["Tuple", ["test", 123, "another"], IsTuple(IsString, IsNumber)], | ||
[ | ||
"A record of literals", | ||
{ t1: "test", wrong_test: "test" }, | ||
IsRecord(IsUnion(IsLiteral("t1"), IsLiteral("t2")), IsString), | ||
], | ||
]) { | ||
@@ -131,0 +148,0 @@ it(`Correctly assignes to false for ${name}`, () => { |
@@ -119,36 +119,31 @@ export type IsType<T> = T extends (arg: any) => arg is infer T ? T : never; | ||
export function IsDictionary<T>(c: Checker<T>): Checker<{ [key: string]: T }> { | ||
return (arg: any, strict: boolean = true): arg is { [key: string]: T } => { | ||
if (strict) { | ||
return ( | ||
Object.keys(arg).find((k) => { | ||
if (!arg.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
export function IsRecord<TKey extends string | symbol, T>( | ||
keys: Checker<TKey>, | ||
c: Checker<T> | ||
): Checker<Record<TKey, T>> { | ||
return (arg: any, strict: boolean = true): arg is Record<TKey, T> => { | ||
const checker = strict | ||
? (a: any) => a == null || !c(a, true) | ||
: (a: any) => a != null && c(a, true); | ||
if (!IsString(k)) { | ||
return false; | ||
} | ||
const res = Object.keys(arg).find((k) => { | ||
if (!arg.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
return arg[k] == null || !c(arg[k], true); | ||
}) == null | ||
); | ||
} | ||
if (!keys(k)) { | ||
return true; | ||
} | ||
return ( | ||
Object.keys(arg).find((k) => { | ||
if (!arg.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
if (!IsString(k)) { | ||
return false; | ||
} | ||
return arg[k] != null && c(arg[k], true); | ||
}) != null | ||
); | ||
return checker(arg[k]); | ||
}); | ||
return strict ? res == null : res != null; | ||
}; | ||
} | ||
export function IsDictionary<T>(c: Checker<T>): Checker<{ [key: string]: T }> { | ||
return IsRecord(IsString, c); | ||
} | ||
export function Optional<T>(c: Checker<T>): Checker<T | null | undefined> { | ||
@@ -155,0 +150,0 @@ return (arg: any): arg is T | null | undefined => { |
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
21668
583