Skip to content

Commit

Permalink
refactor: Move target logic into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Oct 29, 2018
1 parent 3fdabf1 commit 460ba73
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions lib/commands.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { SevenZipStream } from './stream.js'
import { matchBodySymbol, matchBodyHash, matchEndOfBodySymbol, matchEndOfBodyHyphen, matchEndOfHeadersSymbol, matchEndOfHeadersHyphen } from './parser.js'

function setTarget (opts, target) {
const isTargetMultiple = (Array.isArray(target))
if (isTargetMultiple) {
opts._commandArgs = opts._commandArgs.concat(target)
} else if (target) {
opts._commandArgs.push(target)
}
return opts
}

function commandStandard (commandLetter, archive, source, options) {
const opts = Object.assign({}, options)
let opts = Object.assign({}, options)
opts._commandArgs = [commandLetter]
opts._commandArgs.push(archive)

const isSourceMultiple = (Array.isArray(source))
if (isSourceMultiple) {
opts._commandArgs = opts._commandArgs.concat(source)
} else {
opts._commandArgs.push(source)
}

opts = setTarget(opts, source)
opts._matchBody = matchBodySymbol
opts._matchEndOfHeaders = matchEndOfHeadersSymbol
opts._matchEndOfBody = matchEndOfBodySymbol
Expand All @@ -21,20 +24,13 @@ function commandStandard (commandLetter, archive, source, options) {
}

function commandExtract (commandLetter, archive, output, target, options) {
const opts = Object.assign({}, options)
let opts = Object.assign({}, options)
opts._commandArgs = [commandLetter]
opts._commandArgs.push(archive)
if (output) {
opts['o'] = output
}

const isTargetMultiple = (Array.isArray(target))
if (isTargetMultiple) {
opts._commandArgs = opts._commandArgs.concat(target)
} else if (target) {
opts._commandArgs.push(target)
}

opts = setTarget(opts, target)
opts._matchBody = matchBodySymbol
opts._matchEndOfHeaders = matchEndOfHeadersSymbol
opts._matchEndOfBody = matchEndOfBodySymbol
Expand All @@ -43,16 +39,9 @@ function commandExtract (commandLetter, archive, output, target, options) {
}

function commandHash (commandLetter, target, options) {
const opts = Object.assign({}, options)
let opts = Object.assign({}, options)
opts._commandArgs = [commandLetter]

const isTargetMultiple = (Array.isArray(target))
if (isTargetMultiple) {
opts._commandArgs = opts._commandArgs.concat(target)
} else if (target) {
opts._commandArgs.push(target)
}

opts = setTarget(opts, target)
opts._matchBody = matchBodyHash
opts._matchEndOfHeaders = matchEndOfHeadersHyphen
opts._matchEndOfBody = matchEndOfBodyHyphen
Expand Down

0 comments on commit 460ba73

Please sign in to comment.