Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

awesome-json2json

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awesome-json2json - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

2

lib/index.d.ts
import { Template } from './Json2json';
export default function json2json<T>(json: any, template: Template<T>): T;
export default function json2json<T>(json: any, template: Template<T>): any;
export { Template };

@@ -1,5 +0,6 @@

export declare type Template<T> = IFullTemplate<T> | string | Function;
export interface IFullTemplate<T> {
export declare type Template<T = any> = IFullTemplate<T> | string | Function;
export interface IFullTemplate<T = any> {
$path?: string;
$formatting?: Function;
$disable?: Function;
[propName: string]: Template<T>;

@@ -10,6 +11,7 @@ }

private static PATH_ROOT;
private static DISABLED_FIELD;
private template;
private root;
constructor(template: Template<T>);
map(json: any): T;
map(json: any): any;
private mapChild(json, template);

@@ -16,0 +18,0 @@ private getFullTemplate(template);

@@ -24,2 +24,7 @@ "use strict";

currentJSON = this.getPropertySafely(currentJSON, fullTemplate.$path);
if (fullTemplate.$disable) {
if (fullTemplate.$disable(currentJSON)) {
return Json2json.DISABLED_FIELD;
}
}
if (fullTemplate.$formatting) {

@@ -41,3 +46,6 @@ if (/\[\]/.test(fullTemplate.$path)) {

filteredKeys.forEach(function (key) {
result[key] = _this.mapChild(currentJSONItem, fullTemplate[key]);
var childResult = _this.mapChild(currentJSONItem, fullTemplate[key]);
if (childResult !== Json2json.DISABLED_FIELD) {
result[key] = childResult;
}
});

@@ -49,3 +57,6 @@ return result;

filteredKeys.forEach(function (key) {
result[key] = _this.mapChild(currentJSON, fullTemplate[key]);
var childResult = _this.mapChild(currentJSON, fullTemplate[key]);
if (childResult !== Json2json.DISABLED_FIELD) {
result[key] = childResult;
}
});

@@ -56,4 +67,3 @@ return result;

var fullTemplate = {
$path: '',
$formatting: null
$path: ''
};

@@ -111,2 +121,3 @@ if (typeof template === 'string') {

Json2json.PATH_ROOT = '$root';
Json2json.DISABLED_FIELD = '__DISABLED_FIELD__';
return Json2json;

@@ -113,0 +124,0 @@ }());

{
"name": "awesome-json2json",
"version": "0.1.2",
"version": "0.2.0",
"description": "An awesome json to json mapper",

@@ -5,0 +5,0 @@ "main": "./lib/index.js",

@@ -61,3 +61,3 @@ # Awesome json2json

### Template with nested template
### Nested template

@@ -74,3 +74,3 @@ ```js

### Template with nested template with $path and $formatting
### Nested template with $path and $formatting

@@ -92,2 +92,24 @@ ```js

### Nested template with $disable
```js
json2json({ foo: { bar: { baz: 1 }}}, {
new_foo: {
$path: 'foo',
new_bar1: {
$disable: (foo) => {
return foo.bar.baz === 1;
},
new_baz: 'bar.baz'
},
new_bar2: 'bar.baz'
}
});
// {
// new_foo: {
// new_bar2: 1
// }
// }
```
### Template with $root

@@ -133,3 +155,3 @@

### Array template with formatting
### Array template with $formatting

@@ -158,3 +180,3 @@ ```js

### Array template with nested template
### Nested array template

@@ -161,0 +183,0 @@ ```js

@@ -1,6 +0,7 @@

export type Template<T> = IFullTemplate<T> | string | Function;
export type Template<T = any> = IFullTemplate<T> | string | Function;
export interface IFullTemplate<T> {
export interface IFullTemplate<T = any> {
$path?: string;
$formatting?: Function;
$disable?: Function;
[propName: string]: Template<T>;

@@ -12,2 +13,3 @@ }

private static PATH_ROOT = '$root';
private static DISABLED_FIELD = '__DISABLED_FIELD__';
private template: Template<T>;

@@ -20,9 +22,15 @@ private root: any;

this.root = json;
return this.mapChild(json, this.template);
}
private mapChild(json: any, template: Template<T>): T {
private mapChild(json: any, template: Template) {
const fullTemplate = this.getFullTemplate(template);
let currentJSON = json;
currentJSON = this.getPropertySafely(currentJSON, fullTemplate.$path);
if (fullTemplate.$disable) {
if (fullTemplate.$disable(currentJSON)) {
return Json2json.DISABLED_FIELD;
}
}
if (fullTemplate.$formatting) {

@@ -45,3 +53,6 @@ if (/\[\]/.test(fullTemplate.$path)) {

filteredKeys.forEach((key) => {
result[key] = this.mapChild(currentJSONItem, fullTemplate[key]);
const childResult = this.mapChild(currentJSONItem, fullTemplate[key]);
if (childResult !== Json2json.DISABLED_FIELD) {
result[key] = childResult;
}
});

@@ -54,10 +65,12 @@ return result;

filteredKeys.forEach((key) => {
result[key] = this.mapChild(currentJSON, fullTemplate[key]);
const childResult = this.mapChild(currentJSON, fullTemplate[key]);
if (childResult !== Json2json.DISABLED_FIELD) {
result[key] = childResult;
}
});
return (result as T);
return result;
}
private getFullTemplate(template: Template<T>) {
let fullTemplate: IFullTemplate<T> = {
$path: '',
$formatting: null
private getFullTemplate(template: Template) {
let fullTemplate: IFullTemplate = {
$path: ''
};

@@ -64,0 +77,0 @@

@@ -164,2 +164,41 @@ const assert = require('assert');

});
it('should disable a field if $disable returns true', () => {
assert.deepEqual(json2json(FOO_BAR_BAZ, {
new_foo: {
$path: 'foo',
new_bar1: {
$disable: (foo) => {
return foo.bar.baz === 1;
},
new_baz: 'bar.baz'
},
new_bar2: 'bar.baz'
}
}), {
new_foo: {
new_bar2: 1
}
});
});
it('should not disable a field if $disable returns false', () => {
assert.deepEqual(json2json(FOO_BAR_BAZ, {
new_foo: {
$path: 'foo',
new_bar1: {
$disable: (foo) => {
return foo.bar.baz !== 1;
},
new_baz: 'bar.baz'
},
new_bar2: 'bar.baz'
}
}), {
new_foo: {
new_bar1: {
new_baz: 1
},
new_bar2: 1
}
});
});
});

@@ -166,0 +205,0 @@ describe('array template', () => {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc