Skip to content

Commit

Permalink
Light readability improvements around initializers. (#103)
Browse files Browse the repository at this point in the history
* Light readability improvements around initializers.

* Initializers temporary variable.

* Single import from 'lodash'. `npm test` to build before testing.
  • Loading branch information
koresar authored and FredyC committed Jun 20, 2016
1 parent ccb1315 commit 5aae772
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
32 changes: 15 additions & 17 deletions compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ See https://github.com/stampit-org/stamp-specification
The code is optimized to be as readable as possible.
*/

import mergeWith from 'lodash/mergeWith';
import assign from 'lodash/assign';
import isFunction from 'lodash/isFunction';
import isObject from 'lodash/isObject';
import {mergeWith, assign, isFunction, isObject, uniq} from 'lodash';

const isDescriptor = isObject;
export const merge = (dst, src) => mergeWith(dst, src, (dstValue, srcValue) => {
Expand All @@ -30,13 +27,16 @@ function createFactory(descriptor) {
assign(obj, descriptor.properties);
Object.defineProperties(obj, descriptor.propertyDescriptors || {});

if (!descriptor.initializers || descriptor.initializers.length === 0) return obj;

return descriptor.initializers.filter(isFunction).reduce((resultingObj, initializer) => {
const returnedValue = initializer.call(resultingObj, options,
{instance: resultingObj, stamp: Stamp, args: [options].concat(args)});
return returnedValue === undefined ? resultingObj : returnedValue;
}, obj);
return (descriptor.initializers || [])
.filter(isFunction)
.reduce((resultingObj, initializer) => {
const returnedValue = initializer.call(
resultingObj,
options,
{instance: resultingObj, stamp: Stamp, args: [options].concat(args)}
);
return returnedValue === undefined ? resultingObj : returnedValue;
}, obj);
};
}

Expand Down Expand Up @@ -88,12 +88,10 @@ function mergeComposable(dstDescriptor, srcComposable) {
combineProperty('configuration', assign);
combineProperty('deepConfiguration', merge);
if (Array.isArray(srcDescriptor.initializers)) {
dstDescriptor.initializers = srcDescriptor.initializers.reduce((result, init) => {
if (isFunction(init) && result.indexOf(init) < 0) {
result.push(init);
}
return result;
}, Array.isArray(dstDescriptor.initializers) ? dstDescriptor.initializers : []);
const initializers = (dstDescriptor.initializers || [])
.concat(srcDescriptor.initializers)
.filter(isFunction);
dstDescriptor.initializers = uniq(initializers);
}

return dstDescriptor;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"scripts": {
"build": "babel -s -d build compose.js",
"lint": "eslint compose.js",
"pretest": "npm run build",
"test": "check-compose .",
"prepublish": "npm run lint && npm run build && npm test"
"prepublish": "npm run lint && npm test"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 5aae772

Please sign in to comment.