Skip to content

Commit

Permalink
Merge pull request #81 from qubidt/keys_unsorted
Browse files Browse the repository at this point in the history
Add keys_unsorted
  • Loading branch information
01mf02 committed Jun 26, 2023
2 parents a280379 + 0e440f9 commit 00ed64b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
4 changes: 3 additions & 1 deletion jaq-core/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ const CORE: [(&str, usize, RunPtr); 28] = [
Box::new(cv.0.inputs.map(|r| r.map_err(Error::Parse)))
}),
("length", 0, |_, cv| box_once(cv.1.len())),
("keys", 0, |_, cv| box_once(cv.1.keys().map(Val::arr))),
("keys_unsorted", 0, |_, cv| {
box_once(cv.1.keys().map(Val::arr))
}),
("floor", 0, |_, cv| box_once(cv.1.round(|f| f.floor()))),
("round", 0, |_, cv| box_once(cv.1.round(|f| f.round()))),
("ceil", 0, |_, cv| box_once(cv.1.round(|f| f.ceil()))),
Expand Down
12 changes: 6 additions & 6 deletions jaq-core/tests/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ fn json() {
}

#[test]
fn keys() {
give(json!([0, null, "a"]), "keys", json!([0, 1, 2]));
give(json!({"a": 1, "b": 2}), "keys", json!(["a", "b"]));
fn keys_unsorted() {
give(json!([0, null, "a"]), "keys_unsorted", json!([0, 1, 2]));
give(json!({"a": 1, "b": 2}), "keys_unsorted", json!(["a", "b"]));

fail(json!(0), "keys", Error::Keys(Val::Int(0)));
fail(json!(null), "keys", Error::Keys(Val::Null));
fail(json!(0), "keys_unsorted", Error::Keys(Val::Int(0)));
fail(json!(null), "keys_unsorted", Error::Keys(Val::Null));
}

#[test]
Expand Down Expand Up @@ -115,7 +115,7 @@ fn recurse() {

const RECURSE_PATHS: &str = "def paths:
{ x: ., p: [] } |
recurse((.x | keys?)[] as $k | .x |= .[$k] | .p += [$k]) |
recurse((.x | keys_unsorted?)[] as $k | .x |= .[$k] | .p += [$k]) |
.p | if . == [] then empty else . end;";

yields!(
Expand Down
2 changes: 1 addition & 1 deletion jaq-core/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn alt() {
#[test]
fn try_() {
give(json!(0), ".?", json!(0));
give(json!(0), "keys?, 1", json!(1));
give(json!(0), "keys_unsorted?, 1", json!(1));
give(json!(0), "[(1, error, 2)?]", json!([1, 2]));
}

Expand Down
1 change: 1 addition & 0 deletions jaq-std/src/std.jq
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def nth(n): .[ n];
def nth(n; g): last(limit(n + 1; g));

# Objects <-> Arrays
def keys: keys_unsorted | sort;
def to_entries: [keys[] as $k | { key: $k, value: .[$k] }];
def from_entries: map({ (.key): .value }) | add + {};
def with_entries(f): to_entries | map(f) | from_entries;
Expand Down
6 changes: 6 additions & 0 deletions jaq-std/tests/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ yields!(join_nums, r#"[2, 3, 4, 5] | join(1)"#, 17);

yields!(map, "[1, 2] | map(.+1)", [2, 3]);

yields!(
keys,
r#"{"foo":null,"abc":null,"fax":null,"az":null} | keys"#,
["abc", "az", "fax", "foo"]
);

#[test]
fn min_max() {
give(json!([]), "min", json!(null));
Expand Down

0 comments on commit 00ed64b

Please sign in to comment.