diff --git a/bower.json b/bower.json index 7ce9438..b9e2a2f 100644 --- a/bower.json +++ b/bower.json @@ -1,5 +1,5 @@ { - "version": "0.4.1", + "version": "0.4.2", "name": "probe", "description": "Extends the native prototypes with functions from Lo-Dash, Underscore, or another vendor.", "main": "dist/probe.min.js", diff --git a/dist/probe.min.js b/dist/probe.min.js index 17eb5e3..2b4b51b 100644 --- a/dist/probe.min.js +++ b/dist/probe.min.js @@ -1,6 +1,6 @@ /*! - * Titon Probe v0.4.1 + * Titon Probe v0.4.2 * Copyright 2010-2013, The Titon Project - http://titon.io * BSD-2 - https://github.com/titon/probe/blob/master/license.md */ -!function(a){"use strict";function b(a,b,d){var e,f,g,h;for(e=0;g=b[e];e++)for(f=0;h=d[f];f++)!a[h]||g[h]||g.prototype[h]||(g===Object?g[h]=a[h]:c.call(this,a,g,h))}function c(a,b,c){b.prototype[c]=function(){var b=f.call(arguments)||[];return b.unshift(this),a[c].apply(this,b)}}function d(a){try{return require.resolve(a)}catch(b){return!1}}var e,f=Array.prototype.slice,g="undefined"!=typeof module&&module.exports;g?(exports.mapPrototypes=b,exports.extendPrototype=c):a.Probe={mapPrototypes:b,extendPrototype:c},g&&(d("lodash")?exports._=a._=require("lodash"):d("underscore")&&(exports._=a._=require("underscore")));var h=["escape","unescape","template","uniqueId","isBlank","stripTags","capitalize","chop","clean","count","chars","swapCase","escapeHTML","unescapeHTML","escapeRegExp","splice","insert","include","join","lines","reverse","startsWith","endsWith","succ","titleize","camelize","underscored","dasherize","classify","humanize","trim","ltrim","rtrim","truncate","prune","words","pad","lpad","rpad","lrpad","sprintf","vsprintf","toNumber","strRight","strRightBack","strLeft","strLeftBack","slugify","surround","quote","unquote","repeat","naturalCmp","levenshtein","toBoolean"];"undefined"!=typeof a._&&(e=a._,b(e,[Array],["compact","difference","drop","findIndex","findLastIndex","first","flatten","head","indexOf","initial","intersection","last","lastIndexOf","pull","range","remove","rest","sortedIndex","tail","take","union","uniq","unique","unzip","without","xor","zip","zipObject"]),b(e,[Array,Object,String],["all","any","at","collect","contains","countBy","detect","each","eachRight","every","filter","find","findLast","findWhere","foldl","foldr","forEach","forEachRight","groupBy","include","indexBy","inject","invoke","map","max","min","pluck","reduce","reduceRight","reject","sample","select","shuffle","size","some","sortBy","toArray","where","constant"]),b(e,[Function],["bind","compose","curry","debounce","defer","delay","memoize","once","partial","partialRight","throttle","createCallback"]),b(e,[Object],["assign","clone","cloneDeep","defaults","extend","findKey","findLastKey","forIn","forInRight","forOwn","forOwnRight","functions","has","invert","keys","mapValues","merge","methods","mixin","omit","pairs","pick","property","result","transform","values"]),b(e,[String],h),b(e,[Number],["times","parseInt"]),b(e,[Array,Object,String,Number],["isEmpty","isEqual","isUndefined","isNull","toString","valueOf"])),g&&d("underscore.string")&&(exports._s=a._s=require("underscore.string")),"undefined"!=typeof a._&&"undefined"!=typeof a._.str&&(a._s=a._.str),"undefined"!=typeof a._s&&(e=a._s,b(e,[String],h),b(e,[Number],["numberFormat"]))}(this); \ No newline at end of file +(function(root){"use strict";var vendor,slice=Array.prototype.slice,isNode=typeof module!=="undefined"&&module.exports,methodPrefix="_";function mapPrototypes(vendor,protos,funcs){var p,f,proto,func;for(p=0;proto=protos[p];p++){for(f=0;func=funcs[f];f++){if(!vendor[func]||proto[methodPrefix+func]||proto.prototype[methodPrefix+func]){continue}if(proto===Object){proto[methodPrefix+func]=vendor[func]}else{extendPrototype.call(this,vendor,proto,func)}}}}function extendPrototype(vendor,proto,func){proto.prototype[methodPrefix+func]=function(){var args=slice.call(arguments)||[];args.unshift(this);return vendor[func].apply(this,args)}}function moduleExists(name){try{return require.resolve(name)}catch(e){return false}}if(isNode){exports.mapPrototypes=mapPrototypes;exports.extendPrototype=extendPrototype;exports.methodPrefix=methodPrefix}else{root.Probe={mapPrototypes:mapPrototypes,extendPrototype:extendPrototype,methodPrefix:methodPrefix}}if(isNode){if(moduleExists("lodash")){exports._=root._=require("lodash")}else if(moduleExists("underscore")){exports._=root._=require("underscore")}}var stringFunctions=["escape","unescape","template","uniqueId","isBlank","stripTags","capitalize","chop","clean","count","chars","swapCase","escapeHTML","unescapeHTML","escapeRegExp","splice","insert","include","join","lines","reverse","startsWith","endsWith","succ","titleize","camelize","underscored","dasherize","classify","humanize","trim","ltrim","rtrim","truncate","prune","words","pad","lpad","rpad","lrpad","sprintf","vsprintf","toNumber","strRight","strRightBack","strLeft","strLeftBack","slugify","surround","quote","unquote","repeat","naturalCmp","levenshtein","toBoolean"];if(typeof root._!=="undefined"){vendor=root._;mapPrototypes(vendor,[Array],["compact","difference","drop","findIndex","findLastIndex","first","flatten","head","indexOf","initial","intersection","last","lastIndexOf","pull","range","remove","rest","sortedIndex","tail","take","union","uniq","unique","unzip","without","xor","zip","zipObject"]);mapPrototypes(vendor,[Array,Object,String],["all","any","at","collect","contains","countBy","detect","each","eachRight","every","filter","find","findLast","findWhere","foldl","foldr","forEach","forEachRight","groupBy","include","indexBy","inject","invoke","map","max","min","pluck","reduce","reduceRight","reject","sample","select","shuffle","size","some","sortBy","toArray","where","constant"]);mapPrototypes(vendor,[Function],["bind","compose","curry","debounce","defer","delay","memoize","once","partial","partialRight","throttle","createCallback"]);mapPrototypes(vendor,[Object],["assign","clone","cloneDeep","defaults","extend","findKey","findLastKey","forIn","forInRight","forOwn","forOwnRight","functions","has","invert","keys","mapValues","merge","methods","mixin","omit","pairs","pick","property","result","transform","values"]);mapPrototypes(vendor,[String],stringFunctions);mapPrototypes(vendor,[Number],["times","parseInt"]);mapPrototypes(vendor,[Array,Object,String,Number],["isEmpty","isEqual","isUndefined","isNull","toString","valueOf"])}if(isNode&&moduleExists("underscore.string")){exports._s=root._s=require("underscore.string")}if(typeof root._!=="undefined"&&typeof root._.str!=="undefined"){root._s=root._.str}if(typeof root._s!=="undefined"){vendor=root._s;mapPrototypes(vendor,[String],stringFunctions);mapPrototypes(vendor,[Number],["numberFormat"])}})(this); \ No newline at end of file diff --git a/package.json b/package.json index 1042929..2f688c3 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.4.1", + "version": "0.4.2", "name": "titon-probe", "description": "Extends the native prototypes with functions from Lo-Dash, Underscore, or another vendor.", "keywords": ["probe", "titon", "prototype", "lodash", "underscore"], @@ -15,6 +15,10 @@ "name": "Miles Johnson", "url": "http://milesj.me" }, + "contributors": [{ + "name": "Matt Browne", + "url": "https://github.com/mbrowne" + }], "repository": { "type": "git", "url": "https://github.com/titon/probe" diff --git a/readme.md b/readme.md index 9b1c8c3..989a256 100644 --- a/readme.md +++ b/readme.md @@ -11,9 +11,11 @@ _.uniq(_.flatten([1, 2, [2], 3, 2])); // => [1, 2, 3] To be written as: ``` -[1, 2, [2], 3, 2].flatten().uniq(); // => [1, 2, 3] +[1, 2, [2], 3, 2]._flatten()._uniq(); // => [1, 2, 3] ``` +(Note: All non-native methods are prefixed with an underscore.) + ### Using In Node.js ### Add Probe to your NPM `package.json`. diff --git a/src/probe.js b/src/probe.js index 1a9dc67..ce94590 100644 --- a/src/probe.js +++ b/src/probe.js @@ -9,7 +9,8 @@ var vendor, slice = Array.prototype.slice, - isNode = (typeof module !== 'undefined' && module.exports); + isNode = (typeof module !== 'undefined' && module.exports), + methodPrefix = '_'; //prefix used for methods added to native prototypes /** * Loop over each collection and extend the prototypes. @@ -29,14 +30,14 @@ // Skip if the function already exists on the prototype // We don't wont to cause collisions with built-ins or user defined - if (!vendor[func] || proto[func] || proto.prototype[func]) { + if (!vendor[func] || proto[methodPrefix + func] || proto.prototype[methodPrefix + func]) { continue; } // Objects can only use static methods // Applying to the prototype disrupts object literals if (proto === Object) { - proto[func] = vendor[func]; + proto[methodPrefix + func] = vendor[func]; } else { extendPrototype.call(this, vendor, proto, func); } @@ -53,7 +54,7 @@ * @param {Function} func */ function extendPrototype(vendor, proto, func) { - proto.prototype[func] = function() { + proto.prototype[methodPrefix + func] = function() { var args = slice.call(arguments) || []; args.unshift(this); @@ -78,10 +79,12 @@ if (isNode) { exports.mapPrototypes = mapPrototypes; exports.extendPrototype = extendPrototype; + exports.methodPrefix = methodPrefix; } else { root.Probe = { mapPrototypes: mapPrototypes, - extendPrototype: extendPrototype + extendPrototype: extendPrototype, + methodPrefix: methodPrefix }; } @@ -176,4 +179,4 @@ // Number mapPrototypes(vendor, [Number], ['numberFormat']); } -})(this); \ No newline at end of file +})(this); diff --git a/test/cases/lodash.js b/test/cases/lodash.js index 7210dfd..b335332 100644 --- a/test/cases/lodash.js +++ b/test/cases/lodash.js @@ -6,7 +6,7 @@ describe('Lo-Dash', function() { var expect; - + if (typeof module !== 'undefined' && module.exports) { expect = require('chai').expect; } else { @@ -14,15 +14,15 @@ describe('Lo-Dash', function() { } it('should apply Lo-dash functions to the natives', function() { - expect(Array.prototype.union).to.be.defined; + expect(Array.prototype._union).to.be.defined; }); it('should allow for chaining', function() { - var test = [1, 2, 3].union([3, 4, 5]).filter(function(v) { + var test = [1, 2, 3]._union([3, 4, 5])._filter(function(v) { return v != 2; }).reverse(); expect(test).to.deep.equal([5, 4, 3, 1]); - expect(test.last()).to.equal(1); + expect(test._last()).to.equal(1); }); }); \ No newline at end of file diff --git a/test/cases/probe.js b/test/cases/probe.js index 629cd2b..086b80a 100644 --- a/test/cases/probe.js +++ b/test/cases/probe.js @@ -24,21 +24,24 @@ describe('Probe', function() { probe = window.Probe; } - it('should extend the prototype with vendor function', function() { - expect(Number.prototype.foo).to.be.an('undefined'); - expect(Number.prototype.bar).to.be.an('undefined'); + var prefix = probe.methodPrefix, + foo = prefix + 'foo', + bar = prefix + 'bar'; + + it('should extend the prototype with vendor function', function() { + expect(Number.prototype[foo]).to.be.an('undefined'); + expect(Number.prototype[bar]).to.be.an('undefined'); probe.mapPrototypes(vendor, [Number], ['foo', 'bar']); - expect(Number.prototype.foo).to.be.an('function'); - expect(Number.prototype.bar).to.be.an('undefined'); + expect(Number.prototype[foo]).to.be.an('function'); + expect(Number.prototype[bar]).to.be.an('undefined'); }); it('should not overwrite previous functions with the same name', function() { - var string = { - indexOf: function(str, char) { - return 1337; - } + var string = {}; + string[prefix + 'indexOf'] = function(str, char) { + return 1337; }; probe.mapPrototypes(string, [String], ['indexOf']); @@ -47,24 +50,24 @@ describe('Probe', function() { }); it('should not apply to the Object prototype', function() { - expect(Object.foo).to.be.an('undefined'); - expect(Object.prototype.foo).to.be.an('undefined'); + expect(Object[foo]).to.be.an('undefined'); + expect(Object.prototype[foo]).to.be.an('undefined'); probe.mapPrototypes(vendor, [Object], ['foo']); - expect(Object.foo).to.be.an('function'); - expect(Object.prototype.foo).to.be.an('undefined'); + expect(Object[foo]).to.be.an('function'); + expect(Object.prototype[foo]).to.be.an('undefined'); }); it('should pass the type as the first argument', function() { probe.mapPrototypes(vendor, [Number], ['pass']); - expect((10).pass(22)).to.equal(220); + expect((10)[prefix + 'pass'](22)).to.equal(220); }); it('should allow for chaining', function() { probe.mapPrototypes(vendor, [Number], ['chain']); - expect((5).pass(8).chain(12)).to.equal(52); + expect((5)[prefix + 'pass'](8)[prefix + 'chain'](12)).to.equal(52); }); }); \ No newline at end of file diff --git a/test/cases/underscore-string.js b/test/cases/underscore-string.js index bd4057a..60b3a84 100644 --- a/test/cases/underscore-string.js +++ b/test/cases/underscore-string.js @@ -14,13 +14,13 @@ describe('Underscore.String', function() { } it('should apply Underscore.String functions to the natives', function() { - expect(String.prototype.swapCase).to.be.defined; + expect(String.prototype._swapCase).to.be.defined; }); it('should allow for chaining', function() { - var test = 'Titon Probe'.swapCase().insert(6, '- '); + var test = 'Titon Probe'._swapCase()._insert(6, '- '); expect(test).to.equal('tITON - pROBE'); - expect(test.classify()).to.equal('TitonProbe'); + expect('titon-probe'._classify()).to.equal('TitonProbe'); }); }); \ No newline at end of file diff --git a/test/cases/underscore.js b/test/cases/underscore.js index 079709b..bda1f10 100644 --- a/test/cases/underscore.js +++ b/test/cases/underscore.js @@ -18,11 +18,11 @@ describe('Underscore', function() { }); it('should allow for chaining', function() { - var test = [1, 2, 3].union([3, 4, 5]).filter(function(v) { + var test = [1, 2, 3]._union([3, 4, 5])._filter(function(v) { return v != 2; }).reverse(); expect(test).to.deep.equal([5, 4, 3, 1]); - expect(test.last()).to.equal(1); + expect(test._last()).to.equal(1); }); }); \ No newline at end of file diff --git a/version.md b/version.md index 44bb5d1..f7abe27 100644 --- a/version.md +++ b/version.md @@ -1 +1 @@ -0.4.1 \ No newline at end of file +0.4.2 \ No newline at end of file