Skip to content

Commit

Permalink
feat: add transform function option for editing merged object
Browse files Browse the repository at this point in the history
Added a new option `transform`, which takes a function that accepts the merged json object and
returns an object for output. Basically the same as `edit`, but instead of being applied file by
file, it's applied at the end.
  • Loading branch information
joshswan committed Feb 15, 2020
1 parent dae6e98 commit 9aae9bd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gulp.src('jsonFiles/**/*.json')
| ---- | ---- | ------- | ----------- |
| `fileName` | `String` | `combined.json` | Output filename |
| `edit` | `Function` | `json => json` | Edit function (add/remove/edit keys during merge) |
| `transform` | `Function` | `json => json` | Transform final merged object (similar to edit but applied at the end) |
| `startObj` | `Object/Array` | `{}` | Starting object to merge into (useful for providing default values) |
| `endObj` | `Object/Array` | | Object to merge after file merging complete (useful for overwriting with special values) |
| `exportModule` | `Boolean/String` | `false` | Output `module.exports = {MERGED_JSON_DATA};` or `{exportModule} = {MERGED_JSON_DATA}` when string passed |
Expand Down Expand Up @@ -54,6 +55,23 @@ gulp.src('jsonFiles/**/*.json')
}))
.pipe(gulp.dest('./dist'));

/**
* Edit final JSON with transformer function
*/
gulp.src('jsonFiles/**/*.json')
.pipe(merge({
fileName: 'file.json',
transform: (mergedJson) => {
return {
key: {
type: 'data',
...mergedJson,
};
};
},
}))
.pipe(gulp.dest('./dist'));

/**
* Provide a default object (files are merged in order so object values will be overwritten)
*/
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ declare module 'gulp-merge-json' {
* @default json => json
*/
edit?: (json: obj, file: File) => obj | void;
/**
* Transform function (edit final merged object)
* @default json => json
*/
transform?: (json: obj) => obj;
/**
* Starting object to merge into (useful for providing default values)
* @default {}
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = function mergeJson(opts) {
// Defaults
fileName: 'combined.json',
edit: (json) => json,
transform: (json) => json,
startObj: {},
endObj: null,
exportModule: false,
Expand Down Expand Up @@ -101,7 +102,11 @@ module.exports = function mergeJson(opts) {
merged = merge(merged, options.endObj, options);
}

let contents = jsonLib.stringify(merged, options.jsonReplacer, options.jsonSpace);
let contents = jsonLib.stringify(
options.transform(merged),
options.jsonReplacer,
options.jsonSpace,
);

if (options.exportModule === true) {
contents = `module.exports = ${contents};`;
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ describe('gulp-merge-json', () => {
});
});

test('modified output based on transform function', (done) => {
const stream = gulp.src(['test/json/test1.json', 'test/json/test2.json']).pipe(merge({
transform: (json) => ({
name: json.name,
place: json.place,
tags: [...json.tags, 'sweet'],
}),
}));

stream.on('data', (file) => {
const expected = ['{', '\t"name": "Josh",', '\t"place": "San Francisco",', '\t"tags": [', '\t\t"awesome",', '\t\t"fun",', '\t\t"sweet"', '\t]', '}'].join('\n');

expect(file.contents.toString()).toBe(expected);
done();
});
});

test('uses supplied start object as base', (done) => {
const stream = gulp.src(['test/json/test1.json', 'test/json/test2.json']).pipe(merge({
startObj: { initial: 'value' },
Expand Down

0 comments on commit 9aae9bd

Please sign in to comment.