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

method-inject

v0.0.1

Published

A simple system for injecting and transforming methods

Downloads

2

Readme

method-inject

A simple system for injecting and transforming methods

Installation

yarn add method-inject
npm install --save method-inject

Usage

To use method-inject, it's as simple as calling it on a method:

const inject = require('method-inject');

// Transform the first argument and prepend '[INFO] ' to it
const log = inject(console.log).transform(0, text => '[INFO] ' + text);

log('Hello, there!');
// => [INFO] Hello, there!

There are several methods, the rest of which are shown below:

const inject = require('method-inject');
const someObject = {
    buggedMethod(someArg) {
        console.log(someArg + 1);
    }
};

// Hmm, we've been getting `NaN` in the console from someObject.buggedMethod, let's check it out:
someObject.buggedMethod = inject(someObject.buggedMethod, someObject).before(args => console.log('buggedMethod called with the following args:', args));
// Note that we passed someObject as the second parameter to inject, indicating that the method should be bound to that object.

// Problem code:
someObject.buggedMethod('ERRORS');
// => buggedMethod called with the following args: [ 'ERRORS' ]
// => NaN
const inject = require('method-inject');

const db = {
    internal: {
        life: 42
    },
    get(key, default) {
        return this.internal[key] || default;
    }
};

if (process.env.DB_VERBOSE) {
    db.get = inject(db.get, db).after((returnValue, args) => {
        console.log(`DB::GET(key=${args[0]},default=${args[1] || 'none'}) -> ${returnValue}`);
    });
}

// Assuming DB_VERBOSE=true:
console.log(`The meaning of life is ${db.get('life', -1)}`);
// => DB::GET(key=life,default=-1) -> 42
// => The meaning of life is 42
const inject = require('method-inject');

const multiply = (a, b) => {
    return a * b;
};

const multiplyAndSquare = inject(multiply).transformOutput(output => output * output);

// (2 * 3) * (2 * 3) = 6 * 6 = 36
console.log(multiplyAndSquare(2, 3));
// => 36

Methods can also be chained indefinitely:

const inject = require('method-inject');

const multiply = (a, b) => a * b;
const addOne = x => x + 1;
const square = x => x * x;

const verboseComplexMath = inject(multiply)
    .after(output => console.log(`COMPLEX_CALC -> ${output}`))
    .transformOutput(addOne)
    .transformOutput(square);

console.log(verboseComplexMath(4, 5));
// => COMPLEX_CALC -> 441
// => 441

Join Me

Discord Badge