awesome-json2json
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -28,5 +28,5 @@ export interface IFormattingContext { | ||
private getFilteredJSON(currentJSON, fullTemplate, context); | ||
private getJSONByPath(json, path); | ||
private getJSONByPath(json, path, context); | ||
private getFullTemplate(template); | ||
private isArrayTemplate(template); | ||
} |
@@ -27,3 +27,3 @@ "use strict"; | ||
var fullTemplate = this.getFullTemplate(template); | ||
var currentJSON = this.getJSONByPath(json, fullTemplate.$path); | ||
var currentJSON = this.getJSONByPath(json, fullTemplate.$path, context); | ||
if (fullTemplate.$disable) { | ||
@@ -82,3 +82,3 @@ if (this.isArrayTemplate(fullTemplate)) { | ||
// Syntax reference https://github.com/tc39/proposal-optional-chaining | ||
Json2json.prototype.getJSONByPath = function (json, path) { | ||
Json2json.prototype.getJSONByPath = function (json, path, context) { | ||
var _this = this; | ||
@@ -90,4 +90,8 @@ if (path === '' || path.length === 0) | ||
splitedPath.shift(); | ||
return this.getJSONByPath(this.root, splitedPath); | ||
return this.getJSONByPath(this.root, splitedPath, context); | ||
} | ||
if (splitedPath[0] === '$item') { | ||
splitedPath.shift(); | ||
return this.getJSONByPath(context.$item, splitedPath, context); | ||
} | ||
var result = json; | ||
@@ -100,3 +104,3 @@ while (splitedPath.length > 0) { | ||
return result.map(function (jsonItem) { | ||
return _this.getJSONByPath(jsonItem, splitedPath); | ||
return _this.getJSONByPath(jsonItem, splitedPath, __assign({}, context, { $item: jsonItem })); | ||
}); | ||
@@ -103,0 +107,0 @@ } |
{ | ||
"name": "awesome-json2json", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "An awesome json to json mapper", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -337,2 +337,31 @@ # Awesome json2json | ||
### Template with $item | ||
`item` represents the current array item. | ||
```js | ||
json2json({ | ||
foo: [ | ||
{ bar: { baz1: 1, baz2: 2, baz3: 3 }}, | ||
{ bar: { baz1: 1, baz2: 2, baz3: 3 }}, | ||
{ bar: { baz1: 1, baz2: 2, baz3: 3 }} | ||
] | ||
}, { | ||
new_foo: { | ||
$path: 'foo[]', | ||
new_bar: { | ||
$path: 'bar', | ||
new_baz: '$item.bar.baz1' | ||
} | ||
} | ||
}); | ||
// { | ||
// new_foo: [ | ||
// { new_bar: { new_baz: 1 } }, | ||
// { new_bar: { new_baz: 1 } }, | ||
// { new_bar: { new_baz: 1 } }, | ||
// ] | ||
// } | ||
``` | ||
### Clear all empty data | ||
@@ -339,0 +368,0 @@ |
@@ -68,3 +68,3 @@ export interface IFormattingContext { | ||
const fullTemplate = this.getFullTemplate(template); | ||
let currentJSON = this.getJSONByPath(json, fullTemplate.$path); | ||
let currentJSON = this.getJSONByPath(json, fullTemplate.$path, context); | ||
@@ -135,3 +135,3 @@ if (fullTemplate.$disable) { | ||
// Syntax reference https://github.com/tc39/proposal-optional-chaining | ||
private getJSONByPath(json, path: string | string[]) { | ||
private getJSONByPath(json, path: string | string[], context: IFormattingContext) { | ||
if (path === '' || path.length === 0) return json; | ||
@@ -141,4 +141,8 @@ const splitedPath = Array.isArray(path) ? path.slice() : path.split('.'); | ||
splitedPath.shift(); | ||
return this.getJSONByPath(this.root, splitedPath); | ||
return this.getJSONByPath(this.root, splitedPath, context); | ||
} | ||
if (splitedPath[0] === '$item') { | ||
splitedPath.shift(); | ||
return this.getJSONByPath(context.$item, splitedPath, context); | ||
} | ||
let result = json; | ||
@@ -151,3 +155,6 @@ while (splitedPath.length > 0) { | ||
return result.map((jsonItem) => { | ||
return this.getJSONByPath(jsonItem, splitedPath); | ||
return this.getJSONByPath(jsonItem, splitedPath, { | ||
...context, | ||
$item: jsonItem | ||
}); | ||
}); | ||
@@ -154,0 +161,0 @@ } |
@@ -516,2 +516,87 @@ const assert = require('assert'); | ||
}); | ||
describe('$item in $path', () => { | ||
it('should get the $item inside $path', () => { | ||
assert.deepEqual(json2json({ | ||
foo: [ | ||
{ bar: { baz1: 1, baz2: 2, baz3: 3 }}, | ||
{ bar: { baz1: 1, baz2: 2, baz3: 3 }}, | ||
{ bar: { baz1: 1, baz2: 2, baz3: 3 }} | ||
] | ||
}, { | ||
new_foo: { | ||
$path: 'foo[]', | ||
new_bar: { | ||
$path: 'bar', | ||
new_baz: '$item.bar.baz1' | ||
} | ||
} | ||
}), { | ||
new_foo: [ | ||
{ new_bar: { new_baz: 1 } }, | ||
{ new_bar: { new_baz: 1 } }, | ||
{ new_bar: { new_baz: 1 } }, | ||
] | ||
}); | ||
}); | ||
it('should have currect $item in deep array', () => { | ||
assert.deepEqual(json2json({ | ||
foo: [ | ||
{ | ||
bar: [ | ||
{ baz: 1 }, | ||
{ baz: 2 }, | ||
{ baz: 3 } | ||
] | ||
}, | ||
{ | ||
bar: [ | ||
{ baz: 1 }, | ||
{ baz: 2 }, | ||
{ baz: 3 } | ||
] | ||
}, | ||
{ | ||
bar: [ | ||
{ baz: 1 }, | ||
{ baz: 2 }, | ||
{ baz: 3 } | ||
] | ||
}, | ||
] | ||
}, { | ||
new_foo: { | ||
$path: 'foo[]', | ||
new_bar: { | ||
$path: 'bar[]', | ||
new_baz: '$item.baz' | ||
}, | ||
} | ||
}), { | ||
new_foo: [ | ||
{ | ||
new_bar: [ | ||
{ new_baz: 1 }, | ||
{ new_baz: 2 }, | ||
{ new_baz: 3 }, | ||
] | ||
}, | ||
{ | ||
new_bar: [ | ||
{ new_baz: 1 }, | ||
{ new_baz: 2 }, | ||
{ new_baz: 3 }, | ||
] | ||
}, | ||
{ | ||
new_bar: [ | ||
{ new_baz: 1 }, | ||
{ new_baz: 2 }, | ||
{ new_baz: 3 }, | ||
] | ||
}, | ||
] | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -518,0 +603,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54660
1114
397