liquid-node
Version:
Node.js port of Tobias Lütke's Liquid template engine.
49 lines (37 loc) • 1 kB
text/coffeescript
Range = require "./range"
isString = (input) ->
Object::toString.call(input) is "[object String]"
module.exports = class Iterable
first: ->
(0, 1).then (a) -> a[0]
map: ->
args = arguments
().then (a) ->
Promise.all a.map args...
sort: ->
args = arguments
().then (a) ->
a.sort args...
toArray: ->
0
slice: ->
throw new Error "#{@constructor.name}.slice() not implemented"
last: ->
throw new Error "#{@constructor.name}.last() not implemented"
: (v) ->
if v instanceof Iterable
v
else if v instanceof Range
new IterableForArray v.toArray()
else if Array.isArray(v) or isString(v)
new IterableForArray v
else if v?
new IterableForArray [v]
else
new IterableForArray []
class IterableForArray extends Iterable
constructor: () ->
slice: ->
Promise.resolve .slice arguments...
last: ->
Promise.resolve [.length - 1]