Comparing version 1.5.0 to 1.6.0
@@ -129,3 +129,3 @@ /** | ||
* ``` | ||
* @param {Schema} schema Configuration schema to compute data source properties | ||
* @param {Schema} schema Structure-preserving object from a source data towards a target data | ||
* @param {} items Object or Collection to be mapped | ||
@@ -266,3 +266,23 @@ * @param {} type | ||
} | ||
/** | ||
* Function Decorator transforming the return value of the targeted Function using the provided Schema and/or Type | ||
* | ||
* @param {Schema<Target>} schema Structure-preserving object from a source data towards a target data | ||
* @param {Constructable<Target>} [type] Target Class Type | ||
*/ | ||
export declare function morph<Target>(schema: Schema<Target>, type?: Constructable<Target>): (target: any, name: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
/** | ||
* Function Decorator transforming the return value of the targeted Function to JS Object(s) using the provided Schema | ||
* | ||
* @param {StrictSchema<Target>} schema Structure-preserving object from a source data towards a target data | ||
*/ | ||
export declare function toJSObject<Target>(schema: StrictSchema<Target>): (target: any, name: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
/** | ||
* Function Decorator transforming the return value of the targeted Function using the provided Schema and Class Type | ||
* | ||
* @param {Schema<Target>} schema Structure-preserving object from a source data towards a target data | ||
* @param {Constructable<Target>} [type] Target Class Type | ||
*/ | ||
export declare function toClassObject<Target>(schema: Schema<Target>, type: Constructable<Target>): (target: any, name: string, descriptor: PropertyDescriptor) => PropertyDescriptor; | ||
declare const Morphism: typeof morphism & IMorphismRegistry; | ||
export default Morphism; |
@@ -1,1 +0,1 @@ | ||
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))},{})}}]); | ||
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";var n=this&&this.__read||function(r,e){var t="function"==typeof Symbol&&r[Symbol.iterator];if(!t)return r;var n,i,o=t.call(r),u=[];try{for(;(void 0===e||e-- >0)&&!(n=o.next()).done;)u.push(n.value)}catch(r){i={error:r}}finally{try{n&&!n.done&&(t=o.return)&&t.call(o)}finally{if(i)throw i.error}}return u};Object.defineProperty(e,"__esModule",{value:!0});var i=t(1);function o(r,e,t,o){return Object.entries(e).map(function(e){var u,a,c,f,s=n(e,2),p=s[0],l=s[1];if(i.isString(l))return(u={})[p]=i.get(r,l),u;if(i.isFunction(l))return(a={})[p]=l.call(void 0,r,t,o),a;if(Array.isArray(l))return(c={})[p]=i.aggregator(l,r),c;if(i.isObject(l)){var h=void 0;try{var y=void 0;Array.isArray(l.path)?y=i.aggregator(l.path,r):i.isString(l.path)&&(y=i.get(r,l.path)),h=l.fn.call(void 0,y,r,t,o)}catch(r){throw r.message="Unable to set target property ["+p+"].\n \n An error occured when applying ["+l.fn.name+"] on property ["+l.path+"]\n \n Internal error: "+r.message,r}return(f={})[p]=h,f}}).reduce(function(r,e){return i.assignInWith(r,e,function(r,e){return i.isUndefined(e)?i.isUndefined(r)?void 0:r:e})},o)}function u(r,e){return function(t){if(!t)return t;if(Array.isArray(t))return t.map(function(n){if(e){var i=new e;return o(n,r,t,i)}return o(n,r,t,{})});var n=t;if(e){var i=new e;return o(n,r,[n],i)}return o(n,r,[n],{})}}function a(r,e){var t=Object.keys(new r),n=i.zipObject(t,t);return Object.assign(n,e)}function c(r,e,t){if(void 0===e&&void 0===t)return u(r);if(r&&e&&t)return u(a(t,r),t)(e);if(r&&e)return u(r)(e);if(t&&e)return u(a(t,r),t)(e);if(t){var n=a(t,r);return function(r){return u(n,t)(r)}}}e.morphism=c;var f=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=c(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=c(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}();function s(r){return function(e,t,n){var i=n.value;return"function"==typeof i&&(n.value=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=i.apply(this,e);return function(r){if(Promise&&Promise.resolve)return Promise.resolve(r)==r;throw"Promise not supported in your environment"}(n)?Promise.resolve(n).then(function(e){return r(e)}):r(n)}),n}}e.MorphismRegistry=f,e.morph=function(r,e){return s(u(r,e))},e.toJSObject=function(r){return s(u(r))},e.toClassObject=function(r,e){return s(u(r,e))};var p=new f,l=c;l.register=function(r,e){return p.register(r,e)},l.map=function(r,e){return p.map(r,e)},l.getMapper=function(r){return p.getMapper(r)},l.setMapper=function(r,e){return p.setMapper(r,e)},l.deleteMapper=function(r){return p.deleteMapper(r)},l.mappers=p.mappers;var h=l;e.default=h},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},i=this&&this.__read||function(r,e){var t="function"==typeof Symbol&&r[Symbol.iterator];if(!t)return r;var n,i,o=t.call(r),u=[];try{for(;(void 0===e||e-- >0)&&!(n=o.next()).done;)u.push(n.value)}catch(r){i={error:r}}finally{try{n&&!n.done&&(t=o.return)&&t.call(o)}finally{if(i)throw i.error}}return u};function o(r){var e=typeof r;return null!=r&&("object"===e||"function"===e)}function u(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 a(r,e){for(var t=(e=(e=e.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),n=0,i=t.length;n<i;++n){var u=t[n];if(!(o(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 u(r,t,a(e,t))},{})},e.assignInWith=function(r,e,t){return Object.entries(e).forEach(function(e){var n=i(e,2),o=n[0],u=n[1];r[o]=t?t(r[o],u):u}),r},e.isUndefined=function(r){return void 0===r},e.isObject=o,e.isString=function(r){return"string"==typeof r||r instanceof String},e.isFunction=function(r){return"function"==typeof r},e.set=u,e.get=a,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.5.0", | ||
"version": "1.6.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", |
144
README.md
@@ -34,3 +34,7 @@ # Morphism | ||
- [Currying Function Example](#currying-function-example) | ||
- [3. Morphism as Mixin](#3-morphism-as-mixin) | ||
- [3. Morphism as Function Decorators](#3-morphism-as-function-decorators) | ||
- [- `toJsObject` Decorator](#tojsobject-decorator) | ||
- [- `toClassObject` Decorator](#toclassobject-decorator) | ||
- [- `morph` Decorator](#morph-decorator) | ||
- [4. Morphism as Mixin](#4-morphism-as-mixin) | ||
- [More Schema examples](#more-schema-examples) | ||
@@ -230,4 +234,140 @@ - [Flattening or Projection](#flattening-or-projection) | ||
### 3. Morphism as Mixin | ||
### 3. Morphism as Function Decorators | ||
You can also use Function Decorators on your method or functions to transform the return value using `Morphism`: | ||
#### - `toJsObject` Decorator | ||
```ts | ||
import { toJSObject } from 'morphism'; | ||
class Service { | ||
@toJSObject({ | ||
foo: currentItem => currentItem.foo, | ||
baz: 'bar.baz' | ||
}) | ||
async fetch() { | ||
const response = await fetch('https://api.com'); | ||
return response.json(); | ||
// => | ||
// { | ||
// foo: 'fooValue' | ||
// bar: { | ||
// baz: 'bazValue' | ||
// } | ||
// }; | ||
} | ||
} | ||
// await service.fetch() will return | ||
// => | ||
// { | ||
// foo: 'fooValue', | ||
// baz: 'bazValue' | ||
// } | ||
-------------------------------- | ||
// Using Typescript will enforce the key from the target to be required | ||
class Target { | ||
a: string = null; | ||
b: string = null; | ||
} | ||
class Service { | ||
// By Using <Target>, Mapping for Properties `a` and `b` will be required | ||
@toJSObject<Target>({ | ||
a: currentItem => currentItem.foo, | ||
b: 'bar.baz' | ||
}) | ||
fetch(); | ||
} | ||
``` | ||
#### - `toClassObject` Decorator | ||
```ts | ||
import { toClassObject } from 'morphism'; | ||
class Target { | ||
foo = null; | ||
bar = null; | ||
} | ||
const schema = { | ||
foo: currentItem => currentItem.foo, | ||
baz: 'bar.baz' | ||
}; | ||
class Service { | ||
@toClassObject(schema, Target) | ||
async fetch() { | ||
const response = await fetch('https://api.com'); | ||
return response.json(); | ||
// => | ||
// { | ||
// foo: 'fooValue' | ||
// bar: { | ||
// baz: 'bazValue' | ||
// } | ||
// }; | ||
} | ||
} | ||
// await service.fetch() will be instanceof Target | ||
// => | ||
// Target { | ||
// foo: 'fooValue', | ||
// baz: 'bazValue' | ||
// } | ||
``` | ||
#### - `morph` Decorator | ||
Utility decorator wrapping `toClassObject` and `toJSObject` decorators | ||
```ts | ||
import { toClassObject } from 'morphism'; | ||
class Target { | ||
foo = null; | ||
bar = null; | ||
} | ||
const schema = { | ||
foo: currentItem => currentItem.foo, | ||
baz: 'bar.baz' | ||
}; | ||
class Service { | ||
@morph(schema) | ||
async fetch() { | ||
const response = await fetch('https://api.com'); | ||
return response.json(); | ||
// => | ||
// { | ||
// foo: 'fooValue' | ||
// bar: { | ||
// baz: 'bazValue' | ||
// } | ||
// }; | ||
} | ||
@morph(schema, Target) | ||
async fetch2() { | ||
const response = await fetch('https://api.com'); | ||
return response.json(); | ||
} | ||
} | ||
// await service.fetch() will be | ||
// => | ||
// { | ||
// foo: 'fooValue', | ||
// baz: 'bazValue' | ||
// } | ||
// await service.fetch() will be instanceof Target | ||
// => | ||
// Target { | ||
// foo: 'fooValue', | ||
// baz: 'bazValue' | ||
// } | ||
``` | ||
### 4. Morphism as Mixin | ||
Morphism comes along with an internal registry you can use to save your schema attached to a specific **ES6 Class**. | ||
@@ -234,0 +374,0 @@ |
Sorry, the diff of this file is not supported yet
66234
295
541