Adjoin.js
Quality-controlled component for adjoining one function to another in an Aspect-Oriented style.
// ES6; /** * Start with an empty context to exemplify */let context = {}; /** * Start with a function that accepts zero or more arguments */ { thisoriginal = argOne; // this.both was already set in beforeFunction thisboth = thisboth + argTwo;} /** * Create another function with the same * arguments and optional callback. */ { thisbefore = argOne + 10; thisboth = argOne + argTwo;} /** * Adjoin functions together into a new function */const adjoinedFunction = adjoin; /** * The adjoined function calls both functions * in the correct order, with the specified * arguments and context. */; console; // {original:100, before:110, both:500}
Quality and Compatibility
Every build and release is automatically tested on the following platforms:
If your platform is not listed above, you can test your local environment for compatibility by copying and pasting the following commands into your terminal:
npm install adjoin
cd node_modules/adjoin
gulp test-local
Installation
Copy and paste the following command into your terminal to install Adjoin:
npm install adjoin --save
Import / Require
// ES6
import adjoin from "adjoin";
// ES5
var adjoin = require("adjoin");
// Require.js
define(["require"] , function (require) {
var adjoin = require("adjoin");
});
Getting Started
Adjoin.js takes two functions and adjoins then together to form a new function that calls both with the same arguments, in order.
There are currently two ways to adjoin one function to another:
adjoin.before(originalFunction, beforeFunction, [context])
adjoin.after(originalFunction, afterFunction, [context])
Both methods support asynchronous functions, and node-style asynchronous functions with a callback as the last argument.
adjoin.before()
Return an adjoinedFunction
that first calls beforeFunction
, then calls originalFunction
with the supplied arguments. Synchronous and node-style asynchronous functions are supported.
Synchronous Example:
// ES6; /** * Start with an empty context to exemplify */let context = {}; /** * Start with a function that accepts zero or more arguments */ { thisoriginal = argOne; // this.both was already set in beforeFunction thisboth = thisboth + argTwo;} /** * Create another function with the same * arguments and optional callback. */ { thisbefore = argOne + 10; thisboth = argOne + argTwo;} /** * Adjoin functions together into a new function */const adjoinedFunction = adjoin; /** * The adjoined function calls both functions * in the correct order, with the specified * arguments and context. */; console; // {original:100, before:110, both:500}
Node-style asynchronous Example:
// ES6;/*** Start with an empty context to exemplify*/let context = {};/*** Start with a function that accepts zero or* more arguments, and optionally a callback*/{thisoriginal = argOne;thisboth = argOne + argTwo;;}/*** Create another function with the same* arguments and optional callback.*/{thisbefore = argOne + 10;// this.both was already set in originalFunctionthisboth = thisboth + argTwo;;}/*** Adjoin functions together into a new function*/const adjoinedFunction = adjoin;/*** The adjoined function calls both functions* in the correct order, with the specified* arguments and context.*/;
adjoin.after()
Return an adjoinedFunction
that first calls originalFunction
, then calls afterFunction
with the supplied arguments. Synchronous and node-style asynchronous functions are supported.
Synchronous Example:
// ES6; /** * Start with an empty context to exemplify */let context = {}; /** * Start with a function that accepts zero or more arguments */ { thisoriginal = argOne; thisboth = argOne + argTwo;} /** * Create another function with the same * arguments and optional callback. */ { thisafter = argOne + 10; // this.both was already set in originalFunction thisboth = thisboth + argTwo;} /** * Adjoin functions together into a new function */const adjoinedFunction = adjoin; /** * The adjoined function calls both functions * in the correct order, with the specified * arguments and context. */; console; // {original:100, after:110, both:500}
Node-style asynchronous Example:
// ES6;/*** Start with an empty context to exemplify*/let context = {};/*** Start with a function that accepts zero or* more arguments, and optionally a callback*/{thisoriginal = argOne;thisboth = argOne + argTwo;;}/*** Create another function with the same* arguments and optional callback.*/{thisafter = argOne + 10;// this.both was already set in originalFunctionthisboth = thisboth + argTwo;;}/*** Adjoin functions together into a new function*/const adjoinedFunction = adjoin;/*** The adjoined function calls both functions* in the correct order, with the specified* arguments and context.*/;
How to Contribute
See something that could use improvement? Have a great feature idea?
You can submit your ideas through our issues system, or make the modifications yourself and submit them to us in the form of a GitHub pull request.
We always aim to be friendly and helpful.
Running Tests
It's easy to run the test suite locally, and highly recommended if you're using Adjoin.js on a platform we aren't automatically testing for.
npm test