awesome-json2json
Advanced tools
Comparing version 0.5.2 to 0.5.3
@@ -97,14 +97,18 @@ "use strict"; | ||
return json; | ||
var splitedPath = Array.isArray(path) ? path.slice() : path.split('.'); | ||
if (splitedPath[0] === '$root') { | ||
splitedPath.shift(); | ||
return this.getJSONByPath(this.root, splitedPath, context); | ||
var splitPath = Array.isArray(path) ? path.slice() : path.split('.'); | ||
if (splitPath[0] === '$root') { | ||
splitPath.shift(); | ||
return this.getJSONByPath(this.root, splitPath, context); | ||
} | ||
if (splitedPath[0] === '$item') { | ||
splitedPath.shift(); | ||
return this.getJSONByPath(context.$item, splitedPath, context); | ||
if (splitPath[0] === '$item') { | ||
splitPath.shift(); | ||
return this.getJSONByPath(context.$item, splitPath, context); | ||
} | ||
var result = json; | ||
while (splitedPath.length > 0) { | ||
var currentKey = splitedPath.shift(); | ||
while (splitPath.length > 0) { | ||
var currentKey = splitPath.shift(); | ||
if (currentKey === '$head') { | ||
result = (result && result[0]) ? result[0] : null; | ||
continue; | ||
} | ||
if (/\[\]$/.test(currentKey)) { | ||
@@ -120,3 +124,3 @@ currentKey = currentKey.replace(/\[\]$/, ''); | ||
return result.map(function (jsonItem) { | ||
return _this.getJSONByPath(jsonItem, splitedPath, __assign({}, context, { $item: jsonItem })); | ||
return _this.getJSONByPath(jsonItem, splitPath, __assign({}, context, { $item: jsonItem })); | ||
}); | ||
@@ -123,0 +127,0 @@ } |
{ | ||
"name": "awesome-json2json", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "An awesome json to json mapper", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -392,2 +392,14 @@ # Awesome json2json | ||
### Using only the head of an Array | ||
If you need to work with just a the first element of an array you can use `$head` in the path to access it. | ||
```js | ||
json2json( | ||
{ foo: [{ bar: 1 }, { bar: 2 }] }, | ||
{ first_bar: 'foo.$head.bar' } | ||
); | ||
// { first_bar: 1 } | ||
``` | ||
### Clear all empty data | ||
@@ -394,0 +406,0 @@ |
@@ -147,14 +147,18 @@ interface IContext { | ||
if (path === '' || path.length === 0) return json; | ||
const splitedPath = Array.isArray(path) ? path.slice() : path.split('.'); | ||
if (splitedPath[0] === '$root') { | ||
splitedPath.shift(); | ||
return this.getJSONByPath(this.root, splitedPath, context); | ||
const splitPath = Array.isArray(path) ? path.slice() : path.split('.'); | ||
if (splitPath[0] === '$root') { | ||
splitPath.shift(); | ||
return this.getJSONByPath(this.root, splitPath, context); | ||
} | ||
if (splitedPath[0] === '$item') { | ||
splitedPath.shift(); | ||
return this.getJSONByPath(context.$item, splitedPath, context); | ||
if (splitPath[0] === '$item') { | ||
splitPath.shift(); | ||
return this.getJSONByPath(context.$item, splitPath, context); | ||
} | ||
let result = json; | ||
while (splitedPath.length > 0) { | ||
let currentKey = splitedPath.shift(); | ||
while (splitPath.length > 0) { | ||
let currentKey = splitPath.shift(); | ||
if (currentKey === '$head') { | ||
result = (result && result[0]) ? result[0] : null; | ||
continue; | ||
} | ||
if (/\[\]$/.test(currentKey)) { | ||
@@ -170,3 +174,3 @@ currentKey = currentKey.replace(/\[\]$/, ''); | ||
return result.map((jsonItem) => { | ||
return this.getJSONByPath(jsonItem, splitedPath, { | ||
return this.getJSONByPath(jsonItem, splitPath, { | ||
...context, | ||
@@ -208,2 +212,2 @@ $item: jsonItem | ||
} | ||
} | ||
} |
@@ -29,3 +29,3 @@ const assert = require('assert'); | ||
describe('json2json', () => { | ||
describe('complexty example', () => { | ||
describe('complexity example', () => { | ||
it('should match', () => { | ||
@@ -478,3 +478,3 @@ assert.deepEqual(json2json({ | ||
}); | ||
it('should have currect $item in deep array', () => { | ||
it('should have correct $item in deep array', () => { | ||
assert.deepEqual(json2json({ | ||
@@ -599,2 +599,33 @@ foo: [ | ||
describe('$head in the path', () => { | ||
it('should be able to get the first element of an array with $head', () => { | ||
assert.deepEqual( | ||
json2json({ baz: ['a', 'b', 'c'] }, { head: 'baz.$head' }), | ||
{ head: 'a' }, | ||
); | ||
}); | ||
it('should work when $head is used in the middle of a path', () => { | ||
assert.deepEqual( | ||
json2json( | ||
{ foo: [{ bar: 1 }, { bar: 2 }] }, | ||
{ first_bar: 'foo.$head.bar' } | ||
), | ||
{ first_bar: 1 }, | ||
); | ||
}); | ||
it('should work when used with ?.', () => { | ||
assert.deepEqual( | ||
json2json({ foo: { bar: [ { not_baz: 1 }] } }, { head: 'foo.bar.$head?.baz' }), | ||
{ head: undefined }, | ||
); | ||
assert.deepEqual( | ||
json2json({ foo: { } }, { head: 'foo.bar?.$head?.baz' }), | ||
{ head: undefined }, | ||
); | ||
}); | ||
}); | ||
describe('$item in $path', () => { | ||
@@ -624,3 +655,3 @@ it('should get the $item inside $path', () => { | ||
}); | ||
it('should have currect $item in deep array', () => { | ||
it('should have correct $item in deep array', () => { | ||
assert.deepEqual(json2json({ | ||
@@ -627,0 +658,0 @@ foo: [ |
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
61465
1256
435