Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81159 views
1
var util = require('util')
2
3
var INDENT_START = /[\{\[]/
4
var INDENT_END = /[\}\]]/
5
6
module.exports = function() {
7
var lines = []
8
var indent = 0
9
10
var push = function(str) {
11
var spaces = ''
12
while (spaces.length < indent*2) spaces += ' '
13
lines.push(spaces+str)
14
}
15
16
var line = function(fmt) {
17
if (!fmt) return line
18
19
if (INDENT_END.test(fmt.trim()[0]) && INDENT_START.test(fmt[fmt.length-1])) {
20
indent--
21
push(util.format.apply(util, arguments))
22
indent++
23
return line
24
}
25
if (INDENT_START.test(fmt[fmt.length-1])) {
26
push(util.format.apply(util, arguments))
27
indent++
28
return line
29
}
30
if (INDENT_END.test(fmt.trim()[0])) {
31
indent--
32
push(util.format.apply(util, arguments))
33
return line
34
}
35
36
push(util.format.apply(util, arguments))
37
return line
38
}
39
40
line.toString = function() {
41
return lines.join('\n')
42
}
43
44
line.toFunction = function(scope) {
45
var src = 'return ('+line.toString()+')'
46
47
var keys = Object.keys(scope || {}).map(function(key) {
48
return key
49
})
50
51
var vals = keys.map(function(key) {
52
return scope[key]
53
})
54
55
return Function.apply(null, keys.concat(src)).apply(null, vals)
56
}
57
58
if (arguments.length) line.apply(null, arguments)
59
60
return line
61
}
62
63