Permalink
Browse files

added method `m8.cpdef` to copy property descriptors from one Object …

…to another.

`m8.expose` now also exposes a library as an AMD module if AMD requirements are met.
  • Loading branch information...
1 parent 8dfd84d commit c0cad53d1819dbe0f292129878db65a80300cf52 @constantology committed Nov 3, 2012
Showing with 134 additions and 44 deletions.
  1. +50 −21 m8.js
  2. +1 −1 m8.min.js
  3. +1 −1 package.json
  4. +17 −7 src/expose.js
  5. +33 −14 src/lib.js
  6. +32 −0 test/m8.test.js
View
71 m8.js
@@ -91,6 +91,14 @@
return target;
}
+ function cpdef( target, source, no_overwrite ) {
+ no_overwrite = no_overwrite === true; source || ( source = target, target = obj() );
+ return Object.getOwnPropertyNames( source ).reduce( function( o, key ) {
+ ( no_overwrite && has( o, key ) ) || def( o, key, description( source, key ) );
+ return o;
+ }, target );
+ }
+
function def( item, name, desc ) {
var args = slice.call( arguments, 3 ),
defined = got( item, name ), debug, mode, ntype, overwrite;
@@ -118,12 +126,14 @@
}
return __lib__;
}
+
function defs( item, props, mode, overwrite, debug ) {
mode || ( mode = 'cw' );
for ( var key in props )
!has( props, key ) || def( item, key, props[key], mode, overwrite, debug );
return __lib__;
}
+
function describe( desc, mode ) {
return copy( ( nativeType( desc ) == 'object' ? desc : { value : desc } ), ( nativeType( mode ) == 'object' ? mode : modes[String( mode ).toLowerCase()] || modes.cew ), true );
}
@@ -136,21 +146,30 @@
function expose( lib, name, mod ) {
if ( typeof name != 'string' && lib[__name__] ) {
- mod = name; name = lib[__name__];
+ mod = name;
+ name = lib[__name__];
}
- if ( ENV == 'commonjs' && is_mod( mod ) ) mod.exports = lib;
+ var conflict, defaults = obj(); // make sure the exposed library has a type
+ defaults[__name__] = name; defaults[__type__] = 'library'; // of "library" and its name attached to it.
+
+ if ( ENV == 'commonjs' && is_mod( mod ) )
+ mod.exports = lib;
else {
mod || ( mod = root );
- var conflict = mod[name],
- desc = describe( { value : lib }, 'ew' ); // make sure if lib is already defined it's not a primitive value!
- ( conflict && iter( conflict ) ) // don't over-write what's there, just add lib to conflict as conflict.__
- ? def( ( lib = conflict ), '__', desc ) // however, all properties will be added to conflict, not lib and
- : def( mod, name, desc ); // conflict will be returned instead of lib
+
+ if ( ( conflict = mod[name] ) && iter( conflict ) ) {
+ conflict[name] = lib;
+ lib = cpdef( conflict, lib );
+ }
+ else
+ def( mod, name, describe( { value : lib }, 'ew' ) );
+
+ if ( ENV == 'browser' )
+ typeof define != 'function' || !define.amd || define( name, [], function() { return lib; } );
}
- mod = obj(); mod[__name__] = name; mod[__type__] = 'library'; // make sure the exposed library has a type
- defs( lib, mod, 'w', true ); // of "library" and its name attached to it.
+ defs( lib, defaults, 'w', true );
return lib; // return the exposed library, if it already exists this will allow us to re-assign our internal copy
}
@@ -223,14 +242,14 @@
return typeof props == 'object' ? copy( nobj, props ) : nobj;
}
- function property_exists( test, item, property ) {
+ function prop_exists( test, item, property ) {
var key; property = String( property );
if ( arguments.length > 3 ) {
property = slice.call( arguments, 2 );
while ( key = property.shift() )
- if ( property_exists( test, item, key ) )
+ if ( prop_exists( test, item, key ) )
return true;
return false;
@@ -243,7 +262,7 @@
property = property.split( '.' );
while ( key = property.shift() ) {
- if ( !property_exists( test, item, key ) )
+ if ( !prop_exists( test, item, key ) )
return false;
item = item[key];
@@ -296,8 +315,8 @@
return t !== 'object'
? t
- : ( property_exists( has, item, 'configurable', 'enumerable', 'writable' ) && has( item, 'value' )
- || property_exists( has, item, 'get', 'set' ) )
+ : ( prop_exists( has, item, 'configurable', 'enumerable', 'writable' ) && has( item, 'value' )
+ || prop_exists( has, item, 'get', 'set' ) )
? 'descriptor'
: t;
}
@@ -476,14 +495,24 @@
defs( ( __lib__ = expose( __lib__, Name, PACKAGE ) ), {
// properties
- ENV : ENV, global : { value : root }, modes : { value : modes },
+ ENV : ENV, global : { value : root },
+ modes : { value : modes },
// methods
- bless : bless, coerce : coerce, copy : copy, def : def, defs : defs,
- describe : describe, description : description, empty : empty, exists : exists, expose : expose,
- got : property_exists.bind( null, got ), guid : guid, has : property_exists.bind( null, has ),
- id : id, iter : iter, len : len, merge : merge, nativeType : nativeType,
- noop : noop, ntype : nativeType, obj : obj, range : range, remove : remove,
- tostr : tostr, type : type, update : update, valof : valof, x : x
+ bless : bless, coerce : coerce,
+ copy : copy, cpdef : cpdef,
+ def : def, defs : defs,
+ describe : describe, description : description,
+ empty : empty, exists : exists,
+ expose : expose, got : prop_exists.bind( null, got ),
+ guid : guid, has : prop_exists.bind( null, has ),
+ id : id, iter : iter,
+ len : len, merge : merge,
+ nativeType : nativeType, noop : noop,
+ ntype : nativeType, obj : obj,
+ range : range, remove : remove,
+ tostr : tostr, type : type,
+ update : update, valof : valof,
+ x : x
}, 'w' );
x( Object, Array, Boolean, Function );
Oops, something went wrong.

0 comments on commit c0cad53

Please sign in to comment.