Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
e11ace6a23 | |||
f29b632bd2 | |||
25f80b6b59 | |||
176ac34504 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartjson",
|
"name": "@pushrocks/smartjson",
|
||||||
"version": "3.0.11",
|
"version": "4.0.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartjson",
|
"name": "@pushrocks/smartjson",
|
||||||
"version": "3.0.11",
|
"version": "4.0.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "typed json handlers",
|
"description": "typed json handlers",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -26,8 +26,8 @@ tap.test('should create a folded object', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should enfold from object', async () => {
|
tap.test('should enfold from object', async () => {
|
||||||
mySomeClass.enfoldFromObject({ thisis: 'test2' });
|
const mySomeClass2 = SomeClass.enfoldFromObject({ thisis: 'test2' });
|
||||||
expect(mySomeClass)
|
expect(mySomeClass2)
|
||||||
.property('thisis')
|
.property('thisis')
|
||||||
.to.equal('test2');
|
.to.equal('test2');
|
||||||
});
|
});
|
||||||
|
46
ts/index.ts
46
ts/index.ts
@ -1,18 +1,41 @@
|
|||||||
import * as plugins from './smartjson.plugins';
|
import * as plugins from './smartjson.plugins';
|
||||||
|
|
||||||
export class Smartjson {
|
|
||||||
// ======
|
|
||||||
// STATIC
|
|
||||||
// ======
|
|
||||||
/**
|
/**
|
||||||
* allows you to parse a json
|
* allows you to parse a json
|
||||||
*/
|
*/
|
||||||
public static parse = plugins.bufferJson.parse;
|
export const parse = plugins.bufferJson.parse;
|
||||||
|
|
||||||
public static stringify = (objArg: any, optionsArg: plugins.IStableJsonTypes['Options']) => {
|
/**
|
||||||
|
*
|
||||||
|
* @param objArg
|
||||||
|
* @param optionsArg
|
||||||
|
*/
|
||||||
|
export const stringify = (objArg: any, optionsArg: plugins.IStableJsonTypes['Options'] = {}) => {
|
||||||
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);
|
||||||
|
};
|
||||||
|
|
||||||
|
export class Smartjson {
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enfold from json
|
||||||
|
*/
|
||||||
|
public static enfoldFromJson(jsonArg: string) {
|
||||||
|
const objectFromJson = parse(jsonArg);
|
||||||
|
return this.enfoldFromObject(objectFromJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========
|
// ========
|
||||||
@ -33,14 +56,11 @@ export class Smartjson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* enfolds data from an object
|
* folds a class into an object
|
||||||
*/
|
*/
|
||||||
public enfoldFromObject(objectArg) {
|
public foldToJson() {
|
||||||
for (const keyName in objectArg) {
|
const foldedObject = this.foldToObject();
|
||||||
if (this.saveableProperties.indexOf(keyName) !== -1) {
|
return stringify(foldedObject, {});
|
||||||
this[keyName] = objectArg[keyName];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user