1.2.2 • Published 4 years ago

proxy-bind v1.2.2

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

proxy-bind

npm version actions codecov

tl;dr

from

const fn = foobar.fn.bind(foobar);

to

const { fn } = bind(foobar);

Install

npm install proxy-bind

Import

import { bind, bond } from 'proxy-bind';

.

const { bind, bond } = require('proxy-bind');

or

<script type="module">
    import { bind, bond } from 'https://cdn.jsdelivr.net/npm/proxy-bind@1.x/index.mjs';
</script>

.

<script type="javascript">
    (async () => {
        const { bind, bond } = await import('https://cdn.jsdelivr.net/npm/proxy-bind@1.x/index.mjs');
    })();
</script>

the bond is only an alias to avoid naming conflict, just in case.

Example

"point-free" ish

import { bind } from 'proxy-bind';

const { push, join } = bind([ 1, 2, 3 ]);

push(4);

console.log(join('-'));  // 1-2-3-4
import { bind } from 'proxy-bind';

const { resolve } = bind(Promise);

console.log(await resolve(42));  // 42
console.log(await resolve('foobar'));  // foobar

bind this

import { bind } from 'proxy-bind';

const me = {

    name: 'foo',

    greet (you) {
        return `Hi ${ you }, this is ${ this.name }.`;
    },

};

const { greet } = bind(me);

console.log(greet('bar'));  // Hi bar, this is foo.

mirror helper

import { mirror } from 'proxy-bind';

const [ list, { push } ] = mirror([ 1, 2, 3 ]);

push(4);

console.log(list);  // [1, 2, 3, 4]

Methodology

It uses ES6 Proxy to bridge function calls via Function.prototype.bind.

Caveat

  • Doesn't work on primitive types:
    • don't bind(5)
    • do bind(new Number(5))

License

the MIT

1.2.2

4 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.1-0

5 years ago

1.0.0

5 years ago