Skip to content

Commit 317aaf0

Browse files
committed
Implement wrapper code compatibility
1 parent dbda15b commit 317aaf0

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

doc/examples/wrap.js-var.ceson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var config = {
22
"wrapped": true,
3-
"style": "js-var",
3+
"style": "js var",
44
};

parse.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,33 @@
3030
}
3131

3232

33+
function wrapperCodeCompatAtEndOfLine(tx) {
34+
return tx.replace(/[\);]+(\n\s*|)$/, '$1');
35+
}
36+
37+
38+
function extractLineText(ln, lnIdx) {
39+
if (lnIdx === 0) {
40+
// Only in first line of input: strip BOM
41+
if (ln.slice(0, 1) === '\uFEFF') { ln = ln.slice(1); }
42+
43+
// Only in first line of input: strip leading wrapper code
44+
ln = trim(ln
45+
).replace(/^[A-Za-z].*?[\(=]/, '');
46+
}
47+
ln = trim(ln);
48+
return ln;
49+
}
50+
51+
3352
function ceson2json(ceson) {
3453
if (!ceson) { return false; }
3554
if (typeof ceson !== 'string') { return false; }
3655
var json = [], inBlockComment = false, strConcat = false, afterCmt;
37-
ceson = ceson.split(/\n\s*/);
38-
if (ceson[0].slice(0, 1) === '\uFEFF') { ceson[0] = ceson[0].slice(1); }
56+
3957
function parseLine(ln, lnIdx) {
40-
ln = trim(ln);
58+
ln = extractLineText(ln, lnIdx);
59+
4160
//debugp(lnIdx + 1, { trimmed: ln });
4261
if (!ln) { return lnIdx; }
4362
if (inBlockComment) {
@@ -85,8 +104,10 @@
85104
//debugp(' ', { ln: ln }, (strConcat ? '+CONCAT+' : '-'));
86105
json.push(ln);
87106
}
88-
ceson.forEach(parseLine);
89-
return json.join('\n');
107+
ceson.split(/\n/).forEach(parseLine);
108+
json = json.join('\n');
109+
json = wrapperCodeCompatAtEndOfLine(json);
110+
return json;
90111
}
91112

92113

test/all.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// -*- coding: utf-8, tab-width: 2 -*-
22

3+
import './concats.mjs';
34
import './felidae.mjs';
5+
import './wraps.mjs';

test/concats.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// -*- coding: utf-8, tab-width: 2 -*-
2+
3+
import tu from './lib/test_util.mjs';
4+
5+
tu.verifyExampleFile('str-concat.key', {
6+
'concatenated object keys': 'not CESON, but parsers may support it anyway',
7+
});
8+
9+
tu.verifyExampleFile('str-concat.value', { a: 'concat', b: 'concat' });

test/wraps.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// -*- coding: utf-8, tab-width: 2 -*-
2+
3+
import tu from './lib/test_util.mjs';
4+
5+
function w(n) { return { wrapped: true, style: n }; }
6+
7+
tu.verifyExampleFile('wrap.amd', w('amd'));
8+
tu.verifyExampleFile('wrap.commonjs', w('commonjs'));
9+
tu.verifyExampleFile('wrap.jsonp', w('jsonp'));
10+
tu.verifyExampleFile('wrap.js-var', w('js var'));

0 commit comments

Comments
 (0)