smartjson/ts/index.ts

51 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './smartjson.plugins';
export class Smartjson {
// ======
// STATIC
// ======
static parse = JSON.parse;
static stringify = plugins.stableJson;
// ========
// INSTANCE
// ========
2017-02-27 13:05:03 +00:00
2018-07-23 22:38:54 +00:00
saveableProperties: string[];
2017-02-27 13:05:03 +00:00
/**
* folds a class into an object
*/
2018-07-23 22:38:54 +00:00
foldToObject() {
let newFoldedObject: {[key: string]: any} = {};
2017-02-27 13:05:03 +00:00
for (let keyName of this.saveableProperties) {
newFoldedObject[keyName] = plugins.lodash.cloneDeep(this[keyName]);
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
}
/**
* enfolds data from an object
*/
enfoldFromObject(objectArg) {
for (let keyName in objectArg) {
if (this.saveableProperties.indexOf(keyName) !== -1) {
2018-07-23 22:38:54 +00:00
this[keyName] = objectArg[keyName];
2017-02-27 13:05:03 +00:00
}
}
}
}
/**
* Decorator that marks a property as foldable
*/
export let foldDec = () => {
return (target: any, key: string) => {
2018-07-23 22:38:54 +00:00
if (!target.saveableProperties) {
target.saveableProperties = [];
}
target.saveableProperties.push(key);
};
};