rcreasi / RemuteJS

RemuteJS is a tool to produce new immutable object from the previous one using ES6 arrow functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RemuteJS

RemuteJS is TypeScript/JavaScript tool to produce new immutable object from the previous one using ES6 arrow functions.

It is to simplify and reduce amount of boilerplate code working with immutable objects, like produce state in React application.

state = remute(state, x => x.foo.bar, "new value");

RemuteJS exports remute function that accepts

  1. current state
  2. arrow function that is a navigation property pointing to the property you are going to change
  3. new value

New state has updated references for all the objects involved in arrow function. The rest of the references remain untouched.

Obviously using arrow functions along with TypeScript gives you the best coding experience including generics and refactoring features. Alt text

RemuteJS is written using TypeScript and by default is transpiled to ES6 but it works just fine being transpiled to ES5.

It has 0 dependencies and just 75 lines.

Basic example

const expected = {
    firstName: "John",
    lastName: "Doe"
};

const actual = remute(expected, x => x.firstName, "Foo");

console.assert(expected != actual);
console.assert(expected.firstName != actual.firstName);
console.assert(expected.lastName == actual.lastName);
console.assert("Foo" == actual.firstName);
console.assert("Doe" == actual.lastName);

About

RemuteJS is a tool to produce new immutable object from the previous one using ES6 arrow functions


Languages

Language:TypeScript 50.8%Language:JavaScript 48.2%Language:HTML 1.1%