Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
16048fe96d | |||
04d60333a9 | |||
c30f1f4ab9 | |||
fc91dd3171 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartjson",
|
"name": "@pushrocks/smartjson",
|
||||||
"version": "4.0.2",
|
"version": "4.0.4",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartjson",
|
"name": "@pushrocks/smartjson",
|
||||||
"version": "4.0.2",
|
"version": "4.0.4",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "typed json handlers",
|
"description": "typed json handlers",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
13
ts/index.ts
13
ts/index.ts
@ -10,7 +10,7 @@ export const parse = plugins.bufferJson.parse;
|
|||||||
* @param objArg
|
* @param objArg
|
||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
*/
|
*/
|
||||||
export const stringify = (objArg: any, optionsArg: plugins.IStableJsonTypes['Options'] = {}) => {
|
export const stringify = (objArg: any, optionsArg: plugins.IStableJsonTypes['Options'] = {}): string => {
|
||||||
const bufferedJson = plugins.bufferJson.stringify(objArg);
|
const bufferedJson = plugins.bufferJson.stringify(objArg);
|
||||||
objArg = JSON.parse(bufferedJson);
|
objArg = JSON.parse(bufferedJson);
|
||||||
return plugins.stableJson(objArg, optionsArg);
|
return plugins.stableJson(objArg, optionsArg);
|
||||||
@ -49,8 +49,17 @@ export class Smartjson {
|
|||||||
*/
|
*/
|
||||||
public foldToObject() {
|
public foldToObject() {
|
||||||
const newFoldedObject: { [key: string]: any } = {};
|
const newFoldedObject: { [key: string]: any } = {};
|
||||||
|
const trackMap = [];
|
||||||
for (const keyName of this.saveableProperties) {
|
for (const keyName of this.saveableProperties) {
|
||||||
newFoldedObject[keyName] = plugins.lodashCloneDeep(this[keyName]);
|
let value = this[keyName];
|
||||||
|
if (value instanceof Smartjson) {
|
||||||
|
if (trackMap.includes(value)) {
|
||||||
|
throw new Error('cycle detected');
|
||||||
|
}
|
||||||
|
trackMap.push(value);
|
||||||
|
value = value.foldToObject();
|
||||||
|
}
|
||||||
|
newFoldedObject[keyName] = plugins.lodashCloneDeep(value);
|
||||||
}
|
}
|
||||||
return newFoldedObject;
|
return newFoldedObject;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user