MiSawa / xq

Pure rust implementation of jq
MIT License
333 stars 18 forks source link

Document intentional behavior difference #82

Open MiSawa opened 2 years ago

MiSawa commented 2 years ago

TODO:

MiSawa commented 2 years ago

93

MiSawa commented 2 years ago
$ xq -nc '[nan, nan] | group_by(.)'
[[null,null]]
$ jq -nc '[nan, nan] | group_by(.)'
[[null],[null]]
$ xq -nc '[nan < nan, nan > nan]'
[false,false]
$ jq -nc '[nan < nan, nan > nan]'
[true,false]
MiSawa commented 2 years ago

Though I might change the group_by behavior.

MiSawa commented 2 years ago

111

MiSawa commented 2 years ago

https://github.com/MiSawa/xq/blob/e25fa19baff073d0b563bbbe50ce5f819df3f988/tests/hand_written/regex.rs#L138

MiSawa commented 2 years ago

Also -- we currently use oniguruma (via onig crate), but we might want to use regex crate instead.

MiSawa commented 2 years ago

Maybe make flatten(0.1) mean flatten(0) or flatten(1) again? ref: https://twitter.com/itchyny/status/1503660699762913280

Edit: Be consistent about whether or not to handle objects https://twitter.com/itchyny/status/1503756509771354113

MiSawa commented 2 years ago
$ cargo run -- -nc '[1,2,3] | (.[{start:1}], .[{end:2}])'
    Finished dev [unoptimized + debuginfo] target(s) in 0.16s
     Running `target/debug/xq -nc '[1,2,3] | (.[{start:1}], .[{end:2}])'`
[2,3]
[1,2]
$ cargo run -- -nc '[1,2,3] | getpath([{start:1}])'
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
     Running `target/debug/xq -nc '[1,2,3] | getpath([{start:1}])'`
[2,3]
$ jq -nc '[1,2,3] | getpath([{start:1}])'
jq: error (at <unknown>): Start and end indices of an array slice must be numbers
[1]    1878799 exit 5     jq -nc '[1,2,3] | getpath([{start:1}])'
$ jq -nc '[1,2,3] | (.[{start:1}], .[{end:2}])'
jq: error (at <unknown>): Start and end indices of an array slice must be numbers
[1]    1878822 exit 5     jq -nc '[1,2,3] | (.[{start:1}], .[{end:2}])'

or change it to match the behavior of jq

itchyny commented 2 years ago
❯ jq -n '0/0'
null

❯ xq -n '0/0'
Error: DivModByZero

❯ jq -n '1,"",1 | normals'
1
1

❯ xq -n '1,"",1 | normals'
1
Error: InvalidArgType("is_normal", "")

❯ jq -n 'isfinite'
false

❯ xq -n 'isfinite'
Error: InvalidArgType("is_infinite", null)

❯ jq -n 'ltrimstr("")'
null

❯ xq -n 'ltrimstr("")'
Error: InvalidArgType("startswith", null)

❯ jq -n 'rtrimstr("")'
null

❯ xq -n 'rtrimstr("")'
Error: InvalidArgType("endswith", null)
itchyny commented 2 years ago
❯ jq -n '[{"Key":"x","Value":0},{"Name":"y","Value":1}] | from_entries'
{
  "x": 0,
  "y": 1
}

❯ xq -n '[{"Key":"x","Value":0},{"Name":"y","Value":1}] | from_entries'
Error: ObjectIndexByNonString(null)
itchyny commented 2 years ago
❯ jq -n '[39] | implode | @html'
"&apos;"

❯ xq -n '[39] | implode | @html'
"&#x27;"

❯ jq -n '"aGVsbG8====" | @base64d'
"hello"

❯ xq -n '"aGVsbG8====" | @base64d'
Error: InvalidAsBase64(InvalidByte(7, 61))
itchyny commented 2 years ago

The behavior of ltrimstr, rtrimstr is useful when trimming all the strings recursively, but not sure how many people notice this behavior.

❯ jq -n '{a:{b:"hello world",c:["hello test"]},d:"hello sample"} | .. |= ltrimstr("hello ")'
{
  "a": {
    "b": "world",
    "c": [
      "test"
    ]
  },
  "d": "sample"
}

❯ xq -n '{a:{b:"hello world",c:["hello test"]},d:"hello sample"} | .. |= ltrimstr("hello ")'
Error: InvalidArgType("startswith", {"d": "hello sample", "a": {"c": ["hello test"], "b": "hello world"}})

When we use gsub, we need to filter by strings after all.

❯ jq -n '{a:{b:"hello world",c:["hello test"]},d:"hello sample"} | .. |= gsub("hello "; "")'
jq: error (at <unknown>): object ({"a":{"b":"...) cannot be matched, as it is not a string

❯ jq -n '{a:{b:"hello world",c:["hello test"]},d:"hello sample"} | (.. | strings) |= gsub("hello "; "")'
{
  "a": {
    "b": "world",
    "c": [
      "test"
    ]
  },
  "d": "sample"
}