Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
- added m8.merge to deep copy Arrays and Objects
Browse files Browse the repository at this point in the history
- fixed issue with Function.prototype.__name__ not doing the right thing with mimicked functions
  • Loading branch information
constantology committed Jun 7, 2012
1 parent b9164cb commit cc64e2a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -754,8 +754,8 @@ Attempts to resolve a normalised type for any type that inherits from JavaScript

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr><td style="width : 80px ;">m8.js</td><td style="width : 48px ;">3.27kb</td><td>deflate</td>
<tr><td>m8.min.js</td><td>2.47kb</td><td>uglified + deflate</td>
<tr><td style="width : 80px ;">m8.js</td><td style="width : 48px ;">3.73kb</td><td>deflate</td>
<tr><td>m8.min.js</td><td>2.86kb</td><td>uglified + deflate</td>
</tbody>
</table>

Expand Down
43 changes: 40 additions & 3 deletions m8.js
Expand Up @@ -102,8 +102,44 @@
function len(o) {
return ("length" in (o = Object(o)) ? o : Object.keys(o)).length;
}
function obj(o, n) {
return (n = Object.create(null)) && arguments.length >= 1 ? copy(n, o) : n;
function merge(a, b) {
var type;
if (!b) {
switch (type = nativeType(a)) {
case "array":
case "object":
b = a;
a = new (b.constructor || Object);
break;
default:
return a;
}
} else type = nativeType(b);
switch (type) {
case "object":
return Object.keys(b).reduce(merge_object, {
source : b,
target : a
}).target;
case "array":
a.length = b.length;
return b.reduce(merge_array, a);
default:
return b;
}
return a;
}
function merge_array(a, v, i) {
a[i] = merge(v);
return a;
}
function merge_object(o, k) {
o.target[k] = merge(o.source[k]);
return o;
}
function obj(o) {
var n = Object.create(null);
return o && nativeType(o) == "object" ? copy(n, o) : n;
}
function tostr(o) {
return OP.toString.call(o);
Expand Down Expand Up @@ -175,6 +211,7 @@
},
iter : iter,
len : len,
merge : merge,
nativeType : nativeType,
noop : function() {},
obj : obj,
Expand Down Expand Up @@ -242,7 +279,7 @@
__name__ : {
get : function() {
if (!this.__xname__) {
var fn = valof(this), m = fn !== this ? fn.__name__ !== "anonymous" ? fn.__name__ : null : null, n = m || fname(this);
var fn = this.valueOf(), m = fn !== this ? fn.__name__ !== "anonymous" ? fn.__name__ : null : null, n = m || fname(this);
def(this, "__xname__", describe(m || n || "anonymous", "w"));
}
return this.__xname__;
Expand Down
2 changes: 1 addition & 1 deletion m8.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -17,5 +17,5 @@
"type" : "git",
"url" : "git@github.com:constantology/m8.git"
},
"version" : "0.1.3"
"version" : "0.1.4"
}
46 changes: 41 additions & 5 deletions src/m8.js
Expand Up @@ -32,8 +32,44 @@

function iter( o ) { return got( o, 'length' ) || nativeType( o ) == 'object'; }
function len( o ) { return ( 'length' in ( o = Object( o ) ) ? o : Object.keys( o ) ).length; }

function merge( a, b ) {
var type;

if ( !b ) {
switch ( type = nativeType( a ) ) {
case 'array' : case 'object' :
b = a;
a = new ( b.constructor || Object );
break;
default : return a;
}
}
else type = nativeType( b );

switch ( type ) {
case 'object' : return Object.keys( b ).reduce( merge_object, { source : b, target : a } ).target;
case 'array' :
a.length = b.length; // remove any extra items on the merged Array
return b.reduce( merge_array, a );
default : return b;
}

return a;
}
function merge_array( a, v, i ) {
a[i] = merge( v );
return a;
}
function merge_object( o, k ) {
o.target[k] = merge( o.source[k] );
return o;
}

function obj( o, n ) { return ( n = Object.create( null ) ) && arguments.length >= 1 ? copy( n, o ) : n; }
function obj( o ) {
var n = Object.create( null );
return o && nativeType( o ) == 'object' ? copy( n, o ) : n;
}

function tostr( o ) { return OP.toString.call( o ); }
function valof( o ) { return OP.valueOf.call( o ); }
Expand Down Expand Up @@ -84,11 +120,11 @@
return ctx;
},
coerce : function( o, n, s ) { return !isNaN( ( n = Number( o ) ) ) ? n : ( s = String( o ) ) in force ? force[s] : o; },
copy : copy, def : def, defs : defs, describe : describe,
empty : empty, exists : exists, got : got, has : has,
copy : copy, def : def, defs : defs, describe : describe,
empty : empty, exists : exists, got : got, has : has,
id : function( o, prefix ) { return o ? got( o, 'id' ) ? o.id : ( o.id = _id( prefix ) ) : _id( prefix ); },
iter : iter,
len : len, nativeType : nativeType, noop : function() {}, obj : obj,
iter : iter, len : len, merge : merge, nativeType : nativeType,
noop : function() {}, obj : obj,
range : function ( i, j ) {
var a = [i];
while ( ++i <= j ) a.push( i );
Expand Down
2 changes: 1 addition & 1 deletion src/m8.x.js
Expand Up @@ -49,7 +49,7 @@
defs( Type.prototype, {
__name__ : { get : function() { // accessor property
if ( !this.__xname__ ) {
var fn = valof( this ),
var fn = this.valueOf(),
m = fn !== this ? fn.__name__ !== 'anonymous' ? fn.__name__ : null : null, // handles anonymous functions which are mimicking (see mimic below) named functions
n = m || fname( this );
def( this, '__xname__', describe( ( m || n || 'anonymous' ), 'w' ) );
Expand Down

0 comments on commit cc64e2a

Please sign in to comment.