Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cloudflare/util-en-garde

Package Overview
Dependencies
Maintainers
26
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cloudflare/util-en-garde - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

10

CHANGELOG.md

@@ -6,3 +6,3 @@ # Change Log

# 1.2.0 (2019-10-15)
# 2.0.0 (2019-10-28)

@@ -12,2 +12,3 @@

* **stratus:** UI-2859: bump all package versions ([4cb3c90](http://stash.cfops.it:7999/fe/stratus/commits/4cb3c90))
* **stratus:** UI-2859: bump all package versions ([6db21c2](http://stash.cfops.it:7999/fe/stratus/commits/6db21c2))

@@ -19,3 +20,10 @@

* **stratus:** TSX-143 Add util-en-garde module ([6751369](http://stash.cfops.it:7999/fe/stratus/commits/6751369))
* **util-en-garde:** STREAM-1672 Attach failed data and rename optional ([5d20d38](http://stash.cfops.it:7999/fe/stratus/commits/5d20d38))
* **util-en-garde:** TSX-143 Add any and objectWithValuesOf guards ([b0bdaf4](http://stash.cfops.it:7999/fe/stratus/commits/b0bdaf4))
* **util-en-garde:** TSX-146 Add tuple higher order guard ([1ae33bf](http://stash.cfops.it:7999/fe/stratus/commits/1ae33bf))
### BREAKING CHANGES
* **util-en-garde:** Renamed optional type to TypeGuardWithOptional and update TypeGuardError
constructor signature

40

dist/index.d.ts
import { primitiveGuards, higherOrderGuards } from './core';
import { TypeGuard, TypeFromGuard, GuardWithOptional } from './type-helpers';
import { TypeGuard, TypeFromGuard, TypeGuardWithOptional } from './type-helpers';
import { addOptionalToGuard, addOptionalToHigherOrderGuard } from './optional';
declare class TypeGuardError extends Error {
valueThatFailed: unknown;
constructor(message: string, valueThatFailed: unknown);
}
declare const assertShape: <Guard extends TypeGuard<any>>(guard: Guard) => (obj: unknown) => TypeFromGuard<Guard>;
declare const eg: {
string: ((obj: unknown) => obj is string) & {
optional: (obj: unknown) => obj is string | undefined;
};
number: ((obj: unknown) => obj is number) & {
optional: (obj: unknown) => obj is number | undefined;
};
boolean: ((obj: unknown) => obj is boolean) & {
optional: (obj: unknown) => obj is boolean | undefined;
};
null: ((obj: unknown) => obj is null) & {
optional: (obj: unknown) => obj is null | undefined;
};
string: TypeGuardWithOptional<string>;
number: TypeGuardWithOptional<number>;
boolean: TypeGuardWithOptional<boolean>;
null: TypeGuardWithOptional<null>;
/**

@@ -24,3 +18,3 @@ * Accepts a guard and returns a guard that checks that the value is an

*/
arrayOf: <T>(typeGuard: TypeGuard<T>) => GuardWithOptional<T[]>;
arrayOf: <T>(typeGuard: TypeGuard<T>) => TypeGuardWithOptional<T[]>;
/**

@@ -42,3 +36,3 @@ * Accepts any number of guards and returns a type guard that will only

*/
tuple: <TypeGuards extends TypeGuard<any>[]>(...params: TypeGuards) => GuardWithOptional<{
tuple: <TypeGuards extends TypeGuard<any>[]>(...params: TypeGuards) => TypeGuardWithOptional<{
return: [];

@@ -83,3 +77,3 @@ recurse: {

*/
exact: <Definition extends import("./type-helpers").ObjectGuardDefinition>(definition: Definition) => GuardWithOptional<{ [Key_3 in keyof ({ [MandatoryKey in { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]]: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[MandatoryKey]; } & { [OptionalKey in { [Key_2 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: Key_2; }[Exclude<keyof Definition, { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]>]]?: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[OptionalKey] | undefined; })]: ({ [MandatoryKey in { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]]: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[MandatoryKey]; } & { [OptionalKey in { [Key_2 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: Key_2; }[Exclude<keyof Definition, { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]>]]?: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[OptionalKey] | undefined; })[Key_3]; }>;
exact: <Definition extends import("./type-helpers").ObjectGuardDefinition>(definition: Definition) => TypeGuardWithOptional<{ [Key_3 in keyof ({ [MandatoryKey in { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]]: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[MandatoryKey]; } & { [OptionalKey in { [Key_2 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: Key_2; }[Exclude<keyof Definition, { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]>]]?: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[OptionalKey] | undefined; })]: ({ [MandatoryKey in { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]]: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[MandatoryKey]; } & { [OptionalKey in { [Key_2 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: Key_2; }[Exclude<keyof Definition, { [Key_1 in keyof { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }]: undefined extends { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[Key_1] ? never : Key_1; }[keyof Definition]>]]?: { [Key in keyof Definition]: TypeFromGuard<Definition[Key]>; }[OptionalKey] | undefined; })[Key_3]; }>;
/**

@@ -89,3 +83,3 @@ * Accepts a constructor and returns a guard that checks whether a value is

*/
instanceOf: <Constructor extends new (...args: any[]) => any>(ctor: Constructor) => GuardWithOptional<InstanceType<Constructor>>;
instanceOf: <Constructor extends new (...args: any[]) => any>(ctor: Constructor) => TypeGuardWithOptional<InstanceType<Constructor>>;
/**

@@ -96,3 +90,3 @@ * Accepts an object definition where each key must be a guard. Returns

*/
object: <Definition_1 extends import("./type-helpers").ObjectGuardDefinition>(definition: Definition_1) => GuardWithOptional<{ [Key_7 in keyof ({ [MandatoryKey_1 in { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]]: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[MandatoryKey_1]; } & { [OptionalKey_1 in { [Key_6 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: Key_6; }[Exclude<keyof Definition_1, { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]>]]?: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[OptionalKey_1] | undefined; })]: ({ [MandatoryKey_1 in { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]]: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[MandatoryKey_1]; } & { [OptionalKey_1 in { [Key_6 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: Key_6; }[Exclude<keyof Definition_1, { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]>]]?: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[OptionalKey_1] | undefined; })[Key_7]; }>;
object: <Definition_1 extends import("./type-helpers").ObjectGuardDefinition>(definition: Definition_1) => TypeGuardWithOptional<{ [Key_7 in keyof ({ [MandatoryKey_1 in { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]]: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[MandatoryKey_1]; } & { [OptionalKey_1 in { [Key_6 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: Key_6; }[Exclude<keyof Definition_1, { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]>]]?: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[OptionalKey_1] | undefined; })]: ({ [MandatoryKey_1 in { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]]: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[MandatoryKey_1]; } & { [OptionalKey_1 in { [Key_6 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: Key_6; }[Exclude<keyof Definition_1, { [Key_5 in keyof { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }]: undefined extends { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[Key_5] ? never : Key_5; }[keyof Definition_1]>]]?: { [Key_4 in keyof Definition_1]: TypeFromGuard<Definition_1[Key_4]>; }[OptionalKey_1] | undefined; })[Key_7]; }>;
/**

@@ -102,3 +96,3 @@ * Accepts a type guard and returns an index signature where values must

*/
objectWithValuesOf: <T_1, Keys extends string | number | symbol>(typeGuard: TypeGuard<T_1>) => GuardWithOptional<Record<Keys, T_1>>;
objectWithValuesOf: <T_1, Keys extends string | number | symbol>(typeGuard: TypeGuard<T_1>) => TypeGuardWithOptional<Record<Keys, T_1>>;
/**

@@ -108,3 +102,3 @@ * Accepts any number of guards as arguments. Returns a guard that checks if

*/
oneOf: <TypeGuards_1 extends TypeGuard<any>[]>(...params: TypeGuards_1) => GuardWithOptional<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards_1>>>;
oneOf: <TypeGuards_1 extends TypeGuard<any>[]>(...params: TypeGuards_1) => TypeGuardWithOptional<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards_1>>>;
/**

@@ -114,3 +108,3 @@ * Accepts any number of guards as arguments. Returns a guard that checks if

*/
all: <TypeGuards_2 extends TypeGuard<any>[]>(...params: TypeGuards_2) => GuardWithOptional<import("@cloudflare/types/src").UnionToIntersection<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards_2>>>>;
all: <TypeGuards_2 extends TypeGuard<any>[]>(...params: TypeGuards_2) => TypeGuardWithOptional<import("@cloudflare/types/src").UnionToIntersection<TypeFromGuard<import("./type-helpers").TypeOfArray<TypeGuards_2>>>>;
/**

@@ -120,3 +114,3 @@ * Accepts primitives and infers their types. Returns a guard that checks

*/
oneOfLiterals: <Args extends (string | number | boolean | null | undefined)[]>(...params: Args) => GuardWithOptional<import("./type-helpers").TypeOfArray<Args>>;
oneOfLiterals: <Args extends (string | number | boolean | null | undefined)[]>(...params: Args) => TypeGuardWithOptional<import("./type-helpers").TypeOfArray<Args>>;
any: (_: unknown) => _ is any;

@@ -127,2 +121,2 @@ unknown: (_: unknown) => _ is unknown;

export default eg;
export { TypeGuard, TypeFromGuard, GuardWithOptional, primitiveGuards, higherOrderGuards, TypeGuardError, assertShape, addOptionalToGuard, addOptionalToHigherOrderGuard };
export { TypeGuard, TypeFromGuard, TypeGuardWithOptional, primitiveGuards, higherOrderGuards, TypeGuardError, assertShape, addOptionalToGuard, addOptionalToHigherOrderGuard };

@@ -1,5 +0,3 @@

import { TypeGuard, TypeFromGuard, GuardWithOptional } from './type-helpers';
export declare const addOptionalToGuard: <Guard extends TypeGuard<any>>(guard: Guard) => Guard & {
optional: (obj: unknown) => obj is TypeFromGuard<Guard> | undefined;
};
export declare const addOptionalToHigherOrderGuard: <HigherOrderGuardParams extends any[], Guard extends TypeGuard<any>, HigherOrderGuard extends (...params: HigherOrderGuardParams) => Guard>(higherOrderGuard: HigherOrderGuard) => (...params: HigherOrderGuardParams) => GuardWithOptional<TypeFromGuard<Guard>>;
import { TypeGuard, TypeFromGuard, TypeGuardWithOptional } from './type-helpers';
export declare const addOptionalToGuard: <Guard extends TypeGuard<any>>(guard: Guard) => TypeGuardWithOptional<TypeFromGuard<Guard>>;
export declare const addOptionalToHigherOrderGuard: <HigherOrderGuardParams extends any[], Guard extends TypeGuard<any>, HigherOrderGuard extends (...params: HigherOrderGuardParams) => Guard>(higherOrderGuard: HigherOrderGuard) => (...params: HigherOrderGuardParams) => TypeGuardWithOptional<TypeFromGuard<Guard>>;

@@ -25,3 +25,3 @@ import { Reverse, Tail, Head, Prepend } from '@cloudflare/types';

}>;
export declare type GuardWithOptional<T> = TypeGuard<T> & {
export declare type TypeGuardWithOptional<T> = TypeGuard<T> & {
optional: TypeGuard<T | undefined>;

@@ -28,0 +28,0 @@ };

@@ -24,3 +24,3 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

import { primitiveGuards, higherOrderGuards } from './core';
import { TypeGuard, TypeFromGuard, GuardWithOptional } from './type-helpers';
import { TypeGuard, TypeFromGuard, TypeGuardWithOptional } from './type-helpers';
import { addOptionalToGuard, addOptionalToHigherOrderGuard } from './optional';

@@ -33,6 +33,6 @@

function TypeGuardError() {
function TypeGuardError(message, valueThatFailed) {
_classCallCheck(this, TypeGuardError);
return _possibleConstructorReturn(this, _getPrototypeOf(TypeGuardError).apply(this, arguments));
return _possibleConstructorReturn(this, _getPrototypeOf(TypeGuardError).call(this, message));
}

@@ -46,3 +46,3 @@

if (guard(obj)) return obj;
throw new TypeGuardError("Failed type guard check: \n\n".concat(JSON.stringify(obj, undefined, 2)));
throw new TypeGuardError('Failed type guard', obj);
};

@@ -131,2 +131,2 @@ };

export default eg;
export { TypeGuard, TypeFromGuard, GuardWithOptional, primitiveGuards, higherOrderGuards, TypeGuardError, assertShape, addOptionalToGuard, addOptionalToHigherOrderGuard };
export { TypeGuard, TypeFromGuard, TypeGuardWithOptional, primitiveGuards, higherOrderGuards, TypeGuardError, assertShape, addOptionalToGuard, addOptionalToHigherOrderGuard };

@@ -30,6 +30,6 @@ "use strict";

});
Object.defineProperty(exports, "GuardWithOptional", {
Object.defineProperty(exports, "TypeGuardWithOptional", {
enumerable: true,
get: function get() {
return _typeHelpers.GuardWithOptional;
return _typeHelpers.TypeGuardWithOptional;
}

@@ -84,6 +84,6 @@ });

function TypeGuardError() {
function TypeGuardError(message, valueThatFailed) {
_classCallCheck(this, TypeGuardError);
return _possibleConstructorReturn(this, _getPrototypeOf(TypeGuardError).apply(this, arguments));
return _possibleConstructorReturn(this, _getPrototypeOf(TypeGuardError).call(this, message));
}

@@ -99,3 +99,3 @@

if (guard(obj)) return obj;
throw new TypeGuardError("Failed type guard check: \n\n".concat(JSON.stringify(obj, undefined, 2)));
throw new TypeGuardError('Failed type guard', obj);
};

@@ -102,0 +102,0 @@ };

{
"name": "@cloudflare/util-en-garde",
"description": "",
"version": "1.2.0",
"version": "2.0.0",
"types": "./dist/index.d.ts",

@@ -28,3 +28,3 @@ "main": "lib/index.js",

},
"gitHead": "c54d6a7d9b4b8f2512966f9bd6b11a6bf46e42f4"
"gitHead": "60c10b687558d9d4d517bd677d661a748ce02c2f"
}

@@ -11,3 +11,3 @@ import {

const hasProperties = <T extends object, K extends string>(
const hasProperties = <T extends object, K extends keyof any>(
obj: T,

@@ -14,0 +14,0 @@ ...keys: K[]

import { primitiveGuards, higherOrderGuards } from './core';
import { TypeGuard, TypeFromGuard, GuardWithOptional } from './type-helpers';
import {
TypeGuard,
TypeFromGuard,
TypeGuardWithOptional
} from './type-helpers';
import { addOptionalToGuard, addOptionalToHigherOrderGuard } from './optional';
class TypeGuardError extends Error {}
class TypeGuardError extends Error {
constructor(message: string, public valueThatFailed: unknown) {
super(message);
}
}

@@ -11,5 +19,3 @@ const assertShape = <Guard extends TypeGuard<any>>(guard: Guard) => (

if (guard(obj)) return obj;
throw new TypeGuardError(
`Failed type guard check: \n\n${JSON.stringify(obj, undefined, 2)}`
);
throw new TypeGuardError('Failed type guard', obj);
};

@@ -95,3 +101,3 @@

TypeFromGuard,
GuardWithOptional,
TypeGuardWithOptional,
primitiveGuards,

@@ -98,0 +104,0 @@ higherOrderGuards,

import { higherOrderGuards, primitiveGuards } from './core';
import { TypeGuard, TypeFromGuard, GuardWithOptional } from './type-helpers';
import {
TypeGuard,
TypeFromGuard,
TypeGuardWithOptional
} from './type-helpers';
export const addOptionalToGuard = <Guard extends TypeGuard<any>>(
guard: Guard
) =>
): TypeGuardWithOptional<TypeFromGuard<Guard>> =>
Object.assign(guard, {

@@ -19,3 +23,3 @@ optional: higherOrderGuards.oneOf(primitiveGuards.undefined, guard)

...params: HigherOrderGuardParams
): GuardWithOptional<TypeFromGuard<Guard>> =>
): TypeGuardWithOptional<TypeFromGuard<Guard>> =>
addOptionalToGuard(higherOrderGuard(...params));

@@ -41,3 +41,3 @@ import { Reverse, Tail, Head, Prepend } from '@cloudflare/types';

export type GuardWithOptional<T> = TypeGuard<T> & {
export type TypeGuardWithOptional<T> = TypeGuard<T> & {
optional: TypeGuard<T | undefined>;

@@ -44,0 +44,0 @@ };

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc