Permalink
Browse files

Adding Object.isUndefined and .isDefined

  • Loading branch information...
1 parent fbb67d6 commit 5192a501910140393a0b2d014f195e968f62033c @petebrowne committed Dec 1, 2010
Showing with 57 additions and 1 deletion.
  1. +10 −0 dist/classified.js
  2. +1 −1 dist/classified.min.js
  3. +36 −0 spec/classified/objectSpec.js
  4. +10 −0 src/extensions/object.js
View
@@ -552,6 +552,16 @@ extend(Object, function() {
});
return results;
});
+
+ // Returns `true` if `object` is of type `undefined`; `false` otherwise.
+ def('isUndefined', function(value) {
+ return typeof value === 'undefined';
+ });
+
+ // Returns `false` if `object` is of type `undefined`; `true` otherwise.
+ def('isDefined', function(value) {
+ return !Object.isUndefined(value);
+ });
});
classify(RegExp, function() {
@@ -9,4 +9,4 @@
// - classify 0.10.7 http://github.com/petebrowne/classify
//
//--------------------------------------------------------------------------
-(function(){var j='undefined',i=this,d=i.def,k=i.classify,n=i.module,o=i.include,p=i.extend,g=i.alias,h=Array.prototype.slice,l=i.$break={};n('Enumerable',function(){var m=function(a){return a};d('each',function(b,c){var e=0;try{this.__each__(function(a){b.call(c,a,e++)})}catch(error){if(error!=l){throw error;}}return this});d('all',function(c,e){c=c||m;var f=true;this.each(function(a,b){f=f&&!!c.call(e,a,b);if(!f){throw l;}});return f});g('every','all');d('any',function(c,e){c=c||m;var f=false;this.each(function(a,b){if(f=!!c.call(e,a,b)){throw l;}});return f});g('some','any');d('collect',function(e,f){e=e||m;return this.inject([],function(a,b,c){a.push(e.call(f,b,c));return a})});g('map','collect');d('detect',function(c,e){var f=null;this.each(function(a,b){if(c.call(e,a,b)){f=a;throw l;}});return f});g('find','detect');d('include',function(b){if(typeof this.indexOf==='function'){return this.indexOf(b)!=-1}var c=false;this.each(function(a){if(typeof a.value!==j){a=a.value}if(a==b){c=true;throw l;}});return c});g('contains','include');d('inject',function(c,e,f){this.each(function(a,b){c=e.call(f,c,a,b)});return c});g('reduce','inject');d('invoke',function(b){var c=h.call(arguments,1);return this.map(function(a){return a[b].apply(a,c)})});d('pluck',function(b){return this.map(function(a){return a[b]})});d('reject',function(e,f){return this.inject([],function(a,b,c){if(!e.call(f,b,c)){a.push(b)}return a})});g('not','reject');d('select',function(e,f){return this.inject([],function(a,b,c){if(e.call(f,b,c)){a.push(b)}return a})});g('findAll','select');g('filter','select')});n('Events',function(){d('bind',function(a,b){var c=this.__events__||(this.__events__={}),e=c[a]||(c[a]=[]);e.push(b);return this});d('unbind',function(a,b){var c,e;if(!a){delete this.__events__}else if(c=this.__events__){if(!b){delete c[a]}else if(e=c[a]){for(var f=0,m=e.length;f<m;f++){if(e[f]===b){e.splice(f,1);break}}}}return this});d('trigger',function(b){var c;if(c=this.__events__){var e=arguments,f;if(f=c[b]){f.each(function(a){a.apply(this,h.call(e,1))},this)}if(f=c['all']){f.each(function(a){a.apply(this,e)},this)}}return this})});k(Array,function(){o(Enumerable);d('__each__',function(a){for(var b=0,c=this.length;b<c;b++){a.call(null,this[b])}});d('clear',function(){this.length=0;return this});d('clone',function(){return h.call(this,0)});d('compact',function(){return this.reject(function(a){return a==null})});d('first',function(){return this[0]});d('flatten',function(){return this.inject([],function(a,b){if(b instanceof Array){a=a.concat(b.flatten())}else{a.push(b)}return a})});d('isBlank',function(){return this.compact().length==0});d('isEmpty',function(){return this.length==0});if(typeof Array.prototype.indexOf===j){d('indexOf',function(a,b){for(var c=b||0,e=this.length;c<e;c++){if(this[c]===a){return c}}return-1})}d('last',function(){return this[this.length-1]});if(typeof Array.prototype.lastIndexOf===j){d('lastIndexOf',function(a){var b=this.length;while(b--){if(this[b]===a){return b}}return-1})}d('remove',function(a){var b=this.indexOf(a);if(b!=-1){this.splice(b,1);return a}});d('removeAll',function(a){var b=false;while(typeof this.remove(a)!==j){b=true}if(b)return a});d('removeAt',function(a){if(a>0&&a<this.length){return this.splice(a,1)[0]}});d('uniq',function(e){return this.inject([],function(a,b,c){if(c===0||(e?a.last()!=b:!a.include(b))){a.push(b)}return a})})});k(Function,function(){d('bind',function(a){if(arguments.length<2&&a==null){return this}var b=this,c=h.call(arguments,1);return function(){return b.apply(a,c.concat(h.call(arguments)))}});d('curry',function(){if(!arguments.length)return this;var a=this,b=h.call(arguments);return function(){return a.apply(null,b.concat(h.call(arguments)))}});d('delay',function(a){var b=this,c=h.call(arguments,1);if(a<=0){return b.apply(null,c)}else{return this.__timeoutID__=setTimeout(function(){return b.apply(null,c)},a*1000)}});d('defer',function(){return this.delay.apply(this,[0.01].concat(h.call(arguments)))});d('periodical',function(a){var b=this,c=h.call(arguments,1);return this.__intervalID__=setInterval(function(){return b.apply(null,c)},a*1000)});d('stop',function(){clearTimeout(this.__timeoutID__);clearInterval(this.__intervalID__)})});k(Number,function(){d('times',function(a,b){for(var c=0;c<this;c++){a.call(b,c)}return this});(function(b){b.each(function(a){d(a,function(){return Math[a].apply(null,[this].concat(h.call(arguments)))})})})(['abs','acos','asin','atan','atan2','ceil','cos','exp','floor','log','max','min','pow','round','sin','sqrt','tan'])});p(Object,function(){d('extend',function(a,b){for(var c in b){a[c]=b[c]}return a});g('merge','extend');d('clone',function(a){return this.extend({},a)});d('each',function(a,b,c){for(var e in a){if(a.hasOwnProperty(e)){b.call(c,e,a[e])}}});if(typeof Object.keys===j){d('keys',function(c){if(typeof c!=='object')throw new TypeError();var e=[];this.each(c,function(a,b){e.push(a)});return e})}d('values',function(c){var e=[];this.each(c,function(a,b){e.push(b)});return e})});k(RegExp,function(){d(this,'escape',function(a){return String(a).replace(/([.*+?^=!:${}()|[\]\/\\])/g,'\\$1')})});k(String,function(){d('endsWith',function(a){var b=this.length-a.length;return b>=0&&this.indexOf(a,b)===b});d('include',function(a){return this.indexOf(a)>-1});g('contains','include');d('isBlank',function(){return/^\s*$/.test(this)});d('isEmpty',function(){return this==''});d('startsWith',function(a){return this.lastIndexOf(a,0)===0});d('strip',function(){return this.replace(/^\s+/,'').replace(/\s+$/,'')});d('camelize',function(){return this.replace(/[-_]+(.)?/g,function(a,b){return b?b.toUpperCase():''})});d('capitalize',function(){return this.charAt(0).toUpperCase()+this.substring(1)});d('dasherize',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1-$2').replace(/([a-z\d])([A-Z])/g,'$1-$2').replace(/_/g,'-').toLowerCase()});d('humanize',function(){return this.replace(/[_|-]+/g,' ').capitalize()});d('titleize',function(){return this.underscore().humanize().replace(/\b('?[a-z])/g,function(a,b){return b.capitalize()})});g('toTitleCase','titleize');d('underscore',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2').replace(/([a-z\d])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase()})})})();
+(function(){var j='undefined',i=this,d=i.def,k=i.classify,n=i.module,o=i.include,p=i.extend,g=i.alias,h=Array.prototype.slice,l=i.$break={};n('Enumerable',function(){var m=function(a){return a};d('each',function(b,c){var e=0;try{this.__each__(function(a){b.call(c,a,e++)})}catch(error){if(error!=l){throw error;}}return this});d('all',function(c,e){c=c||m;var f=true;this.each(function(a,b){f=f&&!!c.call(e,a,b);if(!f){throw l;}});return f});g('every','all');d('any',function(c,e){c=c||m;var f=false;this.each(function(a,b){if(f=!!c.call(e,a,b)){throw l;}});return f});g('some','any');d('collect',function(e,f){e=e||m;return this.inject([],function(a,b,c){a.push(e.call(f,b,c));return a})});g('map','collect');d('detect',function(c,e){var f=null;this.each(function(a,b){if(c.call(e,a,b)){f=a;throw l;}});return f});g('find','detect');d('include',function(b){if(typeof this.indexOf==='function'){return this.indexOf(b)!=-1}var c=false;this.each(function(a){if(typeof a.value!==j){a=a.value}if(a==b){c=true;throw l;}});return c});g('contains','include');d('inject',function(c,e,f){this.each(function(a,b){c=e.call(f,c,a,b)});return c});g('reduce','inject');d('invoke',function(b){var c=h.call(arguments,1);return this.map(function(a){return a[b].apply(a,c)})});d('pluck',function(b){return this.map(function(a){return a[b]})});d('reject',function(e,f){return this.inject([],function(a,b,c){if(!e.call(f,b,c)){a.push(b)}return a})});g('not','reject');d('select',function(e,f){return this.inject([],function(a,b,c){if(e.call(f,b,c)){a.push(b)}return a})});g('findAll','select');g('filter','select')});n('Events',function(){d('bind',function(a,b){var c=this.__events__||(this.__events__={}),e=c[a]||(c[a]=[]);e.push(b);return this});d('unbind',function(a,b){var c,e;if(!a){delete this.__events__}else if(c=this.__events__){if(!b){delete c[a]}else if(e=c[a]){for(var f=0,m=e.length;f<m;f++){if(e[f]===b){e.splice(f,1);break}}}}return this});d('trigger',function(b){var c;if(c=this.__events__){var e=arguments,f;if(f=c[b]){f.each(function(a){a.apply(this,h.call(e,1))},this)}if(f=c['all']){f.each(function(a){a.apply(this,e)},this)}}return this})});k(Array,function(){o(Enumerable);d('__each__',function(a){for(var b=0,c=this.length;b<c;b++){a.call(null,this[b])}});d('clear',function(){this.length=0;return this});d('clone',function(){return h.call(this,0)});d('compact',function(){return this.reject(function(a){return a==null})});d('first',function(){return this[0]});d('flatten',function(){return this.inject([],function(a,b){if(b instanceof Array){a=a.concat(b.flatten())}else{a.push(b)}return a})});d('isBlank',function(){return this.compact().length==0});d('isEmpty',function(){return this.length==0});if(typeof Array.prototype.indexOf===j){d('indexOf',function(a,b){for(var c=b||0,e=this.length;c<e;c++){if(this[c]===a){return c}}return-1})}d('last',function(){return this[this.length-1]});if(typeof Array.prototype.lastIndexOf===j){d('lastIndexOf',function(a){var b=this.length;while(b--){if(this[b]===a){return b}}return-1})}d('remove',function(a){var b=this.indexOf(a);if(b!=-1){this.splice(b,1);return a}});d('removeAll',function(a){var b=false;while(typeof this.remove(a)!==j){b=true}if(b)return a});d('removeAt',function(a){if(a>0&&a<this.length){return this.splice(a,1)[0]}});d('uniq',function(e){return this.inject([],function(a,b,c){if(c===0||(e?a.last()!=b:!a.include(b))){a.push(b)}return a})})});k(Function,function(){d('bind',function(a){if(arguments.length<2&&a==null){return this}var b=this,c=h.call(arguments,1);return function(){return b.apply(a,c.concat(h.call(arguments)))}});d('curry',function(){if(!arguments.length)return this;var a=this,b=h.call(arguments);return function(){return a.apply(null,b.concat(h.call(arguments)))}});d('delay',function(a){var b=this,c=h.call(arguments,1);if(a<=0){return b.apply(null,c)}else{return this.__timeoutID__=setTimeout(function(){return b.apply(null,c)},a*1000)}});d('defer',function(){return this.delay.apply(this,[0.01].concat(h.call(arguments)))});d('periodical',function(a){var b=this,c=h.call(arguments,1);return this.__intervalID__=setInterval(function(){return b.apply(null,c)},a*1000)});d('stop',function(){clearTimeout(this.__timeoutID__);clearInterval(this.__intervalID__)})});k(Number,function(){d('times',function(a,b){for(var c=0;c<this;c++){a.call(b,c)}return this});(function(b){b.each(function(a){d(a,function(){return Math[a].apply(null,[this].concat(h.call(arguments)))})})})(['abs','acos','asin','atan','atan2','ceil','cos','exp','floor','log','max','min','pow','round','sin','sqrt','tan'])});p(Object,function(){d('extend',function(a,b){for(var c in b){a[c]=b[c]}return a});g('merge','extend');d('clone',function(a){return this.extend({},a)});d('each',function(a,b,c){for(var e in a){if(a.hasOwnProperty(e)){b.call(c,e,a[e])}}});if(typeof Object.keys===j){d('keys',function(c){if(typeof c!=='object')throw new TypeError();var e=[];this.each(c,function(a,b){e.push(a)});return e})}d('values',function(c){var e=[];this.each(c,function(a,b){e.push(b)});return e});d('isUndefined',function(a){return typeof a==='undefined'});d('isDefined',function(a){return!Object.isUndefined(a)})});k(RegExp,function(){d(this,'escape',function(a){return String(a).replace(/([.*+?^=!:${}()|[\]\/\\])/g,'\\$1')})});k(String,function(){d('endsWith',function(a){var b=this.length-a.length;return b>=0&&this.indexOf(a,b)===b});d('include',function(a){return this.indexOf(a)>-1});g('contains','include');d('isBlank',function(){return/^\s*$/.test(this)});d('isEmpty',function(){return this==''});d('startsWith',function(a){return this.lastIndexOf(a,0)===0});d('strip',function(){return this.replace(/^\s+/,'').replace(/\s+$/,'')});d('camelize',function(){return this.replace(/[-_]+(.)?/g,function(a,b){return b?b.toUpperCase():''})});d('capitalize',function(){return this.charAt(0).toUpperCase()+this.substring(1)});d('dasherize',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1-$2').replace(/([a-z\d])([A-Z])/g,'$1-$2').replace(/_/g,'-').toLowerCase()});d('humanize',function(){return this.replace(/[_|-]+/g,' ').capitalize()});d('titleize',function(){return this.underscore().humanize().replace(/\b('?[a-z])/g,function(a,b){return b.capitalize()})});g('toTitleCase','titleize');d('underscore',function(){return this.replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2').replace(/([a-z\d])([A-Z])/g,'$1_$2').replace(/-/g,'_').toLowerCase()})})})();
@@ -77,4 +77,40 @@ describe('Object', function() {
});
});
});
+
+ describe('type checking', function() {
+ describe('#isUndefined', function() {
+ it('returns true when the value is undefined', function() {
+ var value;
+ expect(Object.isUndefined(value)).toBe(true);
+ });
+
+ it('returns false when the value is defined', function() {
+ expect(Object.isUndefined(null)).toBe(false);
+ expect(Object.isUndefined(false)).toBe(false);
+ expect(Object.isUndefined(0)).toBe(false);
+ expect(Object.isUndefined('')).toBe(false);
+ expect(Object.isUndefined(function() {})).toBe(false);
+ expect(Object.isUndefined({})).toBe(false);
+ expect(Object.isUndefined([])).toBe(false);
+ });
+ });
+
+ describe('#isDefined', function() {
+ it('returns false when the value is undefined', function() {
+ var value;
+ expect(Object.isDefined(value)).toBe(false);
+ });
+
+ it('returns true when the value is defined', function() {
+ expect(Object.isDefined(null)).toBe(true);
+ expect(Object.isDefined(false)).toBe(true);
+ expect(Object.isDefined(0)).toBe(true);
+ expect(Object.isDefined('')).toBe(true);
+ expect(Object.isDefined(function() {})).toBe(true);
+ expect(Object.isDefined({})).toBe(true);
+ expect(Object.isDefined([])).toBe(true);
+ });
+ });
+ });
});
@@ -48,4 +48,14 @@ extend(Object, function() {
});
return results;
});
+
+ // Returns `true` if `object` is of type `undefined`; `false` otherwise.
+ def('isUndefined', function(value) {
+ return typeof value === 'undefined';
+ });
+
+ // Returns `false` if `object` is of type `undefined`; `true` otherwise.
+ def('isDefined', function(value) {
+ return !Object.isUndefined(value);
+ });
});

0 comments on commit 5192a50

Please sign in to comment.