-
Notifications
You must be signed in to change notification settings - Fork 75
/
lib.js
163 lines (138 loc) · 4.28 KB
/
lib.js
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
// try to use StringBuffer instead of string concatenation to improve performance
function StringBuffer() {
this.strings = []
for (var idx = 0; idx < arguments.length; idx++)
this.nextPutAll(arguments[idx])
}
StringBuffer.prototype.nextPutAll = function(s) { this.strings.push(s) }
StringBuffer.prototype.contents = function() { return this.strings.join("") }
String.prototype.writeStream = function() { return new StringBuffer(this) }
// make Arrays print themselves sensibly
printOn = function(x, ws) {
if (x === undefined || x === null)
ws.nextPutAll("" + x)
else if (x.constructor === Array) {
ws.nextPutAll("[")
for (var idx = 0; idx < x.length; idx++) {
if (idx > 0)
ws.nextPutAll(", ")
printOn(x[idx], ws)
}
ws.nextPutAll("]")
}
else
ws.nextPutAll(x.toString())
}
Array.prototype.toString = function() { var ws = "".writeStream(); printOn(this, ws); return ws.contents() }
// delegation
objectThatDelegatesTo = function(x, props) {
var f = function() { }
f.prototype = x
var r = new f()
for (var p in props)
if (props.hasOwnProperty(p))
r[p] = props[p]
return r
}
// some reflective stuff
ownPropertyNames = function(x) {
var r = []
for (var name in x)
if (x.hasOwnProperty(name))
r.push(name)
return r
}
isImmutable = function(x) {
return x === null || x === undefined || typeof x === "boolean" || typeof x === "number" || typeof x === "string"
}
String.prototype.digitValue = function() { return this.charCodeAt(0) - "0".charCodeAt(0) }
isSequenceable = function(x) { return typeof x == "string" || x.constructor === Array }
// some functional programming stuff
Array.prototype.delimWith = function(d) {
return this.reduce(
function(xs, x) {
if (xs.length > 0)
xs.push(d)
xs.push(x)
return xs
},
[])
}
// Squeak's ReadStream, kind of
function ReadStream(anArrayOrString) {
this.src = anArrayOrString
this.pos = 0
}
ReadStream.prototype.atEnd = function() { return this.pos >= this.src.length }
ReadStream.prototype.next = function() { return this.src.at(this.pos++) }
// escape characters
String.prototype.pad = function(s, len) {
var r = this
while (r.length < len)
r = s + r
return r
}
escapeStringFor = new Object()
for (var c = 0; c < 128; c++)
escapeStringFor[c] = String.fromCharCode(c)
escapeStringFor["'".charCodeAt(0)] = "\\'"
escapeStringFor['"'.charCodeAt(0)] = '\\"'
escapeStringFor["\\".charCodeAt(0)] = "\\\\"
escapeStringFor["\b".charCodeAt(0)] = "\\b"
escapeStringFor["\f".charCodeAt(0)] = "\\f"
escapeStringFor["\n".charCodeAt(0)] = "\\n"
escapeStringFor["\r".charCodeAt(0)] = "\\r"
escapeStringFor["\t".charCodeAt(0)] = "\\t"
escapeStringFor["\v".charCodeAt(0)] = "\\v"
escapeChar = function(c) {
var charCode = c.charCodeAt(0)
if (charCode < 128)
return escapeStringFor[charCode]
else if (128 <= charCode && charCode < 256)
return "\\x" + charCode.toString(16).pad("0", 2)
else
return "\\u" + charCode.toString(16).pad("0", 4)
}
function unescape(s) {
if (s.charAt(0) == '\\')
switch (s.charAt(1)) {
case "'": return "'"
case '"': return '"'
case '\\': return '\\'
case 'b': return '\b'
case 'f': return '\f'
case 'n': return '\n'
case 'r': return '\r'
case 't': return '\t'
case 'v': return '\v'
case 'x': return String.fromCharCode(parseInt(s.substring(2, 4), 16))
case 'u': return String.fromCharCode(parseInt(s.substring(2, 6), 16))
default: return s.charAt(1)
}
else
return s
}
String.prototype.toProgramString = function() {
var ws = '"'.writeStream()
for (var idx = 0; idx < this.length; idx++)
ws.nextPutAll(escapeChar(this.charAt(idx)))
ws.nextPutAll('"')
return ws.contents()
}
// C-style tempnam function
function tempnam(s) { return (s ? s : "_tmpnam_") + tempnam.n++ }
tempnam.n = 0
// unique tags for objects (useful for making "hash tables")
getTag = (function() {
var numIdx = 0
return function(x) {
if (x === null || x === undefined)
return x
switch (typeof x) {
case "boolean": return x == true ? "Btrue" : "Bfalse"
case "string": return "S" + x
case "number": return "N" + x
default: return x.hasOwnProperty("_id_") ? x._id_ : x._id_ = "R" + numIdx++
}
}
})()