awesome-json2json
Advanced tools
Comparing version 0.1.0 to 0.1.1
export declare type TypeTemplate = IFullTemplate | string | Function; | ||
export interface IFullTemplate { | ||
$path: string; | ||
$formatting: Function; | ||
$path?: string; | ||
$formatting?: Function; | ||
[propName: string]: TypeTemplate; | ||
@@ -6,0 +6,0 @@ } |
@@ -80,7 +80,7 @@ "use strict"; | ||
var result = json; | ||
var _loop_1 = function (i) { | ||
if (/\[\]$/.test(splitedPath[i])) { | ||
var currentKey_1 = splitedPath[i].replace(/\[\]$/, ''); | ||
result = currentKey_1 === '' ? result : result[currentKey_1]; | ||
splitedPath.shift(); | ||
var _loop_1 = function () { | ||
var currentKey = splitedPath.shift(); | ||
if (/\[\]$/.test(currentKey)) { | ||
currentKey = currentKey.replace(/\[\]$/, ''); | ||
result = currentKey === '' ? result : result[currentKey]; | ||
var joinedPath_1 = splitedPath.join('.'); | ||
@@ -91,5 +91,5 @@ return { value: result.map(function (jsonItem) { | ||
} | ||
var currentKey = splitedPath[i].replace(/\?$/, ''); | ||
if (/\?$/.test(splitedPath[i])) { | ||
if (typeof json[currentKey] === 'undefined') { | ||
if (/\?$/.test(currentKey)) { | ||
currentKey = currentKey.replace(/\?$/, ''); | ||
if (typeof result[currentKey] === 'undefined') { | ||
return { value: undefined }; | ||
@@ -100,4 +100,4 @@ } | ||
}; | ||
for (var i = 0; i < splitedPath.length; i++) { | ||
var state_1 = _loop_1(i); | ||
while (splitedPath.length > 0) { | ||
var state_1 = _loop_1(); | ||
if (typeof state_1 === "object") | ||
@@ -104,0 +104,0 @@ return state_1.value; |
{ | ||
"name": "awesome-json2json", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "An awesome json to json mapper", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
# Awesome json2json | ||
[![Build Status](https://img.shields.io/travis/xcatliu/awesome-json2json.svg)](https://travis-ci.org/xcatliu/awesome-json2json) [![npm package](https://img.shields.io/npm/v/awesome-json2json.svg)](https://www.npmjs.org/package/awesome-json2json) [![npm downloads](http://img.shields.io/npm/dm/awesome-json2json.svg)](https://www.npmjs.org/package/awesome-json2json) | ||
An awesome json to jsone mapper | ||
@@ -4,0 +6,0 @@ |
export type TypeTemplate = IFullTemplate | string | Function; | ||
export interface IFullTemplate { | ||
$path: string; | ||
$formatting: Function; | ||
$path?: string; | ||
$formatting?: Function; | ||
[propName: string]: TypeTemplate; | ||
@@ -84,7 +84,7 @@ } | ||
let result = json; | ||
for (let i = 0; i < splitedPath.length; i++) { | ||
if (/\[\]$/.test(splitedPath[i])) { | ||
const currentKey = splitedPath[i].replace(/\[\]$/, ''); | ||
while (splitedPath.length > 0) { | ||
let currentKey = splitedPath.shift(); | ||
if (/\[\]$/.test(currentKey)) { | ||
currentKey = currentKey.replace(/\[\]$/, ''); | ||
result = currentKey === '' ? result : result[currentKey]; | ||
splitedPath.shift(); | ||
const joinedPath = splitedPath.join('.'); | ||
@@ -95,5 +95,5 @@ return result.map((jsonItem) => { | ||
} | ||
const currentKey = splitedPath[i].replace(/\?$/, ''); | ||
if (/\?$/.test(splitedPath[i])) { | ||
if (typeof json[currentKey] === 'undefined') { | ||
if (/\?$/.test(currentKey)) { | ||
currentKey = currentKey.replace(/\?$/, ''); | ||
if (typeof result[currentKey] === 'undefined') { | ||
return undefined; | ||
@@ -100,0 +100,0 @@ } |
@@ -18,2 +18,11 @@ const assert = require('assert'); | ||
} | ||
const ARRAY_FOO_BAR_BAZ = { | ||
foo: { | ||
bar: [ | ||
{ baz: 1 }, | ||
{ baz: 2 }, | ||
{ baz: 3 } | ||
] | ||
} | ||
} | ||
@@ -233,3 +242,14 @@ describe('json2json', () => { | ||
}); | ||
it('should match deep arrayed result', () => { | ||
assert.deepEqual(json2json(ARRAY_FOO_BAR_BAZ, { | ||
new_foo: 'foo.bar[].baz' | ||
}), { | ||
new_foo: [ | ||
1, | ||
2, | ||
3 | ||
] | ||
}); | ||
}); | ||
}); | ||
}); |
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
25837
15
520
181