Giter Club home page Giter Club logo

cruiser-utils's Introduction

Cruiser Utilities

Often times, the biggest challenge with reduces comes from trying to keep your state immutable. Since actions are just factories that create new reducers to manipulate state, we can use generic utilities to create these reducers for us that will keep our data immutable only on the parts of the tree that have actually changed.

Getting Started

Install via npm:

npm install --save cruiser-utils

Usage

import { createStore } from "cruiser";
import { pushItems } from "cruiser-utils";

/**
 * Step 1 - Create a store with a store with a complex
 *          state model (for example purposes).
 */
var store = createStore({
  todosPage: {
    todos: ["Write Example"],
  },
});

/**
 * Step 2 - Create a reducer that pushes new array items
 *          onto the nested `todos` array.
 */
var addTodos = pushItems((...todos) => ({
  todosPage: {
    todos: todos,
  },
}));

/**
 * Step 3 - Invoke the newly created function and pass it
 *          to our store's `.reduce()` method.
 */
store.reduce(addTodos("Write Better Examples", "Go Outside"));

/**
 * Step 4 - PROFIT.  The result should contain the nested
 *          `todos` array with the values:
 *
 *          ["Write Example", "Write Better Examples", "Go Outside"]
 */
console.log(store.getState());

Functions

Reduce Nodes

Invoke the "micro-reducers" on each branch of the returned patched object and set the branch's value to whatever value was returned by the "micro-reducer".

import { reduceNodes } from "cruiser-utils";

var toggleLoader = reduceNodes(function () {
  return { loader: (current) => !current };
});

store.reduce(toggleLoader());

Push Array Items

Merge found array values into the arrays found at the same branch locations.

import { pushItems } from "cruiser-utils";

var addTodo = pushItems(function (newTodo) {
  return { todos: [newTodo] };
});

store.reduce(addTodo("Push value onto todos"));

Remove Array Items

Remove found values from the arrays found at the same branch locations.

import { removeItem } from "cruiser-utils";

var completeTodos = removeItem(function (removeTodo) {
  return { todos: removeTodo };
});

store.reduce(completeTodos("Delete any todo with this value"));

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.