Giter VIP home page Giter VIP logo

bond's Introduction

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

bond's People

Watchers

James Cloos avatar Zachary R. Smith avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.