json10
Version:
Handle circural references, comments and many more inside JSON
60 lines • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSON10 = void 0;
const lib_1 = require("tnp-core/lib");
const lib_2 = require("lodash-walk-object/lib");
const lib_3 = require("typescript-class-helpers/lib");
// import { Log } from 'ng2-logger'
// const log = Log.create('JSON10')
// let counter = 0
class JSON10 {
static structureArray(anyJSON, options) {
let pathes = [];
const { include, exclude } = options || {};
lib_2.walk.Object(anyJSON, (value, lodashPath) => {
if (!lib_1._.isUndefined(value)) {
pathes.push(lodashPath);
}
}, { include, exclude, checkCircural: true });
return pathes;
}
static cleaned(json, onCircs, options) {
// console.log('BETTER SRUGUB', json)
const result = lib_1._.isArray(json) ? [] : {};
const classFN = lib_3.CLASS.OBJECT(json).isClassObject && lib_3.CLASS.getFromObject(json);
const { exclude, include, breadthWalk } = options || { exclude: [], include: [], breadthWalk: false };
const { circs } = lib_2.walk.Object(json, (value, lodashPath, changeValueTo, options) => {
if (lib_1._.isObject(value) && options.isCircural) {
lib_1._.set(result, lodashPath, null);
}
else {
lib_1._.set(result, lodashPath, lib_1._.cloneDeep(value));
}
}, { include, exclude, breadthWalk, checkCircural: true });
if (lib_1._.isFunction(onCircs)) {
onCircs(circs);
}
return lib_1._.isFunction(classFN) ? lib_1._.merge(new classFN(), result) : result;
}
static stringify(anyJSON, replace, spaces, onCircs) {
const json = this.cleaned(anyJSON, onCircs);
return JSON.stringify(json, replace, spaces);
}
static parse(json, circs = []) {
let res = JSON.parse(json);
if (lib_1._.isArray(circs)) {
circs.forEach(({ circuralTargetPath, pathToObj }) => {
if (circuralTargetPath === '') {
lib_1._.set(res, pathToObj, res);
}
else {
let v = lib_1._.get(res, circuralTargetPath);
lib_1._.set(res, pathToObj, v);
}
});
}
return res;
}
}
exports.JSON10 = JSON10;
//# sourceMappingURL=index.js.map