Skip to content

Commit

Permalink
fix: Remove nested-array bug with wildcards
Browse files Browse the repository at this point in the history
When passing wildcards the array resting of .toChildProcessArgs() is no
longer returning and nester array.
Bug spotted thanks to unit testing
Updated test to pass coverage at 100 %
  • Loading branch information
q2s2t committed Aug 23, 2018
1 parent 7315c83 commit 59a006a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
Binary file removed test/.DS_Store
Binary file not shown.
14 changes: 11 additions & 3 deletions test/util/switches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ describe('Utility: `switches`', function () {
expect(r).to.contain('-y')
})

it('should return complex values with spaces and emoji', function () {
it('should return complex values with spaces and quotes', function () {
var r = toChildProcessArgs({
ssc: true,
ssw: true,
m0: '=BCJ',
m1: '=LZMA:d=21',
p: 'My Super Pasw,àù£*'
p: 'My Super Pasw,àù£*"'
})
expect(r).to.contain('-ssc')
expect(r).to.contain('-ssw')
expect(r).to.contain('-m0=BCJ')
expect(r).to.contain('-m1=LZMA:d=21')
expect(r).to.contain('-pMy Super Pasw,àù£*')
expect(r).to.contain('-pMy Super Pasw,àù£*"')
expect(r).to.contain('-y')
})

Expand All @@ -91,4 +91,12 @@ describe('Utility: `switches`', function () {
expect(r).to.contain('-i!*.png')
expect(r).to.contain('-r0')
})

it('should add wildcards', function () {
var r = toChildProcessArgs({
wildcards: ['*.jpg', '*.png']
})
expect(r).to.contain('*.jpg')
expect(r).to.contain('*.png')
})
})
39 changes: 25 additions & 14 deletions util/switches.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* are based on the 7-zip documentation.
*/
const swDefaultBool = {
r: false, // Recurse subdirectories. For `-r0` usage see `raw`
sdel: false, // Delete files after compression
spl: false, // Set Large Pages mode
sni: false, // Store NT security information
Expand All @@ -28,10 +29,12 @@ const swContextBool = {
* Switches that can be applied multiple times
*/
const swRepeating = {
ai: undefined,
ax: undefined,
i: undefined,
x: undefined
ai: undefined, // Include archive filenames
ax: undefined, // Exclude archive filenames
i: undefined, // Include filenames
m: undefined, // Set Compression Method
x: undefined, // Exclude filenames
raw: undefined // Special `node-7z` option
}

/**
Expand All @@ -40,41 +43,49 @@ const swRepeating = {
* @param {Object} switches An object of options
* @return {array} Array to pass to the `run` function.
*/
export default function toChildProcessArgs (switches = swDefaultBool) {
export function toChildProcessArgs (switches) {
const swDefaultBoolKeys = Object.keys(swDefaultBool)
const swContextBoolKeys = Object.keys(swContextBool)
const swRepeatingKeys = Object.keys(swRepeating)
const swCopy = {...swDefaultBool}
Object.assign(swCopy, switches)
let cmdArgs = []

Object.entries(switches).forEach(([swName, swVal]) => {
Object.entries(swCopy).forEach(function ([swName, swVal]) {
// Handle wildcards
if (swName === 'wildcards') {
cmdArgs.unshift(swVal)
const isWildcard = (swName === 'wildcards')
if (isWildcard) {
cmdArgs = swVal.concat(cmdArgs)
return
}

// Handle boolean switches
if (swDefaultBoolKeys.includes(swName)) {
const isDefaultBool = (swDefaultBoolKeys.includes(swName))
if (isDefaultBool) {
if (swVal === true) {
cmdArgs.push(`-${swName}`)
}
return
}

// Handle context boolean switches
if (swContextBoolKeys.includes(swName)) {
const isContextBool = (swContextBoolKeys.includes(swName))
if (isContextBool) {
let swSuffix = (swVal === true) ? '' : '-'
cmdArgs.push(`-${swName}${swSuffix}`)
return
}

// Handle repeating switches. They can be string or array
if (swRepeatingKeys.includes(swName)) {
if (typeof swVal === 'string') {
const isRepeating = (swRepeatingKeys.includes(swName))
if (isRepeating) {
const isString = (typeof swVal === 'string')
if (isString) {
swVal = [swVal]
}
swVal.forEach(([swRepeatingString]) => {
cmdArgs.push(`-${swName}${swRepeatingString}`)
swVal.forEach(function (swRepeatingString) {
const swPrefix = (swName === 'raw') ? '' : `-${swName}`
cmdArgs.push(`${swPrefix}${swRepeatingString}`)
})
return
}
Expand Down

0 comments on commit 59a006a

Please sign in to comment.