awesome-json2json
Advanced tools
Comparing version 0.5.0 to 0.5.1
import { Template, IJson2jsonOptions } from './Json2json'; | ||
export default function json2json<T>(json: any, template: Template<T>, options?: IJson2jsonOptions): any; | ||
export { Template, IJson2jsonOptions }; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -19,7 +19,7 @@ export declare type Template<T = any> = IFullTemplate<T> | string | Function; | ||
map(json: any): any; | ||
private mapChild(json, template, context); | ||
private getFilteredJSON(currentJSON, fullTemplate, context); | ||
private getJSONByPath(json, path, context); | ||
private getFullTemplate(template); | ||
private isArrayTemplate(template); | ||
private mapChild; | ||
private getFilteredJSON; | ||
private getJSONByPath; | ||
private getFullTemplate; | ||
private isArrayTemplate; | ||
} |
@@ -51,5 +51,7 @@ "use strict"; | ||
if (Object.keys(fullTemplate).some(function (key) { return !(/^\$/.test(key)); })) { | ||
currentJSON = this.getFilteredJSON(currentJSON, fullTemplate, context); | ||
return this.getFilteredJSON(currentJSON, fullTemplate, context); | ||
} | ||
return currentJSON; | ||
else { | ||
return currentJSON; | ||
} | ||
}; | ||
@@ -60,9 +62,11 @@ Json2json.prototype.getFilteredJSON = function (currentJSON, fullTemplate, context) { | ||
if (this.isArrayTemplate(fullTemplate)) { | ||
var index_1 = 0; | ||
return currentJSON.map(function (currentJSONItem) { | ||
var result = {}; | ||
filteredKeys.forEach(function (key) { | ||
var childResult = _this.mapChild(currentJSONItem, fullTemplate[key], __assign({}, context, { $item: currentJSONItem })); | ||
var childResult = _this.mapChild(currentJSONItem, fullTemplate[key], __assign({}, context, { $item: currentJSONItem, $index: index_1 })); | ||
if (childResult !== Json2json.DISABLED_FIELD) { | ||
result[key] = childResult; | ||
} | ||
index_1 += 1; | ||
}); | ||
@@ -154,2 +158,3 @@ return result; | ||
var clearedJSON = Object.keys(json).reduce(function (prev, key) { | ||
var _a; | ||
var clearedJSONItem = Json2json.clearEmpty(json[key]); | ||
@@ -159,3 +164,2 @@ if (clearedJSONItem === undefined) | ||
return __assign({}, prev, (_a = {}, _a[key] = clearedJSONItem, _a)); | ||
var _a; | ||
}, {}); | ||
@@ -162,0 +166,0 @@ if (Object.keys(clearedJSON).length === 0) |
{ | ||
"name": "awesome-json2json", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "An awesome json to json mapper", | ||
@@ -12,2 +12,3 @@ "main": "./lib/index.js", | ||
"build:ts:watch": "tsc --declaration --watch", | ||
"pretest": "npm run build:ts", | ||
"test": "mocha" | ||
@@ -14,0 +15,0 @@ }, |
@@ -19,5 +19,9 @@ # Awesome json2json | ||
json2json({ foo: { bar: { baz: 1 }}}, { | ||
let sourceJson = { foo: { bar: { baz: 1 }}}; | ||
let template = { | ||
new_foo: 'foo.bar.baz' | ||
}); | ||
}; | ||
json2json(sourceJson, template); | ||
// { new_foo: 1 } | ||
@@ -24,0 +28,0 @@ ``` |
interface IContext { | ||
$root: any; | ||
$item?: any; | ||
$index?: any; | ||
} | ||
@@ -95,6 +96,6 @@ | ||
if (Object.keys(fullTemplate).some((key) => !(/^\$/.test(key)))) { | ||
currentJSON = this.getFilteredJSON(currentJSON, fullTemplate, context); | ||
return this.getFilteredJSON(currentJSON, fullTemplate, context); | ||
} else { | ||
return currentJSON; | ||
} | ||
return currentJSON; | ||
} | ||
@@ -105,2 +106,3 @@ private getFilteredJSON(currentJSON, fullTemplate: IFullTemplate, context: IContext) { | ||
if (this.isArrayTemplate(fullTemplate)) { | ||
let index = 0; | ||
return currentJSON.map((currentJSONItem) => { | ||
@@ -111,3 +113,4 @@ let result = {}; | ||
...context, | ||
$item: currentJSONItem | ||
$item: currentJSONItem, | ||
$index: index | ||
}); | ||
@@ -117,2 +120,3 @@ if (childResult !== Json2json.DISABLED_FIELD) { | ||
} | ||
index += 1; | ||
}); | ||
@@ -119,0 +123,0 @@ return result; |
@@ -525,18 +525,19 @@ const assert = require('assert'); | ||
}); | ||
// it('should have $index context in $formatting argument', () => { | ||
// assert.deepEqual(json2json(ARRAY_FOO_BAR, { | ||
// new_foo: { | ||
// $path: 'foo[].bar', | ||
// new_bar: (barValue, { $index, $root }) => { | ||
// return barValue + '_formatted_' + $root.foo[$index].bar; | ||
// } | ||
// } | ||
// }), { | ||
// new_foo: [ | ||
// { new_bar: '1_formatted_1' }, | ||
// { new_bar: '2_formatted_2' }, | ||
// { new_bar: '3_formatted_3' } | ||
// ] | ||
// }); | ||
// }); | ||
it('should have $index context in $formatting argument', () => { | ||
assert.deepEqual(json2json(ARRAY_FOO_BAR, { | ||
new_foo: { | ||
$path: 'foo[].bar', | ||
new_bar: (barValue, { $index }) => { | ||
return barValue + '_formatted_with_index_' + $index; | ||
} | ||
} | ||
}), { | ||
new_foo: [ | ||
{ new_bar: '1_formatted_with_index_0' }, | ||
{ new_bar: '2_formatted_with_index_1' }, | ||
{ new_bar: '3_formatted_with_index_2' } | ||
] | ||
}); | ||
}); | ||
}); | ||
@@ -543,0 +544,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
56038
1151
401
14