smart-mixin
Advanced tools
Comparing version 1.1.0 to 1.2.0
87
index.js
var objToStr = function(x){ return Object.prototype.toString.call(x); }; | ||
var thrower = function(error){ | ||
throw error; | ||
}; | ||
var mixins = module.exports = function makeMixinFunction(rules, _opts){ | ||
@@ -27,7 +31,2 @@ var opts = _opts || {}; | ||
// TODO: improve | ||
var thrower = function(error){ | ||
throw error; | ||
}; | ||
function setNonEnumerable(target, key, value){ | ||
@@ -55,5 +54,5 @@ if (key in target){ | ||
var wrapIfFunction = function(thing){ | ||
return typeof thing !== "function" ? thing | ||
return typeof thing !== "function" ? thing | ||
: function(){ | ||
return thing.call(this, arguments, thrower); | ||
return thing.call(this, arguments); | ||
}; | ||
@@ -72,3 +71,3 @@ }; | ||
var rightIsFn = typeof right === "function"; | ||
// check to see if they're some combination of functions or undefined | ||
@@ -89,6 +88,39 @@ // we already know there's no rule, so use the unknown function behavior | ||
source[key] = opts.nonFunctionProperty(left, right, key); | ||
}); | ||
}); | ||
}; | ||
}; | ||
mixins._mergeObjects = function(obj1, obj2) { | ||
var assertObject = function(obj, obj2){ | ||
var type = objToStr(obj); | ||
if (type !== '[object Object]') { | ||
var displayType = obj.constructor ? obj.constructor.name : 'Unknown'; | ||
var displayType2 = obj2.constructor ? obj2.constructor.name : 'Unknown'; | ||
thrower('cannot merge returned value of type ' + displayType + ' with an ' + displayType2); | ||
} | ||
}; | ||
if (Array.isArray(obj1) && Array.isArray(obj2)) { | ||
return obj1.concat(obj2); | ||
} | ||
assertObject(obj1, obj2); | ||
assertObject(obj2, obj1); | ||
var result = {}; | ||
Object.keys(obj1).forEach(function(k){ | ||
if (Object.prototype.hasOwnProperty.call(obj2, k)) { | ||
thrower('cannot merge returns because both have the ' + JSON.stringify(k) + ' key'); | ||
} | ||
result[k] = obj1[k]; | ||
}); | ||
Object.keys(obj2).forEach(function(k){ | ||
// we can skip the conflict check because all conflicts would already be found | ||
result[k] = obj2[k]; | ||
}); | ||
return result; | ||
} | ||
// define our built-in mixin types | ||
@@ -99,3 +131,3 @@ mixins.ONCE = function(left, right, key){ | ||
} | ||
var fn = left || right; | ||
@@ -115,31 +147,16 @@ | ||
mixins.MANY_MERGED_LOOSE = function(left, right, key) { | ||
if(left && right) { | ||
return mixins._mergeObjects(left, right); | ||
} | ||
return left || right; | ||
} | ||
mixins.MANY_MERGED = function(left, right, key){ | ||
return function(args, thrower){ | ||
return function(args){ | ||
var res1 = right && right.apply(this, args); | ||
var res2 = left && left.apply(this, args); | ||
if (res1 && res2) { | ||
var assertObject = function(obj, obj2){ | ||
var type = objToStr(obj); | ||
if (type !== '[object Object]') { | ||
var displayType = obj.constructor ? obj.constructor.name : 'Unknown'; | ||
var displayType2 = obj2.constructor ? obj2.constructor.name : 'Unknown'; | ||
thrower('cannot merge returned value of type ' + displayType + ' with an ' + displayType2); | ||
} | ||
}; | ||
assertObject(res1, res2); | ||
assertObject(res2, res1); | ||
var result = {}; | ||
Object.keys(res1).forEach(function(k){ | ||
if (Object.prototype.hasOwnProperty.call(res2, k)) { | ||
thrower('cannot merge returns because both have the ' + JSON.stringify(k) + ' key'); | ||
} | ||
result[k] = res1[k]; | ||
}); | ||
Object.keys(res2).forEach(function(k){ | ||
// we can skip the conflict check because all conflicts would already be found | ||
result[k] = res2[k]; | ||
}); | ||
return result; | ||
return mixins._mergeObjects(res1, res2) | ||
} | ||
@@ -146,0 +163,0 @@ return res2 || res1; |
{ | ||
"name": "smart-mixin", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,3 +35,2 @@ ![travis](https://travis-ci.org/brigand/smart-mixin.svg) | ||
// TODO: this isn't currently implemented, PR welcome (or I'll get around to it) | ||
// like MANY_MERGED but also handles arrays, and non-function properties | ||
@@ -55,2 +54,4 @@ // the behavior expressed in pseudo pattern matching syntax: | ||
// define your own handler for it | ||
@@ -57,0 +58,0 @@ // the two operands are the value of onKeyPress on each object |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24041
465
129