@cloudflare/util-en-garde
Advanced tools
Comparing version 6.2.1 to 6.4.0
@@ -6,10 +6,25 @@ # Change Log | ||
## [6.2.1](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-en-garde@6.2.0...@cloudflare/util-en-garde@6.2.1) (2019-11-26) | ||
# [6.4.0](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-en-garde@6.2.0...@cloudflare/util-en-garde@6.4.0) (2019-12-05) | ||
**Note:** Version bump only for package @cloudflare/util-en-garde | ||
### Features | ||
* **util-en-garde:** ONB-296 Add useful utils and enum support ([538c10a](http://stash.cfops.it:7999/fe/stratus/commits/538c10a)) | ||
* **util-en-garde:** TSX-159 Add branded types ([daba6dc](http://stash.cfops.it:7999/fe/stratus/commits/daba6dc)) | ||
# [6.3.0](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-en-garde@6.2.0...@cloudflare/util-en-garde@6.3.0) (2019-12-03) | ||
### Features | ||
* **util-en-garde:** TSX-159 Add branded types ([daba6dc](http://stash.cfops.it:7999/fe/stratus/commits/daba6dc)) | ||
# [6.2.0](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-en-garde@6.0.0...@cloudflare/util-en-garde@6.2.0) (2019-11-26) | ||
@@ -16,0 +31,0 @@ |
import * as t from 'io-ts'; | ||
import { Compute } from './type-helpers'; | ||
/** | ||
@@ -111,11 +112,38 @@ * Why does this wrapper exist? | ||
export declare const eg: { | ||
/** | ||
* Picks a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to pick from | ||
* @param keys Keys to pick | ||
* @returns Subset codec that only has 'keys'. | ||
*/ | ||
pick: <C extends ObjectCodec<any>, Keys extends keyof C["props"]>(codec: C, keys: Keys[]) => ObjectCodec<{ [key in Keys]: C["props"][key]; }>; | ||
/** | ||
* Omits a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to omit from | ||
* @param keys Keys to omit | ||
* @returns Subset codec without 'keys'. | ||
*/ | ||
omit: <C_1 extends ObjectCodec<any>, Keys_1 extends keyof C_1["props"]>(codec: C_1, keys: Keys_1[]) => ObjectCodec<Compute<Pick<C_1["props"], Exclude<keyof C_1["props"], Keys_1>>>>; | ||
/** | ||
* Creates union-of-literals LiteralType codec from ObjectCodec. | ||
* | ||
* @param Object codec | ||
* @returns LiteralType of keys, where keys are keyof codec. | ||
*/ | ||
keyOf: <C_2 extends ObjectCodec<any>>(codec: C_2) => Codec<t.LiteralType<Extract<keyof C_2["props"], string>>>; | ||
object: <P extends t.Props>(p: P, name?: string | undefined) => ObjectCodec<P>; | ||
tuple: <Codecs extends never[] | [t.Any, ...t.Any[]]>(codecs: Codecs, name?: string | undefined) => Codec<t.TupleC<Exclude<Codecs, never[]>>>; | ||
intersection: <Codecs_1 extends [t.Any, t.Any, ...t.Any[]]>(codecs: Codecs_1, name?: string | undefined) => Codec<t.IntersectionC<Codecs_1>>; | ||
array: <C extends t.Mixed>(codec: C, name?: string | undefined) => Codec<t.ArrayC<C>>; | ||
brand: <Name extends string, BaseCodec extends Codec<any>>(name: Name, base: BaseCodec, predicate: (value: BaseCodec["_A"]) => boolean) => Codec<t.BrandC<BaseCodec, { [N in Name]: symbol; }>>; | ||
enum: <Enum extends { | ||
[key: string]: string | number; | ||
}>(e: Enum) => Codec<t.LiteralType<Enum[keyof Enum]>>; | ||
array: <C_3 extends t.Mixed>(codec: C_3, name?: string | undefined) => Codec<t.ArrayC<C_3>>; | ||
union: <CS extends [t.Mixed, t.Mixed, ...t.Mixed[]]>(codecs: CS, name?: string | undefined) => Codec<t.UnionC<CS>>; | ||
record: <D extends t.Mixed, C_1 extends t.Mixed>(domain: D, codomain: C_1, name?: string | undefined) => Codec<t.RecordC<D, C_1>>; | ||
record: <D extends t.Mixed, C_4 extends t.Mixed>(domain: D, codomain: C_4, name?: string | undefined) => Codec<t.RecordC<D, C_4>>; | ||
partial: <P_1 extends t.Props>(props: P_1, name?: string | undefined) => Codec<t.PartialC<P_1>>; | ||
literal: <V extends string | number | boolean>(value: V, name?: string | undefined) => Codec<t.LiteralC<V>>; | ||
exact: <C_2 extends t.HasProps>(codec: C_2, name?: string | undefined) => Codec<t.ExactC<C_2>>; | ||
exact: <C_5 extends t.HasProps>(codec: C_5, name?: string | undefined) => Codec<t.ExactC<C_5>>; | ||
string: Codec<t.StringC>; | ||
@@ -122,0 +150,0 @@ number: Codec<t.NumberC>; |
@@ -32,2 +32,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 * as t from 'io-ts'; | ||
import { objectKeys, hasAtLeastTwo } from './type-helpers'; | ||
/** | ||
@@ -216,2 +217,20 @@ * Why does this wrapper exist? | ||
}, | ||
brand: function brand(name, base, predicate) { | ||
return new Codec(t.brand(base, predicate, name)); | ||
}, | ||
enum: function _enum(e) { | ||
var keyLiterals = objectKeys(e).map(function (k) { | ||
return t.literal(e[k]); | ||
}); | ||
if (hasAtLeastTwo(keyLiterals)) { | ||
return new Codec(t.union(keyLiterals)); | ||
} | ||
if (keyLiterals.length === 1) { | ||
return new Codec(keyLiterals[0]); | ||
} | ||
return new Codec(t.never); | ||
}, | ||
// The types for these higher order codecs work with simple hoc wrapper | ||
@@ -225,4 +244,59 @@ array: wrapHigherOrderCodec(t.array), | ||
}; | ||
export var eg = _objectSpread({}, primitiveCodecs, higherOrderCodecs); // re-export io-ts lib | ||
var helpers = { | ||
/** | ||
* Picks a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to pick from | ||
* @param keys Keys to pick | ||
* @returns Subset codec that only has 'keys'. | ||
*/ | ||
pick: function pick(codec, keys) { | ||
var props = codec.props; | ||
return eg.object(keys.reduce(function (acc, key) { | ||
acc[key] = props[key]; | ||
return acc; | ||
}, {})); | ||
}, | ||
/** | ||
* Omits a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to omit from | ||
* @param keys Keys to omit | ||
* @returns Subset codec without 'keys'. | ||
*/ | ||
omit: function omit(codec, keys) { | ||
var props = _objectSpread({}, codec.props); | ||
return eg.object(keys.reduce(function (acc, key) { | ||
delete acc[key]; | ||
return acc; | ||
}, props)); | ||
}, | ||
/** | ||
* Creates union-of-literals LiteralType codec from ObjectCodec. | ||
* | ||
* @param Object codec | ||
* @returns LiteralType of keys, where keys are keyof codec. | ||
*/ | ||
keyOf: function keyOf(codec) { | ||
var props = codec.props; | ||
var keyLiterals = objectKeys(props).map(function (k) { | ||
return t.literal(k); | ||
}); | ||
if (hasAtLeastTwo(keyLiterals)) { | ||
return new Codec(t.union(keyLiterals)); | ||
} | ||
if (keyLiterals.length === 1) { | ||
return new Codec(keyLiterals[0]); | ||
} | ||
return new Codec(t.never); | ||
} | ||
}; | ||
export var eg = _objectSpread({}, primitiveCodecs, higherOrderCodecs, helpers); // re-export io-ts lib | ||
export { t }; |
@@ -12,2 +12,4 @@ "use strict"; | ||
var _typeHelpers = require("./type-helpers"); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
@@ -161,2 +163,20 @@ | ||
}, | ||
brand: function brand(name, base, predicate) { | ||
return new Codec(t.brand(base, predicate, name)); | ||
}, | ||
enum: function _enum(e) { | ||
var keyLiterals = (0, _typeHelpers.objectKeys)(e).map(function (k) { | ||
return t.literal(e[k]); | ||
}); | ||
if ((0, _typeHelpers.hasAtLeastTwo)(keyLiterals)) { | ||
return new Codec(t.union(keyLiterals)); | ||
} | ||
if (keyLiterals.length === 1) { | ||
return new Codec(keyLiterals[0]); | ||
} | ||
return new Codec(t.never); | ||
}, | ||
// The types for these higher order codecs work with simple hoc wrapper | ||
@@ -170,6 +190,61 @@ array: wrapHigherOrderCodec(t.array), | ||
}; | ||
var helpers = { | ||
/** | ||
* Picks a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to pick from | ||
* @param keys Keys to pick | ||
* @returns Subset codec that only has 'keys'. | ||
*/ | ||
pick: function pick(codec, keys) { | ||
var props = codec.props; | ||
return eg.object(keys.reduce(function (acc, key) { | ||
acc[key] = props[key]; | ||
return acc; | ||
}, {})); | ||
}, | ||
var eg = _objectSpread({}, primitiveCodecs, higherOrderCodecs); // re-export io-ts lib | ||
/** | ||
* Omits a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to omit from | ||
* @param keys Keys to omit | ||
* @returns Subset codec without 'keys'. | ||
*/ | ||
omit: function omit(codec, keys) { | ||
var props = _objectSpread({}, codec.props); | ||
return eg.object(keys.reduce(function (acc, key) { | ||
delete acc[key]; | ||
return acc; | ||
}, props)); | ||
}, | ||
/** | ||
* Creates union-of-literals LiteralType codec from ObjectCodec. | ||
* | ||
* @param Object codec | ||
* @returns LiteralType of keys, where keys are keyof codec. | ||
*/ | ||
keyOf: function keyOf(codec) { | ||
var props = codec.props; | ||
var keyLiterals = (0, _typeHelpers.objectKeys)(props).map(function (k) { | ||
return t.literal(k); | ||
}); | ||
if ((0, _typeHelpers.hasAtLeastTwo)(keyLiterals)) { | ||
return new Codec(t.union(keyLiterals)); | ||
} | ||
if (keyLiterals.length === 1) { | ||
return new Codec(keyLiterals[0]); | ||
} | ||
return new Codec(t.never); | ||
} | ||
}; | ||
var eg = _objectSpread({}, primitiveCodecs, higherOrderCodecs, helpers); // re-export io-ts lib | ||
exports.eg = eg; |
{ | ||
"name": "@cloudflare/util-en-garde", | ||
"description": "", | ||
"version": "6.2.1", | ||
"version": "6.4.0", | ||
"types": "./dist/index.d.ts", | ||
@@ -29,3 +29,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "55bb599b15248fe154b90e3c349dddc83dd6f7f5" | ||
"gitHead": "9c58a8b6feec8aaa0d52e15511f66d39616d696a" | ||
} |
103
src/index.ts
import * as t from 'io-ts'; | ||
import { Refinement } from 'fp-ts/lib/function'; | ||
import { Compute, objectKeys, hasAtLeastTwo, ValueOf } from './type-helpers'; | ||
@@ -185,2 +187,32 @@ /** | ||
brand: <Name extends string, BaseCodec extends Codec<any>>( | ||
name: Name, | ||
base: BaseCodec, | ||
predicate: (value: TypeFromCodec<BaseCodec>) => boolean | ||
) => | ||
new Codec( | ||
t.brand( | ||
base, | ||
predicate as Refinement< | ||
BaseCodec['_A'], | ||
t.Branded<BaseCodec['_A'], { [N in Name]: symbol }> | ||
>, | ||
name | ||
) | ||
), | ||
enum: <Enum extends { [key: string]: string | number }>( | ||
e: Enum | ||
): Codec<t.LiteralType<ValueOf<Enum>>> => { | ||
const keyLiterals = objectKeys(e).map(k => t.literal(e[k] as any)); | ||
if (hasAtLeastTwo(keyLiterals)) { | ||
return new Codec(t.union(keyLiterals)) as any; | ||
} | ||
if (keyLiterals.length === 1) { | ||
return new Codec(keyLiterals[0]); | ||
} | ||
return new Codec(t.never as any) as any; | ||
}, | ||
// The types for these higher order codecs work with simple hoc wrapper | ||
@@ -195,2 +227,70 @@ array: wrapHigherOrderCodec(t.array), | ||
const helpers = { | ||
/** | ||
* Picks a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to pick from | ||
* @param keys Keys to pick | ||
* @returns Subset codec that only has 'keys'. | ||
*/ | ||
pick: <C extends ObjectCodec<any>, Keys extends keyof C['props']>( | ||
codec: C, | ||
keys: Keys[] | ||
) => { | ||
const props = codec.props; | ||
return eg.object( | ||
keys.reduce((acc, key) => { | ||
acc[key] = props[key]; | ||
return acc; | ||
}, {} as { [key in Keys]: C['props'][key] }) | ||
); | ||
}, | ||
/** | ||
* Omits a subset of ObjectCodec and returns new ObjectCodec. The subset is determined by keys array. | ||
* | ||
* @param codec Codec to omit from | ||
* @param keys Keys to omit | ||
* @returns Subset codec without 'keys'. | ||
*/ | ||
omit: <C extends ObjectCodec<any>, Keys extends keyof C['props']>( | ||
codec: C, | ||
keys: Keys[] | ||
) => { | ||
const props = { ...codec.props }; | ||
return eg.object( | ||
keys.reduce((acc, key) => { | ||
delete acc[key]; | ||
return acc; | ||
}, props) as Compute<Omit<C['props'], Keys>> | ||
); | ||
}, | ||
/** | ||
* Creates union-of-literals LiteralType codec from ObjectCodec. | ||
* | ||
* @param Object codec | ||
* @returns LiteralType of keys, where keys are keyof codec. | ||
*/ | ||
keyOf: <C extends ObjectCodec<any>>( | ||
codec: C | ||
): Codec<t.LiteralType<Extract<keyof C['props'], string>>> => { | ||
const props: C['props'] = codec.props; | ||
const keyLiterals = objectKeys(props).map(k => t.literal(k as any)); | ||
if (hasAtLeastTwo(keyLiterals)) { | ||
return new Codec(t.union(keyLiterals)) as any; | ||
} | ||
if (keyLiterals.length === 1) { | ||
return new Codec(keyLiterals[0]); | ||
} | ||
return new Codec(t.never as any) as any; | ||
} | ||
}; | ||
export type TypeFromCodec<T extends IoTsCodec> = t.TypeOf<T>; | ||
@@ -200,3 +300,4 @@ | ||
...primitiveCodecs, | ||
...higherOrderCodecs | ||
...higherOrderCodecs, | ||
...helpers | ||
}; | ||
@@ -203,0 +304,0 @@ |
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
53834
11
1020