proxy-bind
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

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';

.

import { bind, bond } from 'https://deno.land/x/proxy_bind/mod.ts';

.

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

Readme

Keywords

Package Sidebar

Install

npm i proxy-bind

Weekly Downloads

61

Version

1.2.2

License

MIT

Unpacked Size

6.13 kB

Total Files

7

Last publish

Collaborators

  • imcotton