npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

observable-function

v0.1.4

Published

Wraps the function and makes it observable what allows to control the params passed to function and the result of performing.

Downloads

2

Readme

observable-function

Wraps the function and makes it observable what allows to control the params passed to function and the result of performing.

Install

Install on Node.JS with npm

$ npm install observable-function

Install for the browser

This package include a build observable-function.js ready for the browser as well as it minified version observable-function.min.js. Also you can use Browserify to organize your browser code and load modules installed by npm.

Usage

For more use-cases try examples

Just required function wrap from module:

const { wrap } = require('observable-function')

Then we can wrap function and add some validator for arguments and logger for input and output values:

const { logFunc, errFunc, PropertyError } = require('./common')

const mul10 = e => e * 10;
const checkArgCount = count =>
  p => (p.length < count) ? new PropertyError('arguments', p, `must be ${count}`) : undefined

const add = wrap((a, b) => a + b)
  .before(checkArgCount(2))
  .before(logFunc('before: '))
  .after(logFunc('after: '))
  .berror(errFunc('berror: '))
  .error(errFunc('error: '))

console.log('1. result = ', add(3, 5))

console.log('2. result = ', add(4))

Further we can add error handler (invoked before main function) which resolve issue with missing some arguments:

add
  .berror(increaseArgsCount)

console.log('3. result = ', add(4))

Also we can clone wrapper and change incoming and outcoming values so that it will be imperceptible both for initial function and for it callers:

const cloned_add = add.clone()
  .before(a => a.map(mul10))
  .before(logFunc('2. before: '))
  .after(mul10)
  .after(logFunc('2. after: '))

console.log('4. result = ', cloned_add(8, 7))

console.log('5. result = ', cloned_add(2))

console.log('6. result = ', cloned_add())

If you pass new function as argument of clone method then we get a new wrapper with another kernel function.

const cloned_subtract = add.clone((a, b) => a - b)

console.log('7. result = ', cloned_subtract(10, 9))

console.log('8. result = ', cloned_subtract(6))

API

Supported chainable notation. Handlers may be several. Each handler receive one argument (array) and could return the result which will be used as argument for next handler (or wrapped function). If the type of returned value is Error then will be invoked sequentially all error handlers. If the error is processed and returned another value (not error) then processing will continue.

before(function)

Attach function which will be called before wrapped function.

const checkArgCount = count =>
  p => (p.length<count) ? new PropertyError('arguments', p, `must be ${count}`) : undefined

let add = wrap((a, b) => a + b)

add.before(checkArgCount(2))

after(function)

Attach function which will be called after wrapped function.

const logFunc = message => args => { console.log(message, args); return args }

let add = wrap((a, b) => a + b)

add.after(logFunc('1. after: '))

berror(function)

Attach an error handler to which will be passed to the error occurred before calling the wrapped function.

const increaseArgsCount = e => (e.property === 'arguments') ? e.values.concat(0, 0) : e

add
  .berror(increaseArgsCount)

error(function)

Attach an error handler to which will be passed to the error occurred during or after calling the wrapped function.

const errFunc = message => err => {
  if (err instanceof PropertyError) {
    console.warn(`${message} [${err.name}] - ${err.message} ${err.property}`)
  } else {
    console.error(message, err.name, err.message)
  }
  return err
}

let add = wrap((a, b) => a + b)

add.error(errFunc('1. error'))

clone(function)

Returns a new wrapper with the same kernel function and sets of handlers. If you pass new function as argument of clone method then we get a new wrapper with another kernel function.

const cloned_add = add.clone()

const cloned_subtract = add.clone((a, b) => a - b)

License

MIT © Taras Panasyuk