-
Notifications
You must be signed in to change notification settings - Fork 0
/
crockford.js
146 lines (133 loc) · 4.06 KB
/
crockford.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
// http://javascript.crockford.com/remedial.html
'use strict';
/*jslint indent: 4 */
// In the source code that follows, the priorities were portability and
// compactness.
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (value instanceof Array) {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
/* The typeOf function above will only recognize arrays that are created in the
same context (or window or frame). If we want to recognize arrays that are
constructed in a different frame, then we need to do something more
complicated.
*/
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (Object.prototype.toString.call(value) == '[object Array]') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
/* isEmpty(v) returns true if v is an object containing no enumerable members.
*/
function isEmpty(o) {
var i, v;
if (typeOf(o) === 'object') {
for (i in o) {
v = o[i];
if (v !== undefined && typeOf(v) !== 'function') {
return false;
}
}
}
return true;
}
/* entityify() produces a string in which '<', '>', and '&' are replaced with
their HTML entity equivalents. This is essential for placing arbitrary
strings into HTML texts. So,
"if (a < b && b > c) {".entityify()
produces
"if (a < b && b > c) {"
*/
if (!String.prototype.entityify) {
String.prototype.entityify = function () {
return this.replace(/&/g, "&").replace(/</g,
"<").replace(/>/g, ">");
};
}
/* quote() produces a quoted string. This method returns a string that is like
the original string except that it is wrapped in quotes and all quote and
backslash characters are preceded with backslash.
*/
if (!String.prototype.quote) {
String.prototype.quote = function () {
var c, i, l = this.length, o = '"';
for (i = 0; i < l; i += 1) {
c = this.charAt(i);
if (c >= ' ') {
if (c === '\\' || c === '"') {
o += '\\';
}
o += c;
} else {
switch (c) {
case '\b':
o += '\\b';
break;
case '\f':
o += '\\f';
break;
case '\n':
o += '\\n';
break;
case '\r':
o += '\\r';
break;
case '\t':
o += '\\t';
break;
default:
c = c.charCodeAt();
o += '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}
}
}
return o + '"';
};
}
/*
supplant() does variable substitution on the string. It scans through the string
looking for expressions enclosed in { } braces. If an expression is found, use
it as a key on the object, and if the key has a string value or number value, it
is substituted for the bracket expression and it repeats. This is useful for
automatically fixing URLs. So
param = {domain: 'valvion.com', media: 'http://media.valvion.com/'};
url = "{media}logo.gif".supplant(param);
produces a url containing "http://media.valvion.com/logo.gif".
*/
if (!String.prototype.supplant) {
String.prototype.supplant = function (o) {
return this.replace(
/\{([^{}]*)\}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
}
/* The trim() method removes whitespace characters from the beginning and end of
the string.
*/
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1");
};
}