Skip to content

Commit

Permalink
restore a missing commit
Browse files Browse the repository at this point in the history
  • Loading branch information
koresar committed Sep 15, 2019
1 parent f17ed31 commit 3f62c95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
25 changes: 11 additions & 14 deletions compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The code is optimized to be as readable as possible.
*/
'use strict'; // eslint-disable-line

const {isObject, isFunction, isPlainObject, uniq, isArray, merge} = require('lodash');
const {isObject, isFunction, isPlainObject, uniq, isArray} = require('lodash');

function getOwnPropertyKeys(obj) {
return Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj));
Expand All @@ -17,8 +17,6 @@ function assign(dst, src) {
const keys = getOwnPropertyKeys(src);
for (let i = 0; i < keys.length; i += 1) {
const desc = Object.getOwnPropertyDescriptor(src, keys[i]);
// Make sure we can override any future getter/setter
desc.configurable = true;
Object.defineProperty(dst, keys[i], desc);
}
}
Expand Down Expand Up @@ -61,25 +59,24 @@ function mergeOne(dst, src) {
// Note that functions are also assigned! We do not deep merge functions.
if (!isPlainObject(src)) return src;

// See if 'dst' is allowed to be mutated. If not - it's overridden with a new plain object.
const returnValue = isPlainObject(dst) ? dst : {};
// List both: regular value properties, and getters/setters. Object.keys() lists only regular
getOwnPropertyKeys(src).forEach((key) => {
const desc = Object.getOwnPropertyDescriptor(src, key);
// is this a regular property?
if (desc.hasOwnProperty('value')) { // eslint-disable-line
// Do not merge properties with the 'undefined' value.
if (desc.value === undefined) return;

const srcValue = src[key];
const newDst = isPlainObject(dst[key]) || isArray(srcValue) ? dst[key] : {};
// deep merge each property. Recursion!
returnValue[key] = mergeOne(returnValue[key], src[key]);
dst[key] = mergeOne(newDst, srcValue);
} else { // nope, it looks like a getter/setter
// Make it rewritable because two stamps can have same named getter/setter
desc.configurable = true;
Object.defineProperty(returnValue, key, desc);
Object.defineProperty(dst, key, desc);
}
});

return returnValue;
return dst;
}

/**
Expand All @@ -89,7 +86,7 @@ function mergeOne(dst, src) {
* @returns {*} Typically it's the 'dst' itself, unless it was an array or a non-mergeable.
* Or the 'src' itself if the 'src' is a non-mergeable.
*/
function mergeDescriptor(dst, ...srcs) {
function merge(dst, ...srcs) {
return srcs.reduce((target, src) => mergeOne(target, src), dst);
}

Expand Down Expand Up @@ -187,13 +184,13 @@ function mergeComposable(dstDescriptor, srcComposable) {

combineProperty('methods', assign);
combineProperty('properties', assign);
combineProperty('deepProperties', mergeDescriptor);
combineProperty('deepProperties', merge);
combineProperty('propertyDescriptors', assign);
combineProperty('staticProperties', assign);
combineProperty('staticDeepProperties', mergeDescriptor);
combineProperty('staticDeepProperties', merge);
combineProperty('staticPropertyDescriptors', assign);
combineProperty('configuration', assign);
combineProperty('deepConfiguration', mergeDescriptor);
combineProperty('deepConfiguration', merge);

// Initializers and Composers must be concatenated.
concatAssignFunctions('initializers');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stamp-specification",
"version": "1.5.1",
"version": "1.6.0",
"description": "The Composables specification and example implementation",
"main": "compose.js",
"engines": {
Expand Down

0 comments on commit 3f62c95

Please sign in to comment.