Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

m8.got and m8.has can now both check for existence of nested properties

  • Loading branch information...
commit 31a7172e3537098a760bfbcbdbbe3f74dbaa5312 1 parent 4c175ea
christos constandinou authored October 31, 2012
62  m8.js
... ...
@@ -1,7 +1,7 @@
1 1
 ;!function( root, Name, PACKAGE ) {
2 2
 	"use strict";
3 3
 
4  
-/*~  m8/src/vars.js  ~*/
  4
+/*~  src/vars.js  ~*/
5 5
 // if ENV === commonjs we want root to be global
6 6
 	typeof global == 'undefined' ? root : ( root = global );
7 7
 
@@ -51,7 +51,7 @@
51 51
 			'Number' : [], 'Object'  : [], 'RegExp' : [], 'String'   : []
52 52
 		};
53 53
 
54  
-/*~  m8/src/lib.js  ~*/
  54
+/*~  src/lib.js  ~*/
55 55
 	function __lib__( val ) { return val; }
56 56
 
57 57
 	function bless( ns, ctx ) {
@@ -152,7 +152,9 @@
152 152
 
153 153
 	function fname( fn ) { return fn.name || fn.displayName || ( String( fn ).match( re_name ) || ['', ''] )[1].trim(); }
154 154
 
155  
-	function got( obj, key ) { return arguments.length > 2 ? hasSome( got, obj, Array.coerce( arguments, 1 ) ) : key in Object( obj ); }
  155
+	function got( item, property ) {
  156
+		return String( property ) in Object( item );
  157
+	}
156 158
 
157 159
 	// credit for guid goes here: gist.github.com/2295777
158 160
 	function guid() { return tpl_guid.replace( re_guid, guid_replace ); }
@@ -161,8 +163,9 @@
161 163
 		return ( match == 'x' ? num : ( num & 0x3 | 0x8 ) ).toString( 16 );
162 164
 	}
163 165
 
164  
-	function has( obj, key ) { return arguments.length > 2 ? hasSome( has, obj, Array.coerce( arguments, 1 ) ) : OP.hasOwnProperty.call( obj, key ); }
165  
-	function hasSome( test, obj, keys ) { return keys.some( function( key ) { return test( obj, key ); } ); }
  166
+	function has( item, property ) {
  167
+		return OP.hasOwnProperty.call( Object( item ), String( property ) );
  168
+	}
166 169
 
167 170
 	function id( item, prefix ) { return item ? got( item, 'id' ) && !empty( item.id ) ? item.id : ( item.id = id_create( prefix ) ) : id_create( prefix ); }
168 171
 	function id_create( prefix ) { return ( prefix || id_prefix ) + '-' + ( ++id_count ); }
@@ -215,6 +218,35 @@
215 218
 		return typeof props == 'object' ? copy( nobj, props ) : nobj;
216 219
 	}
217 220
 
  221
+	function property_exists( test, item, property ) {
  222
+		var key; property = String( property );
  223
+
  224
+		if ( arguments.length > 3 ) {
  225
+			property = slice.call( arguments, 2 );
  226
+
  227
+			while ( key = property.shift() )
  228
+				if ( property_exists( test, item, key ) )
  229
+					return true;
  230
+
  231
+			return false;
  232
+		}
  233
+
  234
+		if ( !!~property.indexOf( '.' ) ) {
  235
+			property = property.split( '.' );
  236
+
  237
+			while ( key = property.shift() ) {
  238
+				if ( !property_exists( test, item, key ) )
  239
+					return false;
  240
+
  241
+				item = item[key];
  242
+			}
  243
+
  244
+			return true;
  245
+		}
  246
+
  247
+		return test( item, property );
  248
+	}
  249
+
218 250
 	function range( i, j ) {
219 251
 		var a = [i];
220 252
 		while ( ++i <= j ) a.push( i );
@@ -284,7 +316,7 @@
284 316
 		return o;
285 317
 	}
286 318
 
287  
-/*~  m8/src/lib.x.js  ~*/
  319
+/*~  src/lib.x.js  ~*/
288 320
 // Commonjs Modules 1.1.1: http://wiki.commonjs.org/wiki/Modules/1.1.1
289 321
 // notes section:          http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension
290 322
 // specifies the possibility of sandboxing JavaScript Natives in Modules in future versions
@@ -311,7 +343,7 @@
311 343
 		Type[__xid__] = extenders.length;                           // assigned every time __lib__.x() is called, and
