aspects
before, after, and around hooks for sync and async functions
also known as aspect-oriented programming
npm install --save aspects
example
const { before, after, around } = require('aspects').sync
const { all: catNames, random: catName } = require('cat-names')
function hello (name) {
return `Hello, ${name}!`
}
console.log(hello(catName()))
function onlyCats ([name]) {
return (catNames.indexOf(name) !== -1)
? [name]
: new Error(`${name} is not a cat name.`)
}
console.log(before(hello, onlyCats)(catName()))
console.log(before(hello, onlyCats)(reverse(catName())))
function loud (value) {
return value.toUpperCase()
}
console.log(after(hello, loud)(catName()))
function character (fn, args) {
return fn.apply(null, args.map(a => a[0])) + '!!!'
}
console.log(around(hello, character)(catName()))
function reverse (str) {
return str.split('').reverse().join('')
}
usage
aspects = require('aspects')
{ before, after, around } = aspects
fn = before.sync(fn, hook)
fn
is sync function: uses return
hook
is function of shape (args) => Error | newArgs
fn = before.async(fn, hook)
fn
is async function: the last argument is an error-first callback
hook
is function of shape (args, cb) => cb(Error, newArgs)
fn = after.sync(fn, hook)
fn
is sync function: uses return
hook
is function of shape (value) => Error | newValue
fn = after.async(fn, hook)
fn
is async function: the last argument is an error-first callback
hook
is function of shape (value, cb) => cb(Error, newValue)
fn = around.sync(fn, hook)
fn
is sync function: uses return
hook
is function of shape (fn, args) => newFn
fn = around.async(fn, hook)
fn
is async function: the last argument is an error-first callback
hook
is function of shape (fn, args, cb) => newFn
inspiration
license
The Apache License
Copyright © 2016 Michael Williams
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.