Skip to content

Added method prefix #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions dist/probe.min.js

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

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
15 changes: 9 additions & 6 deletions src/probe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand All @@ -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);

Expand All @@ -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
};
}

Expand Down Expand Up @@ -176,4 +179,4 @@
// Number
mapPrototypes(vendor, [Number], ['numberFormat']);
}
})(this);
})(this);
8 changes: 4 additions & 4 deletions test/cases/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

describe('Lo-Dash', function() {
var expect;

if (typeof module !== 'undefined' && module.exports) {
expect = require('chai').expect;
} else {
expect = chai.expect;
}

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);
});
});
33 changes: 18 additions & 15 deletions test/cases/probe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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);
});
});
6 changes: 3 additions & 3 deletions test/cases/underscore-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
4 changes: 2 additions & 2 deletions test/cases/underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion version.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.1
0.4.2