Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Type shortcuts #4

Closed
wants to merge 4 commits into from

1 participant

Nathaniel Higgins
Nathaniel Higgins
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.

Nathaniel Higgins
Owner

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

Nathaniel Higgins
Owner

Should use overload.types instead.

Nathaniel Higgins nathggns closed this August 19, 2013
Nathaniel Higgins nathggns reopened this August 19, 2013
Nathaniel Higgins
Owner

merged this into development.

Nathaniel Higgins nathggns closed this August 19, 2013
Nathaniel Higgins nathggns deleted the branch August 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
36  README.md
Source Rendered
@@ -63,6 +63,18 @@ overload.add(Person.prototype, 'speak', function(method, args) {
63 63
     console.log('I\'m too busy for this!');
64 64
 });
65 65
 
  66
+// We can also do type shortcuts
  67
+var Present = function() {};
  68
+var Coin = function() {};
  69
+
  70
+overload.add(Person.prototype, 'speak', [Present], function() {
  71
+    console.log('Bah, humbug!'); 
  72
+});
  73
+
  74
+overload.add(Person.prototype, 'speak', [Coin], function() {
  75
+    console.log('Ooh, money!');
  76
+});
  77
+
66 78
 var scrooge = new Person();
67 79
 
68 80
 scrooge.speak();
@@ -71,11 +83,17 @@ scrooge.speak();
71 83
 scrooge.speak('christmas');
72 84
 // Bah, humbug!
73 85
 
74  
-scrooge.speak('money');
75  
-// money
  86
+scrooge.speak('Ooh, money');
  87
+// Ooh, money
76 88
 
77 89
 scrooge.speak('money', 'fame');
78 90
 // I'm too busy for this!
  91
+
  92
+scrooge.speak(new Present());
  93
+// Bah, humbug
  94
+
  95
+scrooge.speak(new Coin());
  96
+// Ooh, money!
79 97
 ```
80 98
 
81 99
 ### Arity
@@ -119,6 +137,20 @@ overload.add(obj, 'method', two_or_more, function() {
119 137
 });
120 138
 ```
121 139
 
  140
+### Types
  141
+
  142
+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.
  143
+
  144
+```js
  145
+var func = overload.types(['string']);
  146
+
  147
+overload.add(obj, 'method', func, function(str) {
  148
+    // str is string
  149
+});
  150
+```
  151
+
  152
+**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"`. 
  153
+
122 154
 ### Getting a clean version of Overload
123 155
 
124 156
 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).
51  src/overload.js
@@ -133,6 +133,39 @@
133 133
     };
134 134
 
135 135
     /**
  136
+     * Generate a condition function that checks types
  137
+     * @param  {Array}    types Types to check
  138
+     * @return {Function}       Condition function
  139
+     */
  140
+    var types = exports.types = function(types) {
  141
+        return function(method, args) {
  142
+            /**
  143
+             * Shortcut the relatively intensive task
  144
+             * of comparing types by just checking the length
  145
+             * first. Will find negatives much more quickly
  146
+             */
  147
+            var res = args.length === types.length;
  148
+
  149
+            if (res) {
  150
+
  151
+                types.forEach(function(type, i) {
  152
+                    if (typeof type === 'function') {
  153
+                        if (!(args[i] instanceof type)) {
  154
+                            res = false;
  155
+                        }
  156
+                    } else {
  157
+                        if (typeof args[i] !== type) {
  158
+                            res = false;
  159
+                        }
  160
+                    }
  161
+                });
  162
+            }
  163
+
  164
+            return res;
  165
+        };
  166
+    };
  167
+
  168
+    /**
136 169
      * If condition is simply a value, we'll wrap that value in a function
137 170
      */
138 171
     overload(exports, 'add', function(method, args) {
@@ -202,4 +235,22 @@
202 235
         return overload.apply(this, args);
203 236
     });
204 237
 
  238
+    /**
  239
+     * Handle calls where passing an array of types
  240
+     */
  241
+    overload(exports, 'add', function(method, args) {
  242
+        /**
  243
+         * @todo Maybe make this a better "is array" detector.
  244
+         *       Use Array.isArray. (ES5 I think?)
  245
+         */
  246
+        return typeof args[2] === 'object' &&
  247
+                typeof args[2].length !== 'undefined';
  248
+    }, function() {
  249
+        var args = arguments;
  250
+
  251
+        args[2] = types(arguments[2]);
  252
+
  253
+        return overload.apply(this, args);
  254
+    });
  255
+
205 256
 }));
36  tests/overload.js
@@ -143,5 +143,41 @@ describe('overload', function() {
143 143
 
144 144
             res.should.eql(obj.rand);
145 145
         });
  146
+
  147
+        it('should work with using objects as type shortcuts', function() {
  148
+            var i = 0;
  149
+            var obj = {
  150
+                work: function() {
  151
+                    i = i ^ 1;
  152
+                }
  153
+            };
  154
+
  155
+            var SomeType = function() {};
  156
+            var SomeOtherType = function() {};
  157
+
  158
+            overload.add(obj, 'work', [SomeType], function() {
  159
+                i = i ^ 2;
  160
+            });
  161
+
  162
+            overload.add(obj, 'work', [SomeOtherType], function() {
  163
+                i = i ^ 4;
  164
+            });
  165
+
  166
+            overload.add(obj, 'work', ['string', SomeType], function() {
  167
+                i = i ^ 8;
  168
+            });
  169
+
  170
+            obj.work();
  171
+            obj.work(new SomeType());
  172
+            obj.work('abc', new SomeType());
  173
+
  174
+            (!!(
  175
+                i & 1 &&
  176
+                i & 2 &&
  177
+                i & 8
  178
+            )).should.eql(true);
  179
+
  180
+            (!!(i & 4)).should.eql(false);
  181
+        });
146 182
     });
147 183
 });
46  tests/types.js
... ...
@@ -0,0 +1,46 @@
  1
+var overload = require('../src/overload');
  2
+var should = require('should');
  3
+
  4
+describe('overload', function() {
  5
+    describe('types', function() {
  6
+        it('should exist', function() {
  7
+            overload.should.have.property('types');
  8
+        });
  9
+
  10
+        it('should work', function() {
  11
+            var i = 0;
  12
+            var obj = {
  13
+                work: function() {
  14
+                    i = i ^ 1;
  15
+                }
  16
+            };
  17
+
  18
+            var SomeType = function() {};
  19
+            var SomeOtherType = function() {};
  20
+
  21
+            overload.add(obj, 'work', overload.types([SomeType]), function() {
  22
+                i = i ^ 2;
  23
+            });
  24
+
  25
+            overload.add(obj, 'work', overload.types([SomeOtherType]), function() {
  26
+                i = i ^ 4;
  27
+            });
  28
+
  29
+            overload.add(obj, 'work', overload.types(['string', SomeType]), function() {
  30
+                i = i ^ 8;
  31
+            });
  32
+
  33
+            obj.work();
  34
+            obj.work(new SomeType());
  35
+            obj.work('abc', new SomeType());
  36
+
  37
+            (!!(
  38
+                i & 1 &&
  39
+                i & 2 &&
  40
+                i & 8
  41
+            )).should.eql(true);
  42
+
  43
+            (!!(i & 4)).should.eql(false);
  44
+        });
  45
+    });
  46
+});
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.