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

sbo

v1.1.3

Published

Support the Bind Operator

Downloads

11,213

Readme

sbo

SBO stands for Support the Bind Operator.

Converts value::yourFunction(arg) to yourFunction(value, arg)—but lets your function support either.

Installation

Requires Node.js 6.0.0 or above.

npm i sbo

API

The module exports a single function.

Parameters

  1. Optional: Object argument:
    • Optional: arg (integer): The argument index at which this should be inserted. Defaults to 0.
    • Optional: path (string): The dot-separated key path of an options object argument located at index arg into which this should be inserted.
    • Optional: ignoreThis (object, array, or function): this is ignored if ignoreThis strictly equals this, or if ignoreThis is an array containing this, or if ignoreThis is a function which returns true when given this.
  2. fn (Function): The function which should receive bind operator support.

Return Value

A wrapper function with bind operator support that calls fn.

Tutorial

Let’s say, for the sake of example, that you have a function called addSuffix:

const addSuffix = (str, suffix) => str + suffix

You use the sbo module to add support for the bind operator:

const supportBindOperator = require('sbo')
const addSuffix = supportBindOperator((str, suffix) => str + suffix)

Now your function can be called either the normal way or with the bind operator:

addSuffix('Hello, world', '!') // 'Hello, world!'
'Hello, world'::addSuffix('!') // 'Hello, world!'

Specifying a Parameter Index

Now let’s swap the order of the parameters:

const addSuffix = (suffix, str) => str + suffix

A bound this would now need to become the argument with an index of 1. To do this, pass an extra argument to sbo:

const supportBindOperator = require('sbo')
const addSuffix = supportBindOperator({arg: 1}, (suffix, str) => str + suffix)

addSuffix('!', 'Hello, world') // 'Hello, world!'
'Hello, world'::addSuffix('!') // 'Hello, world!'

Specifying an Object Argument Key

Let’s try using a deconstructed object parameter:

const addSuffix = ({str, suffix}) => str + suffix

Do the following to direct a bound this to the str key of the object argument at index zero (i.e. the first, and in this case the only, argument):

const supportBindOperator = require('sbo')
const addSuffix = supportBindOperator({path: 'str'}, ({str, suffix}) => str + suffix)

addSuffix({str: 'Hello, world', suffix: '!'}) // 'Hello, world!'
'Hello, world'::addSuffix({suffix: '!'}) // 'Hello, world!'

If you have a nested object parameter, you can use a dot-separated key path.

Related

Check out these other function utility packages.

  • efn: Extracted Function
  • ffn: Filtering Function
  • jfn: Joined Function
  • mfn: Memoized Function
  • ofn: Overloaded Function
  • pfn: Possible Function
  • qfn: Qualified Function
  • vfn: Variadic Function
  • wfn: Wrapper Function
  • xfn: Extended Function
  • 3fn: Three-Way Comparison Function