Skip to content

Commit

Permalink
feat: added sort_by
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Feb 5, 2018
1 parent 7a7f3be commit d53d583
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/jq.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@
const mapped = to_entries(input).map(arg)
return from_entries(mapped)
},
"join": arg => input => input.join(arg(input))
"join": arg => input => input.join(arg(input)),
"sort_by": arg => input => unpack(input).sort((a, b) => {
const va = arg(a)
const vb = arg(b)
if (va < vb) return -1
if (va > vb) return 1
return 0
})
}
}

Expand Down
1 change: 1 addition & 0 deletions src/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
["tonumber", ["tonumber"], [1, -1, "1", "-1", "1.23", "-.1"]],
["tostring", ["tostring"], [1, -1, "1", "-1", "1.23", "-.1", [1], {"a": 2}]],
["sort", ["sort", "[4, 5, 6] | sort"], [[1, -1], ["1", "-1", "1.23", "-.1"]]],
["sort_by", ["sort_by(-.)", "sort_by(1 + .)", "sort_by(1)"], [[1, -1], [4.12, 42.3, -342, 0, 32, 1, 2, 3, 6, 6]]],
["join", ["join(\", \")", "join(\"\")", "join(.[0])"], [["a","b,c,d","e"], ["foo"], [], ["foo", "bar"]]],
["Additive inverse", ["-(1 + 3)", "-(-1)", ".a | -(.b)", "[--1]"], [
{"a": {"b": 3}},
Expand Down

0 comments on commit d53d583

Please sign in to comment.