mirror of
				https://github.com/flynx/object.js.git
				synced 2025-10-30 19:10:11 +00:00 
			
		
		
		
	added 'soft' mode to .mixinFlat(..)...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									93cd9a5365
								
							
						
					
					
						commit
						41735e6380
					
				
							
								
								
									
										24
									
								
								object.js
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								object.js
									
									
									
									
									
								
							| @ -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,12 +1048,19 @@ 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.getOwnPropertyDescriptor(cur, k)) }) | 						&& Object.defineProperty(base, 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...
 | ||||||
| @ -1272,7 +1288,9 @@ Constructor('Mixin', { | |||||||
| 		typeof(target) == typeof('str') | 		typeof(target) == typeof('str') | ||||||
| 			&& ([_, 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){ | ||||||
|  | |||||||
| @ -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": { | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								test.js
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								test.js
									
									
									
									
									
								
							| @ -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') | ||||||
|  | 
 | ||||||
| 	}, | 	}, | ||||||
| }) | }) | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user