Functional Weave
Code in Python

collections.group-by-key@1.0.0

vectors.json

4,532 bytes · view raw

[
  {
    "name": "records fall into buckets named by the value at the key",
    "args": [[{ "name": "Ada", "team": "core" }, { "name": "Grace", "team": "ops" }, { "name": "Linus", "team": "core" }], "team"],
    "expect": {
      "core": [{ "name": "Ada", "team": "core" }, { "name": "Linus", "team": "core" }],
      "ops": [{ "name": "Grace", "team": "ops" }]
    }
  },
  {
    "name": "grouping a sorted list does not unsort it: input order survives inside each group",
    "args": [[{ "seq": 1, "team": "a" }, { "seq": 2, "team": "b" }, { "seq": 3, "team": "a" }, { "seq": 4, "team": "b" }, { "seq": 5, "team": "a" }], "team"],
    "expect": {
      "a": [{ "seq": 1, "team": "a" }, { "seq": 3, "team": "a" }, { "seq": 5, "team": "a" }],
      "b": [{ "seq": 2, "team": "b" }, { "seq": 4, "team": "b" }]
    }
  },
  {
    "name": "one record is one group of one",
    "args": [[{ "name": "Ada", "team": "core" }], "team"],
    "expect": { "core": [{ "name": "Ada", "team": "core" }] }
  },
  {
    "name": "no records is no groups, not a group of nothing",
    "args": [[], "team"],
    "expect": {}
  },
  {
    "name": "every distinct value is its own group",
    "args": [[{ "team": "a" }, { "team": "b" }, { "team": "c" }], "team"],
    "expect": { "a": [{ "team": "a" }], "b": [{ "team": "b" }], "c": [{ "team": "c" }] }
  },
  {
    "name": "one shared value is one group holding everything",
    "args": [[{ "seq": 1, "team": "a" }, { "seq": 2, "team": "a" }, { "seq": 3, "team": "a" }], "team"],
    "expect": { "a": [{ "seq": 1, "team": "a" }, { "seq": 2, "team": "a" }, { "seq": 3, "team": "a" }] }
  },
  {
    "name": "a missing key groups under the empty string rather than dropping the record",
    "args": [[{ "seq": 1, "team": "a" }, { "seq": 2 }], "team"],
    "expect": { "a": [{ "seq": 1, "team": "a" }], "": [{ "seq": 2 }] }
  },
  {
    "name": "a null value and a missing key land in the same group, because they mean the same",
    "args": [[{ "seq": 1, "team": null }, { "seq": 2 }, { "seq": 3, "team": "a" }], "team"],
    "expect": { "": [{ "seq": 1, "team": null }, { "seq": 2 }], "a": [{ "seq": 3, "team": "a" }] }
  },
  {
    "name": "integers name their group by their decimal digits",
    "args": [[{ "name": "Ada", "owner": 17 }, { "name": "Grace", "owner": 42 }, { "name": "Linus", "owner": 17 }], "owner"],
    "expect": {
      "17": [{ "name": "Ada", "owner": 17 }, { "name": "Linus", "owner": 17 }],
      "42": [{ "name": "Grace", "owner": 42 }]
    }
  },
  {
    "name": "a negative integer keeps its sign in the group name",
    "args": [[{ "delta": -3 }, { "delta": 0 }, { "delta": -3 }], "delta"],
    "expect": { "-3": [{ "delta": -3 }, { "delta": -3 }], "0": [{ "delta": 0 }] }
  },
  {
    "name": "booleans render as true and false in every language, not True and False",
    "args": [[{ "seq": 1, "active": true }, { "seq": 2, "active": false }, { "seq": 3, "active": true }], "active"],
    "expect": {
      "true": [{ "seq": 1, "active": true }, { "seq": 3, "active": true }],
      "false": [{ "seq": 2, "active": false }]
    }
  },
  {
    "name": "records with different shapes group on the one field they share",
    "args": [[{ "kind": "invoice", "total": 120 }, { "kind": "credit", "reason": "returned" }, { "kind": "invoice", "lines": [1, 2] }], "kind"],
    "expect": {
      "invoice": [{ "kind": "invoice", "total": 120 }, { "kind": "invoice", "lines": [1, 2] }],
      "credit": [{ "kind": "credit", "reason": "returned" }]
    }
  },
  {
    "name": "the empty string is an ordinary group name when it is the real value",
    "args": [[{ "seq": 1, "team": "" }, { "seq": 2, "team": "a" }], "team"],
    "expect": { "": [{ "seq": 1, "team": "" }], "a": [{ "seq": 2, "team": "a" }] }
  },
  {
    "name": "a fractional number cannot name a group: the three languages print it differently",
    "args": [[{ "rate": 1.5 }], "rate"],
    "expectError": "fractional number"
  },
  {
    "name": "a list cannot name a group",
    "args": [[{ "tags": ["a", "b"] }], "tags"],
    "expectError": "list or map"
  },
  {
    "name": "a map cannot name a group",
    "args": [[{ "meta": { "team": "a" } }], "meta"],
    "expectError": "list or map"
  },
  {
    "name": "an integer too large to survive a JavaScript round trip is rejected",
    "args": [[{ "id": 9007199254740993 }], "id"],
    "expectError": "out-of-range number"
  },
  {
    "name": "an empty key name is a caller bug",
    "args": [[{ "team": "a" }], ""],
    "expectError": "non-empty key name"
  }
]