0.0.33 • Published 4 years ago

imj v0.0.33

Weekly downloads
34
License
ISC
Repository
github
Last release
4 years ago

imj

Not fastest but powerful immutable helper

Simple update

import imj from "imj";

const Increase = imj({
  count: ({ value }) => value + 1
});
const Decrease = imj({
  count: ({ value }) => value - 1
});
Increase({ count: 1 }); // { count: 2 }
Decrease({ count: 1 }); // { count: 0 }

Retrieve argument values

import imj from "imj";

const Increase = imj({
  count: ({ value, $1 = 1 }) => value + $1
});

Increase({ count: 1 }); // { count: 2 }
Increase({ count: 1 }, 2); // { count: 3 }

Update array

import imj from "imj";

const AddTodo = imj({
  // map second argument to $text and the third to $done
  $args: "$text $done",
  todos: ({ $text, $done, push }) =>
    // push new item to todos array
    push({ text: $text, done: $done })
});

AddTodo({ todos: [{ text: "first", done: false }] }, "second", true);
// { todos: [ { text: 'first', done: false }, { text: 'second', done: true } ] }

Update specified items in array

import imj from "imj";

const ToggleDoneSpecs = { done: ({ toggle }) => toggle() };

const ToggleTodoByIndex = imj({
  $args: "$index",
  // create custom specs
  $extend: ({ $index }) => ({
    todos: {
      // toggle done property of specified item ($index)
      [$index]: ToggleDoneSpecs
    }
  })
});

const ToggleOneTodoByText = imj({
  $args: "$text",
  todos: {
    // update first match only
    $one: ({ value, $text }) =>
      value.text === $text
        ? // return specs for matched item
          ToggleDoneSpecs
        : // unless do nothing
          null
  }
});

const ToggleAllTodoByText = imj({
  $args: "$text",
  todos: {
    // update all
    $many: ({ value, $text }) =>
      value.text === $text
        ? // return specs for matched item
          ToggleDoneSpecs
        : // unless do nothing
          null
  }
});

const ToggleAll = imj({
  todos: {
    $many: ToggleDoneSpecs
  }
});

Simple redux reducer

import imj from "imj";

const IncreaseAction = 1;
const DecreaseAction = 2;
const reducer = imj({
  $when: [
    "$1.type",
    {
      [IncreaseAction]: {
        count: ({ value, $1: { payload = 1 } }) => value + payload
      },
      [DecreaseAction]: {
        count: ({ value, $1: { payload = 1 } }) => value - payload
      }
    }
  ]
});
reducer({ count: 1 }, { type: IncreaseAction }); // { count: 2 }
reducer({ count: 1 }, { type: IncreaseAction, payload: 2 }); // { count: 3 }
reducer({ count: 1 }, { type: DecreaseAction }); // { count: 0 }
reducer({ count: 1 }, { type: DecreaseAction, payload: 2 }); // { count: -1 }

Other implementation

import imj from "imj";

const IncreaseAction = 1;
const DecreaseAction = 2;
const reducer = imj({
  // define named $when
  $when_increase: [
    "$1.type",
    IncreaseAction,
    {
      count: ({ value, $1: { payload = 1 } }) => value + payload
    }
  ],
  $when_decrease: [
    "$1.type",
    DecreaseAction,
    {
      count: ({ value, $1: { payload = 1 } }) => value - payload
    }
  ]
});
0.0.33

4 years ago

0.0.30

4 years ago

0.0.31

4 years ago

0.0.32

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.27

4 years ago

0.0.26

4 years ago

0.0.25

4 years ago

0.0.24

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago