added 'soft' mode to .mixinFlat(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-22 22:42:11 +03:00
parent 93cd9a5365
commit 41735e6380
3 changed files with 28 additions and 4 deletions

View File

@ -1027,9 +1027,17 @@ Object.assign(Constructor, {
// Mix a set of methods/props/attrs into an object... // Mix a set of methods/props/attrs into an object...
// //
// Mix objects into base...
// mixinFlat(base, object, ...) // mixinFlat(base, object, ...)
// -> base // -> 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 // NOTE: essentially this is just like Object.assign(..) but copies
// properties directly rather than copying property values... // properties directly rather than copying property values...
@ -1040,11 +1048,18 @@ Object.assign(Constructor, {
var mixinFlat = var mixinFlat =
module.mixinFlat = module.mixinFlat =
function(base, ...objects){ function(base, ...objects){
var soft = base === 'soft'
if(soft){
base = objects.shift()
objects = objects
.slice()
.reverse() }
return objects return objects
.reduce(function(base, cur){ .reduce(function(base, cur){
Object.keys(cur) Object.keys(cur)
.map(function(k){ .map(function(k){
Object.defineProperty(base, k, ;(!soft || !base.hasOwnProperty(k))
&& Object.defineProperty(base, k,
Object.getOwnPropertyDescriptor(cur, k)) }) Object.getOwnPropertyDescriptor(cur, k)) })
return base }, base) } return base }, base) }
@ -1258,6 +1273,7 @@ Constructor('Mixin', {
// This can be: // This can be:
// 'proto' - mix data into prototype chain (default) // 'proto' - mix data into prototype chain (default)
// 'flat' - use mixinFlat(..) to copy data // 'flat' - use mixinFlat(..) to copy data
// 'softflat' - like 'flat' but uses mixinFlat('soft', ..)
mode: 'proto', mode: 'proto',
// base API... // base API...
@ -1273,6 +1289,8 @@ Constructor('Mixin', {
&& ([_, mode, target] = arguments) && ([_, mode, target] = arguments)
return mode == 'flat' ? 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) }, : this.constructor.mixin(target, this.data) },
__init__: function(name, ...data){ __init__: function(name, ...data){

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-object", "name": "ig-object",
"version": "5.4.9", "version": "5.4.10",
"description": "", "description": "",
"main": "object.js", "main": "object.js",
"scripts": { "scripts": {

View File

@ -760,6 +760,12 @@ var cases = test.Cases({
assert('b' in x == false, 'post-mixout content gone') assert('b' in x == false, 'post-mixout content gone')
assert('c' 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')
}, },
}) })