Skip to content

Commit

Permalink
Make tonumber work on true and false jqlang#3112
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfetter committed May 7, 2024
1 parent ed8f715 commit 4698694
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ static jv f_tonumber(jq_state *jq, jv input) {
jv_free(input);
return number;
}
if (jv_get_kind(input) == JV_KIND_FALSE) {
return jv_number(0);
}
if (jv_get_kind(input) == JV_KIND_TRUE) {
return jv_number(1);
}
return type_error(input, "cannot be parsed as a number");
}

Expand Down
4 changes: 2 additions & 2 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -2038,8 +2038,8 @@ null
2

.[] |= try tonumber
["1", "2a", "3", " 4 ", "5.67", ".89", "-876", "+5.43", 21]
[1, 3, 5.67, 0.89, -876, 5.43, 21]
["1", "2a", "3", " 4 ", "5.67", ".89", "-876", "+5.43", 21, true, false]
[1, 3, 5.67, 0.89, -876, 5.43, 21, 1, 0]

# Also 1859, but from 2073
any(keys[]|tostring?;true)
Expand Down
4 changes: 2 additions & 2 deletions tests/man.test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4698694

Please sign in to comment.