-
Notifications
You must be signed in to change notification settings - Fork 0
/
underscore.inspector.cof
94 lines (79 loc) · 2.89 KB
/
underscore.inspector.cof
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
###
coffee -cb /opt/node/bb/dev/node_modules/underscore.inspector/underscore.inspector.cof
###
_ = require 'underscore'
_.mixin require 'underscore.string'
# wait for debugger
# node-inspector &
# coffee --nodejs --debug /opt/...
# http://bb.btq.in:8080/debug?port=5858
# rdd -> start dev with debugger
x=1
exports.wait = (cb) ->
console.log 'Node execution is frozen waiting on debugger (in underscore.inspector)'
do recurse = -> if x=1 then setTimeout recurse, 200 else cb()
exports.xTypeof = xTypeof = (v) ->
if _.isUndefined v then return "undefined"
if _.isNull v then return "null"
if _.isBoolean v then return "boolean"
if _.isNumber v then return "number"
if _.isNaN v then return "nan"
if _.isString v then return "string"
if _.isArray v then return "array"
if _.isFunction v then return "function"
if _.isRegExp v then return "regexp"
if _.isArguments v then return "arguments"
if typeof v == 'object' then return 'object'
return 'unknown'
exports.firstType = (list, type) ->
for item in list then if xTypeof(item) == type then return item
null
inspectRecurse = (objList, arg, depth, key = '') ->
str = ''; for i in [0...depth] then str += ' '
if objList.length > 100 then return str + '... (Exceeded 100 object limit)\n'
toa = xTypeof arg
str += key + '<' + toa
switch toa
when "array", "arguments", "object"
objListIdx = _.indexOf(objList, arg)
idx = (if objListIdx != -1 then objListIdx else objList.length)
str += ':' + idx + ':' + (if toa == "object" then _.size(arg) else arg.length) + '>'
if objListIdx != -1 then (return str + ' *duplicate*\n') else str += '\n'
objList.push arg
recurse = (k,v) -> str += inspectRecurse objList, v, depth + 1, k + ': '
if toa == "object" then for own k,v of arg then recurse k,v
else for v,i in arg then recurse i,v
str
when "function"
hdr = str + '>'
hdr + arg.toString().replace(/\s+/g, ' ')[8..100-hdr.length] + '\n'
when "null", "undefined", "nan" then str + '>\n'
else str + '> ' + arg.toString() + '\n'
exports.inspect = inspect = (arg, depth = 0) ->
objList = []
inspectRecurse(objList, arg, depth)[0..-2]
exports.dump = dump = (arg, depth) -> console.log inspect arg, depth
exports.dbg = (args...) ->
if typeof args[0] == 'string'
console.log '-- DBG: ' + args.shift()
for arg in args
dump arg, 1
exports.l = (s) ->
res = "console.log();"
[lbl, s] = s.split ':'
if s
res += "console.log('------ #{lbl} ------');"
else
s = lbl
vars = s.split ','
for v in vars
res += "console.log('#{v}: ' + _.inspect(#{v}));"
res
fs = require 'fs'
atbar = require 'atbar'
exports.log = (path, msg) -> atbar.run 'trace', ->
fd = null; msg = (''+new Date)[0...24] + ' ' + msg + '\n'
@_ -> '_.log'; fs.open path, 'a', @$()
@_ (_throw, d) -> fd = d; fs.write fd, msg, null, null, @$()
@_ (_throw) -> fs.close fd
@_ 'catch', (err) -> console.log 'underscore.inspector.log err: ' + err