Skip to content

Instantly share code, notes, and snippets.

@isaacs
Forked from Gozala/case.js
Created November 6, 2012 00:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save isaacs/4021416 to your computer and use it in GitHub Desktop.
Save isaacs/4021416 to your computer and use it in GitHub Desktop.
npm testcase
var npm = require("npm")
var path = require("path")
npm.load({ prefix: path.join(process.env.HOME, ".orchestrator") }, function(e, npm) {
npm.commands.install(["method"], function(err, _) {
npm.commands.ls([], console.log)
})
})
npm http GET https://registry.npmjs.org/method
npm http 304 https://registry.npmjs.org/method
method@0.1.1 ../../.orchestrator/node_modules/method
/Users/isaacs/.orchestrator
└── method@0.1.1
null { dependencies:
{ method:
{ name: 'method',
id: 'method',
version: '0.1.1',
description: 'Functional polymorphic method dispatch',
keywords: [Object],
author: [Object],
homepage: 'https://github.com/Gozala/method',
main: './core.js',
repository: [Object],
bugs: [Object],
devDependencies: [Object],
scripts: [Object],
licenses: [Object],
readme: '# method\n\n[![Build Status](https://secure.travis-ci.org/Gozala/method.png)](http://travis-ci.org/Gozala/method)\n\nLibrary provides an API for defining polymorphic methods that dispatch on the\nfirst argument type. This provides a powerful way for decouple abstraction\ninterface definition from an actual implementation per type, without risks\nof interference with other libraries.\n\n### Motivation\n\n - Provide a high-performance, dynamic polymorphism construct as an\n alternative to existing object methods that does not provides any\n mechanics for guarding against name conflicts.\n - Allow independent extension of types, and implementations of methods\n on types, by different parties.\n\n## Install\n\n npm install method\n\n## Use\n\n```js\nvar Method = require(\'method\')\n\n// Define `isWatchable` method that can be implemented for any type.\nvar isWatchable = Method()\n\n// If you call it on any object it will\n// throw as nothing implements that method yet.\n//isWatchable({}) // => Exception: Method is not implemented\n\n// If you define private method on `Object.prototype`\n// all objects will inherit it.\nObject.prototype[isWatchable] = function() {\n return false;\n}\n\nisWatchable({}) // => false\n\n\n// Although `isWatchable` property above will be enumerable and there for\n// may damage some assumbtions made by other libraries. There for it\'s\n// recomended to use built-in helpers methods that will define extension\n// without breaking assumbtions made by other libraries:\n\nisWatchable.define(Object, function() { return false })\n\n\n// There are primitive types in JS that won\'t inherit methods from Object:\nisWatchable(null) // => Exception: Method is not implemented\n\n// One could either implement methods for such types:\nisWatchable.define(null, function() { return false })\nisWatchable.define(undefined, function() { return false })\n\n// Or simply define default implementation:\nisWatchable.define(function() { return false })\n\n// Alternatively default implementation may be provided at creation:\nisWatchable = Method(function() { return false })\n\n// Method dispatches on an first argument type. That allows us to create\n// new types with an alternative implementations:\nfunction Watchable() {}\nisWatchable.define(Watchable, function() { return true })\n\n// This will make all `Watchable` instances watchable!\nisWatchable(new Watchable()) // => true\n\n// Arbitrary objects can also be extended to implement given method. For example\n// any object can simply made watchable:\nfunction watchable(object) {\n return isWatchable.implement(objct, function() { return true })\n}\n\nisWatchable(watchable({})) // => true\n\n// Full protocols can be defined with such methods:\nvar _watchers = Method()\nvar watchers = Method()\nvar watch = Method()\nvar unwatch = Method()\n\nwatchers.define(Watchable, function(target) {\n return target[_watchers] || (target[_watchers] = [])\n})\n\nwatch.define(Watchable, function(target, watcher) {\n var observers = watchers(target)\n if (observers.indexOf(watcher) < 0) observers.push(watcher)\n return target\n})\nunwatch.define(Watchable, function(target, watcher) {\n var observers = watchers(target)\n var index = observers.indexOf(watcher)\n if (observers.indexOf(watcher) >= 0) observers.unshift(watcher)\n return target\n})\n\n// Define type Port that inherits form Watchable\n\nfunction Port() {}\nPort.prototype = Object.create(Watchable.prototype)\n\nvar emit = Method()\nemit.define(Port, function(port, message) {\n watchers(port).slice().forEach(function(watcher) {\n watcher(message)\n })\n})\n\nvar p = new Port()\nwatch(p, console.log)\nemit(p, \'hello world\') // => info: "hello world"\n```\n',
readmeFilename: 'Readme.md',
_id: 'method@0.1.1',
realName: 'method',
dependencies: {},
path: '/Users/isaacs/.orchestrator/node_modules/method',
realPath: '/Users/isaacs/.orchestrator/node_modules/method',
link: undefined,
parent: [Circular],
depth: 1 } },
path: '/Users/isaacs/.orchestrator',
realName: undefined,
realPath: '/Users/isaacs/.orchestrator',
link: undefined,
depth: 0 } { dependencies: { method: { version: '0.1.1' } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment