Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Type shortcuts #4

Closed
wants to merge 4 commits into from

1 participant

@nathggns
Owner

Something like this

overload.add(obj, 'method', ['string', 'Array'], function(a, b) {
    // a is a string, b is an array
});

If the passed variable is an object, it would use instanceof to compare, unless the type is declared to be exactly object. Otherwise, it will use typeof to compare.

@nathggns
Owner

Woops, can't merge into master. Will need to manually merge this into development. Sucks :(

@nathggns
Owner

Should use overload.types instead.

@nathggns nathggns closed this
@nathggns nathggns reopened this
@nathggns
Owner

merged this into development.

@nathggns nathggns closed this
@nathggns nathggns deleted the issue-4 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 19, 2013
  1. Use overload.types instead.

    authored
  2. Fix docs

    authored
This page is out of date. Refresh to see the latest.
Showing with 167 additions and 2 deletions.
  1. +34 −2 README.md
  2. +51 −0 src/overload.js
  3. +36 −0 tests/overload.js
  4. +46 −0 tests/types.js
View
36 README.md
@@ -63,6 +63,18 @@ overload.add(Person.prototype, 'speak', function(method, args) {
console.log('I\'m too busy for this!');
});
+// We can also do type shortcuts
+var Present = function() {};
+var Coin = function() {};
+
+overload.add(Person.prototype, 'speak', [Present], function() {
+ console.log('Bah, humbug!');
+});
+
+overload.add(Person.prototype, 'speak', [Coin], function() {
+ console.log('Ooh, money!');
+});
+
var scrooge = new Person();
scrooge.speak();
@@ -71,11 +83,17 @@ scrooge.speak();
scrooge.speak('christmas');
// Bah, humbug!
-scrooge.speak('money');
-// money
+scrooge.speak('Ooh, money');
+// Ooh, money
scrooge.speak('money', 'fame');
// I'm too busy for this!
+
+scrooge.speak(new Present());
+// Bah, humbug
+
+scrooge.speak(new Coin());
+// Ooh, money!
```
### Arity
@@ -119,6 +137,20 @@ overload.add(obj, 'method', two_or_more, function() {
});
```
+### Types
+
+As well as a being able to pass an array of types instead of a condition function, as demonstrated in "Usage", you can also generate a condition function from a type-array, like so.
+
+```js
+var func = overload.types(['string']);
+
+overload.add(obj, 'method', func, function(str) {
+ // str is string
+});
+```
+
+**Warning:** As JS has type-specific objects, you'll be able to look for them use the `String` constructor. However, using a `String` constructor in the type array will not accept a string created using the string literals, i.e `"string"`.
+
### Getting a clean version of Overload
For whatever reason, you might want to extend your own shortcuts onto a clean version of Overload (the initial function before we overload it with shortcuts).
View
51 src/overload.js
@@ -133,6 +133,39 @@
};
/**
+ * Generate a condition function that checks types
+ * @param {Array} types Types to check
+ * @return {Function} Condition function
+ */
+ var types = exports.types = function(types) {
+ return function(method, args) {
+ /**
+ * Shortcut the relatively intensive task
+ * of comparing types by just checking the length
+ * first. Will find negatives much more quickly
+ */
+ var res = args.length === types.length;
+
+ if (res) {
+
+ types.forEach(function(type, i) {
+ if (typeof type === 'function') {
+ if (!(args[i] instanceof type)) {
+ res = false;
+ }
+ } else {
+ if (typeof args[i] !== type) {
+ res = false;
+ }
+ }
+ });
+ }
+
+ return res;
+ };
+ };
+
+ /**
* If condition is simply a value, we'll wrap that value in a function
*/
overload(exports, 'add', function(method, args) {
@@ -202,4 +235,22 @@
return overload.apply(this, args);
});
+ /**
+ * Handle calls where passing an array of types
+ */
+ overload(exports, 'add', function(method, args) {
+ /**
+ * @todo Maybe make this a better "is array" detector.
+ * Use Array.isArray. (ES5 I think?)
+ */
+ return typeof args[2] === 'object' &&
+ typeof args[2].length !== 'undefined';
+ }, function() {
+ var args = arguments;
+
+ args[2] = types(arguments[2]);
+
+ return overload.apply(this, args);
+ });
+
}));
View
36 tests/overload.js
@@ -143,5 +143,41 @@ describe('overload', function() {
res.should.eql(obj.rand);
});
+
+ it('should work with using objects as type shortcuts', function() {
+ var i = 0;
+ var obj = {
+ work: function() {
+ i = i ^ 1;
+ }
+ };
+
+ var SomeType = function() {};
+ var SomeOtherType = function() {};
+
+ overload.add(obj, 'work', [SomeType], function() {
+ i = i ^ 2;
+ });
+
+ overload.add(obj, 'work', [SomeOtherType], function() {
+ i = i ^ 4;
+ });
+
+ overload.add(obj, 'work', ['string', SomeType], function() {
+ i = i ^ 8;
+ });
+
+ obj.work();
+ obj.work(new SomeType());
+ obj.work('abc', new SomeType());
+
+ (!!(
+ i & 1 &&
+ i & 2 &&
+ i & 8
+ )).should.eql(true);
+
+ (!!(i & 4)).should.eql(false);
+ });
});
});
View
46 tests/types.js
@@ -0,0 +1,46 @@
+var overload = require('../src/overload');
+var should = require('should');
+
+describe('overload', function() {
+ describe('types', function() {
+ it('should exist', function() {
+ overload.should.have.property('types');
+ });
+
+ it('should work', function() {
+ var i = 0;
+ var obj = {
+ work: function() {
+ i = i ^ 1;
+ }
+ };
+
+ var SomeType = function() {};
+ var SomeOtherType = function() {};
+
+ overload.add(obj, 'work', overload.types([SomeType]), function() {
+ i = i ^ 2;
+ });
+
+ overload.add(obj, 'work', overload.types([SomeOtherType]), function() {
+ i = i ^ 4;
+ });
+
+ overload.add(obj, 'work', overload.types(['string', SomeType]), function() {
+ i = i ^ 8;
+ });
+
+ obj.work();
+ obj.work(new SomeType());
+ obj.work('abc', new SomeType());
+
+ (!!(
+ i & 1 &&
+ i & 2 &&
+ i & 8
+ )).should.eql(true);
+
+ (!!(i & 4)).should.eql(false);
+ });
+ });
+});
Something went wrong with that request. Please try again.