-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelpers.coffee
83 lines (70 loc) · 1.98 KB
/
helpers.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class FlashMessage
constructor: (@type, @messages) ->
if typeof @messages == 'string'
@messages = [@messages]
icon: () ->
switch @type
when 'info' then 'ui-icon-info'
when 'error' then 'ui-icon-alert'
stateClass: () ->
switch @type
when 'info' then 'ui-state-highlight'
when 'error' then 'ui-state-error'
toHTML: () ->
'<div class="ui-widget flash">' +
'<div class="' + @stateClass() + ' ui-corner-all">' +
'<p><span class="ui-icon ' + @icon() + '"></span>' + @messages.join(', ') + '</p>' +
'</div></div>'
exports.dynamicHelpers =
flashMessages: (req, res) ->
html = ''
['error', 'info'].forEach (type) ->
messages = req.flash type
if messages.length > 0
html += new FlashMessage(type, messages).toHTML()
return html
exports.helpers = (app, options) ->
return {
appName: 'Valtra'
version: '0.0.1'
nameAndVersion: (name, version) ->
name + ' v' + version
truncate: (str, num) ->
limit = num || 20
if str && str.length > limit
return str.slice(0, limit) + '...'
else
return str
dateFormat: require('dateformat')
}
exports.range = (low, high, step) ->
matrix = []
walker = 1 unless step
chars = false
inival = endval = plus = null
if !isNaN(low) && !isNaN(high)
inival = low
endval = high
else if isNaN(low) && isNaN(high)
chars = true
inival = low.charCodeAt(0)
endval = high.charCodeAt(0)
else
inival = (isNaN(low) ? 0 : low)
endval = (isNaN(high) ? 0 : high)
plus = !(inival > endval)
if plus
tmp = while inival <= endval
if chars
matrix.push(String.fromCharCode(inival))
else
matrix.push(inival)
inival += walker
else
tmp = while inival >= endval
if chars
matrix.push(String.fromCharCode(inival))
else
matrix.push(inival)
inival -= walker
return matrix