@gucciogucci/contented
Advanced tools
Comparing version 1.1.0 to 1.2.0
import { ContentedError } from './ContentedError'; | ||
import { ExpectedType, NonFatalErrorType, Type } from './Type'; | ||
import { ErrorType, ExpectedType, NonFatalErrorType, Type } from './Type'; | ||
export declare function combine<E extends ContentedError, Ts extends Type<unknown, E>[], O>(fn: (...args: [...ExpectedTypes<Ts>]) => O, ...types: [...Ts]): Type<CombinationOf<Ts, O>, UnionOfErrorTypes<Ts>>; | ||
declare type CombinationOf<Ts, O> = UnionOfNonFatalErrorTypes<Ts> extends never ? O : O | [O, UnionOfNonFatalErrorTypes<Ts>[]]; | ||
declare type UnionOfNonFatalErrorTypes<Ts> = NonFatalErrorTypes<Ts>[number]; | ||
declare type ErrorType<T> = T extends Type<any, infer E> ? E : never; | ||
declare type ExpectedTypes<Ts> = Ts extends [infer Head, ...infer Tail] ? [ExpectedType<Head>, ...ExpectedTypes<Tail>] : []; | ||
@@ -8,0 +7,0 @@ declare type UnionOfErrorTypes<Ts> = ErrorTypes<Ts>[number]; |
@@ -6,5 +6,7 @@ export * from './always'; | ||
export * from './combine'; | ||
export * from './combineIntoObject'; | ||
export * from './fallback'; | ||
export * from './match'; | ||
export * from './number'; | ||
export * from './permissiveArrayOf'; | ||
export * from './optional'; | ||
@@ -11,0 +13,0 @@ export * from './string'; |
@@ -19,5 +19,7 @@ "use strict"; | ||
__exportStar(require("./combine"), exports); | ||
__exportStar(require("./combineIntoObject"), exports); | ||
__exportStar(require("./fallback"), exports); | ||
__exportStar(require("./match"), exports); | ||
__exportStar(require("./number"), exports); | ||
__exportStar(require("./permissiveArrayOf"), exports); | ||
__exportStar(require("./optional"), exports); | ||
@@ -24,0 +26,0 @@ __exportStar(require("./string"), exports); |
@@ -15,2 +15,3 @@ import { ContentedError } from './ContentedError'; | ||
export declare type ExpectedType<T> = [T] extends [Type<infer A, any>] ? ExpectedType<A> : HasNonFatalErrors<T> extends true ? T extends [infer U, any] ? U : never : T; | ||
export declare type ErrorType<T> = T extends Type<any, infer E> ? E : never; | ||
declare type OrErrors<E, F> = EnumerateErrors<StripJoint<E>, StripJoint<F>> extends never ? never : Joint<EnumerateErrors<StripJoint<E>, StripJoint<F>>>; | ||
@@ -17,0 +18,0 @@ declare type EnumerateErrors<E, F> = E extends never ? never : F extends never ? never : E extends unknown[] ? F extends unknown[] ? [...E, ...F] : [...E, F] : F extends unknown[] ? [E, ...F] : [E, F]; |
{ | ||
"name": "@gucciogucci/contented", | ||
"description": "A library to coerce values at run-time.", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "Gucci <npm@gucci.com>", | ||
@@ -6,0 +6,0 @@ "license": "LGPL-3.0-only", |
@@ -36,2 +36,3 @@ <div align="center"> | ||
- [`combine(fn, ...Ts)`](#combinefn-ts) | ||
- [`combineIntoObject({ [...Keys]: [...Ts] })`](#combineintoobject-keys-ts-) | ||
- [`T1.or(T2)`](#t1ort2) | ||
@@ -137,3 +138,3 @@ - [`optional(T)`](#optionalt) | ||
const stringAtAB = at(['a', 'b'], string) | ||
const stringAtAB = at(['a', 'b'], string); | ||
@@ -272,2 +273,31 @@ coerceTo(stringAtAB, { a: { b: 'hello' } }); | ||
#### `combineIntoObject({ [...Keys]: [...Ts] })` | ||
`combineIntObject` is a convenience function that may be used every time there is the need of combining some known run-time types `Ts` into an object of known keys. In other words, instead of writing the following: | ||
```typescript | ||
import { string, number, at, combine, coerceTo } from '@gucciogucci/contented'; | ||
const Image = combine( | ||
(url, size) => ({ url, size }), | ||
at('url', string), | ||
at(['metadata', 'size'], number) | ||
); | ||
const image = coerceTo(Image, data); | ||
``` | ||
One may simply write: | ||
```typescript | ||
import { string, number, at, combineIntoObject, coerceTo } from '@gucciogucci/contented'; | ||
const Image = combineIntoObject({ | ||
url: at('url', string), | ||
size: at(['metadata', 'size'], number) | ||
}); | ||
const image = coerceTo(Image, data); | ||
``` | ||
#### `T1.or(T2)` | ||
@@ -274,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
53886
50
624
422
0