312 344
 	}                                                               // potentilly throwing overwrite errors.
313 345
 
314  
-/*~  m8/src/nativex.js  ~*/
  346
+/*~  src/nativex.js  ~*/
315 347
 	x.cache( 'Array', function( Type ) {
316 348
 		def( Type, 'coerce', function( a, i, j ) {
317 349
 			if ( !got( a, 'length' ) ) return [a];
@@ -425,19 +457,19 @@
425 457
 		}, 'w' );
426 458
 	} );
427 459
 
428  
-/*~  m8/src/expose.js  ~*/
  460
+/*~  src/expose.js  ~*/
429 461
 	iter( PACKAGE ) || ( PACKAGE = ENV == 'commonjs' ? module : root );
430 462
 
431 463
 	defs( ( __lib__ = expose( __lib__, Name, PACKAGE ) ), {
432 464
 	// properties
433  
-		ENV      : ENV,      global     : { value : root },            modes  : { value : modes },
  465
+		ENV      : ENV,      global : { value : root },            modes  : { value : modes },
434 466
 	// methods
435  
-		bless    : bless,    coerce     : coerce,     copy   : copy,   def    : def,    defs  : defs,
436  
-		describe : describe, empty      : empty,      exists : exists, expose : expose, got   : got,
437  
-		guid     : guid,     has        : has,        id     : id,     iter   : iter,   len   : len,
438  
-		merge    : merge,    nativeType : nativeType, noop   : noop,   obj    : obj,    range : range,
439  
-		remove   : remove,   tostr      : tostr,      type   : type,   update : update, valof : valof,
440  
-		x        : x
  467
+		bless    : bless,    coerce : coerce,         copy   : copy,   def    : def,    defs       : defs,
  468
+		describe : describe, empty  : empty,          exists : exists, expose : expose,
  469
+		got      : property_exists.bind( null, got ), guid   : guid,   has    : property_exists.bind( null, has ),
  470
+		id       : id,       iter   : iter,           len    : len,    merge  : merge,  nativeType : nativeType,
  471
+		noop     : noop,     obj    : obj,            range  : range,  remove : remove, tostr      : tostr,
  472
+		type     : type,     update : update,         valof  : valof,  x      : x
441 473
 	}, 'w' );
442 474
 
443 475
 	x( Object, Array, Boolean, Function );
