Merge pull request #628 from slapresta/map-on-objects

Modify map\1 so that it works on objects
This commit is contained in:
Nico Williams committed 2014-12-26 21:50:11 -06:00
commit 36077da66a
2 files changed
+10 -2

No files matched your search

+1
View File
@@ -961,6 +961,7 @@ static block bind_bytecoded_builtins(block b) {
static const char* const jq_builtins[] = { static const char* const jq_builtins[] = {
"def break: error(\"break\");", "def break: error(\"break\");",
"def map(f): [.[] | f];", "def map(f): [.[] | f];",
"def map_values(f): .[] |= f;",
"def select(f): if f then . else empty end;", "def select(f): if f then . else empty end;",
"def sort_by(f): _sort_by_impl(map([f]));", "def sort_by(f): _sort_by_impl(map([f]));",
"def group_by(f): _group_by_impl(map([f]));", "def group_by(f): _group_by_impl(map([f]));",
+9 -2
View File
@@ -777,21 +777,28 @@ sections:
null and objects would, but with the given message as the null and objects would, but with the given message as the
error's value. error's value.
- title: "`map(x)`" - title: "`map(x)`, `map_values(x)`"
body: | body: |
For any filter `x`, `map(x)` will run that filter for each For any filter `x`, `map(x)` will run that filter for each
element of the input array, and produce the outputs a new element of the input array, and produce the outputs a new
array. `map(.+1)` will increment each element of an array of numbers. array. `map(.+1)` will increment each element of an array of numbers.
Similarly, `map_values(x)` will run that filter for each element,
but it will return an object when an object is passed.
`map(x)` is equivalent to `[.[] | x]`. In fact, this is how `map(x)` is equivalent to `[.[] | x]`. In fact, this is how
it's defined. it's defined. Similarly, `map_values(x)` is defined as `.[] |= x`.
examples: examples:
- program: 'map(.+1)' - program: 'map(.+1)'
input: '[1,2,3]' input: '[1,2,3]'
output: ['[2,3,4]'] output: ['[2,3,4]']
- program: 'map_values(.+1)'
input: '{"a": 1, "b": 2, "c": 3}'
output: ['{"a": 2, "b": 3, "c": 4}']
- title: "`paths`, `paths(node_filter)`, `leaf_paths`" - title: "`paths`, `paths(node_filter)`, `leaf_paths`"
body: | body: |