// @ts-nocheck /* ============================================================================ - TAON READONLY SOURCE - DO NOT MODIFY THIS FILE. - Changes may be overwritten. - Use command: taon build:watch:lib inside json10 project repository for watch development of third party modules like this. * ========================================================================== */ import { _ } from 'tnp-core/websql'; import { walk, Models } from 'lodash-walk-object/websql'; export { Circ } from 'lodash-walk-object/websql'; import { Circ } from 'lodash-walk-object/websql'; import { CLASS } from 'typescript-class-helpers/websql'; export class JSON10 { public static structureArray(anyJSON: Object, options?: { include?: string[]; exclude?: string[]; }) { let pathes = []; const { include, exclude } = options || ({} as any); walk.Object(anyJSON, (value, lodashPath) => { if (!_.isUndefined(value)) { pathes.push(lodashPath); } }, { include, exclude, checkCircural: true }); return pathes; } public static cleaned(json, onCircs?: (circs: Circ[]) => any, options?: { exclude?: string[]; include?: string[]; breadthWalk?: boolean; }): typeof json { const result = _.isArray(json) ? [] : {}; const { exclude, include, breadthWalk } = options || { exclude: [], include: [], breadthWalk: false, }; const { circs } = walk.Object(json, (value, lodashPath, changeValueTo, options) => { if (_.isObject(value) && options.isCircural) { _.set(result, lodashPath, null); } else { _.set(result, lodashPath, _.cloneDeep(value)); } }, { include, exclude, breadthWalk, checkCircural: true, walkGetters: false, }); if (_.isFunction(onCircs)) { onCircs(circs); } return result; } public static stringify(anyJSON: Object, replace?: any, spaces?: number, onCircs?: (circs: Circ[]) => any): string { const json = this.cleaned(anyJSON, onCircs); return JSON.stringify(json, replace, spaces); } public static applyCircularMapping(json: object, circs: Circ[] = []): any { if (circs && _.isArray(circs)) { circs.forEach(({ circuralTargetPath, pathToObj }) => { if (_.isString(circuralTargetPath)) { if (circuralTargetPath === '') { _.set(json, pathToObj, json); } else { let v = _.get(json, circuralTargetPath); _.set(json, pathToObj, v); } } else { _.set(json, pathToObj, circuralTargetPath); } }); } return json; } public static parse(json: string, circs: Circ[] = []): any { let res = JSON.parse(json); res = JSON10.applyCircularMapping(res, circs); return res; } }