167 lines
4.5 KiB
TypeScript
167 lines
4.5 KiB
TypeScript
import * as plugins from './smartjson.plugins.js';
|
|
import * as bufferhandling from './bufferhandling.js';
|
|
|
|
interface JsonObject {
|
|
[key: string]: any;
|
|
}
|
|
|
|
/**
|
|
* allows you to parse a json
|
|
*/
|
|
export const parse = bufferhandling.parse;
|
|
|
|
export const parseJsonL = (jsonlData: string): JsonObject[] => {
|
|
const lines = jsonlData.split('\n');
|
|
const parsedData: JsonObject[] = lines.reduce((acc, line) => {
|
|
const trimmed = line.trim();
|
|
if (trimmed.length > 0) {
|
|
acc.push(parse(trimmed));
|
|
}
|
|
return acc;
|
|
}, [] as JsonObject[]);
|
|
return parsedData;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param objArg
|
|
* @param optionsArg
|
|
*/
|
|
export const stringify = (
|
|
objArg: any,
|
|
simpleOrderArray?: string[],
|
|
optionsArg: plugins.IStableJsonTypes['Options'] = {}
|
|
): string => {
|
|
const bufferedJson = bufferhandling.stringify(objArg);
|
|
objArg = JSON.parse(bufferedJson);
|
|
let returnJson = plugins.stableJson(objArg, optionsArg);
|
|
return returnJson;
|
|
};
|
|
|
|
export const stringifyPretty = (objectArg: any) => {
|
|
const stringified = stringify(objectArg);
|
|
const object = JSON.parse(stringified);
|
|
return JSON.stringify(object, null, 2);
|
|
}
|
|
|
|
export const stringifyBase64 = (...args: Parameters<typeof stringify>): string => {
|
|
const stringifiedResult = stringify(...args);
|
|
return plugins.smartstring.base64.encodeUri(stringifiedResult);
|
|
};
|
|
|
|
export const parseBase64 = (base64JsonStringArg: string) => {
|
|
const base64 = plugins.smartstring.base64 as any;
|
|
const decodeFn: (input: string) => string = base64.decodeUri || base64.decode;
|
|
const simpleStringified = decodeFn(base64JsonStringArg);
|
|
return parse(simpleStringified);
|
|
};
|
|
|
|
export class Smartjson {
|
|
/**
|
|
* enfolds data from an object
|
|
*/
|
|
public static enfoldFromObject<T extends typeof Smartjson>(this: T, objectArg: any): InstanceType<T> {
|
|
const newInstance = new this() as InstanceType<T>;
|
|
for (const keyName in objectArg) {
|
|
if (newInstance.saveableProperties.indexOf(keyName) !== -1) {
|
|
newInstance[keyName] = objectArg[keyName];
|
|
}
|
|
}
|
|
return newInstance;
|
|
}
|
|
|
|
/**
|
|
* enfold from json
|
|
*/
|
|
public static enfoldFromJson<T extends typeof Smartjson>(this: T, jsonArg: string): InstanceType<T> {
|
|
const objectFromJson = parse(jsonArg);
|
|
return this.enfoldFromObject(objectFromJson);
|
|
}
|
|
|
|
// ========
|
|
// INSTANCE
|
|
// ========
|
|
|
|
public saveableProperties: string[];
|
|
|
|
/**
|
|
* folds a class into an object
|
|
*/
|
|
public foldToObject() {
|
|
const newFoldedObject: { [key: string]: any } = {};
|
|
const trackSet = new Set<Smartjson>();
|
|
const foldValue = (val: any): any => {
|
|
if (val instanceof Smartjson) {
|
|
if (trackSet.has(val)) {
|
|
throw new Error('cycle detected');
|
|
}
|
|
trackSet.add(val);
|
|
return val.foldToObjectInternal(trackSet);
|
|
}
|
|
if (Array.isArray(val)) {
|
|
return val.map((item) => foldValue(item));
|
|
}
|
|
return plugins.lodashCloneDeep(val);
|
|
};
|
|
for (const keyName of this.saveableProperties) {
|
|
const value = this[keyName];
|
|
newFoldedObject[keyName] = foldValue(value);
|
|
}
|
|
return newFoldedObject;
|
|
}
|
|
|
|
private foldToObjectInternal(trackSet: Set<Smartjson>) {
|
|
const result: { [key: string]: any } = {};
|
|
const foldValue = (val: any): any => {
|
|
if (val instanceof Smartjson) {
|
|
if (trackSet.has(val)) {
|
|
throw new Error('cycle detected');
|
|
}
|
|
trackSet.add(val);
|
|
return val.foldToObjectInternal(trackSet);
|
|
}
|
|
if (Array.isArray(val)) {
|
|
return val.map((item) => foldValue(item));
|
|
}
|
|
return plugins.lodashCloneDeep(val);
|
|
};
|
|
for (const keyName of this.saveableProperties) {
|
|
const value = this[keyName];
|
|
result[keyName] = foldValue(value);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* folds a class into an object
|
|
*/
|
|
public foldToJson() {
|
|
const foldedObject = this.foldToObject();
|
|
return stringify(foldedObject);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Decorator that marks a property as foldable
|
|
*/
|
|
export const foldDec = () => {
|
|
return (target: any, key: string) => {
|
|
if (!target.saveableProperties) {
|
|
target.saveableProperties = [];
|
|
}
|
|
target.saveableProperties.push(key);
|
|
};
|
|
};
|
|
|
|
export const deepEqualObjects = (object1: any, object2: any): boolean => {
|
|
const object1String = stringify(object1);
|
|
const object2String = stringify(object2);
|
|
return object1String === object2String;
|
|
};
|
|
|
|
export const deepEqualJsonLStrings = (jsonLString1: string, jsonLString2: string): boolean => {
|
|
const firstArray = parseJsonL(jsonLString1);
|
|
const secondArray = parseJsonL(jsonLString2);
|
|
return deepEqualObjects(firstArray, secondArray);
|
|
}
|