proxy-bind
v1.2.2
Published
foobar.fn.bind(foobar) -> bind(foobar).fn
Downloads
322
Readme
proxy-bind
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/[email protected]/index.mjs';
</script>
.
<script type="javascript">
(async () => {
const { bind, bond } = await import('https://cdn.jsdelivr.net/npm/[email protected]/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))
- don't
License
the MIT