Comparing version 1.3.0 to 1.4.0
@@ -10,3 +10,3 @@ export declare const aggregator: (paths: any, object: any) => any; | ||
}; | ||
export declare function get(object: object, path: string): object; | ||
export declare function get(object: any, path: string): any; | ||
export declare function zipObject(props: string[], values: any[]): {}; |
/** | ||
* A String path that indicates where to find the property in the source input | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* const source = { | ||
* foo: 'baz', | ||
* bar: ['bar', 'foo'], | ||
* baz: { | ||
* qux: 'bazqux' | ||
* } | ||
* }; | ||
* const schema = { | ||
* foo: 'foo', // Simple Projection | ||
* bazqux: 'baz.qux' // Grab a value from a nested property | ||
* }; | ||
* | ||
* morphism(schema, source); | ||
* //=> { foo: 'baz', bazqux: 'bazqux' } | ||
* ``` | ||
* "path.to.property" | ||
* "sourceProperty" | ||
* ``` | ||
* | ||
@@ -17,7 +31,66 @@ */ | ||
* @param target The current element transformed | ||
* @example | ||
* ```typescript | ||
* | ||
* const source = { | ||
* foo: { | ||
* bar: 'bar' | ||
* } | ||
* }; | ||
* let schema = { | ||
* bar: iteratee => { | ||
* // Apply a function over the source propery | ||
* return iteratee.foo.bar; | ||
* } | ||
* }; | ||
* | ||
* morphism(schema, source); | ||
* //=> { bar: 'bar' } | ||
* ``` | ||
* | ||
*/ | ||
(iteratee: any, source: any | any[], target: any): any; | ||
}; | ||
/** | ||
* An Array of String that allows to perform a function over source property | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* const source = { | ||
* foo: 'foo', | ||
* bar: 'bar' | ||
* }; | ||
* let schema = { | ||
* fooAndBar: ['foo', 'bar'] // Grab these properties into fooAndBar | ||
* }; | ||
* | ||
* morphism(schema, source); | ||
* //=> { fooAndBar: { foo: 'foo', bar: 'bar' } } | ||
* ``` | ||
*/ | ||
export declare type ActionAggregator = string[]; | ||
/** | ||
* An Object that allows to perform a function over a source property's value | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* const source = { | ||
* foo: { | ||
* bar: 'bar' | ||
* } | ||
* }; | ||
* let schema = { | ||
* barqux: { | ||
* path: 'foo.bar', | ||
* fn: value => `${value}qux` // Apply a function over the source property's value | ||
* } | ||
* }; | ||
* | ||
* morphism(schema, source); | ||
* //=> { barqux: 'barqux' } | ||
*``` | ||
* | ||
*/ | ||
export declare type ActionSelector = { | ||
@@ -62,26 +135,9 @@ path: string | string[]; | ||
} | ||
interface MorphismFunction { | ||
/** | ||
* Currying function that either outputs a mapping function or the transformed data. | ||
* | ||
* @example | ||
* ```js | ||
// => Outputs a function when only a schema is provided | ||
const fn = morphism(schema); | ||
const result = fn(data); | ||
// => Outputs the transformed data when a schema and the input data is provided | ||
const result = morphism(schema, data); | ||
// => Outputs the transformed data as instance of the Class provided | ||
const result = morphism(schema, data, Foo); | ||
// result is instance of Foo | ||
* ``` | ||
* @param {Schema} schema Configuration schema to compute data source properties | ||
* @param {} items Object or Collection to be mapped | ||
* @param {} type | ||
* | ||
*/ | ||
(schema: Schema, items?: any, type?: any): typeof type; | ||
interface Constructable<T> { | ||
new (...args: any[]): T; | ||
} | ||
interface Mapper<Target> { | ||
<Source>(source: Source[]): Target[]; | ||
<Source>(source: Source): Target; | ||
} | ||
/** | ||
@@ -109,12 +165,28 @@ * Currying function that either outputs a mapping function or the transformed data. | ||
*/ | ||
export declare function morphism(schema: Schema, items?: any, type?: any): typeof type; | ||
interface IMorphismRegistry { | ||
export declare function morphism<TSchema extends Schema>(schema: TSchema, items: any[]): { | ||
[P in keyof TSchema]: any; | ||
}[]; | ||
export declare function morphism<TSchema extends Schema>(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<{ | ||
[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(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 interface IMorphismRegistry { | ||
/** | ||
* Register a mapping schema for a Class. | ||
* | ||
* @param {Type} type Class Type to be registered | ||
* @param {Object} schema Configuration of how properties are computed from the source | ||
* @param type Class Type to be registered | ||
* @param schema Structure-preserving object from a source data towards a target data. | ||
* | ||
*/ | ||
register: (type: any, schema?: Schema) => any; | ||
register<Target, TSchema extends Schema>(type: Constructable<Target>, schema?: TSchema): Mapper<Target>; | ||
/** | ||
@@ -127,3 +199,5 @@ * Transform any input in the specified Class | ||
*/ | ||
map: (type: any, data?: any) => any; | ||
map<Target>(type: Target): Mapper<Target>; | ||
map<Target, Source>(type: Constructable<Target>, data: Source[]): Target[]; | ||
map<Target, Source>(type: Constructable<Target>, data: Source): Target; | ||
/** | ||
@@ -135,3 +209,3 @@ * Get a specific mapping function for the provided Class | ||
*/ | ||
getMapper: (type: any) => any; | ||
getMapper<Target>(type: Constructable<Target>): Mapper<Target>; | ||
/** | ||
@@ -144,3 +218,3 @@ * Set a schema for a specific Class Type | ||
*/ | ||
setMapper: (type: any, schema: Schema) => any; | ||
setMapper<Target, TSchema extends Schema>(type: Constructable<Target>, schema: TSchema): Mapper<Target>; | ||
/** | ||
@@ -152,6 +226,12 @@ * Delete a registered schema associated to a Class | ||
*/ | ||
deleteMapper: (type: any) => any; | ||
deleteMapper<Target>(type: Constructable<Target>): any; | ||
/** | ||
* Get the list of the mapping function registered | ||
* Check if a schema has already been registered for this type | ||
* | ||
* @param {*} type | ||
*/ | ||
exists<Target>(type: Target): boolean; | ||
/** | ||
* Get the list of the mapping functions registered | ||
* | ||
* @param {Type} type Class Type of the ouput Data | ||
@@ -162,3 +242,62 @@ * | ||
} | ||
declare const MorphismObject: MorphismFunction & IMorphismRegistry; | ||
export default MorphismObject; | ||
export declare class MorphismRegistry implements IMorphismRegistry { | ||
private _registry; | ||
/** | ||
*Creates an instance of MorphismRegistry. | ||
* @param {Map<any, any>} cache Cache implementation to store the mapping functions. | ||
*/ | ||
constructor(cache?: Map<any, any> | WeakMap<any, any>); | ||
/** | ||
* Register a mapping schema for a Class. | ||
* | ||
* @param type Class Type to be registered | ||
* @param schema Structure-preserving object from a source data towards a target data. | ||
* | ||
*/ | ||
register<Target, TSchema extends Schema>(type: Constructable<Target>, schema?: TSchema): Mapper<Target>; | ||
/** | ||
* Transform any input in the specified Class | ||
* | ||
* @param {Type} type Class Type of the ouput Data | ||
* @param {Object} data Input data to transform | ||
* | ||
*/ | ||
map(type: any, data?: any): any; | ||
/** | ||
* Get a specific mapping function for the provided Class | ||
* | ||
* @param {Type} type Class Type of the ouput Data | ||
* | ||
*/ | ||
getMapper<Target>(type: Constructable<Target>): any; | ||
/** | ||
* Set a schema for a specific Class Type | ||
* | ||
* @param {Type} type Class Type of the ouput Data | ||
* @param {Schema} schema Class Type of the ouput Data | ||
* | ||
*/ | ||
setMapper<Target>(type: Constructable<Target>, schema: Schema): Mapper<Target>; | ||
/** | ||
* Delete a registered schema associated to a Class | ||
* | ||
* @param type ES6 Class Type of the ouput Data | ||
* | ||
*/ | ||
deleteMapper(type: any): any; | ||
/** | ||
* Check if a schema has already been registered for this type | ||
* | ||
* @param {*} type | ||
*/ | ||
exists(type: any): any; | ||
/** | ||
* Get the list of the mapping functions registered | ||
* | ||
* @param {Type} type Class Type of the ouput Data | ||
* | ||
*/ | ||
readonly mappers: Map<any, any>; | ||
} | ||
declare const Morphism: typeof morphism & IMorphismRegistry; | ||
export default Morphism; |
@@ -1,1 +0,1 @@ | ||
module.exports=function(e){var r={};function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var i in e)t.d(n,i,function(r){return e[r]}.bind(null,i));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var n=t(1);function i(e,r,t,i){return Object.entries(r).map(function(r){var o,u,a,c,s=r[0],f=r[1];if(n.isString(f))return(o={})[s]=n.get(e,f),o;if(n.isFunction(f))return(u={})[s]=f.call(void 0,e,t,i),u;if(Array.isArray(f))return(a={})[s]=n.aggregator(f,e),a;if(n.isObject(f)){var p=void 0;try{var g=void 0;Array.isArray(f.path)?g=n.aggregator(f.path,e):n.isString(f.path)&&(g=n.get(e,f.path)),p=f.fn.call(void 0,g,e,t,i)}catch(e){throw e.message="Unable to set target property ["+s+"].\n \n An error occured when applying ["+f.fn.name+"] on property ["+f.path+"]\n \n Internal error: "+e.message,e}return(c={})[s]=p,c}}).reduce(function(e,r){return n.assignInWith(e,r,function(e,r){return n.isUndefined(r)?n.isUndefined(e)?void 0:e:r})},i)}var o=function(e,r){return function(t){if(!t)return t;if(Array.isArray(t))return t.map(function(n){if(r){var o=new r;return i(n,e,t,o)}return i(n,e,t,{})});var n=t;if(r){var o=new r;return i(n,e,[n],o)}return i(n,e,[n],{})}},u=function(e,r){var t=Object.keys(new e),i=n.zipObject(t,t);return Object.assign(i,r)};function a(e,r,t){if(void 0===r&&void 0===t)return o(e);if(e&&r&&t){var n=u(t,e);return o(n,t)(r)}if(e&&r)return o(e)(r);if(t&&r){n=u(t,e);return o(n,t)(r)}if(t){var i=u(t,e);return function(e){return o(i,t)(e)}}}r.morphism=a;var c=new(function(){function e(){this._registry={cache:new Map}}return e.prototype.register=function(e,r){if(!e&&!r)throw new Error("type paramater is required when register a mapping");if(this.exists(e))throw new Error("A mapper for "+e.name+" has already been registered");var t=a(r,null,e);return this._registry.cache.set(e,t),t},e.prototype.map=function(e,r){if(!this.exists(e)){var t=this.register(e);if(void 0===r)return t}return this.getMapper(e)(r)},e.prototype.getMapper=function(e){return this._registry.cache.get(e)},Object.defineProperty(e.prototype,"mappers",{get:function(){return this._registry.cache},enumerable:!0,configurable:!0}),e.prototype.exists=function(e){return this._registry.cache.has(e)},e.prototype.setMapper=function(e,r){if(r){if(this.exists(e)){var t=a(r,null,e);return this._registry.cache.set(e,t),t}throw new Error("The type "+e.name+" is not registered. Register it using `Mophism.register("+e.name+", schema)`")}throw new Error("The schema must be an Object. Found "+r)},e.prototype.deleteMapper=function(e){return this._registry.cache.delete(e)},e}()),s=a;s.register=function(e,r){return c.register(e,r)},s.map=function(e,r){return c.map(e,r)},s.getMapper=function(e){return c.getMapper(e)},s.setMapper=function(e,r){return c.setMapper(e,r)},s.deleteMapper=function(e){return c.deleteMapper(e)},s.mappers=c.mappers,r.default=s},function(e,r,t){"use strict";var n=this&&this.__assign||Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var i in r=arguments[t])Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i]);return e};function i(e){var r=typeof e;return null!=e&&("object"===r||"function"===r)}function o(e,r,t){var i,o=(r=(r=r.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),u=o.pop(),a=o.reduceRight(function(e,r){var t;return(t={})[r]=e,t},((i={})[u]=t,i));return n({},e,a)}function u(e,r){for(var t=(r=(r=r.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),n=0,o=t.length;n<o;++n){var u=t[n];if(!(i(e)&&u in e))return;e=e[u]}return e}Object.defineProperty(r,"__esModule",{value:!0}),r.aggregator=function(e,r){return e.reduce(function(e,t){return o(e,t,u(r,t))},{})},r.assignInWith=function(e,r,t){return Object.entries(r).forEach(function(r){var n=r[0],i=r[1];e[n]=t?t(e[n],i):i}),e},r.isUndefined=function(e){return void 0===e},r.isObject=i,r.isString=function(e){return"string"==typeof e||e instanceof String},r.isFunction=function(e){return"function"==typeof e},r.set=o,r.get=u,r.zipObject=function(e,r){return e.reduce(function(e,t,i){var o;return n({},e,((o={})[t]=r[i],o))},{})}}]); | ||
module.exports=function(r){var e={};function t(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return r[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}return t.m=r,t.c=e,t.d=function(r,e,n){t.o(r,e)||Object.defineProperty(r,e,{enumerable:!0,get:n})},t.r=function(r){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})},t.t=function(r,e){if(1&e&&(r=t(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var i in r)t.d(n,i,function(e){return r[e]}.bind(null,i));return n},t.n=function(r){var e=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(e,"a",e),e},t.o=function(r,e){return Object.prototype.hasOwnProperty.call(r,e)},t.p="",t(t.s=0)}([function(r,e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=t(1);function i(r,e,t,i){return Object.entries(e).map(function(e){var o,u,a,c,s=e[0],f=e[1];if(n.isString(f))return(o={})[s]=n.get(r,f),o;if(n.isFunction(f))return(u={})[s]=f.call(void 0,r,t,i),u;if(Array.isArray(f))return(a={})[s]=n.aggregator(f,r),a;if(n.isObject(f)){var p=void 0;try{var g=void 0;Array.isArray(f.path)?g=n.aggregator(f.path,r):n.isString(f.path)&&(g=n.get(r,f.path)),p=f.fn.call(void 0,g,r,t,i)}catch(r){throw r.message="Unable to set target property ["+s+"].\n \n An error occured when applying ["+f.fn.name+"] on property ["+f.path+"]\n \n Internal error: "+r.message,r}return(c={})[s]=p,c}}).reduce(function(r,e){return n.assignInWith(r,e,function(r,e){return n.isUndefined(e)?n.isUndefined(r)?void 0:r:e})},i)}function o(r,e){return function(t){if(!t)return t;if(Array.isArray(t))return t.map(function(n){if(e){var o=new e;return i(n,r,t,o)}return i(n,r,t,{})});var n=t;if(e){var o=new e;return i(n,r,[n],o)}return i(n,r,[n],{})}}function u(r,e){var t=Object.keys(new r),i=n.zipObject(t,t);return Object.assign(i,e)}function a(r,e,t){if(void 0===e&&void 0===t)return o(r);if(r&&e&&t)return o(u(t,r),t)(e);if(r&&e)return o(r)(e);if(t&&e)return o(u(t,r),t)(e);if(t){var n=u(t,r);return function(r){return o(n,t)(r)}}}e.morphism=a;var c=function(){function r(r){this._registry=null,this._registry=r||{cache:new Map}}return r.prototype.register=function(r,e){if(!r&&!e)throw new Error("type paramater is required when register a mapping");if(this.exists(r))throw new Error("A mapper for "+r.name+" has already been registered");var t=a(e,null,r);return this._registry.cache.set(r,t),t},r.prototype.map=function(r,e){if(!this.exists(r)){var t=this.register(r);if(void 0===e)return t}return this.getMapper(r)(e)},r.prototype.getMapper=function(r){return this._registry.cache.get(r)},r.prototype.setMapper=function(r,e){if(e){if(this.exists(r)){var t=a(e,null,r);return this._registry.cache.set(r,t),t}throw new Error("The type "+r.name+" is not registered. Register it using `Mophism.register("+r.name+", schema)`")}throw new Error("The schema must be an Object. Found "+e)},r.prototype.deleteMapper=function(r){return this._registry.cache.delete(r)},r.prototype.exists=function(r){return this._registry.cache.has(r)},Object.defineProperty(r.prototype,"mappers",{get:function(){return this._registry.cache},enumerable:!0,configurable:!0}),r}();e.MorphismRegistry=c;var s=new c,f=a;f.register=function(r,e){return s.register(r,e)},f.map=function(r,e){return s.map(r,e)},f.getMapper=function(r){return s.getMapper(r)},f.setMapper=function(r,e){return s.setMapper(r,e)},f.deleteMapper=function(r){return s.deleteMapper(r)},f.mappers=s.mappers;var p=f;e.default=p},function(r,e,t){"use strict";var n=this&&this.__assign||Object.assign||function(r){for(var e,t=1,n=arguments.length;t<n;t++)for(var i in e=arguments[t])Object.prototype.hasOwnProperty.call(e,i)&&(r[i]=e[i]);return r};function i(r){var e=typeof r;return null!=r&&("object"===e||"function"===e)}function o(r,e,t){var i,o=(e=(e=e.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),u=o.pop(),a=o.reduceRight(function(r,e){var t;return(t={})[e]=r,t},((i={})[u]=t,i));return n({},r,a)}function u(r,e){for(var t=(e=(e=e.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),n=0,o=t.length;n<o;++n){var u=t[n];if(!(i(r)&&u in r))return;r=r[u]}return r}Object.defineProperty(e,"__esModule",{value:!0}),e.aggregator=function(r,e){return r.reduce(function(r,t){return o(r,t,u(e,t))},{})},e.assignInWith=function(r,e,t){return Object.entries(e).forEach(function(e){var n=e[0],i=e[1];r[n]=t?t(r[n],i):i}),r},e.isUndefined=function(r){return void 0===r},e.isObject=i,e.isString=function(r){return"string"==typeof r||r instanceof String},e.isFunction=function(r){return"function"==typeof r},e.set=o,e.get=u,e.zipObject=function(r,e){return r.reduce(function(r,t,i){var o;return n({},r,((o={})[t]=e[i],o))},{})}}]); |
{ | ||
"name": "morphism", | ||
"version": "1.3.0", | ||
"version": "1.4.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", |
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
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
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
57712
304
1