🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

assign-args

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assign-args

A funner Function.prototype.bind

1.0.0
latest
Source
npm
Version published
Maintainers
1
Created
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

function

FAQs

Package last updated on 01 Feb 2018

Did you know?

Socket

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