Chain spellbook methods:

This method is usable from 0.1.x version.

sb.chain([{ a: 'asdasd' }, { a: 35 }])
.reverse()
.toString()
.toJSON()
.filterBy({ a : 35 })
.first()
.repeatify(5)
.size()
.repeatify(10)
.toString()
.json()
.get('2')
.duplicate()
.repeatify(2)
.map(x => x + 1)
.contains(11)
.return()

-> true

Can chain any method from Spellbook

sb.chain([{ a: 'hi' }, { a: 35 }])
.first()
.get('a')
.toUpperCase()
.return()

-> "HI"

For return a value can use method "value()", "return()", "v()" or "r()"

sb.chain(100)
.numerator(2)
.mult(1002)
.toString()
.size()
.divide(2)
.sum(6)
.toString()
.split('.')
.map(sb.toInt)
.max()
.value()

-> 8

Playing with chain

sb.chain([{ a: 'asdasd' }, { a: 35 }])
.reverse()
.toString()
.toJSON()
.filterBy({ a: 35 })
.first()
.repeatify(5)
.size()
.repeatify(10)
.toString()
.json()
.get('2')
.duplicate()
.repeatify(2)
.map(x => x + 1)
.value()

-> [11,11]

Iteration asyncronous chain

sb.chain([{ a: 10 }, { a: 3 }])
.toString()
.toJSON()
.filterBy({ a: 3 })
.first()
.repeatify(5)
.size()
.repeatify(10)
.toString()
.json()
.get('2')
.duplicate()
.repeatify(3)
.each((x, index, done, end) => {
  setTimeout(() => {
    console.log(`Index: ${index} -> ${x}`)
    done()
  }, index * 1000)
})

-> Index: 0 -> 10 -> Index: 1 -> 10 -> Index: 2 -> 10

Chaining comparision and functions

sb.chain(sb.range(1,5))
.size()
.isNumber()
.ifElse(
 () => "hello",
 () => "bye"
 )
.function(x => `Res: ${x}`)
.value()

-> "Res: hello"

Make a normal event

sb.on('evrec', console.log)

Make an event from chain

sb.chain('message')
.on('evrec2', (x, y) => {
 console.log(`${y} : ${x}`)
})
.value()

-> "message"

Emit from chain

sb.chain([{ a: 'asdasd' }, { a: 35 }])
.reverse()
.toString()
.toJSON()
.filterBy({ a: 35 })
.first()
.repeatify(5)
.size()
.repeatify(10)
.toString()
.json()
.get('2')
.duplicate()
.repeatify(2)
.map(x => x + 1)
.emit('evrec')
.value()

-> Rec: 11 -> [11,11]

Emit asyncronous events

sb.chain([{ a: 'asdasd' }, { a: 35 }])
.each((item, index, next, end) => {
  setTimeout(() => {
    sb.emit('evrec2', item)
    done(item)
  }, 1000)
},
res => {
  console.log(res)
})

results matching ""

    No results matching ""