Skip to content
James edited this page May 29, 2017 · 6 revisions

chain-able exports

all exports are available as modular imports as well

import ChainedMap from 'chain-able/ChainedMap'

common interface

these core functions call chain.store, when updating the store, this is returned, otherwise the value is returned

👍👎 the general rule of thumb is:

  • is it a list? use Set
  • otherwise... use Map

⛓ Chainable

  • .parent

  • clear(): Chain clears the store, resets to empty

  • delete(): Chain

  • has(key): bool

  • set(key, val): Chain

  • get(key): any

  • clean(): Array (undefined and null values are removed)

  • values(): [index: value]

  • compose([Classes, Objects]): Chain example

  • end(): Chain.parent

  • when(`condition`, `onTrue`, `onFalse`): Chain - when(`condition`, `onTrue`, `onFalse`): `Chain`

    when the condition is true, trueBrancher is called, else, falseBrancher is called

    const prod = process.env.NODE_ENV === 'production'
    chains.when(prod, c => c.set('prod', true), c => c.set('prod', false))
  • from(arg): Chain
    • a simplified, optimized .merge which works similar, but does not deeply merge objects except when there are properties are chainable instances

    • defaults to existing any existing values or set them initially, for hydrating or transferring (for example, to-from localstorage, to-from webworker)

  • merge(arg): Chain
    const chain = new ChainedMap()
    chain.merge({ehOh: true}) // same as chain.set('ehOh', true)
    chain.entries() === {ehOh: true}
    • in ChainedSet arg is an Iteratable (e.g. Array)
    • in ChainedMap arg is an Object
    • iterate over the object properties: - if there a property matching the key - if it's a chainable instance (e.g. `this.list = new ChainedSet(this)`) - call `.merge` on the instance - if it's a method - if the value is in the store, [tap](tap) the value & deeply merge the value with the existing one - call the method with the value - default to `.set` on the store, merge when existing value

🔢 ChainedSet

extension of Set

  • add(val): Chain
  • prepend(val): Chain
  • merge(Iteratable): Chain
  • values(): Array
  • concat(Iteratable): Chain
  • append(val): Chain

🗺 ChainedMap

extension of Map

  • entries(): {key: value}

  • extend(Array<string>): Chain

class Chained extends Chainable {
  constructor(parent) {
    super(parent)
    this.extend(['eh', 'canada'])
  }
}

// doing the above is the same as
class Chained extends Chainable {
  eh(arg) {
    return this.set('eh', arg)
  }
  canada(arg) {
    return this.set('canada', arg)
  }
}
Clone this wiki locally