Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tonumber work on true and false #3112 #3113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading