API Reference

Top-level Exports

  • deduce(rootReducer)

  • D.[ACTION METHOD]({ configuration })

deduce

D.[ACTION METHOD]_[ | IN | ALL]

Primitive Actions

SET

Replace the root value in the state-tree.

Works on: Strings, Numbers, Booleans,

// STATE = ""

D.SET({ "value": "Ash Ketchum" })

// STATE = "Ash Ketchum"
// STATE = 0

D.SET({ "value": 4 })

// STATE = 4
// STATE = false

D.SET({ "value": true })

// STATE = true

Objects, Arrays

INCREMENT

// STATE = 1

D.INCREMENT({ "value": 1 })

// STATE = 2

DECREMENT

// STATE = 1

D.INCREMENT({ "value": 1 })

// STATE = 0

SET_ALL - Objects, Arrays

Arrays

// STATE = ["0", "0", "0"]

D.SET_ALL({ "value": "1" })

// STATE = ["1", "1", "1"]
// STATE = [1, 2, 3]

D.SET_ALL({ "value": 3 })

// STATE = [3, 3, 3]
// STATE = [false, false, false]

D.SET_ALL({ "value": true })

// STATE = [true, true, true]

Objects

/* 
STATE = {
  "Pokemon1": "dead",
  "Pokemon2": "dead",
  "Pokemon3": "dead",
}
*/

D.SET_ALL({ "value": "alive" })

/* 
STATE = {
  "Pokemon1": "alive",
  "Pokemon2": "alive",
  "Pokemon3": "alive",
}
*/
/* 
STATE = {
  "Score1": 300,
  "Score2": 400,
  "Score3": 200,
}
*/

D.SET_ALL({ "value": 0 })

/* 
STATE = {
  "Pokemon1": 0,
  "Pokemon2": 0,
  "Pokemon3": 0,
}
*/
/* 
STATE = {
  "Pokemon1": false,
  "Pokemon2": false,
  "Pokemon3": false,
}
*/

D.SET_ALL({ "value": true })

/* 
STATE = {
  "Pokemon1": "alive",
  "Pokemon2": "alive",
  "Pokemon3": "alive",
}
*/

TOGGLE

// STATE = true

D.TOGGLE({ "value": false })

// STATE = false

INSERT

// STATE = ["Pickachu", "Bulbasaur"]

D.INSERT({ "value": "Squirtle", "index": 2 })

// STATE = ["Pickachu", "Bulbasaur", "Squirtle"]
// STATE = [ 100, 500 ]

D.INSERT({ "value": 400, "index": 2 })

// STATE = [ 100, 500, 400 ]
// STATE = [ false, true ]

D.INSERT({ "value": true, "index": 2 })

// STATE = [ false, true, true ]

REMOVE

// TO BE IMPLEMENTED

MERGE

/*
STATE = {
    "Ash": "Online"
}
*/

D.MERGE({ "value": { "Mindy": "Online" } })

/*
STATE = {
    "Ash": "Online",
    "Mindy": "Online"
}
*/

CONCAT

/*
STATE = [ "Ash" ]
*/

D.CONCAT({ "value": [ "Mindy" ] })

/*
STATE = [ "Ash", "Mindy" ]
*/

Using IN and ALL

SET_IN

// STATE = { }

D.SET_IN({ "trainer": "Ash Ketchum" })
D.SET_IN({ "badges": 4 })
D.SET_IN({ "isBattling": true })

/* 
STATE = {
     "trainer": "Ash Ketchum",
     "badges": 4,
     "isBattling": true
}
*/

INCREMENT_IN

// STATE = { "pokemon": 3 }

D.INCREMENT_IN({ "path": "POKEMON", "value": 1})

// STATE = { "pokemon": 4 }

INCREMENT_ALL

// STATE = [ 200, 400 ]

D.INCREMENT_ALL({ "value": 100 })

// STATE = [ 300, 500 ]

DECREMENT_IN

// STATE = { "pokemon": 4 }

D.INCREMENT_IN({ "path": "POKEMON", "value": 1})

// STATE = { "pokemon": 3 }

DECREMENT_ALL

// STATE = [ 200, 400 ]

D.INCREMENT_ALL({ "value": 100 })

// STATE = [ 300, 500 ]

TOGGLE_IN

// STATE = { "caught": false }

D.TOGGLE_IN({ "path": "CAUGHT" })

// STATE = { "caught": true }

TOGGLE_ALL

// STATE = [ false, false, false ]

D.TOGGLE_IN({})

// STATE = [ true, true, true ]

ADD_IN

// STATE = { "trainer": {} }

D.ADD_IN({ "path": "TRAINER", "value": { "badges": 5 } })

// STATE

ADD

// STATE = [ "Mew", "Geodude", ]

D.ADD({ "value": "Pickachu" })

// STATE = [ "Mew", "Geodude", "Pickachu" ]

INSERT_IN

// STATE = { "pokemon": ["Pickachu", "Bulbasaur"] }

D.INSERT_IN({ "value": "Squirtle", "index": 2, "path": "POKEMON" })

// STATE = { "pokemon": ["Pickachu", "Bulbasaur", "Squirtle"] }
// STATE = { "scores": [ 100, 500 ] }

D.INSERT_IN({ "value": 400, "index": 2, "path": "" })

// STATE = [ 100, 500, 400 ]
// STATE = [ false, true ]

D.INSERT_IN({ "value": true, "index": 2, "path": "POKEMON" })

// STATE = [ false, true, true ]

UPDATE_IN

// TO BE IMPLEMENT

REMOVE_IN

// STATE = [ "Wombat", "Golbat", "Geodude" ]

D.REMOVE_IN({ 
  "where": (v) => v == "Golbat"
})

// STATE = [ "Wombat", "Geodude" ]

REMOVE_ALL

// STATE = [ "Wombat", "Golbat", "Geodude" ]

D.REMOVE_ALL({ })

// STATE = [ ]

MERGE_IN

/*
STATE = {
  "Ash": {
    "pokemon": []
    },
  "Mindy": {
    "pokemon": [],
    }
}
*/

D.MERGE_IN({ "key": "Ash", "value": { "playing": true } })

/*
STATE = {
  "Ash": {
    "pokemon": [],
    "playing": true
    },
  "Mindy": {
    "pokemon": [],
    }
}
*/

MERGE_ALL

/*
STATE = {
  "Ash": {
    "pokemon": []
  },
  "Mindy": {
    "pokemon": []
  }
}
*/

D.MERGE_ALL({ "value": { "playing": true } })

/*
STATE = {
  "Ash": {
    "pokemon": [],
    "playing": true
  },
  "Mindy": {
    "pokemon": [],
    "playing": true
  }
}
*/

Last updated