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

fkt

v0.1.5

Published

A collection of very simple but occasionally useful functions, primarily intended for use with (and as) callbacks and in unit tests.

Downloads

13

Readme

fkt

Author: Raphael Pigulla [email protected]

Version: 0.1.5

A collection of very simple but occasionally useful functions, primarily intended for use with (and as) callbacks and in unit tests..

Runs in both Node.js and the browser. Available on npm, Jam and Bower. Implements the UMD pattern for compatibility with both native browsers environments and AMD loaders such as requirejs.

Installation

Simply do one of the following:

npm install fkt
jam install fkt
bower install fkt

or just download directly from GitHub.

API


bare(fn, scope)

Wraps a function to receive only its first argument.

Can be used to discard unneeded arguments for callbacks, especially when using libraries like async.

// Simplify this:
async.waterfall([
    function (cb) {
        loadFooBarAndBaz(function (err, foo, bar, baz) {
            // if we pass cb in directly, the next function in the chain would
            // be called with three unused arguments which we want to avoid
            cb(err);
        });
    }
], function (cb) {
    // ...
});

// to this:
async.waterfall[
    function (cb) {
        loadFooBarAndBaz(fkt.bare(cb));
    }
], function (err) {
    // ...
});
Params:
  • function fn The function to wrap.
  • Object scope The scope in which to execute fn. (optional)
Return:
  • function Returns the wrapped function.

callbackify(fn, scope)

Creates a callback version of the given synchronous function.

If fn throws an exception, the callback receives it as its first argument. Otherwise, it is invoked with the return value of fn.

async.waterfall([
    function (cb) {
       fs.readFile('a.json', cb);
    },
    fkt.callbackify(JSON.parse)
], function (error, result) {
    // error is set if fs.readFile failed or JSON.parse threw an exception
});
Params:
  • function fn The function to wrap.
  • Object scope The scope in which to execute fn.
Return:
  • function Returns the wrapped function.

catch(fn, scope)

Wraps a function so that it never throws an exception. Don't use this in production code!

The return value of the wrapped function is the return value of the original function. If an exception was thrown, a reference to fkt.undefined is returned (note that this is not the same as the value undefined).

var result = fkt.catch(someFunction);
if (result === fkt.undefined) {
    // some exception was thrown
} else {
    // it's all good
}
Params:
  • function fn The function to wrap.
  • Object scope The scope in which to execute fn. (optional)
Return:
  • function Returns the wrapped function.

constant(c)

Creates a function that always returns the specified value.

Params:
  • mixed c The value you want to be returned.
Return:
  • function Returns a function that always returns c.

false()

A function that always returns false.

// useful in Backbone.Views when you need to stop event propagation:
events: {
    'click ul.items li': fkt.false
}
Return:
  • boolean Always returns false.

identity(x)

The identity function that always returns its first argument.

Can be used to easily remove falsy values from an array:

[0, 1, 2, false, {}].filter(fkt.identity);
// returns [1, 2, {}]
Params:
  • mixed x
Return:
  • mixed Returns the input value x.

narrow(n, fn, scope)

Wraps a callback to only be invoked with its first n arguments.

var narrowed = fkt.narrow(2, function () {
    console.dir(arguments);
});
narrowed(1, 2, 3, 4);  // outputs [1, 2]
Params:
  • number n The number of parameters to keep.
  • function fn The function to wrap.
  • Object scope The scope in which to execute fn. (optional)
Return:
  • function Returns the wrapped function.

negate(fn, scope)

A function that wraps the given function and always returns the negated value of it.

One purpose for this is using Array.filter with a function that happens to return false for values you want to keep (see the example).

// instead of this
var myArray = someArray.filter(function (el) {
    return !userFunction(el);
});
// we can do
var myArray = someArray.filter(fkt.negate(userFunction));
Params:
  • function fn The function to negate.
  • Object scope The scope in which to execute fn. (optional)
Return:
  • function Returns the wrapped function.

noop()

A function that does nothing and returns nothing.

Can be used as a default value for callbacks. Saves you a few bytes of memory by not having to create a new anonymous function every time.

// explicitly specify a "do nothing" function (which creates a new function every time)
var callback = config.cb || function () {};
// alternatively, only invoke the callback if defined (fairly verbose)
if (callback) { callback(someValue); }

// instead, do this
var callback = config.cb || fkt.noop;
// or if it makes you feel fuzzy even
(callback || fkt.noop)(someValue);
Return:
  • undefined

safe(fn, scope)

Wraps a callback to always be invoked with null as its first argument (i.e., it will never fail).

Can be used to "swallow" errors if you know that it is safe to ignore them, which may be helpful when using libraries like async (e.g., async.map() will immediately abort when it encounters the first error which may not be what you want).

// assuming file2 doesn't exist
async.map(['file1', 'file2', 'file3'], fkt.safe(fs.readFile), function (error, result) {
    // error is guaranteed to be null
    // result is [<buffer>, undefined, <buffer>]
}));
Params:
  • function fn The function to wrap.
  • Object scope The scope in which to execute fn. (optional)
Return:
  • function Returns the wrapped function.

true()

A function that always returns true.

function getCars(tickets, filterFn) {
    return cars.filter(filterFn || fkt.true);
}

getCars();  // get all cars
getCars(function (car) { return car.color === 'red'; });  // get red cars only
Return:
  • boolean Always returns true.

undefined()

A function that always returns undefined.

This is an alias for fkt.noop(). You can use whichever makes more sense semantically (i.e., makes your code more readable).

Return:
  • undefined