Comparing version 1.4.0 to 1.5.0
@@ -99,37 +99,8 @@ /** | ||
}; | ||
/** | ||
* A structure-preserving object from a source data towards a target data. | ||
* | ||
* The keys of the schema match the desired destination structure. | ||
* Each value corresponds to an Action applied by Morphism when iterating over the input data | ||
* @example | ||
* ```typescript | ||
* | ||
* const input = { | ||
* foo: { | ||
* baz: 'value1' | ||
* } | ||
* }; | ||
* | ||
* const schema: Schema = { | ||
* bar: 'foo', // ActionString | ||
* qux: ['foo', 'foo.baz'], // ActionAggregator | ||
* quux: (iteratee, source, destination) => { // ActionFunction | ||
* return iteratee.foo; | ||
* }, | ||
* corge: { // ActionSelector | ||
* path: 'foo.baz', | ||
* fn: (propertyValue, source) => { | ||
* return propertyValue; | ||
* } | ||
* } | ||
* }; | ||
* | ||
* morphism(schema, input); | ||
* ``` | ||
*/ | ||
export interface Schema { | ||
/** `destinationProperty` is the name of the property of the target object you want to produce */ | ||
[destinationProperty: string]: ActionString | ActionFunction | ActionAggregator | ActionSelector; | ||
} | ||
export declare type StrictSchema<Target> = { | ||
[destinationProperty in keyof Target]: ActionString | ActionFunction | ActionAggregator | ActionSelector; | ||
}; | ||
export declare type Schema<Target> = { | ||
[destinationProperty in keyof Target]?: ActionString | ActionFunction | ActionAggregator | ActionSelector; | ||
}; | ||
interface Constructable<T> { | ||
@@ -164,19 +135,19 @@ new (...args: any[]): T; | ||
*/ | ||
export declare function morphism<TSchema extends Schema>(schema: TSchema, items: any[]): { | ||
export declare function morphism<TSchema extends Schema<any>>(schema: TSchema, items: any[]): { | ||
[P in keyof TSchema]: any; | ||
}[]; | ||
export declare function morphism<TSchema extends Schema>(schema: TSchema, items: any): { | ||
export declare function morphism<TSchema extends Schema<any>>(schema: TSchema, items: any): { | ||
[P in keyof TSchema]: any; | ||
}; | ||
export declare function morphism<Target>(schema: Schema, items: any[]): Target[]; | ||
export declare function morphism<Target>(schema: Schema, items: any): Target; | ||
export declare function morphism<TSchema extends Schema>(schema: TSchema): Mapper<{ | ||
export declare function morphism<Target>(schema: Schema<Target>, items: any[]): Target[]; | ||
export declare function morphism<Target>(schema: Schema<Target>, items: any): Target; | ||
export declare function morphism<TSchema extends Schema<any>>(schema: TSchema): Mapper<{ | ||
[P in keyof TSchema]: any; | ||
}>; | ||
export declare function morphism<Target>(schema: Schema): Mapper<Target>; | ||
export declare function morphism(schema: Schema, items: null): undefined; | ||
export declare function morphism<Target>(schema: Schema<Target>): Mapper<Target>; | ||
export declare function morphism(schema: Schema<any>, items: null): undefined; | ||
export declare function morphism(schema: null, items: {}): undefined; | ||
export declare function morphism<Target, Source>(schema: Schema, items: null, type: Constructable<Target>): Mapper<Target>; | ||
export declare function morphism<Target, Source>(schema: Schema, items: Source[], type: Constructable<Target>): Target[]; | ||
export declare function morphism<Target, Source>(schema: Schema, items: Source, type: Constructable<Target>): Target; | ||
export declare function morphism<Target, Source>(schema: Schema<Target>, items: null, type: Constructable<Target>): Mapper<Target>; | ||
export declare function morphism<Target, Source>(schema: Schema<Target>, items: Source[], type: Constructable<Target>): Target[]; | ||
export declare function morphism<Target, Source>(schema: Schema<Target>, items: Source, type: Constructable<Target>): Target; | ||
export interface IMorphismRegistry { | ||
@@ -190,3 +161,3 @@ /** | ||
*/ | ||
register<Target, TSchema extends Schema>(type: Constructable<Target>, schema?: TSchema): Mapper<Target>; | ||
register<Target, TSchema extends Schema<Target>>(type: Constructable<Target>, schema?: TSchema): Mapper<Target>; | ||
/** | ||
@@ -216,3 +187,3 @@ * Transform any input in the specified Class | ||
*/ | ||
setMapper<Target, TSchema extends Schema>(type: Constructable<Target>, schema: TSchema): Mapper<Target>; | ||
setMapper<Target, TSchema extends Schema<Target>>(type: Constructable<Target>, schema: TSchema): Mapper<Target>; | ||
/** | ||
@@ -253,3 +224,3 @@ * Delete a registered schema associated to a Class | ||
*/ | ||
register<Target, TSchema extends Schema>(type: Constructable<Target>, schema?: TSchema): Mapper<Target>; | ||
register<Target, TSchema extends Schema<Target>>(type: Constructable<Target>, schema?: TSchema): Mapper<Target>; | ||
/** | ||
@@ -277,3 +248,3 @@ * Transform any input in the specified Class | ||
*/ | ||
setMapper<Target>(type: Constructable<Target>, schema: Schema): Mapper<Target>; | ||
setMapper<Target>(type: Constructable<Target>, schema: Schema<Target>): Mapper<Target>; | ||
/** | ||
@@ -280,0 +251,0 @@ * Delete a registered schema associated to a Class |
{ | ||
"name": "morphism", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Library to transform any Object / JSON to JavaScript Object Literals, and ES6 Class Objects. Help you scale your data processing", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/nobrainr/morphism", |
@@ -30,2 +30,3 @@ # Morphism | ||
- [Schema Example](#schema-example) | ||
- [1.1 Using a strict Schema](#11-using-a-strict-schema) | ||
- [2. Morphism as Currying Function](#2-morphism-as-currying-function) | ||
@@ -178,2 +179,20 @@ - [API](#api) | ||
#### 1.1 Using a strict Schema | ||
You might want to enforce the keys provided in your schema using `Typescript`. This is possible using a `StrictSchema`. Doing so will require to map every field of the `Target` type provided. | ||
```ts | ||
interface IFoo { | ||
foo: string; | ||
bar: number; | ||
} | ||
const schema: StrictSchema<IFoo> = { foo: 'qux', bar: () => 'test' }; | ||
const source = { qux: 'foo' }; | ||
const target = morphism(schema, source); | ||
// { | ||
// "foo": "qux", | ||
// "bar": "test" | ||
// } | ||
``` | ||
### 2. Morphism as Currying Function | ||
@@ -180,0 +199,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
58222
401
0
275