Skip to content

Commit

Permalink
Convert objects to key value lists. Closes #167 (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjstires authored and sindresorhus committed Apr 27, 2017
1 parent 55004ca commit 9ddcbd0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const gutil = require('gulp-util');
const through = require('through2');
// TODO: Use execa localDir option when available
const npmRunPath = require('npm-run-path');
const utils = require('./utils');

const convertObjectToList = utils.convertObjectToList;

const HUNDRED_MEGABYTES = 1000 * 1000 * 100;

Expand All @@ -25,6 +28,10 @@ module.exports = opts => {

if (MULTIPLE_OPTS.indexOf(key) > 0 && Array.isArray(val)) {
opts[key] = val.join(',');

// Convert an object into comma separated list.
} else if (typeof val === 'object') {
opts[key] = convertObjectToList(val);
}
}

Expand Down Expand Up @@ -57,10 +64,10 @@ module.exports = opts => {
this.emit('_result', result);
done();
})
.catch(err => {
this.emit('error', new gutil.PluginError('gulp-mocha', err));
done();
});
.catch(err => {
this.emit('error', new gutil.PluginError('gulp-mocha', err));
done();
});

if (!opts.suppress) {
proc.stdout.pipe(process.stdout);
Expand Down
16 changes: 16 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const assert = require('assert');
const utils = require('../utils');

const convertObjectToList = utils.convertObjectToList;

describe('Utils', () => {
describe('convertObjectToList', () => {
it('produces a comma separated string of k=v', done => {
const actual = convertObjectToList({key1: 'value1', key2: 'value2', key99: 'value99'});
const expected = 'key1=value1,key2=value2,key99=value99';
assert(actual === expected);
done();
});
});
});
20 changes: 20 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

function objectEntries(object) {
const entries = [];
for (const key of Object.keys(object)) {
const value = object[key];
entries.push([key, value]);
}
return entries;
}

function convertObjectToList(object) {
return objectEntries(object)
.reduce((result, current) => result.concat(`${current[0]}=${current[1]}`), [])
.join(',');
}

module.exports = {
convertObjectToList
};

0 comments on commit 9ddcbd0

Please sign in to comment.