bbo
Version:
bbo is a utility library of zero dependencies for javascript.
29 lines (23 loc) • 472 B
JavaScript
;
/**
* to json
*/
// eval hack
var evil = fn => {
// A variable points to Function, preventing reporting errors
var Fn = Function;
return new Fn('return ' + fn)();
}; // bbo.toJSON = bbo.tojson = bbo.toJson
var toJson = res => {
if (!res) return null;
if (typeof res === 'string') {
try {
return JSON.parse(res);
} catch (e) {
return evil('(' + res + ')');
}
} else {
return res;
}
};
module.exports = toJson;