Skip to content

Commit

Permalink
Refactor createCommand and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Jun 13, 2016
1 parent 78b8629 commit 57d370d
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/jq.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,33 @@ const validateJsonPath = (json) => {
return json
}

const runJqNullInput = (filter, json) => {
return new Promise((resolve, reject) => {
execa(
'jq',
[
'--null-input',
`${filter} | ${JSON.stringify(json)}`
]
)
.then(({ stdout }) => {
resolve(JSON.parse(stdout))
})
.catch(reject)
})
const buildNullInputParams = (filter, json) => {
return [
'--null-input',
`${filter} | ${JSON.stringify(json)}`
]
}

const runJq = (filter, jsonPath) => {
const createJqCommand = (filter, json, options = {}) => {
const command = {
cmd: 'jq',
params: []
}

if (options.nullInput === true) {
command.params = buildNullInputParams(filter, json)
} else {
validateJsonPath(json)
command.params = [filter, json]
}

return command
}

const runJq = (filter, json, options) => {
return new Promise((resolve, reject) => {
execa(
'jq',
[filter, jsonPath]
)
const { cmd, params } = createJqCommand(filter, json, options)
execa(cmd, params)
.then(({ stdout }) => {
resolve(JSON.parse(stdout))
})
Expand All @@ -62,11 +67,6 @@ const runJq = (filter, jsonPath) => {

export const run = (filter, json, options = {}) => {
return new Promise((resolve, reject) => {
if (options.nullInput === true) {
runJqNullInput(filter, json).then(resolve).catch(reject)
} else {
const jsonPath = validateJsonPath(json)
runJq(filter, jsonPath).then(resolve).catch(reject)
}
runJq(filter, json, options).then(resolve).catch(reject)
})
}

0 comments on commit 57d370d

Please sign in to comment.