Socket
Socket
Sign inDemoInstall

assign-args

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    assign-args

A funner Function.prototype.bind


Version published
Weekly downloads
1
Maintainers
1
Install size
87.4 kB
Created
Weekly downloads
 

Readme

Source

Assign Args

Like Function.prototype.bind, but let's you assign farther-right arguments first, incrementally assign properties on an object argument, and overwrite previously assigned arguments.

Installation

$ npm install assign-args

Usage

// How to require:
const assign = require('assign-args');
test('assigning farther-right arguments first', () => {
  const y = (m, x, b) => m*x + b;
  const oneArg = assign(y, undefined, undefined, 3);
  const twoArgs = assign(oneArg, undefined, 2);

  expect(twoArgs(1)).toBe(5);
});

test('incrementally assigning properties on an object argument', () => {
  const y = ({ m, x }, b) => m*x + b;
  const oneArg = assign(y, undefined, 3);
  const twoArgs = assign(oneArg, { x: 2 });

  expect(twoArgs({ m: 1 })).toBe(5);
});

test('overwriting previously assigned args', () => {
  const y = ({ m, x }, b) => m*x + b;
  const withArgs = assign(y, { m: 2, x: 2 }, 5);
  expect(withArgs()).toBe(9);
  const withArgsRedone = assign(withArgs, { m: 3 }, 10);

  expect(withArgsRedone()).toBe(16);
});

Binding context/this still works:

const getFoo = function(...args) {
  return this.foo + args.join('');
};

test('binding context/this before', () => {
  const withCtx = getFoo.bind({ foo: 'bar' });
  const withCtxAndArg = assign(withCtx, 1);

  expect(withCtxAndArg()).toBe('bar1');
});

test('binding context/this between', () => {
  const withArg = assign(getFoo, undefined, 2);
  const withCtxAndArg = withArg.bind({ foo: 'bar' });
  const withCtxAnd2Args = assign(withCtxAndArg, 1);

  expect(withCtxAnd2Args()).toBe('bar12');
})

test('binding contet/this after', () => {
  const withArg = assign(getFoo, 1);
  const withCtxAndArg = withArg.bind({ foo: 'bar' });

  expect(withCtxAndArg()).toBe('bar1');
});

Keywords

FAQs

Last updated on 01 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc