Skip to content

Commit

Permalink
Not sure whether this is working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
David Fetter committed Mar 20, 2019
1 parent 3ea0199 commit b4a089d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
19 changes: 12 additions & 7 deletions docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -935,19 +935,21 @@ sections:
input: '{"a":{"b":1},"x":{"y":2}}'
output: ['{"a":{},"x":{"y":2}}']

- title: "`to_entries`, `from_entries`, `with_entries`"
- title: "`to_entries`, `from_entries`, `with_entries`, `pipe_entries`"
body: |
These functions convert between an object and an array of
key-value pairs. If `to_entries` is passed an object, then
for each `k: v` entry in the input, the output array
includes `{"key": k, "value": v}`.
`from_entries` does the opposite conversion, and
`from_entries` does the opposite conversion.
`with_entries(foo)` is a shorthand for `to_entries |
map(foo) | from_entries`, useful for doing some operation to
all keys and values of an object. `from_entries` accepts key, Key,
name, Name, value and Value as keys.
all keys and values of an object. `pipe_entries(foo)` is shorthand
for `to_entries | foo | from_entries`, useful for doing some
operation on the entire object like sorting it by its keys.
`from_entries` accepts key, Key, name, Name, value and Value as keys.
examples:
- program: 'to_entries'
Expand All @@ -959,6 +961,9 @@ sections:
- program: 'with_entries(.key |= "KEY_" + .)'
input: '{"a": 1, "b": 2}'
output: ['{"KEY_a": 1, "KEY_b": 2}']
- program: 'pipe_entries(sort_by(.key))'
input: '{"foo": 1, "bar": 2}'
output: ['{"bar": 2, "foo": 1}']


- title: "`select(boolean_expression)`"
Expand Down Expand Up @@ -2529,7 +2534,7 @@ sections:

- title: 'Destructuring Alternative Operator: `?//`'
body: |
The destructuring alternative operator provides a concise mechanism
for destructuring an input that can take one of several forms.
Expand All @@ -2538,10 +2543,10 @@ sections:
the first event for each resource. The API (having been clumsily
converted from XML) will only wrap the events in an array if the resource
has multiple events:
{"resources": [{"id": 1, "kind": "widget", "events": {"action": "create", "user_id": 1, "ts": 13}},
{"id": 2, "kind": "widget", "events": [{"action": "create", "user_id": 1, "ts": 14}, {"action": "destroy", "user_id": 1, "ts": 15}]}]}
We can use the destructuring alternative operator to handle this structural change simply:
.resources[] as {$id, $kind, events: {$user_id, $ts}} ?// {$id, $kind, events: [{$user_id, $ts}]} | {$user_id, $kind, $id, $ts}
Expand Down
1 change: 1 addition & 0 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def recurse_down: recurse;
def to_entries: [keys_unsorted[] as $k | {key: $k, value: .[$k]}];
def from_entries: map({(.key // .Key // .name // .Name): (if has("value") then .value else .Value end)}) | add | .//={};
def with_entries(f): to_entries | map(f) | from_entries;
def pipe_entries(f): to_entries | f | from_entries;
def reverse: [.[length - 1 - range(0;length)]];
def indices($i): if type == "array" and ($i|type) == "array" then .[$i]
elif type == "array" then .[[$i]]
Expand Down

0 comments on commit b4a089d

Please # to comment.