Skip to content

Commit

Permalink
feat!: return undefined when no match in json output mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mackermans committed May 22, 2024
1 parent d87c19f commit 4cd1e1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/jq.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { PartialOptions } from "./options"

export function run(filter: string, json: any, options?: PartialOptions, jqPath?: string, cwd?: string, detached?: boolean): Promise<object | string>
export function run(filter: string, json: any, options?: PartialOptions, jqPath?: string, cwd?: string, detached?: boolean): Promise<object | string | undefined>
4 changes: 4 additions & 0 deletions src/jq.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const run = (filter, json, options = {}, jqPath, cwd, detached) => {
exec(command, args, stdin, cwd, detached)
.then((stdout) => {
if (options.output === 'json') {
if (stdout === '') {
return resolve(undefined)
}

let result
try {
result = JSON.parse(stdout)
Expand Down
22 changes: 22 additions & 0 deletions src/jq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,26 @@ describe('jq core', () => {
done(error)
})
})

it('should output empty string for a match on json output', done => {
run('.foo', { foo: '' }, { input: 'json', output: 'json' })
.then(output => {
expect(output).to.equal('')
done()
})
.catch(error => {
done(error)
})
})

it('should output undefined for no match on json output', done => {
run('select(.foo == "bar")', { foo: '' }, { input: 'json', output: 'json' })
.then(output => {
expect(output).to.equal(undefined)
done()
})
.catch(error => {
done(error)
})
})
})

0 comments on commit 4cd1e1c

Please sign in to comment.