-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
54 lines (45 loc) · 1.61 KB
/
main.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
// Based on ThiefMaster's code from StackOverflow
// http://stackoverflow.com/a/10574546/298479
var _ = require("underscore");
function attemptParse(open, close, str) {
try {
return JSON.parse(str.substring(open, close + 1));
} catch (e) {
return false;
}
}
module.exports = function(optsArg) {
var defaultOpts = {
"multiple": false,
"error": new Error("No JSON object found")
}, opts = _.extend(defaultOpts, optsArg);
function recur(open, close, str, acc, cb) {
var candidate;
// TERMINAL CONDITIONS -----------------------------------------------
if ((open === -1) || (close <= open)) {
if (acc && acc.length === 0) { // nothing found
return cb(opts.error);
}
return cb(null, acc); // Finished searching
}
candidate = attemptParse(open, close, str);
if (candidate) {
// With default options we're done here.
if (opts.multiple === false) {
return cb(null, candidate, open, close + 1);
}
acc.push([candidate, open, close + 1]);
// Terminate based on the "multiple" option
if (opts.multiple === acc.length) {
return cb(null, acc);
}
recur(str.indexOf("{", open + 1),
str.lastIndexOf("}"), str, acc, cb);
} else {
recur(open, str.substr(0, close).lastIndexOf("}"), str, acc, cb);
}
}
return function(str, callback) {
recur(str.indexOf("{"), str.lastIndexOf("}"), str, [], callback);
};
};