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

configurable-function

v0.0.3

Published

Configurable functions

Downloads

7

Readme

Configurable Function

API

createConfigurableFunction

Configurable functions take a single argument, an object, and inspect that object's values instead of taking multiple parameters. Using the destructuring syntax in modern javascript, they look pretty similar, you simply wrap the param names in curly braces, so a map function would look something like this:

// regular
const map = (arr, iteratee) => arr.map(iteratee);
map([1, 2], x => x * 2);

// configurable
const map = ({ arr, iteratee }) => arr.map(iteratee);
map({ arr: [1, 2], iteratee: x => x * 2 });

The benefit comes when we want to provide some arguments in advance. For regular functions we can use partial application, which looks like this:

const map = arr => iteratee => arr.map(iteratee);
const mapWithFixedArray = map([1, 2]);
mapWithFixedArray(x => x * 2);
// can't build mapWithFixedIteratee without creating a custom function

With this implementation the order of the arguments is extremely important. We cannot provide the iteratee without having the array provided earlier.

With configurable functions you can lock arguments down, or set overrideable defaults, and you can provide them in any order. A configurable version of map would look like this:

const map = ({ arr, iteratee }) => arr.map(iteratee);
const configurableMap = createConfigurableFunction(map);
const mapWithFixedArray = configurableMap.lock({ arr: [1, 2] });
const mapWithFixedIteratee = configurableMap.lock({ iteratee: x => x * 2 });

Configurable functions provide the following API

lock

lock takes an object with params to lock down and returns a new configurable function where the locked values cannot be overridden.

const concat = ({ a, b, c }) => `${a} ${b} ${c}`;
const configurableConcat = createConfigurableFunction(concat);
const concatWith1 = configurableConcat.lock({ a: 1 })
concatWith1({ b: 4, c: 5}); // "1 4 5"
concatWith1({ a: 3, b: 4, c: 5}); // "1 4 5"

default

default takes an object with params to set defaults for and returns a new configurable function where the defaulted values can be overridden, but are no longer required.

const concat = ({ a, b, c }) => `${a} ${b} ${c}`;
const configurableConcat = createConfigurableFunction(concat);
const concatWith1 = configurableConcat.default({ a: 1 })
concatWith1({ b: 4, c: 5}); // "1 4 5"
concatWith1({ a: 3, b: 4, c: 5}); // "3 4 5"

splat

splat takes a series of strings as arguments, where each string refers to the name of an argument. It returns a regular function which takes each of those params, in order.

const concat = ({ a, b, c }) => `${a} ${b} ${c}`;
const configurableConcat = createConfigurableFunction(concat);
const concatWith1 = configurableConcat.lock({ a: 1 })
const concatSplat = concatWith1.splat('b', 'c');
concatSplat(4, 5); // "1 4 5"

splatLast

splatLast is similar to splat, but if there are more arguments than strings that were provided, all additional ones will be collected together into an array for the last param name.

const map = ({ arr, iteratee }) => arr.map(iteratee);
const configurableMap = createConfigurableFunction(map);
const splattedMapToUpperCase = configurableMap.splatLast('iteratee', 'arr');
splattedMapToUpperCase(x => x.toUpperCase(), 'a', 'b', 'c'); // ['A', 'B', 'C']