JSPM

  • Created
  • Published
  • 0
  • Score
    100M100P100Q41173F
  • License ISC

Not fastest but powerful immutable helper

Package Exports

  • imj

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (imj) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

imj

Not fastest but powerful immutable helper

Simple reducer

import imj from "imj";

const reducer = imj({
  counter: ({ value }) => value + 1
});

Reducer with input arguments

import imj from "imj";

const actionTypes = {
  increase: 1,
  decrease: 2
};
// using $when and return individual specs for each action type
// can specific specs to update multiple props at once
const reducer1 = imj({
  // indicate that reducer retrieves 2 arguments, the first one is current target that need to update, the second one is 'action'
  $args: "action",
  $when_increase: [
    "action.type",
    actionTypes.increase,
    { count: ({ value }) => value + 1 }
  ],
  $when_decrease: [
    "action.type",
    actionTypes.decrease,
    { count: ({ value }) => value - 1 }
  ]
});
// using normal updater and check action.type before return approx value
// this works with only prop
const reducer2 = imj({
  $args: "action",
  count: ({ value, action }) =>
    action.type === actionTypes.increase ? value + 1 : value - 1
});