Socket
Socket
Sign inDemoInstall

munkey

Package Overview
Dependencies
1
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    munkey

Mini AOP for JavaScript


Version published
Weekly downloads
4
increased by300%
Maintainers
1
Install size
106 kB
Created
Weekly downloads
 

Readme

Source

munkey

var µ = require("munkey");

/**
 * Simple Object with a straightforward 'add' method
 */
var original = {
  add: function (x, y) {
    return x + y;
  }
};
console.log(original.add(1.3, 1.4)); // 2.7

/**
 * Mutated version that floors both arguments before applying the original 'add'
 */
var floorArgs = µ(original).before("add", function (x, y) {
  // Return an array which content will be passed as 
  return [Math.floor(x), Math.floor(y)];
});
console.log(floorArgs.add(1.3, 1.4)); // 2

/**
 * Mutated version that rounds the result of the original 'add'
 */
var roundResult = µ(original).after("add", function (result) {
  return Math.round(result);
});
console.log(roundResult.add(1.3, 1.4)); // 3

/**
 * Mutated version that subtracts instead of adding
 */
var subtract = µ(original).insteadOf("add", function (x, y) {
  return x - y;
});
console.log(subtract.add(1, 1)); // 0

/**
 * Mutated version for tracing (won't work in strict mode)
 */
 var tracer = µ(original).intercept("add", function interceptor (method, original, args, self) {
  var result = original.apply(self, args);
  console.log("'add' was called from", method.caller.name);
  console.log("arguments: (", args[0], ",", args[1], ")");
  return result;
 });
 (function main () {
  console.log(tracer.add(1, 2));  // 'add' was called from main
                                  // arguments: ( 1 , 2 )
                                  // 3
 }());

Keywords

FAQs

Last updated on 16 Nov 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc