Creating an array where under some conditions we include extra pairs of values.
var mysqlArgs = ['-h', mysqlConfig.host, '-d', mysqlConfig.database];
if (mysqlConfig.user) {
mysqlArgs.push('-U', mysqlConfig.user);
}
if (mysqlConfig.password) {
mysqlArgs.push('-p', mysqlConfig.password);
}
var mysqlArgs = _({
'-h': mysqlConfig.host,
'-d': mysqlConfig.database,
'-U': mysqlConfig.user,
'-p': mysqlConfig.password
})
.pick(_.identity) // remove falsey values
.pairs() // turn into an array of ['-key', 'value'] pairs
.flatten() // turn into a single array like ['-key1' 'value1' '-key2' 'value2']
.value();
pick(_.identity) is really slick