with-iterator

node v8.17.0
version: 2.0.0
endpointsharetweet
const { isIterable, getIterator, withIterator, valueOf } = require('with-iterator@2.0.0')
isIterable => ( a: any ) : boolean
const o = { '1. array': isIterable([]), '2. string': isIterable(''), '3. object': isIterable(withIterator({})), '4. withIterator(number)': isIterable(withIterator(42)), '5. withIterator(null)': isIterable(withIterator(null)) }
withIterator => ( factory: function, a: any ) : object
const sum = function* allThatCameBefore() { let i = 0 let n = 0 while (i < this.length) { yield (n += this[i]) i++ } } const accumulated = withIterator(sum, [1, 2, 3, 4, 5]) Array.from(accumulated)
withIterator => ( factory: function ) : function => ( a: any ) : object
const hex = ['123456', 'cc9933', 'fedcba'] const rgb = withIterator(function*() { const re = /../g let next while ((next = re.exec(this))) yield Number.parseInt(next, 16) }) hex.map(rgb).map(code => `hex: ${code} - rgb: (${[...code]})` )
using prototypes
class Digits extends Number {} withIterator( function*(){ for(let d of getIterator(String).call(this)) yield Number(d) }, Digits.prototype ) const digits = new Digits(54321) console.log(digits) Array.from(digits)
explicitly resolving boxed values
const nul = withIterator(null) const und = withIterator(undefined) const num = withIterator(42) const m = new Map([ [nul, valueOf(nul)], [und, valueOf(und)], [num, valueOf(num)] ])
Loading…

no comments

    sign in to comment