Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
| "use strict"; | ||
| require("test").run(require("./index")) |
| "use strict"; | ||
| module.exports = require("./common") |
| "use strict"; | ||
| require("test").run(require("./browser")) |
| "use strict"; | ||
| require("retape")(require("./index")) |
| "use strict"; | ||
| require("retape")(require("./browser")) |
+12
-10
@@ -49,3 +49,3 @@ "use strict"; | ||
| function Method(hint) { | ||
| function Method(id) { | ||
| /** | ||
@@ -57,8 +57,11 @@ Private Method is a callable private name that dispatches on the first | ||
| Optionally hint string may be provided that will be used in generated names | ||
| to ease debugging. | ||
| It is supposed to be given **unique** `id` preferably in `"jump@package"` | ||
| like form so it won't collide with `id's` other users create. If no argument | ||
| is passed unique id is generated, but it's proved to be problematic with | ||
| npm where it's easy to end up with a copies of same module where each copy | ||
| will have a different name. | ||
| ## Example | ||
| var foo = Method() | ||
| var foo = Method("foo@awesomeness") | ||
@@ -76,5 +79,5 @@ // Implementation for any types | ||
| // Create an internal unique name if `hint` is provided it is used to | ||
| // prefix name to ease debugging. | ||
| var name = (hint || "") + "#" + Math.random().toString(32).substr(2) | ||
| // Create an internal unique name if one is not provided, also prefix it | ||
| // to avoid collision with regular method names. | ||
| var name = "λ:" + String(id || Math.random().toString(32).substr(2)) | ||
@@ -116,3 +119,2 @@ function dispatch(value) { | ||
| // If implementation is still not found (which also means there is no | ||
@@ -150,4 +152,4 @@ // default) just throw an error with a descriptive message. | ||
| // and implementations through them. | ||
| var implement = Method("implement") | ||
| var define = Method("define") | ||
| var implement = Method("implement@method") | ||
| var define = Method("define@method") | ||
@@ -154,0 +156,0 @@ |
+5
-0
| # Changes | ||
| ## 2.0.0 / 2013-03-12 | ||
| - Make naming methods with unique `id` users responsibility. | ||
| - Fix `npm dedup` issue by making unique `id`'s users responsibility. | ||
| ## 1.0.2 / 2012-12-26 | ||
@@ -4,0 +9,0 @@ |
+51
-7
| { | ||
| "name": "method", | ||
| "id": "method", | ||
| "version": "1.0.2", | ||
| "version": "2.0.0", | ||
| "description": "Functional polymorphic method dispatch", | ||
@@ -26,11 +26,55 @@ "keywords": [ | ||
| "test": "~0.x.0", | ||
| "repl-utils": "~2.0.1", | ||
| "phantomify": "~0.1.0" | ||
| "phantomify": "~0.x.0", | ||
| "retape": "~0.x.0", | ||
| "tape": "~0.1.5" | ||
| }, | ||
| "scripts": { | ||
| "test": "npm run test-node && npm run test-browser", | ||
| "test-browser": "node ./node_modules/phantomify/bin/cmd.js ./test/browser.js", | ||
| "test-node": "node ./test/common.js", | ||
| "repl": "node node_modules/repl-utils" | ||
| "test": "npm run test-node && npm run test-browser && npm run test-tap", | ||
| "test-browser": "node ./node_modules/phantomify/bin/cmd.js ./test/phantom-index.js", | ||
| "test-node": "node ./test/common-index.js", | ||
| "test-tap": "node ./test/tap-index.js" | ||
| }, | ||
| "testling": { | ||
| "files": "test/testling-index.js", | ||
| "browsers": { | ||
| "iexplore": [ | ||
| 6, | ||
| 7, | ||
| 8, | ||
| 9, | ||
| 10 | ||
| ], | ||
| "chrome": [ | ||
| 16, | ||
| 20, | ||
| 25, | ||
| "canary" | ||
| ], | ||
| "firefox": [ | ||
| 10, | ||
| 15, | ||
| 16, | ||
| 17, | ||
| 18, | ||
| 19, | ||
| "nightly" | ||
| ], | ||
| "safari": [ | ||
| 5, | ||
| 6 | ||
| ], | ||
| "opera": [ | ||
| 10, | ||
| 11, | ||
| 12, | ||
| "next" | ||
| ], | ||
| "iphone": [ | ||
| 6 | ||
| ], | ||
| "ipad": [ | ||
| 6 | ||
| ] | ||
| } | ||
| }, | ||
| "licenses": [ | ||
@@ -37,0 +81,0 @@ { |
+21
-13
@@ -5,6 +5,10 @@ # method | ||
| [](http://ci.testling.com/Gozala/method) | ||
| Library provides an API for defining polymorphic methods that dispatch on the | ||
| first argument type. This provides a powerful way for decouple abstraction | ||
| interface definition from an actual implementation per type, without risks | ||
| of interference with other libraries. | ||
| interface definition from an actual implementations per type / instance, | ||
| without risks of interference with other libraries. | ||
@@ -26,10 +30,14 @@ ### Motivation | ||
| ```js | ||
| var Method = require("method") | ||
| var method = require("method") | ||
| // Define `isWatchable` method that can be implemented for any type. | ||
| var isWatchable = Method() | ||
| // Use some UNIQUE identifer for the method to avoid any naming collisions. | ||
| // If not provided one will be generate but with npm it's easy to end up | ||
| // with copies of same library and there for copies of the same function | ||
| // leading to surprises. So just really pick a name that is unique! | ||
| var isWatchable = method("isWatchable@watchables") | ||
| // If you call it on any object it will | ||
| // throw as nothing implements that method yet. | ||
| //isWatchable({}) // => Exception: Method is not implemented | ||
| //isWatchable({}) // => Exception: method is not implemented | ||
@@ -54,3 +62,3 @@ // If you define private method on `Object.prototype` | ||
| // There are primitive types in JS that won"t inherit methods from Object: | ||
| isWatchable(null) // => Exception: Method is not implemented | ||
| isWatchable(null) // => Exception: method is not implemented | ||
@@ -65,3 +73,3 @@ // One could either implement methods for such types: | ||
| // Alternatively default implementation may be provided at creation: | ||
| isWatchable = Method(function() { return false }) | ||
| isWatchable = method(function() { return false }) | ||
@@ -85,9 +93,9 @@ // Method dispatches on an first argument type. That allows us to create | ||
| // Full protocols can be defined with such methods: | ||
| var _watchers = Method() | ||
| var watchers = Method() | ||
| var watch = Method() | ||
| var unwatch = Method() | ||
| var observers = "observers@" + module.filename | ||
| var watchers = method("watchers@watchables") | ||
| var watch = method("watch@watchables") | ||
| var unwatch = method("unwatch@watchables") | ||
| watchers.define(Watchable, function(target) { | ||
| return target[_watchers] || (target[_watchers] = []) | ||
| return target[observers] || (target[observers] = []) | ||
| }) | ||
@@ -112,3 +120,3 @@ | ||
| var emit = Method() | ||
| var emit = method("emit") | ||
| emit.define(Port, function(port, message) { | ||
@@ -115,0 +123,0 @@ watchers(port).slice().forEach(function(watcher) { |
+0
-2
@@ -19,3 +19,1 @@ "use strict"; | ||
| } | ||
| require("test").run(exports) |
+19
-21
@@ -40,3 +40,3 @@ "use strict"; | ||
| exports["test throws if not implemented"] = function(assert) { | ||
| var method = Method("nope") | ||
| var method = Method("nope#1") | ||
@@ -53,3 +53,3 @@ assert.throws(function() { | ||
| exports["test all types inherit from default"] = function(assert) { | ||
| var isImplemented = Method("isImplemented") | ||
| var isImplemented = Method("isImplemented#2") | ||
| isImplemented.define(function() { return true }) | ||
@@ -64,3 +64,3 @@ | ||
| exports["test default can be implemented later"] = function(assert) { | ||
| var isImplemented = Method("isImplemented") | ||
| var isImplemented = Method("isImplemented#3") | ||
| isImplemented.define(function() { | ||
@@ -77,3 +77,3 @@ return true | ||
| exports["test dispatch not-implemented"] = function(assert) { | ||
| var isDefault = Method("isDefault") | ||
| var isDefault = Method("isDefault#4") | ||
| values.forEach(function(value) { | ||
@@ -87,3 +87,3 @@ assert.throws(function() { | ||
| exports["test dispatch default"] = function(assert) { | ||
| var isDefault = Method("isDefault") | ||
| var isDefault = Method("isDefault#5") | ||
@@ -98,3 +98,3 @@ // Implement default | ||
| exports["test dispatch null"] = function(assert) { | ||
| var isNull = Method("isNull") | ||
| var isNull = Method("isNull#6") | ||
@@ -111,3 +111,3 @@ // Implement default | ||
| exports["test dispatch undefined"] = function(assert) { | ||
| var isUndefined = Method("isUndefined") | ||
| var isUndefined = Method("isUndefined#7") | ||
@@ -124,3 +124,3 @@ // Implement default | ||
| exports["test dispatch object"] = function(assert) { | ||
| var isObject = Method("isObject") | ||
| var isObject = Method("isObject#8") | ||
@@ -139,3 +139,3 @@ // Implement default | ||
| exports["test dispatch number"] = function(assert) { | ||
| var isNumber = Method("isNumber") | ||
| var isNumber = Method("isNumber#9") | ||
| isNumber.define(False) | ||
@@ -152,3 +152,3 @@ isNumber.define(Number, True) | ||
| exports["test dispatch string"] = function(assert) { | ||
| var isString = Method("isString") | ||
| var isString = Method("isString#10") | ||
| isString.define(False) | ||
@@ -164,3 +164,3 @@ isString.define(String, True) | ||
| exports["test dispatch function"] = function(assert) { | ||
| var isFunction = Method("isFunction") | ||
| var isFunction = Method("isFunction#11") | ||
| isFunction.define(False) | ||
@@ -177,3 +177,3 @@ isFunction.define(Function, True) | ||
| exports["test dispatch date"] = function(assert) { | ||
| var isDate = Method("isDate") | ||
| var isDate = Method("isDate#12") | ||
| isDate.define(False) | ||
@@ -190,3 +190,3 @@ isDate.define(Date, True) | ||
| exports["test dispatch RegExp"] = function(assert) { | ||
| var isRegExp = Method("isRegExp") | ||
| var isRegExp = Method("isRegExp#13") | ||
| isRegExp.define(False) | ||
@@ -203,3 +203,3 @@ isRegExp.define(RegExp, True) | ||
| exports["test redefine for descendant"] = function(assert) { | ||
| var isFoo = Method("isFoo") | ||
| var isFoo = Method("isFoo#14") | ||
| var ancestor = {} | ||
@@ -216,3 +216,3 @@ isFoo.implement(ancestor, function() { return true }) | ||
| function Bar() {} | ||
| var isBar = Method("isBar") | ||
| var isBar = Method("isBar#15") | ||
@@ -225,3 +225,3 @@ isBar.define(function() { return false }) | ||
| var isObject = Method("isObject") | ||
| var isObject = Method("isObject#15") | ||
| isObject.define(function() { return false }) | ||
@@ -241,3 +241,3 @@ isObject.define(Object, function() { return true }) | ||
| exports["test error types"] = function(assert) { | ||
| var isError = Method("isError") | ||
| var isError = Method("isError#16") | ||
| isError.define(function() { return false }) | ||
@@ -259,3 +259,3 @@ isError.define(Error, function() { return true }) | ||
| var fn = Method("fn") | ||
| var fn = Method("fn#17") | ||
| var methods = {} | ||
@@ -277,3 +277,3 @@ implement(define, fn, function(method, label, implementation) { | ||
| var fn = Method("fn") | ||
| var fn = Method("fn#18") | ||
| var methods = {} | ||
@@ -290,3 +290,1 @@ define.implement(fn, function(method, label, implementation) { | ||
| } | ||
| require("test").run(exports) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
1422968
12.38%23
53.33%427
2.4%126
6.78%4
33.33%