Skip to content

Commit 9b5652a

Browse files
committed
flatten() tests
1 parent 24a94fd commit 9b5652a

File tree

3 files changed

+139
-15
lines changed

3 files changed

+139
-15
lines changed

public/js/misc.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function append(arr, x) {
2+
arr[arr.length] = x;
3+
return arr;
4+
}
5+
6+
function flatten(x, rejectSpace, acc) {
7+
acc = acc || [];
8+
if (x == null || x == undefined) {
9+
if (!rejectSpace) {
10+
return append(acc, x);
11+
}
12+
return acc;
13+
}
14+
if (x.length == undefined) { // Just an object, not a string or array.
15+
return append(acc, x);
16+
}
17+
if (rejectSpace &&
18+
((x.length == 0) ||
19+
(typeof(x) == "string" &&
20+
x.match(/^\s*$/)))) {
21+
return acc;
22+
}
23+
if (typeof(x) == "string") {
24+
return append(acc, x);
25+
}
26+
for (var i = 0; i < x.length; i++) {
27+
flatten(x[i], rejectSpace, acc);
28+
}
29+
return acc;
30+
}
31+
32+
function flatstr(x, rejectSpace, joinChar) {
33+
return flatten(x, rejectSpace, []).join(joinChar || '');
34+
}
35+

public/test/test-misc.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2+
<html>
3+
<head>
4+
<script type="text/javascript" src="/js/peg-0.6.1.min.js"></script>
5+
<script type="text/javascript" src="/js/qunit/qunit.js"></script>
6+
<script type="text/javascript" src="/js/misc.js"></script>
7+
<link rel="stylesheet" type="text/css" href="/js/qunit/qunit.css" media="screen">
8+
</head>
9+
<body>
10+
<script type="text/pegjs" id="calc_pegjs">
11+
start
12+
= additive
13+
14+
additive
15+
= left:multiplicative "+" right:additive { return left + right; }
16+
/ multiplicative
17+
18+
multiplicative
19+
= left:primary "*" right:multiplicative { return left * right; }
20+
/ primary
21+
22+
primary
23+
= integer
24+
/ "(" additive:additive ")" { return additive; }
25+
26+
integer "integer"
27+
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
28+
</script>
29+
<script>
30+
module("misc", {
31+
setup: function() {
32+
}
33+
});
34+
35+
test("flatten", 9, function() {
36+
deepEqual(flatten(null), [null]);
37+
deepEqual(flatten(undefined), [undefined]);
38+
deepEqual(flatten([]), []);
39+
deepEqual(flatten([[]]), []);
40+
deepEqual(flatten([null, [null]]), [null, null]);
41+
deepEqual(flatten([1]), [1]);
42+
deepEqual(flatten([1, []]), [1]);
43+
deepEqual(flatten([1,2,3]), [1,2,3]);
44+
deepEqual(flatten([1,{},3]), [1,{},3]);
45+
});
46+
</script>
47+
48+
<div>
49+
<h1 id="qunit-header">Misc Test Suite</h1>
50+
<h2 id="qunit-banner"></h2>
51+
<div id="qunit-testrunner-toolbar"></div>
52+
<h2 id="qunit-userAgent"></h2>
53+
<ol id="qunit-tests"></ol>
54+
<div id="qunit-fixture">test markup</div>
55+
</div>
56+
</body>
57+
</html>

