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

fun-helpers

v2.6.0

Published

JavaScript library providing some functional helpers, all very Promise-friendly

Downloads

5

Readme

Build Status npm version Flattr this git repo Gittip donate

fun-helpers

fun-helpers is a small collection of simple functions I've got used to use daily with promises. I finally packaged them here. It's very similar to fn.js or fun.js except its main purpose is to be "Promise-friendly" and as light as possible (no fancy autoCurry).

What I mean by "Promise-friendly" is that every method of this API returns a single-parameter function, ready to be used in your thens. For example map will not take two arguments but only one, the mapping function, and will return a function taking an array as parameter and returning the result of applying the mapping function over it.

Example

var lowerNames = getUsers()
  .then(map(get("name"))) // get all users' names
  .then(call("strToLower")) // call method from each string
  .then(neutral(console.log)) // make a function neutral (return its first argument)

Compatibility

Written for Node.js but should be compatible with any modern browser (including Internet Explorer ≥ 9) using browserify or the pre-built versions in dist folder.

Installation

For Node.js:

npm install fun-helpers

If you target browser and don't use browserify, you can download a pre-built standalone version:

API

Read next section if you don't know how to read this API.

Note: All methods are safe (they do not mutate their input), and will have no side-effect in case of multiple thens on same promise.

  • all(test) any -> boolean -> [any] -> boolean
    • Checks if all elements of the promised array pass the test
  • any(test) any -> boolean -> [any] -> boolean
    • Checks if any element in the promised array pass the test
  • call(name, args…) (string, any…) -> Object -> any
    • Calls method "name" of the promised object, with optional additional arguments
  • defaults(values) Object -> Object -> Object
    • Sets all undefined keys of promised object to those from input object
  • equals(value) any -> any -> boolean
    • Checks if promised value is identical (triple equal) to input value
  • every(test) is an alias to all(test)
  • filter(test) any -> boolean -> [any] -> [any]
    • Filters promised array, keeping only values passing the test
  • get(property) string -> Object -> any
    • Returns property by its name from promised object
  • ifndef(default) -> any -> any -> any
    • Returns default value if promised value is null or undefined (default can be a function returning the value, or directly the fallback value)
  • isA(type) string or function -> any -> boolean
    • Checks promised value is of the given type (can be a type name or a constructor function)
  • is(value) is an alias to equals(value)
  • map(transform) any -> any -> [any] -> [any]
    • Returns the result of applying transform on every item of promised array
  • merge
    • Sets all keys of promised object (defined or not, it overwrites) to those from input object
  • neutral(foo) any… -> any -> any -> any
    • Will apply foo on promised value and return promised value, whatever foo was supposed to return
    • This is especially useful for loggers or function that do not return their input and would break the chain
  • not(foo) any -> boolean -> any -> boolean
    • Applies foo on promised value and returns reversed result
  • partial(foo, fixedArgs…) (any… -> any), any… -> any… -> any
    • The usual partial tool, will return a function like foo but with the n first parameters fixed
  • promisify(foo, PromiseImpl) (any…, (Error, any<T>)) -> any… -> Promise<T>
    • Transform a callback-style asynchronous function into a function returning a promise (provide Promise implementation if no global Promise object exists)
  • reduce(foo) any, any, number, [any] -> any -> [any] -> any
  • reduceRight is like reduce but will work with reversed array
  • resolve(value) any -> any -> any
    • Returns a function returning the given value whatever its input is.
  • set(property, value) string, any -> Object -> Object
    • Sets the property of promised object to given value
  • set(object, property) Object, string -> any -> Object
    • Sets the property of given object to promised value
  • some is an alias to any(test)
  • spread(foo) any… -> any -> [any] -> any
    • Will create a new function accepting a single Array argument, and returning the result of foo applied to those spread arguments
    • An additional parameter can be passed to set foo's this
  • unset(property) string -> Object -> Object
    • Deletes property from promised object

How to read type notation

Notation used here is inspired by (more and more common) Haskell type formatting:

(x, y) -> z

Can be read as a function taking two arguments of type x and y, and returning a value of type z.

Take all(test) for example:

  • all (any -> boolean) -> [any] -> boolean

Means all takes a parameter like function (anything) { return aBoolean }, and returns a result like function (array) { return aBoolean }.