Permalink
Browse files

added new static method m8.update

  • Loading branch information...
constantology committed Sep 26, 2012
1 parent 1f3ccf8 commit 4c175ea1fa050de136265648ef35ecea8ded3c04
Showing with 110 additions and 16 deletions.
  1. +8 −3 README.md
  2. +30 −1 m8.js
  3. +1 −1 m8.min.js
  4. +1 −1 package.json
  5. +2 −1 src/expose.js
  6. +28 −0 src/lib.js
  7. +40 −9 test/m8.test.js
View
@@ -172,8 +172,8 @@ Attempts to coerce primitive values "trapped" in Strings, into their real types.
```
-### m8.copy( destination:Object, source:Object[, no_overwrite:Boolean] ):Object
-Copies the properties – accessible via `Object.keys` – from the `source` Object to the `destination` Object and returns the `destination` Object.
+### m8.copy( target:Object, source:Object[, no_overwrite:Boolean] ):Object
+Copies the properties – accessible via `Object.keys` – from the `source` Object to the `target` Object and returns the `target` Object.
#### Example:
@@ -488,7 +488,7 @@ Tries the returns the `length` property of the passed `item`.
```
-### m8.merge( destination:Array|Object, source:Array|Object ):Boolean
+### m8.merge( target:Array|Object, source:Array|Object ):Boolean
Performs a "deep copy" of all the properties in `source` to `target`, so that `target` does not reference any child Arrays and/ or Objects that belong to `source`.
### m8.nativeType( item:Mixed ):String
@@ -618,6 +618,11 @@ Returns the normalised `type` of the passed item.
```
+### m8.update( target:Array|Object, source:Array|Object ):Boolean
+Performs a "deep copy" of all the properties in `source` **that are not already contained in** `target`, so that `target` does not reference any child Arrays and/ or Objects that belong to `source`.
+
+This works similarly to `m8.merge` except that existing properties are not overwritten.
+
## static properties
### m8.ENV:String
View
31 m8.js
@@ -256,6 +256,34 @@
: UNDEF;
}
+ function update( target, source ) {
+ if ( !source ) return merge( target );
+
+ switch ( nativeType( source ) ) {
+ case 'object' : return Object.keys( source ).reduce( update_object, { source : source, target : target } ).target;
+ case 'array' : return source.reduce( update_array, target );
+ default : return target;
+ }
+ }
+
+ function update_array( target, source, i ) {
+ target[i] = !got( target, i )
+ ? merge( source )
+ : nativeType( target[i] ) == nativeType( source )
+ ? update( target[i], source )
+ : target[i];
+ return target;
+ }
+
+ function update_object( o, key ) {
+ o.target[key] = !got( o.target, key )
+ ? merge( o.source[key] )
+ : nativeType( o.target[key] ) == nativeType( o.source[key] )
+ ? update( o.target[key], o.source[key] )
+ : o.target[key];
+ return o;
+ }
+
/*~ m8/src/lib.x.js ~*/
// Commonjs Modules 1.1.1: http://wiki.commonjs.org/wiki/Modules/1.1.1
// notes section: http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension
@@ -408,7 +436,8 @@
describe : describe, empty : empty, exists : exists, expose : expose, got : got,
guid : guid, has : has, id : id, iter : iter, len : len,
merge : merge, nativeType : nativeType, noop : noop, obj : obj, range : range,
- remove : remove, tostr : tostr, type : type, valof : valof, x : x
+ remove : remove, tostr : tostr, type : type, update : update, valof : valof,
+ x : x
}, 'w' );
x( Object, Array, Boolean, Function );
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -27,5 +27,5 @@
"scripts" : {
"test" : "mocha -c --ignore-leaks -R spec -u tdd ./test/*.test.js"
},
- "version" : "0.2.8"
+ "version" : "0.2.9"
}
View
@@ -8,7 +8,8 @@
describe : describe, empty : empty, exists : exists, expose : expose, got : got,
guid : guid, has : has, id : id, iter : iter, len : len,
merge : merge, nativeType : nativeType, noop : noop, obj : obj, range : range,
- remove : remove, tostr : tostr, type : type, valof : valof, x : x
+ remove : remove, tostr : tostr, type : type, update : update, valof : valof,
+ x : x
}, 'w' );
x( Object, Array, Boolean, Function );
View
@@ -201,3 +201,31 @@
? 'nullobject'
: UNDEF;
}
+
+ function update( target, source ) {
+ if ( !source ) return merge( target );
+
+ switch ( nativeType( source ) ) {
+ case 'object' : return Object.keys( source ).reduce( update_object, { source : source, target : target } ).target;
+ case 'array' : return source.reduce( update_array, target );
+ default : return target;
+ }
+ }
+
+ function update_array( target, source, i ) {
+ target[i] = !got( target, i )
+ ? merge( source )
+ : nativeType( target[i] ) == nativeType( source )
+ ? update( target[i], source )
+ : target[i];
+ return target;
+ }
+
+ function update_object( o, key ) {
+ o.target[key] = !got( o.target, key )
+ ? merge( o.source[key] )
+ : nativeType( o.target[key] ) == nativeType( o.source[key] )
+ ? update( o.target[key], o.source[key] )
+ : o.target[key];
+ return o;
+ }
Oops, something went wrong.

0 comments on commit 4c175ea

Please sign in to comment.