-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.coffee
206 lines (168 loc) · 5.45 KB
/
utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
GLOBAL = exports ? this
util = {}
###*
* Special handling for
*
* RegExp - converts it to a string literal, otherwise it will be decoded
* as object "{}" and the regular expression would be lost.
*
* Array - print all values on a single line
*
###
util.adjustLiteral = (key, value) ->
if key is 'value' and _.isRegExp value
value.toString()
else if _.isArray(value) and _.all(value, (v) -> not _.isObject v)
'[' + value.join(' ') + ']'
else
value
util.printJSON = (json) ->
str = JSON.stringify json, util.adjustLiteral, 4
str.replace(/['"]/g, '')
###*
* Convert codeMirror location object to 0 based index for lines
* @param {Location Object} loc has both 'line' and 'ch' positions.
* @return {Location Object} normalized location.
###
util.convertLoc = (loc) ->
line: loc.line - 1
column: loc.ch || loc.column
###*
* Construct a CodeMirror Location object
* @param {int} line line number
* @param {int} ch column number
* @return {location} location object
###
util.toLoc = (line, ch) ->
line: line
ch: ch
###*
* Test to see if a position is within a codeMirror Range array
* @param {int} pos position.
* @param {array} range (range[0], range[1]).
* @return {boolean} true if pos is within range, false otherwise.
###
util.withinRange = (pos, range) ->
range? and range.length is 2 and range[0] <= pos <= range[1]
###*
* Number to hex function
* @param {int} num input.
* @param {int} length optional length of output. Default to the full length of the input.
* @return {string} hex number string.
###
util.toHex = (num, length) ->
hex = Math.floor(num).toString(16)
length ?= hex.length
# Pad output
for i in [0...length - hex.length]
hex = "0" + hex
hex.slice(-length)
###*
* Map a value [0,1] to [min, max]
* @param {float} value a value between 0 and 1 inclusive.
* @param {float} min lowerbound.
* @param {float} max upperbound.
* @return {float} mapped value.
###
util.mapValue = (value, min, max) ->
range = max - min
value * range + min
###*
* Insert helper functions on AST nodes
* @param {AST Node} node AST generated by esprima
* @param {AST Node} parent AST generated by esprima
* @param {array} chunks chunks array of source code
* @param {int} depth scope depth
###
util.insertHelpers = (node, parent, chunks, depth) ->
return unless node.range
node.depth = depth
node.parent = parent
node.source = ->
chunks[node.range[0]...node.range[1]].join('')
node.updateSource = (s) ->
chunks[node.range[0]] = s
chunks[i] = '' for i in [(node.range[0] + 1)...node.range[1]]
s
node.insertBefore = (s) ->
chunks[node.range[0]] = s + '\n' + chunks[node.range[0]]
s
node.insertAfter = (s) ->
endIndex = node.range[1] - 1
chunks[endIndex] = chunks[endIndex] + '\n' + s
s
node
###*
* Traverse AST
* @param {AST} ast Generated by esprima
* @param {array} chunks chunks array of source code
* @param {function} prefunc function called on children then parent
* @param {function} postfunc function called on parent and then children
###
util.traverse = (ast, chunks, prefunc, postfunc) ->
walk = (node, parent, depth = 0) =>
postfunc.call(@, node, parent, chunks, depth) if postfunc?
_.each node, (child, key) =>
return if key in ['parent', 'range', 'loc']
if _.isArray(child)
_.each child, (grandchild) ->
walk(grandchild, node, depth + 1) if grandchild and typeof grandchild.type is 'string'
else if child? and typeof child.type is 'string'
postfunc.call(@, child, node, chunks, depth) if postfunc?
walk(child, node, depth)
prefunc.call(@, node, parent, chunks) if prefunc?
walk(ast)
util.scopeLookup = (node, scopes) ->
itr = node
while itr?
scope = _.find scopes, (value, key) ->
return value.node == itr
break if scope?
itr = itr.parent
scope or scopes['global']
util.scopeName = (name, scope) ->
namespace = if scope.name is 'global' then '' else scope.name + '.'
namespace + name
util.unscopeName = (name) ->
name.split('.').slice(-1)[0]
util.objGet = (obj, path) ->
segments = path.split '.'
while segments.length and obj?
obj = obj[segments.shift()]
obj
util.objSet = (obj, path, value) ->
segments = path.split '.'
while segments.length > 1
segment = segments.shift()
obj[segment] = obj[segment] or {}
obj = obj[segment]
obj[segments[0]] = value
allSame = (list) ->
return _.all list, (item) -> item is list[0]
formatValWithIndent = (indent, depth) ->
(values, name) ->
line = indent + ' '
if _.isArray values
if allSame values
line += name + ':\t' + JSON.stringify values[0]
else
vals = _.map values, (value) -> JSON.stringify value
line += name + ':\t' + vals.join ' | '
else if _.isObject values
line += name + ':\t' + util.formatVarJSON(values, depth + 1)
else
line += name + ':\t' + values
line
util.formatVal = formatValWithIndent('', 0)
util.formatVarJSON = (obj, depth = 0) ->
indent = (' ' for i in [0..depth]).join ''
content = _.map obj, formatValWithIndent(indent, depth)
'{\n' + content.join(',\n') + "\n#{indent}}"
util.getParameterByName = (name) ->
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]')
regexS = "[\\?&]#{name}=([^&#]*)"
regex = new RegExp(regexS)
results = regex.exec(window.location.search)
if results?
decodeURIComponent results[1].replace(/\+/g, ' ')
GLOBAL.util = util