smartjson/ts/index.ts

116 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-06-09 17:54:43 +00:00
import * as plugins from './smartjson.plugins.js';
2020-10-05 11:22:16 +00:00
/**
* allows you to parse a json
*/
export const parse = plugins.bufferJson.parse;
2020-10-05 11:22:16 +00:00
/**
2020-10-05 11:26:06 +00:00
*
2020-10-05 11:22:16 +00:00
* @param objArg
* @param optionsArg
*/
2022-06-09 17:54:43 +00:00
export const stringify = (
objArg: any,
simpleOrderArray?: string[],
optionsArg: plugins.IStableJsonTypes['Options'] = {}
): string => {
2020-10-05 11:22:16 +00:00
const bufferedJson = plugins.bufferJson.stringify(objArg);
objArg = JSON.parse(bufferedJson);
2022-06-09 17:54:43 +00:00
let returnJson = plugins.stableJson(objArg, optionsArg);
return returnJson;
2020-10-05 11:22:16 +00:00
};
2023-08-24 08:48:51 +00:00
export const stringifyPretty = (objectArg: any) => {
const stringified = stringify(objectArg);
const object = JSON.parse(stringified);
return JSON.stringify(object, null, 2);
}
2022-09-13 19:37:42 +00:00
export const stringifyBase64 = (...args: Parameters<typeof stringify>): string => {
const stringifiedResult = stringify(...args);
return plugins.smartstring.base64.encodeUri(stringifiedResult);
2022-10-26 08:56:01 +00:00
};
2022-09-13 19:37:42 +00:00
export const parseBase64 = (base64JsonStringArg: string) => {
const simpleStringified = plugins.smartstring.base64.decode(base64JsonStringArg);
return parse(simpleStringified);
2022-10-26 08:56:01 +00:00
};
2022-09-13 19:37:42 +00:00
2022-10-26 08:56:01 +00:00
parse;
2022-09-13 19:37:42 +00:00
2020-10-05 11:22:16 +00:00
export class Smartjson {
2020-10-05 11:13:17 +00:00
/**
* enfolds data from an object
*/
public static enfoldFromObject(objectArg) {
const newInstance = new this();
for (const keyName in objectArg) {
if (newInstance.saveableProperties.indexOf(keyName) !== -1) {
newInstance[keyName] = objectArg[keyName];
}
}
return newInstance;
}
2020-10-05 11:22:16 +00:00
/**
* enfold from json
*/
public static enfoldFromJson(jsonArg: string) {
const objectFromJson = parse(jsonArg);
return this.enfoldFromObject(objectFromJson);
}
// ========
// INSTANCE
// ========
2017-02-27 13:05:03 +00:00
2019-08-09 08:55:17 +00:00
public saveableProperties: string[];
2017-02-27 13:05:03 +00:00
/**
* folds a class into an object
*/
2019-08-09 08:55:17 +00:00
public foldToObject() {
const newFoldedObject: { [key: string]: any } = {};
2020-10-05 15:28:29 +00:00
const trackMap = [];
2019-08-09 08:55:17 +00:00
for (const keyName of this.saveableProperties) {
2020-10-05 15:28:29 +00:00
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);
2017-02-27 13:05:03 +00:00
}
2018-07-23 22:38:54 +00:00
return newFoldedObject;
2017-02-27 13:05:03 +00:00
}
2020-10-05 11:22:16 +00:00
/**
* folds a class into an object
*/
public foldToJson() {
const foldedObject = this.foldToObject();
2022-06-09 17:54:43 +00:00
return stringify(foldedObject);
2020-10-05 11:22:16 +00:00
}
2017-02-27 13:05:03 +00:00
}
/**
* Decorator that marks a property as foldable
*/
2021-01-28 02:21:23 +00:00
export const foldDec = () => {
2017-02-27 13:05:03 +00:00
return (target: any, key: string) => {
2018-07-23 22:38:54 +00:00
if (!target.saveableProperties) {
target.saveableProperties = [];
}
target.saveableProperties.push(key);
};
};
2021-01-28 02:21:23 +00:00
export const deepEqualObjects = (object1: any, object2: any): boolean => {
const object1String = stringify(object1);
const object2String = stringify(object2);
return object1String === object2String;
2022-06-09 17:54:43 +00:00
};