Skip to content

Commit

Permalink
Merge pull request #12 from emyann/fix/Documentation-v0.4.0
Browse files Browse the repository at this point in the history
Fix(Docs): Update global documentation. Bugs fixes
  • Loading branch information
emyann authored Jan 9, 2018
2 parents 5a21a78 + aafdc26 commit ec07557
Show file tree
Hide file tree
Showing 3 changed files with 7,242 additions and 53 deletions.
141 changes: 88 additions & 53 deletions lib/morphism.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,90 @@ function transformValuesFromObject(object, schema, items){
});
}

/**
* Morphism curried function.
* @param {Map<string, any>} schema
* @param {} items
* @param {} type
*/
export default function Morphism(schema, items, type) {

const transformItems = customizer => input => {
if(!input){
return input;
}
if (_.isArray(input)){
return input.map(obj => {
if(customizer){
return customizer(transformValuesFromObject(obj, schema, input));
}else{
return transformValuesFromObject(obj, schema, input);
}
});
}else{
const object = input;
const transformItems = (schema, customizer) => input => {
if(!input){
return input;
}
if (_.isArray(input)){
return input.map(obj => {
if(customizer){
return customizer(transformValuesFromObject(object, schema, [object]));
return customizer(transformValuesFromObject(obj, schema, input));
}else{
return transformValuesFromObject(object, schema, [object]);
return transformValuesFromObject(obj, schema, input);
}
});
}else{
const object = input;
if(customizer){
return customizer(transformValuesFromObject(object, schema, [object]));
}else{
return transformValuesFromObject(object, schema, [object]);
}
}

};
/**
* Object Literals Mapper (Curried Function)
* Only gives a Schema as parameter will output a mapper function to pass items to.
* Pass a Schema and items to map the input straight away.
*
* @param {Map<string, any>} schema Configuration schema to compute data source properties
* @param {} items Object or Collection to be mapped
* @param {} type
* @example
*
* const mapper = Morphism(schema);
*
* // => Map a single Object
*
* const mappedObject= mapper(sourceObject);
*
* // => Map a collection of Objects
*
* const mappedObjects = collectionOfObjects.map(mapper);
*
* // => Map everything straight away
*
* const output = Morphism(schema, input);
*/
export function Morphism(schema, items, type) {
const customizer = data => {
const undefinedValueCheck = (destination, source) => {
if (_.isUndefined(source)) return destination;
};
return _.assignInWith(new type(), data, undefinedValueCheck);
};
if (items === undefined && type === undefined) {
return transformItems(null);
} else if (type) {

return (input) => {
const customizer = data => {
const undefinedValueCheck = (destination, source) => {
if (_.isUndefined(source)) return destination;
};
return _.assignInWith(new type(), data, undefinedValueCheck);
};
return transformItems(customizer)(input);
return transformItems(schema, null);
} else if(schema && items && type) {
return transformItems(schema, customizer)(items);
} else if(schema && items) {
return transformItems(schema, null)(items);
}else if(type && items){
let finalSchema = getSchemaForType(type, schema);
return transformItems(finalSchema, customizer)(items);
}else if(type){
let finalSchema = getSchemaForType(type, schema);
return (futureInput) => {
return transformItems(finalSchema, customizer)(futureInput);
};
}
else {
return transformItems(null)(items);
}
}

function factory(type, schema, items) {
const getSchemaForType = (type, baseSchema) => {
let typeFields = _.keys(new type());
let defaultSchema = _.zipObject(typeFields, typeFields);
let finalSchema = _.assign(defaultSchema, schema);
let finalSchema = _.assign(defaultSchema, baseSchema);
return finalSchema;
};
/**
* Type Mapper Factory
* @param {type} type Class Type to be registered
* @param {Object} schema Configuration of how properties are computed from the source
* @param {Object | Array } items Object or Collection to be mapped
*/
function factory(type, schema, items) {
let finalSchema = getSchemaForType(type, schema);

return Morphism(finalSchema, items, type);
}
Expand All @@ -109,17 +140,22 @@ const _registry = _.memoize(factory);

class _Morphism {
/**
* Register a mapper for a specific type against morphism
* Register a mapping schema for a Type aimed to be used later
*
* @param {any} type
* @param {any} schema
* @param {Type} type Class Type to be registered
* @param {Object} schema Configuration of how properties are computed from the source
* @param {Object | Array } items Object or Collection to be mapped
* @returns {Function<T>} Mapper function to be used against a data source
*/
static register(type, schema) {
if (!type && !schema) {
throw new Error('type paramater is required when register a mapping');
} else if (_Morphism.exists(type)) {
throw new Error(`A mapper for ${type.name} has already been registered`);
}
/**
* @param {Object | Array } items Object or Collection to be mapped
*/
return _registry(type, schema); // Store the result of the executed function in a WeakMap cache object
}
/**
Expand Down Expand Up @@ -171,14 +207,13 @@ class _Morphism {
}
}

let api = {
register: _Morphism.register,
map: _Morphism.map,
getMapper: _Morphism.getMapper,
setMapper: _Morphism.setMapper,
deleteMapper: _Morphism.deleteMapper,
mappers: _Morphism.mappers
};

_.assignIn(Morphism, api);
/** API */
Morphism.register = _Morphism.register;
Morphism.map = _Morphism.map;
Morphism.getMapper = _Morphism.getMapper;
Morphism.setMapper = _Morphism.setMapper;
Morphism.deleteMapper = _Morphism.deleteMapper;
Morphism.mappers = _Morphism.mappers;
/** API */

export default Morphism;
Loading

0 comments on commit ec07557

Please sign in to comment.