Skip to content

Commit

Permalink
Merge b584967 into 63d746d
Browse files Browse the repository at this point in the history
  • Loading branch information
emyann committed Jul 4, 2017
2 parents 63d746d + b584967 commit 746740e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/morphism.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import * as _ from 'lodash';

export default function Morphism(schema, items, type) {
let _schema = _.clone(schema);
let transformer = (items) => {
let transformer = (items) => { // Low Level transformer function (without type consideration)
return _.chain(items)
.map((obj) => {
let transformedObject = _.mapValues(_schema, (predicate) => {
if (!_.isObject(predicate)) { // a predicate string path
let transformedObject = _.mapValues(_schema, (predicate) => { // iterate on every predicate of the schema
if (!_.isObject(predicate)) { // a predicate string path: [ desintation: 'source' ]
return _.get(obj, predicate);
}
else if (_.isFunction(predicate)) { // a predicate function without a path
else if (_.isFunction(predicate)) { // a predicate function without a path: [ destination: (object) => {...} ]
return predicate(obj, items);
}
else if (_.isArray(predicate)) { // a predicate array of string path => agregator
else if (_.isArray(predicate)) { // a predicate array of string path => agregator: [ destination: ['source1', 'source2', 'source3'] ]
return _.reduce(predicate, (delta, path) => {
return _.set(delta, path, _.get(obj, path));
}, {});
}
else { // a predicate object with a path and a function
else { // a predicate object with a path and a function: [ destination : { path: 'source', fn:(fieldValue, items) }]
let delta = _.get(obj, predicate.path);
return predicate.fn(delta, items);
}
Expand All @@ -32,7 +32,11 @@ export default function Morphism(schema, items, type) {
};
} else if (type) {
return (items) => {
return transformer(items).map((o) => { return _.assignIn(new type(), o); }).value(); // TODO: Refactor this in a chain of responsibility pattern
const customizer = (destination, source) => {
if(_.isUndefined(source))
return destination;
};
return transformer(items).map((o) => _.assignInWith(new type(), o, customizer)).value(); // TODO: Refactor this in a chain of responsibility pattern
};
}
else {
Expand Down
2 changes: 2 additions & 0 deletions test/models.mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export class User {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;

this.type = 'User'; // Use to test default value scenario
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/morphism.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,40 @@ describe('Morphism', function () {
expect(() => { Morphism.setMapper(User, {}); }).toThrow();
});
});

describe('Class Type Mapping', function () {

beforeEach(()=>{
Morphism.deleteMapper(User);
});

it('should use the constructor default value if source value is undefined', function () {
let phoneNumber = [
{
type: 'home',
number: '212 555-1234'
},
{
type: 'fax',
number: '646 555-4567'
}
];
let desiredResult = new User('John', 'Smith', phoneNumber);

let mapper = Morphism.register(User, {});
expect(mapper(this.dataToCrunch)[0]).toEqual(desiredResult);
});

it('should override the default value if source value is defined', function () {
let sourceData = {
phoneNumber: null
};

let mapper = Morphism.register(User, {});
let result = mapper([sourceData])[0];
expect(result.phoneNumber).toEqual(null);
});

});
});

0 comments on commit 746740e

Please sign in to comment.