sql.pegjs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@
33
// or with comments have manual edits.
44
//
55
{
6+
function append(arr, x) {
7+
arr[arr.length] = x;
8+
return arr;
9+
}
610
function flatten(x, rejectSpace, acc) {
711
acc = acc || [];
8-
if ((x.length == undefined) || // Just an object, not a string or array.
9-
(!rejectSpace ||
10-
(typeof(x) != "string" ||
11-
!x.match(/^\s*$/)))) {
12-
acc[acc.length] = x;
12+
if (x == null || x == undefined) {
13+
if (!rejectSpace) {
14+
return append(acc, x);
15+
}
16+
return acc;
17+
}
18+
if (x.length == undefined) { // Just an object, not a string or array.
19+
return append(acc, x);
20+
}
21+
if (rejectSpace &&
22+
((x.length == 0) ||
23+
(typeof(x) == "string" &&
24+
x.match(/^\s*$/)))) {
1325
return acc;
1426
}
27+
if (typeof(x) == "string") {
28+
return append(acc, x);
29+
}
1530
for (var i = 0; i < x.length; i++) {
1631
flatten(x[i], rejectSpace, acc);
1732
}
@@ -47,7 +62,8 @@ sql_stmt =
4762
/ update_stmt / update_stmt_limited
4863
// / vacuum_stmt
4964
) )
50-
{ return { explain: flatstr(explain), stmt: stmt } }
65+
{ return { explain: flatstr(explain),
66+
stmt: stmt } }
5167

5268
alter_table_stmt =
5369
( ( ALTER TABLE table_ref )
@@ -202,8 +218,15 @@ literal_value =
202218
/ NULL / CURRENT_TIME / CURRENT_DATE / CURRENT_TIMESTAMP )
203219

204220
numeric_literal =
205-
( ( ( ( digit )+ ( decimal_point ( digit )+ )? )
206-
/ ( decimal_point ( digit )+ ) ) ( E ( plus / minus )? ( digit )+ )? )
221+
digits:( ( ( ( digit )+ ( decimal_point ( digit )+ )? )
222+
/ ( decimal_point ( digit )+ ) )
223+
( E ( plus / minus )? ( digit )+ )? )
224+
{ var x = flatstr(digits);
225+
if (x.indexOf('.') >= 0) {
226+
return parseFloat(x);
227+
}
228+
return parseInt(x);
229+
}
207230

208231
insert_stmt =
209232
( ( ( INSERT ( OR ( ROLLBACK / ABORT / REPLACE / FAIL / IGNORE ) )? )
@@ -247,24 +270,30 @@ select_core =
247270

248271
result_column =
249272
( whitespace
250-
( ( ( call_function
251-
/ column_ref)
252-
( ( AS )? whitespace column_alias )? )
253-
/ ( ( table_name dot )? star ) ) )
273+
( ( c: ( call_function
274+
/ column_ref)
275+
a: ( AS ? whitespace a: column_alias
276+
{ return a })? )
277+
{ return { column: c,
278+
alias: a } }
279+
/ s: ( ( table_name dot )? star )
280+
{ star: s } ) )
254281

255282
join_source =
256283
( single_source ( join_op single_source join_constraint )* )
257284

258285
single_source =
259286
( whitespace
260287
( ( t: ( ( database_name dot )? table_name )
261-
a: ( ( AS ? table_alias )? )
288+
a: ( ( AS ? a: table_alias
289+
{ return a })? )
262290
i: ( ( ( INDEXED BY index_name )
263291
/ ( NOT INDEXED ) )? ) )
264292
{ return { table: flatstr(t, true),
265293
alias: flatstr(a, true),
266294
index: flatstr(i, true) } }
267-
/ ( lparen s: select_stmt rparen a: ( AS ? table_alias )? )
295+
/ ( lparen s: select_stmt rparen a: ( AS ? a: table_alias
296+
{ return a })? )
268297
{ return { select: flatstr(s, true),
269298
alias: flatstr(a, true) } }
270299
/ ( lparen j: join_source rparen )
@@ -363,7 +392,10 @@ digit = [0-9]
363392
decimal_point = dot
364393
equal = '='
365394

366-
name = [A-Za-z0-9_]+
395+
name =
396+
str:[A-Za-z0-9_]+
397+
{ return str.join('') }
398+
367399
database_name = name
368400
table_name = name
369401
table_alias = name

0 commit comments

Comments
 (0)