2  m8.min.js
... ...
@@ -1 +1 @@
1  
-!function(e,t,n){"use strict";function N(e){return e}function C(e,t){switch(st(e)){case"array":break;case"string":e=e.split(".");break;default:return k(t)}y.test(e[0])&&(t=N,e.shift());if(!e.length)return k(t);e[0].indexOf("^")||(t||e[0]=="^"?e.shift():e[0]=e[0].substring(1)),t=k(t);var n;while(n=e.shift())t=t[n]||(t[n]=G());return t}function k(t){return s=="commonjs"?t?W(t)?t.exports:t:module.exports:t||e}function L(e){var t=Number(e),n;return isNaN(t)?(n=String(e))in f?f[n]:e:t}function A(e,t,n){n=n===!0,t||(t=e,e=G());for(var r in t)!q(t,r)||n&&q(e,r)||(e[r]=t[r]);return e}function O(e,n,r){var i=E.call(arguments,3),s=j(e,n),o,u,a,f;switch(st(i[0])){case"string":u=d[i.shift()];break;case"object":u=i.shift();break;default:a=st(r),u=a!="object"&&s?Object.getOwnPropertyDescriptor(e,n):null,!u||(u=a=="function"?d.cw:d.cew)}return f=i.shift()===!0,o=i.shift()===!0,s&&!f?o&&new Error(t+".def cannot overwrite existing property: "+n+", in item type: "+ot(e)+"."):(a!="object"&&u&&(r=_(r,u)),(r.get||r.set)&&delete r.writable,Object.defineProperty(e,n,r)),N}function M(e,t,n,r,i){n||(n="cw");for(var s in t)!q(t,s)||O(e,s,t[s],n,r,i);return N}function _(e,t){return A(st(e)=="object"?e:{value:e},st(t)=="object"?t:d[String(t).toLowerCase()]||d.cew,!0)}function D(e){return!P(e)||!V(e)&&X(e)||!1}function P(e){return!(e===null||e===u||typeof e=="number"&&isNaN(e))}function H(t,n,i){typeof n!="string"&&t[r]&&(i=n,n=t[r]);if(s=="commonjs"&&W(i))i.exports=t;else{i||(i=e);var o=i[n],u=_({value:t},"ew");o&&X(o)?O(t=o,"__",u):O(i,n,u)}return i=G(),i[r]=n,i[__type__]="library",M(t,i,"w",!0),t}function B(e){return e.name||e.displayName||(String(e).match(b)||["",""])[1].trim()}function j(e,t){return arguments.length>2?R(j,e,Array.coerce(arguments,1)):t in Object(e)}function F(){return S.replace(g,I)}function I(e){var t=v()*16|0;return(e=="x"?t:t&3|8).toString(16)}function q(e,t){return arguments.length>2?R(q,e,Array.coerce(arguments,1)):o.hasOwnProperty.call(e,t)}function R(e,t,n){return n.some(function(n){return e(t,n)})}function U(e,t){return e?j(e,"id")&&!D(e.id)?e.id:e.id=z(t):z(t)}function z(e){return(e||p)+"-"+ ++h}function W(e){if(a===null)return!1;try{return e instanceof a}catch(t){return!1}}function X(e){return j(e,"length")||st(e)=="object"}function V(e){return("length"in(e=Object(e))?e:Object.keys(e)).length}function $(e,t){var n;if(!t)switch(n=st(e)){case"array":case"object":t=e,e=new(t.constructor||Object);break;default:return e}else n=st(t);switch(n){case"object":return Object.keys(t).reduce(K,{source:t,target:e}).target;case"array":return e.length=t.length,t.reduce(J,e);default:return t}}function J(e,t,n){return e[n]=st(e[n])===st(t)?$(e[n],t):$(t),e}function K(e,t){return e.target[t]=st(e.target[t])===st(e.source[t])?$(e.target[t],e.source[t]):$(e.source[t]),e}function Q(){}function G(e){var t=Object.create(null);return typeof e=="object"?A(t,e):t}function Y(e,t){var n=[e];while(++e<=t)n.push(e);return n}function Z(e,t){t=Array.isArray(t)?t:E.call(arguments,1);var n=Array.isArray(e)?et:tt;return t.forEach(n,e),e}function et(e){var t=this.indexOf(e);t=~t?t:!isNaN(e)&&e in this?e:t,t<0||this.splice(t,1)}function tt(e){delete this[e]}function nt(e){return o.toString.call(e)}function rt(e){return o.valueOf.call(e)}function it(e){return e==c?c:e==l||e=="nodelist"?l:!e.indexOf("htm")&&e.lastIndexOf("element")+7===e.length?"htmlelement":!1}function st(e){var t=nt(e);return t in x?x[t]:x[t]=t.split(" ")[1].split("]")[0].replace(w,"$1").toLowerCase()}function ot(e){return e===null||e===u?!1:j(e,__type__)?e[__type__]:Object.getPrototypeOf(e)===null?"nullobject":u}function ut(e,t){if(!t)return $(e);switch(st(t)){case"object":return Object.keys(t).reduce(ft,{source:t,target:e}).target;case"array":return t.reduce(at,e);default:return e}}function at(e,t,n){return e[n]=j(e,n)?st(e[n])==st(t)?ut(e[n],t):e[n]:$(t),e}function ft(e,t){return e.target[t]=j(e.target,t)?st(e.target[t])==st(e.source[t])?ut(e.target[t],e.source[t]):e.target[t]:$(e.source[t]),e}function lt(){return E.call(arguments).forEach(ht),N}function ct(e){e(this,N)}function ht(e){j(e,i)||O(e,i,0,"w");var t=T[e[r]||B(e)];if(!t)return;t.slice(e[i]).forEach(ct,e),e[i]=t.length}typeof global=="undefined"?e:e=global;var r="__name__",__type__="__type__",i="__xid__",s=typeof module!="undefined"&&"exports"in module&&typeof require=="function"?"commonjs":typeof navigator!="undefined"?"browser":"other",o=Object.prototype,u,a=s!="commonjs"?null:require("module"),f=[!1,NaN,null,!0,u].reduce(function(e,t){return e[String(t)]=t,e},G()),l="htmlcollection",c="htmldocument",h=999,p="anon",d=function(){var e={ce:"ec",cw:"wc",ew:"we",cew:"cwe ecw ewc wce wec".split(" ")},t="configurable enumerable writable".split(" "),n={c:[!0,!1,!1],ce:[!0,!0,!1],cew:[!0,!0,!0],cw:[!0,!1,!0],e:[!1,!0,!1],ew:[!1,!0,!0],r:[!1,!1,!1],w:[!1,!1,!0]},r=Object.keys(n).reduce(function(r,i){function s(e){r[e]=r[i]}var o=e[i];return r[i]=t.reduce(function(e,t,r){return e[t]=n[i][r],e},G()),!o||(Array.isArray(o)?o.forEach(s):s(o)),r},G());return delete r[u],r}(),v=Math.random,m=/global|window/i,g=/[xy]/g,y=new RegExp("^\\u005E?"+t),b=/[\s\(]*function([^\(]+).*/,w=/^[Ww]ebkit|[Mm]oz|O|[Mm]s|[Kk]html(.*)$/,E=Array.prototype.slice,S="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx",x={"[object Object]":"object"},T={Array:[],Boolean:[],Date:[],Function:[],Number:[],Object:[],RegExp:[],String:[]};O(lt,"cache",function(e,t){return typeof e=="string"||(e=e[r]||B(e)),T[e]||(T[e]=[]),T[e].push(t),N},"w"),lt.cache("Array",function(e){O(e,"coerce",function(e,t,n){return j(e,"length")?(t=ot(t)=="number"?t>0?t:0:0,n=ot(n)=="number"?n>t?n:n<=0?e.length+n:t+n:e.length,E.call(e,t,n)):[e]},"w"),O(e.prototype,"find",function(e,t){var n=-1,r=this.length>>>0;t||(t=this);while(++n<r)if(!!e.call(t,this[n],n,this))return this[n];return null},"w")}),lt.cache("Boolean",function(e){O(e,"coerce",function(e){switch(ot(e)){case"boolean":return e;case"nan":case!1:return!1;case"number":case"string":return e in f?!!f[e]:Number(e)!==0}return!0},"w")}),lt.cache("Function",function(e){function t(e){return!e||e in o}function n(){return this.toString()}function i(){return this}var s="__xname__",o={Anonymous:!0,anonymous:!0},u={mimic:function(e,t){var r=e.valueOf();return M(this,{displayName:t||B(r),toString:n.bind(r),valueOf:i.bind(r)},"c",!0),this}};u[r]={get:function(){if(!this[s]){var e=this.valueOf(),n=e!==this?t(e[r])?null:e[r]:null,i=n||B(this);!t(i)||t(this.displayName)||(i=this.displayName),O(this,s,i||"anonymous","w")}return this[s]}},M(e.prototype,u,"w"),O(e,"anon_list",{value:o},"w")}),lt.cache("Object",function(e){O(e.prototype,__type__,A({get:function(){var e,t=this,n=t.constructor,i=st(t),s=it(i)||(m.test(i)?"global":!1);return s?s:i=="number"?isNaN(t)?"nan":"number":i=="object"&&typeof n=="function"&&n[__type__]!="function"?(e=String(n[r]).toLowerCase(),!e||e=="anonymous"?n[__type__]||i:e):i}},d.r)),M(e,{key:function(t,n){return e.keys(e(t)).find(function(e){return t[e]===n})},reduce:function(t,n,r){return e.keys(e(t)).reduce(function(e,r,i){return e=n.call(t,e,t[r],r,t,i),e},r)},value:function(t,n){if(isNaN(n)){if(j(t,n))return t[n];if(!!~n.indexOf(".")){var r;n=n.split(".");while(r=n.shift())if((t=e.value(t,r))===u)break;return t}}return D(t)?u:P(t[n])?t[n]:typeof t.get=="function"?t.get(n):typeof t.getAttribute=="function"?t.getAttribute(n):u},values:function(t){return e.keys(Object(t)).map(function(e){return t[e]})}},"w")}),X(n)||(n=s=="commonjs"?module:e),M(N=H(N,t,n),{ENV:s,global:{value:e},modes:{value:d},bless:C,coerce:L,copy:A,def:O,defs:M,describe:_,empty:D,exists:P,expose:H,got:j,guid:F,has:q,id:U,iter:X,len:V,merge:$,nativeType:st,noop:Q,obj:G,range:Y,remove:Z,tostr:nt,type:ot,update:ut,valof:rt,x:lt},"w"),lt(Object,Array,Boolean,Function)}(this,"m8")
  1
+!function(e,t,n){"use strict";function N(e){return e}function C(e,t){switch(st(e)){case"array":break;case"string":e=e.split(".");break;default:return k(t)}y.test(e[0])&&(t=N,e.shift());if(!e.length)return k(t);e[0].indexOf("^")||(t||e[0]=="^"?e.shift():e[0]=e[0].substring(1)),t=k(t);var n;while(n=e.shift())t=t[n]||(t[n]=Q());return t}function k(t){return s=="commonjs"?t?z(t)?t.exports:t:module.exports:t||e}function L(e){var t=Number(e),n;return isNaN(t)?(n=String(e))in f?f[n]:e:t}function A(e,t,n){n=n===!0,t||(t=e,e=Q());for(var r in t)!q(t,r)||n&&q(e,r)||(e[r]=t[r]);return e}function O(e,n,r){var i=E.call(arguments,3),s=j(e,n),o,u,a,f;switch(st(i[0])){case"string":u=d[i.shift()];break;case"object":u=i.shift();break;default:a=st(r),u=a!="object"&&s?Object.getOwnPropertyDescriptor(e,n):null,!u||(u=a=="function"?d.cw:d.cew)}return f=i.shift()===!0,o=i.shift()===!0,s&&!f?o&&new Error(t+".def cannot overwrite existing property: "+n+", in item type: "+ot(e)+"."):(a!="object"&&u&&(r=_(r,u)),(r.get||r.set)&&delete r.writable,Object.defineProperty(e,n,r)),N}function M(e,t,n,r,i){n||(n="cw");for(var s in t)!q(t,s)||O(e,s,t[s],n,r,i);return N}function _(e,t){return A(st(e)=="object"?e:{value:e},st(t)=="object"?t:d[String(t).toLowerCase()]||d.cew,!0)}function D(e){return!P(e)||!X(e)&&W(e)||!1}function P(e){return!(e===null||e===u||typeof e=="number"&&isNaN(e))}function H(t,n,i){typeof n!="string"&&t[r]&&(i=n,n=t[r]);if(s=="commonjs"&&z(i))i.exports=t;else{i||(i=e);var o=i[n],u=_({value:t},"ew");o&&W(o)?O(t=o,"__",u):O(i,n,u)}return i=Q(),i[r]=n,i[__type__]="library",M(t,i,"w",!0),t}function B(e){return e.name||e.displayName||(String(e).match(b)||["",""])[1].trim()}function j(e,t){return String(t)in Object(e)}function F(){return S.replace(g,I)}function I(e){var t=v()*16|0;return(e=="x"?t:t&3|8).toString(16)}function q(e,t){return o.hasOwnProperty.call(Object(e),String(t))}function R(e,t){return e?j(e,"id")&&!D(e.id)?e.id:e.id=U(t):U(t)}function U(e){return(e||p)+"-"+ ++h}function z(e){if(a===null)return!1;try{return e instanceof a}catch(t){return!1}}function W(e){return j(e,"length")||st(e)=="object"}function X(e){return("length"in(e=Object(e))?e:Object.keys(e)).length}function V(e,t){var n;if(!t)switch(n=st(e)){case"array":case"object":t=e,e=new(t.constructor||Object);break;default:return e}else n=st(t);switch(n){case"object":return Object.keys(t).reduce(J,{source:t,target:e}).target;case"array":return e.length=t.length,t.reduce($,e);default:return t}}function $(e,t,n){return e[n]=st(e[n])===st(t)?V(e[n],t):V(t),e}function J(e,t){return e.target[t]=st(e.target[t])===st(e.source[t])?V(e.target[t],e.source[t]):V(e.source[t]),e}function K(){}function Q(e){var t=Object.create(null);return typeof e=="object"?A(t,e):t}function G(e,t,n){var r;n=String(n);if(arguments.length>3){n=E.call(arguments,2);while(r=n.shift())if(G(e,t,r))return!0;return!1}if(!~n.indexOf("."))return e(t,n);n=n.split(".");while(r=n.shift()){if(!G(e,t,r))return!1;t=t[r]}return!0}function Y(e,t){var n=[e];while(++e<=t)n.push(e);return n}function Z(e,t){t=Array.isArray(t)?t:E.call(arguments,1);var n=Array.isArray(e)?et:tt;return t.forEach(n,e),e}function et(e){var t=this.indexOf(e);t=~t?t:!isNaN(e)&&e in this?e:t,t<0||this.splice(t,1)}function tt(e){delete this[e]}function nt(e){return o.toString.call(e)}function rt(e){return o.valueOf.call(e)}function it(e){return e==c?c:e==l||e=="nodelist"?l:!e.indexOf("htm")&&e.lastIndexOf("element")+7===e.length?"htmlelement":!1}function st(e){var t=nt(e);return t in x?x[t]:x[t]=t.split(" ")[1].split("]")[0].replace(w,"$1").toLowerCase()}function ot(e){return e===null||e===u?!1:j(e,__type__)?e[__type__]:Object.getPrototypeOf(e)===null?"nullobject":u}function ut(e,t){if(!t)return V(e);switch(st(t)){case"object":return Object.keys(t).reduce(ft,{source:t,target:e}).target;case"array":return t.reduce(at,e);default:return e}}function at(e,t,n){return e[n]=j(e,n)?st(e[n])==st(t)?ut(e[n],t):e[n]:V(t),e}function ft(e,t){return e.target[t]=j(e.target,t)?st(e.target[t])==st(e.source[t])?ut(e.target[t],e.source[t]):e.target[t]:V(e.source[t]),e}function lt(){return E.call(arguments).forEach(ht),N}function ct(e){e(this,N)}function ht(e){j(e,i)||O(e,i,0,"w");var t=T[e[r]||B(e)];if(!t)return;t.slice(e[i]).forEach(ct,e),e[i]=t.length}typeof global=="undefined"?e:e=global;var r="__name__",__type__="__type__",i="__xid__",s=typeof module!="undefined"&&"exports"in module&&typeof require=="function"?"commonjs":typeof navigator!="undefined"?"browser":"other",o=Object.prototype,u,a=s!="commonjs"?null:require("module"),f=[!1,NaN,null,!0,u].reduce(function(e,t){return e[String(t)]=t,e},Q()),l="htmlcollection",c="htmldocument",h=999,p="anon",d=function(){var e={ce:"ec",cw:"wc",ew:"we",cew:"cwe ecw ewc wce wec".split(" ")},t="configurable enumerable writable".split(" "),n={c:[!0,!1,!1],ce:[!0,!0,!1],cew:[!0,!0,!0],cw:[!0,!1,!0],e:[!1,!0,!1],ew:[!1,!0,!0],r:[!1,!1,!1],w:[!1,!1,!0]},r=Object.keys(n).reduce(function(r,i){function s(e){r[e]=r[i]}var o=e[i];return r[i]=t.reduce(function(e,t,r){return e[t]=n[i][r],e},Q()),!o||(Array.isArray(o)?o.forEach(s):s(o)),r},Q());return delete r[u],r}(),v=Math.random,m=/global|window/i,g=/[xy]/g,y=new RegExp("^\\u005E?"+t),b=/[\s\(]*function([^\(]+).*/,w=/^[Ww]ebkit|[Mm]oz|O|[Mm]s|[Kk]html(.*)$/,E=Array.prototype.slice,S="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx",x={"[object Object]":"object"},T={Array:[],Boolean:[],Date:[],Function:[],Number:[],Object:[],RegExp:[],String:[]};O(lt,"cache",function(e,t){return typeof e=="string"||(e=e[r]||B(e)),T[e]||(T[e]=[]),T[e].push(t),N},"w"),lt.cache("Array",function(e){O(e,"coerce",function(e,t,n){return j(e,"length")?(t=ot(t)=="number"?t>0?t:0:0,n=ot(n)=="number"?n>t?n:n<=0?e.length+n:t+n:e.length,E.call(e,t,n)):[e]},"w"),O(e.prototype,"find",function(e,t){var n=-1,r=this.length>>>0;t||(t=this);while(++n<r)if(!!e.call(t,this[n],n,this))return this[n];return null},"w")}),lt.cache("Boolean",function(e){O(e,"coerce",function(e){switch(ot(e)){case"boolean":return e;case"nan":case!1:return!1;case"number":case"string":return e in f?!!f[e]:Number(e)!==0}return!0},"w")}),lt.cache("Function",function(e){function t(e){return!e||e in o}function n(){return this.toString()}function i(){return this}var s="__xname__",o={Anonymous:!0,anonymous:!0},u={mimic:function(e,t){var r=e.valueOf();return M(this,{displayName:t||B(r),toString:n.bind(r),valueOf:i.bind(r)},"c",!0),this}};u[r]={get:function(){if(!this[s]){var e=this.valueOf(),n=e!==this?t(e[r])?null:e[r]:null,i=n||B(this);!t(i)||t(this.displayName)||(i=this.displayName),O(this,s,i||"anonymous","w")}return this[s]}},M(e.prototype,u,"w"),O(e,"anon_list",{value:o},"w")}),lt.cache("Object",function(e){O(e.prototype,__type__,A({get:function(){var e,t=this,n=t.constructor,i=st(t),s=it(i)||(m.test(i)?"global":!1);return s?s:i=="number"?isNaN(t)?"nan":"number":i=="object"&&typeof n=="function"&&n[__type__]!="function"?(e=String(n[r]).toLowerCase(),!e||e=="anonymous"?n[__type__]||i:e):i}},d.r)),M(e,{key:function(t,n){return e.keys(e(t)).find(function(e){return t[e]===n})},reduce:function(t,n,r){return e.keys(e(t)).reduce(function(e,r,i){return e=n.call(t,e,t[r],r,t,i),e},r)},value:function(t,n){if(isNaN(n)){if(j(t,n))return t[n];if(!!~n.indexOf(".")){var r;n=n.split(".");while(r=n.shift())if((t=e.value(t,r))===u)break;return t}}return D(t)?u:P(t[n])?t[n]:typeof t.get=="function"?t.get(n):typeof t.getAttribute=="function"?t.getAttribute(n):u},values:function(t){return e.keys(Object(t)).map(function(e){return t[e]})}},"w")}),W(n)||(n=s=="commonjs"?module:e),M(N=H(N,t,n),{ENV:s,global:{value:e},modes:{value:d},bless:C,coerce:L,copy:A,def:O,defs:M,describe:_,empty:D,exists:P,expose:H,got:G.bind(null,j),guid:F,has:G.bind(null,q),id:R,iter:W,len:X,merge:V,nativeType:st,noop:K,obj:Q,range:Y,remove:Z,tostr:nt,type:ot,update:ut,valof:rt,x:lt},"w"),lt(Object,Array,Boolean,Function)}(this,"m8")
2  package.json
@@ -27,5 +27,5 @@
27 27
 	"scripts"         : {
28 28
 		"test"        : "mocha -c --ignore-leaks -R spec -u tdd ./test/*.test.js"
29 29
 	},
30  
-	"version"         : "0.2.9"
  30
+	"version"         : "0.3.0"
31 31
 }
14  src/expose.js
@@ -2,14 +2,14 @@
2 2
 
3 3
 	defs( ( __lib__ = expose( __lib__, Name, PACKAGE ) ), {
4 4
 	// properties
5  
-		ENV      : ENV,      global     : { value : root },            modes  : { value : modes },
  5
+		ENV      : ENV,      global : { value : root },            modes  : { value : modes },
6 6
 	// methods
7  
-		bless    : bless,    coerce     : coerce,     copy   : copy,   def    : def,    defs  : defs,
8  
-		describe : describe, empty      : empty,      exists : exists, expose : expose, got   : got,
9  
-		guid     : guid,     has        : has,        id     : id,     iter   : iter,   len   : len,
10  
-		merge    : merge,    nativeType : nativeType, noop   : noop,   obj    : obj,    range : range,
11  
-		remove   : remove,   tostr      : tostr,      type   : type,   update : update, valof : valof,
12  
-		x        : x
  7
+		bless    : bless,    coerce : coerce,         copy   : copy,   def    : def,    defs       : defs,
  8
+		describe : describe, empty  : empty,          exists : exists, expose : expose,
  9
+		got      : property_exists.bind( null, got ), guid   : guid,   has    : property_exists.bind( null, has ),
  10
+		id       : id,       iter   : iter,           len    : len,    merge  : merge,  nativeType : nativeType,
  11
+		noop     : noop,     obj    : obj,            range  : range,  remove : remove, tostr      : tostr,
  12
+		type     : type,     update : update,         valof  : valof,  x      : x
13 13
 	}, 'w' );
14 14
 
15 15
 	x( Object, Array, Boolean, Function );
38  src/lib.js
@@ -98,7 +98,9 @@
98 98
 
99 99
 	function fname( fn ) { return fn.name || fn.displayName || ( String( fn ).match( re_name ) || ['', ''] )[1].trim(); }
100 100
 
101  
-	function got( obj, key ) { return arguments.length > 2 ? hasSome( got, obj, Array.coerce( arguments, 1 ) ) : key in Object( obj ); }
  101
+	function got( item, property ) {
  102
+		return String( property ) in Object( item );
  103
+	}
102 104
 
103 105
 	// credit for guid goes here: gist.github.com/2295777
104 106
 	function guid() { return tpl_guid.replace( re_guid, guid_replace ); }
@@ -107,8 +109,9 @@
107 109
 		return ( match == 'x' ? num : ( num & 0x3 | 0x8 ) ).toString( 16 );
108 110
 	}
109 111
 
110  
-	function has( obj, key ) { return arguments.length > 2 ? hasSome( has, obj, Array.coerce( arguments, 1 ) ) : OP.hasOwnProperty.call( obj, key ); }
111  
-	function hasSome( test, obj, keys ) { return keys.some( function( key ) { return test( obj, key ); } ); }
  112
+	function has( item, property ) {
  113
+		return OP.hasOwnProperty.call( Object( item ), String( property ) );
  114
+	}
112 115
 
113 116
 	function id( item, prefix ) { return item ? got( item, 'id' ) && !empty( item.id ) ? item.id : ( item.id = id_create( prefix ) ) : id_create( prefix ); }
114 117
 	function id_create( prefix ) { return ( prefix || id_prefix ) + '-' + ( ++id_count ); }
@@ -161,6 +164,35 @@
161 164
 		return typeof props == 'object' ? copy( nobj, props ) : nobj;
162 165
 	}
163 166
 
  167
+	function property_exists( test, item, property ) {
  168
+		var key; property = String( property );
  169
+
  170
+		if ( arguments.length > 3 ) {
  171
+			property = slice.call( arguments, 2 );
  172
+
  173
+			while ( key = property.shift() )
  174
+				if ( property_exists( test, item, key ) )
  175
+					return true;
  176
+
  177
+			return false;
  178
+		}
  179
+
  180
+		if ( !!~property.indexOf( '.' ) ) {
  181
+			property = property.split( '.' );
  182
+
  183
+			while ( key = property.shift() ) {
  184
+				if ( !property_exists( test, item, key ) )
  185
+					return false;
  186
+
  187
+				item = item[key];
  188
+			}
  189
+
  190
+			return true;
  191
+		}
  192
+
  193
+		return test( item, property );
  194
+	}
  195
+
164 196
 	function range( i, j ) {
165 197
 		var a = [i];
166 198
 		while ( ++i <= j ) a.push( i );
12  test/m8.test.js
@@ -141,6 +141,12 @@ suite( 'm8', function() {
141 141
 		expect( m8.got( new Test(), 'foo', 'baz' ) ).to.equal( true );
142 142
 		expect( m8.got( new Test(), 'baz', 'bam' ) ).to.equal( true );
143 143
 		expect( m8.got( new Test( 'val' ), 'foo', 'bam', 'val' ) ).to.equal( true );
  144
+		expect( m8.got( { foo : { bar : 'baz' } }, 'foo.bar' ) ).to.equal( true );
  145
+		expect( m8.got( { foo : { bar : 'baz' } }, 'foo.baz' ) ).to.equal( false );
  146
+		expect( m8.got( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'items.0.foo.bar' ) ).to.equal( true );
  147
+		expect( m8.got( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'zoo', 'goo.bar', 'items.0.foo.bar' ) ).to.equal( true );
  148
+		expect( m8.got( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'items.1.foo.bar' ) ).to.equal( false );
  149
+		expect( m8.got( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'zoo', 'goo.bar', 'items.1.foo.bar' ) ).to.equal( false );
144 150
 
145 151
 		done();
146 152
 	} );
@@ -155,6 +161,12 @@ suite( 'm8', function() {
155 161
 		expect( m8.has( new Test(), 'foo', 'baz' ) ).to.equal( false );
156 162
 		expect( m8.has( new Test(), 'bar', 'bam' ) ).to.equal( false );
157 163
 		expect( m8.has( new Test( 'value' ), 'foo', 'bam', 'value' ) ).to.equal( true );
  164
+		expect( m8.has( { foo : { bar : 'baz' } }, 'foo.bar' ) ).to.equal( true );
  165
+		expect( m8.has( { foo : { bar : 'baz' } }, 'foo.baz' ) ).to.equal( false );
  166
+		expect( m8.has( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'items.0.foo.bar' ) ).to.equal( true );
  167
+		expect( m8.has( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'zoo', 'goo.bar', 'items.0.foo.bar' ) ).to.equal( true );
  168
+		expect( m8.has( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'items.1.foo.bar' ) ).to.equal( false );
  169
+		expect( m8.has( { foo : { bar : 'baz' }, items : [{ foo : { bar : 'baz' } }] }, 'zoo', 'goo.bar', 'items.1.foo.bar' ) ).to.equal( false );
158 170
 
159 171
 		done();
160 172
 	} );

0 notes on commit 31a7172

Please sign in to comment.
Something went wrong with that request. Please try again.