# sbo

> Support the Bind Operator

Latest version **1.1.3** (published 2021-11-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install sbo
pnpm add sbo
yarn add sbo
bun add sbo
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2021-11-03 |
| First published | 2018-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 9 |
| Unpacked size | 6.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Fr. John Lamansky |
| Maintainers | lamansky |
| Keywords | bind, bind operator, bindable, function |

## Links

- npm: https://www.npmjs.com/package/sbo
- Repository: https://github.com/lamansky/sbo
- Issues: https://github.com/lamansky/sbo/issues
- npm.io page: https://npm.io/package/sbo

## Dependencies (9)

- [ffn](https://npm.io/package/ffn.md) ^2.1.0
- [ofn](https://npm.io/package/ofn.md) ^1.0.0
- [wfn](https://npm.io/package/wfn.md) ^1.0.0
- [is-nil](https://npm.io/package/is-nil.md) ^1.0.1
- [plainify](https://npm.io/package/plainify.md) ^1.0.0
- [array-pad](https://npm.io/package/array-pad.md) 0.0.1
- [is-object](https://npm.io/package/is-object.md) ^1.0.2
- [lodash.set](https://npm.io/package/lodash.set.md) ^4.3.2
- [is-global-object](https://npm.io/package/is-global-object.md) ^1.0.0

## Recent versions

- 1.1.3 (latest) — 2021-11-03
- 1.1.2 — 2021-11-02
- 1.1.1 — 2020-07-31
- 1.1.0 — 2018-04-18
- 1.0.0 — 2018-02-03

## README

# sbo

SBO stands for Support the [Bind Operator](https://github.com/tc39/proposal-bind-operator).

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

## Installation

Requires [Node.js](https://nodejs.org/) 6.0.0 or above.

```bash
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`:

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

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

```javascript
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:

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

### Specifying a Parameter Index

Now let’s swap the order of the parameters:

```javascript
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`:

```javascript
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:

```javascript
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):

```javascript
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](https://github.com/lamansky/efn): Extracted Function
* [ffn](https://github.com/lamansky/ffn): Filtering Function
* [jfn](https://github.com/lamansky/jfn): Joined Function
* [mfn](https://github.com/lamansky/mfn): Memoized Function
* [ofn](https://github.com/lamansky/ofn): Overloaded Function
* [pfn](https://github.com/lamansky/pfn): Possible Function
* [qfn](https://github.com/lamansky/qfn): Qualified Function
* [vfn](https://github.com/lamansky/vfn): Variadic Function
* [wfn](https://github.com/lamansky/wfn): Wrapper Function
* [xfn](https://github.com/lamansky/xfn): Extended Function
* [3fn](https://github.com/lamansky/3fn): Three-Way Comparison Function

---
_Source: https://npm.io/package/sbo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
