Extra builtins for jq.
Warning
This is mostly a proof of concept at the moment.
To install first time or update run make install:
$ make install
Installing ~/.jq symlink
Rebuilding .jq
$ jq -r gron <<< '{"hello": "world"}'
. = {}
.hello = "world"make install includes src/*.jq so you can put your own functions in src/local.jq for example.
from_basefrom_base($base)from_base($base; $table)to_base($base)to_base($base; prefix)to_base($base; prefix; $table)chunks($size)chunks($size; s)countcount_by(f)from_durationto_durationgronrunsruns_by(f)runs_by(f; s)from_urito_uriwith_uri(f)
"0xff" | from_base→255
"ff" | from_base(16)→255
"baab" | from_base(2; {"a": 0, "b": 1})→9
255 | to_base(16; "")→"0xff"
255 | to_base(16; "")→"ff"
to_base($base; prefix; $table) - Convert number to string in base using custom prefix and digits table.
9 | to_base(2; "2#"; "ab")→"2#baab"
[1,2,3,4,5,6] | chunks(2)→[1, 2], [3, 4], [5, 6]
chunks(2; 1,2,3,4,5,6)→[1, 2], [3, 4], [5, 6]
["a","b","b","b","c","c"] | count→[["a",1],["b",3],["c",2]]
Similar to group_by(f) but counts instead.
[101,201,300] | count_by(. % 10)→[[0,1],[1,2]]
Convert a duration string into seconds.
"01:02:03.45" | from_duration→3723.45
Convert seconds into duration string.
3723.45 | to_duration→"01:02:03.45"
Similar to https://github.com/tomnomnom/gron.
$ jq -r gron <<< '{"a":1}'`
. = {}
.a = 1{a: [1], b: true} | gron→". = {}", ".a = []", ".a[0] = 1", ".b = true"
[1, 2, 2, 3] | runs→[1], [2, 2], [3]
[1, 2, 2.4, 3] | runs_by(floor)→[1], [2, 2.4], [3]
[{a:1,b:1}, {a:2,b:2}, {a:3,b:2}] | runs_by(.[]; .b)→[[{"a":1,"b":1}], [{"a":2,"b":2},{"a":3,"b":2}]
Implements rfc3986: Uniform Resource Identifier (URI): Generic Syntax
Directly based on Appendix B. Parsing a URI Reference with a Regular Expression
"scheme://authority/path?key=value#fragment" | from_uri→{ "scheme": "scheme", "authority": "authority", "path": "/path", "query": { "key": "value" }, "fragment": "fragment" }
Implementation based on Section 5.3. Component Recomposition
{ "scheme": "scheme", "authority": "authority", "path": "/path", "query": { "key": "value" }, "fragment": "fragment" }"scheme://authority/path?key=value#fragment" | to_uri | "scheme://authority/path?key=value#fragment"
Useful for mutating from one URI to another
"https://example.com/some-random-id?si=tracking-parameter" | with_uri(del(.query.si))→"https://example.com/some-random-id"
# run tests
$ make test
# regenerate README.md documentation
$ make README.md- Functions:
ungronexpr_to_path/path_to_expr- More array functions
- json5?
- Include/Exclude config?
- Test with jq, gojq and jaq
- As all don't support run-tests maybe generate jq code?
- Use public domain license to be copy/paste friendly?
See LICENSE.