Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
rejas committed Dec 26, 2018
1 parent ba06147 commit e6293ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
var jsbeautify = require('js-beautify').js_beautify;
var merge = require('deepmerge');
var deepmerge = require('deepmerge');
var through = require('through2');
var PluginError = require('plugin-error');
var detectIndent = require('detect-indent');

module.exports = function (editor, jsbeautifyOptions, mergeOptions) {

module.exports = function (editor, jsbeautifyOptions, deepmergeOptions) {

/*
* extras merge options
* this options is only pass to deepmerge
* deepmerge options
*/
mergeOptions = mergeOptions || {};
deepmergeOptions = deepmergeOptions || {};

/*
* create 'editBy' function from 'editor'
Expand All @@ -23,7 +21,7 @@ module.exports = function (editor, jsbeautifyOptions, mergeOptions) {
}
else if (typeof editor === 'object') {
// edit JSON object by merging with user specific object
editBy = function(json) { return merge(json, editor, mergeOptions); };
editBy = function(json) { return deepmerge(json, editor, deepmergeOptions); };
}
else if (typeof editor === 'undefined') {
throw new PluginError('gulp-json-editor', 'missing "editor" option');
Expand Down Expand Up @@ -59,7 +57,7 @@ module.exports = function (editor, jsbeautifyOptions, mergeOptions) {
var indent = detectIndent(file.contents.toString('utf8'));

// beautify options for this particular file
var beautifyOptions = merge({}, jsbeautifyOptions); // make copy
var beautifyOptions = deepmerge({}, jsbeautifyOptions); // make copy
beautifyOptions.indent_size = beautifyOptions.indent_size || indent.amount || 2;
beautifyOptions.indent_char = beautifyOptions.indent_char || (indent.type === 'tab' ? '\t' : ' ');
beautifyOptions.beautify = !('beautify' in beautifyOptions && !beautifyOptions.beautify);
Expand Down
10 changes: 5 additions & 5 deletions test/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ it('should bypass beautification when property is set', function(done) {
});


it('should merged with arrayMerge of overwriteMerge', function (done) {
it('should pass-through third argument to deepmerge and do an overwriteMerge', function(done) {

var stream = gulp.src('test/test.json').pipe(json({
"authors": ["tomcat"]
authors: ['tomcat'],
},{},{
arrayMerge: function (dist,source,options) {
arrayMerge: function(dist,source,options) {
return source;
}
},
}));

stream.on('data', function (file) {
stream.on('data', function(file) {
var expected =
'{\n' +
' "name": "test object",\n' +
Expand Down

0 comments on commit e6293ad

Please sign in to comment.