Skip to content

Commit

Permalink
unit test and fix for flatten
Browse files Browse the repository at this point in the history
- expand still broken

Fixes: IBM-Cloud#18
  • Loading branch information
srl295 committed Jul 10, 2018
1 parent 0227516 commit 96adad5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ module.exports.flatten = function flatten(obj, opts) {
return jsonpath
.nodes(obj, '$..*')
.reduce((p,v) => {
if ( !opts.flattenAll && typeof v.value === 'string' && v.path.length === 2) {
if ( !opts.flattenAll &&
typeof v.value === 'string' &&
v.path.length === 2 &&
v.path[1].substring(0,2) !== '$.') {
// It is a simple {key: "value"} … do not modify it
p[v.path[1]] = v.value;
} else if ( typeof v.value !== 'object') {
Expand Down
39 changes: 39 additions & 0 deletions test/resfiltertest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright IBM Corp. 2015,2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const expect = require('chai').expect;
const flatten = require('../');
const dataInput = require('./gp-res-filter.d/input.json');
const dataOutput = require('./gp-res-filter.d/output.json');
const expectOutput = dataOutput.reduce((p, v) => {
const {key,value} = v;
p[key] = value;
return p;
}, {});

describe('test vs. gp-res-filter files', function() {
it('should be able to flatten gp-res-filter/input.json', function() {
const input = dataInput;
const output = flatten.flatten(input);
expect(output).to.deep.equal(expectOutput);
});
it('should be able to expand gp-res-filter/input.json', function() {
const input = expectOutput;
const output = flatten.expand(input);
expect(output).to.deep.equal(dataInput);
});
});

0 comments on commit 96adad5

Please sign in to comment.