made with requirebin
Last active
November 8, 2015 13:39
-
-
Save zkochan/1a137a69042e22a91758 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var magicHook = require('magic-hook'); | |
function Logger() { | |
magicHook(this, ['log']); | |
} | |
Logger.prototype.log = function(msg) { | |
console.log(msg); | |
}; | |
var logger = new Logger(); | |
logger.pre('log', function(next, msg) { | |
console.log('Hello world'); | |
next(msg); | |
}); | |
logger.log('la-la'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({"magic-hook":[function(require,module,exports){"use strict";function magicHook(obj,methods){if(typeof obj!=="object"){throw new Error("obj should be an object")}if(arguments.length>1&&!(methods instanceof Array)){throw new Error("methods should be an Array")}if(typeof obj.pre!=="undefined"){throw new Error("Hooked object already has a pre property")}var pres={};if(!methods){methods=Object.getOwnPropertyNames(obj).filter(function(propName){return typeof obj[propName]==="function"})}function hook(name,fn){pres[name]=pres[name]||[];obj[name]=function(){var current=-1;function next(){current++;if(pres[name].length<=current){return fn.apply(obj,arguments)}var args=Array.prototype.slice.call(arguments);return pres[name][current].apply(obj,[next].concat(args))}return next.apply(obj,arguments)}}for(var i=methods.length;i--;){hook(methods[i],obj[methods[i]])}obj.pre=function(name,fn){if(typeof name!=="string"){throw new Error("name should be a string")}if(typeof fn!=="function"){throw new Error("fn should be a function")}if(!pres[name]){throw new Error("There's no hook with the passed name")}pres[name].push(fn)};obj.removePre=function(name,fnToRemove){if(typeof name!=="string"){throw new Error("name should be a string")}if(!obj[name]){return}if(arguments.length===1){pres[name]=[];return}pres[name]=pres[name].filter(function(currFn){return currFn!==fnToRemove})}}module.exports=magicHook},{}]},{},[]);"use strict";var magicHook=require("magic-hook");function Logger(){magicHook(this,["log"])}Logger.prototype.log=function(msg){console.log(msg)};var logger=new Logger;logger.pre("log",function(next,msg){console.log("Hello world");next(msg)});logger.log("la-la"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"magic-hook": "0.1.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment