UNPKG

2.98 kBtext/coffeescriptView Raw
1Handlebars.registerHelper 'first', (array, count) ->
2 if Utils.isUndefined(count) then array[0] else array.slice 0, count
3
4Handlebars.registerHelper 'withFirst', (array, count, options) ->
5 if Utils.isUndefined count
6 options = count
7 options.fn array[0]
8 else
9 array = array.slice 0, count
10 result = ''
11 for item of array then result += options.fn array[item]
12 result
13
14Handlebars.registerHelper 'last', (array, count) ->
15 if Utils.isUndefined(count) then array[array.length - 1] else array.slice -count
16
17Handlebars.registerHelper 'withLast', (array, count, options) ->
18 if Utils.isUndefined count
19 options = count
20 options.fn array[array.length - 1]
21 else
22 array = array.slice -count
23 result = ''
24 for item of array then result += options.fn array[item]
25 result
26
27Handlebars.registerHelper 'after', (array, count) ->
28 array.slice count
29
30Handlebars.registerHelper 'withAfter', (array, count, options) ->
31 array = array.slice count
32 result = ''
33 for item of array then result += options.fn array[item]
34 result
35
36Handlebars.registerHelper 'before', (array, count) ->
37 array.slice 0, -count
38
39Handlebars.registerHelper 'withBefore', (array, count, options) ->
40 array = array.slice 0, -count
41 result = ''
42 for item of array then result += options.fn array[item]
43 result
44
45Handlebars.registerHelper 'join', (array, separator) ->
46 array.join if Utils.isUndefined(separator) then ' ' else separator
47
48Handlebars.registerHelper 'sort', (array, field) ->
49 if Utils.isUndefined field
50 array.sort()
51 else
52 array.sort (a, b) -> a[field] > b[field]
53
54Handlebars.registerHelper 'withSort', (array, field, options) ->
55 result = ''
56
57 if Utils.isUndefined field
58 options = field
59 array = array.sort()
60 result += options.fn(item) for item in array
61 else
62 array = array.sort (a, b) -> a[field] > b[field]
63 result += options.fn(array[item]) for item of array
64
65 result
66
67Handlebars.registerHelper 'length', (array) ->
68 array.length
69
70Handlebars.registerHelper 'lengthEqual', (array, length, options) ->
71 if array.length is length then options.fn(@) else options.inverse(@)
72
73Handlebars.registerHelper 'empty', (array, options) ->
74 if array.length <= 0 then options.fn(@) else options.inverse(@)
75
76Handlebars.registerHelper 'any', (array, options) ->
77 if array.length > 0 then options.fn(@) else options.inverse(@)
78
79Handlebars.registerHelper 'inArray', (array, value, options) ->
80 if array.indexOf(value) isnt -1 then options.fn(@) else options.inverse(@)
81
82Handlebars.registerHelper 'eachIndex', (array, options) ->
83 result = ''
84
85 for value, index in array
86 result += options.fn item: value, index: index
87
88 result
89
90Handlebars.registerHelper 'eachProperty', (obj, options) ->
91 result = ''
92
93 for key, value of obj
94 result += options.fn key: key, value: value
95
96 result