diff --git a/object.js b/object.js index b2ab737..47351c0 100755 --- a/object.js +++ b/object.js @@ -1027,9 +1027,17 @@ Object.assign(Constructor, { // Mix a set of methods/props/attrs into an object... // +// Mix objects into base... // mixinFlat(base, object, ...) // -> base // +// Soft mix objects into base... +// mixinFlat('soft', base, object, ...) +// -> base +// +// +// 'soft' mode only mixies in props if base does not define them already +// // // NOTE: essentially this is just like Object.assign(..) but copies // properties directly rather than copying property values... @@ -1040,12 +1048,19 @@ Object.assign(Constructor, { var mixinFlat = module.mixinFlat = function(base, ...objects){ + var soft = base === 'soft' + if(soft){ + base = objects.shift() + objects = objects + .slice() + .reverse() } return objects .reduce(function(base, cur){ Object.keys(cur) .map(function(k){ - Object.defineProperty(base, k, - Object.getOwnPropertyDescriptor(cur, k)) }) + ;(!soft || !base.hasOwnProperty(k)) + && Object.defineProperty(base, k, + Object.getOwnPropertyDescriptor(cur, k)) }) return base }, base) } @@ -1258,6 +1273,7 @@ Constructor('Mixin', { // This can be: // 'proto' - mix data into prototype chain (default) // 'flat' - use mixinFlat(..) to copy data + // 'softflat' - like 'flat' but uses mixinFlat('soft', ..) mode: 'proto', // base API... @@ -1272,7 +1288,9 @@ Constructor('Mixin', { typeof(target) == typeof('str') && ([_, mode, target] = arguments) return mode == 'flat' ? - this.constructor.mixinFlat(target, this.data) + this.constructor.mixinFlat(target, this.data) + : mode == 'softflat' ? + this.constructor.mixinFlat('soft', target, this.data) : this.constructor.mixin(target, this.data) }, __init__: function(name, ...data){ diff --git a/package.json b/package.json index abcacb9..9d08e02 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-object", - "version": "5.4.9", + "version": "5.4.10", "description": "", "main": "object.js", "scripts": { diff --git a/test.js b/test.js index 90ce9db..0bba893 100755 --- a/test.js +++ b/test.js @@ -760,6 +760,12 @@ var cases = test.Cases({ assert('b' in x == false, 'post-mixout content gone') assert('c' in x == false, 'post-mixout content gone') + var z = assert(C('softflat', {a:'zzz'}), `C("flat", {})`) + + assert(z.a == 'zzz', 'no overwrite') + assert(z.b == 'bbb', 'mixin content from C') + assert(z.c == 'ccc', 'mixin content from C') + }, })