Giter VIP home page Giter VIP logo

sbo's Introduction

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

sbo's People

Contributors

lamansky avatar

Watchers

James Cloos avatar  avatar

Forkers

waki285

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.