Skip to content

Commit 4e5188b

Browse files
author
Yannick Galatol
committed
Add a stringify method which format a JSON and returns markers
1 parent f4d8e0c commit 4e5188b

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed

src/jsonselect.js

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
(function(exports) {
4343

4444
var // localize references
45-
toString = Object.prototype.toString;
45+
toString = Object.prototype.toString,
46+
line;
4647

4748
function jsonParse(str) {
4849
try {
@@ -535,6 +536,96 @@
535536
return a;
536537
}
537538

539+
function stringify(sel, obj, id, num, tot, level, indent, matches) {
540+
var a = (sel[0] === ",") ? sel.slice(1) : [sel],
541+
a0 = [],
542+
call = false,
543+
lineStart, columnStart, lineEnd, columnEnd,
544+
i = 0, j = 0, k, x, y, ret;
545+
for (i = 0; i < a.length; i++) {
546+
x = mn(obj, a[i], id, num, tot);
547+
if (x[0]) {
548+
call = true;
549+
lineStart = line;
550+
columnStart = ((level - 1) * indent) + id.length + 4;
551+
}
552+
for (j = 0; j < x[1].length; j++) {
553+
a0.push(x[1][j]);
554+
}
555+
}
556+
557+
if (a0.length && a0.length >= 1) {
558+
a0.unshift(",");
559+
}
560+
561+
ret = '';
562+
if (typeof id !== 'undefined') {
563+
ret += '"' + id + '": ';
564+
}
565+
if (typeof obj === "object") {
566+
y = [];
567+
if (isArray(obj)) {
568+
if (obj.length > 0) {
569+
for (i = 0; i < obj.length; i++) {
570+
line += 1;
571+
y.push(stringify(a0, obj[i], undefined, i, obj.length, level + 1, indent, matches));
572+
}
573+
ret += '[\n' + pad(level * indent) + y.join(',' + '\n' + pad(level * indent)) + '\n' + pad((level - 1) * indent) + ']';
574+
line += 1;
575+
columnEnd = ((level - 1) * indent) + 1;
576+
} else {
577+
ret += '[]';
578+
columnEnd = columnStart + 2;
579+
}
580+
} else {
581+
for (k in obj) {
582+
if (obj.hasOwnProperty(k)) {
583+
line += 1;
584+
y.push(stringify(a0, obj[k], k, undefined, undefined, level + 1, indent, matches));
585+
}
586+
}
587+
if (y.length > 0) {
588+
ret += '{\n' + pad(level * indent) + y.join(',' + '\n' + pad(level * indent)) + '\n' + pad((level - 1) * indent) + '}';
589+
line += 1;
590+
columnEnd = ((level - 1) * indent) + 1;
591+
} else {
592+
ret += '{}';
593+
columnEnd = columnStart + 2;
594+
}
595+
}
596+
}
597+
else if (typeof obj === "string") {
598+
ret += '"' + obj + '"';
599+
columnEnd = columnStart + obj.length + 2;
600+
}
601+
else {
602+
ret += String(obj);
603+
columnEnd = columnStart + String(obj).length;
604+
}
605+
606+
if (call) {
607+
lineEnd = line;
608+
matches.push({
609+
match: obj,
610+
lineStart: lineStart,
611+
columnStart: columnStart,
612+
lineEnd: lineEnd,
613+
columnEnd: columnEnd
614+
});
615+
}
616+
617+
return ret;
618+
}
619+
620+
function pad(num, char) {
621+
char = char || ' ';
622+
var r = '', i;
623+
for (i = 0; i < num; ++i) {
624+
r += char;
625+
}
626+
return r;
627+
}
628+
538629
function format(sel, arr) {
539630
sel = sel.replace(/\?/g, function() {
540631
if (arr.length === 0) throw "too few parameters given";
@@ -554,6 +645,16 @@
554645
},
555646
forEach: function(obj, fun) {
556647
return forEach(this.sel, obj, fun);
648+
},
649+
stringify: function (obj, indent) {
650+
indent = typeof indent === 'undefined' ? 2 : indent;
651+
line = 1;
652+
var matches = [];
653+
var formattedJson = stringify(this.sel, obj, undefined, undefined, undefined, 1, indent, matches);
654+
return {
655+
matches: matches,
656+
json: formattedJson
657+
};
557658
}
558659
};
559660
}
@@ -568,5 +669,9 @@
568669
if (!fun) { fun = obj; obj = arr; arr = undefined; }
569670
return compile(sel, arr).forEach(obj, fun);
570671
};
672+
exports.stringify = function (sel, arr, obj, indent) {
673+
if (!obj) { obj = arr; arr = undefined; }
674+
return compile(sel, arr).stringify(obj, indent);
675+
};
571676
exports.compile = compile;
572677
})(typeof exports === "undefined" ? (window.JSONSelect = {}) : exports);

0 commit comments

Comments
 (0)