collections.group-by-key
Group records by the value at one key, preserving input order inside each group.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 18 tests, run in TypeScript, Python and Rust.
What it does
Within a group, records keep the order they arrived in. That is the property callers actually depend on - grouping a sorted list must not unsort it - and it is guaranteed in all three languages.
A missing key and a null value are the same thing: both group under the empty string "". A JSON document that omits a field and one that sets it to null mean the same thing to every reader, and dropping those records would silently lose data from a total. The cost is that a record whose value is genuinely "" lands in the same group; in practice an empty string is a missing value, and if the distinction matters, normalise before calling.
For example
groupByKey(records ×3, team)→ core ×2, ops ×1 records fall into buckets named by the value at the keygroupByKey(records ×5, team)→ a ×3, b ×2 grouping a sorted list does not unsort it: input order survives inside each groupgroupByKey(records ×1, team)→ core ×1 one record is one group of one
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
export function groupByKey(records: readonly Readonly<Record<string, unknown>>[], key: string): Readonly<Record<string, readonly Readonly<Record<string, unknown>>[]>>
| records | record[] | open JSON-ish maps; shapes may differ between records |
| key | string | the field whose value names the group |
| returns | map<record[]> | map from group name to the records in that group, in input order |
Your code names it in one line, in the file that uses it
import { groupByKey } from "#fune/collections.group-by-key@^1";
/** An open record, the manifest's `record`: a JSON-ish map whose shape is not known ahead of time. */
type GroupableRecord = Readonly<Record<string, unknown>>;
/** Largest integer JavaScript can hold exactly; beyond it the three languages disagree. */
const SAFE_INTEGER = 9007199254740991;
/**
* The name of the group a value belongs to.
*
* Each language has its own default string conversion and they disagree
* (Python prints True where JavaScript prints true, and 1.0 where JavaScript
* prints 1), so the rendering is spelled out here instead of inherited.
*/
function groupNameOf(value: unknown, key: string): string {
// Absent and null are the same thing: a document that omits a field and one
// that nulls it mean the same to every reader.
if (value === undefined || value === null) return "";
if (typeof value === "string") return value;
if (typeof value === "boolean") return value ? "true" : "false";
if (typeof value === "number") {
if (!Number.isInteger(value)) {
throw new TypeError(`cannot group by the fractional number ${value} at "${key}"`);
}
if (Math.abs(value) > SAFE_INTEGER) {
throw new RangeError(`cannot group by the out-of-range number ${value} at "${key}"`);
}
return String(value);
}
throw new TypeError(`cannot group by the list or map at "${key}"`);
}
/**
* Group `records` by the value at `key`.
*
* Records keep their input order inside each group, so grouping a sorted list
* never unsorts it - the property callers actually depend on.
*/
export function groupByKey(
records: readonly GroupableRecord[],
key: string,
): Readonly<Record<string, readonly GroupableRecord[]>> {
if (!Array.isArray(records)) {
throw new TypeError("groupByKey needs a list of records");
}
if (typeof key !== "string" || key.length === 0) {
throw new TypeError("groupByKey needs a non-empty key name");
}
// A null prototype, so that a record grouping under "__proto__" or
// "constructor" creates an ordinary group instead of corrupting the result.
const groups: { [group: string]: GroupableRecord[] } = Object.create(null);
for (const record of records) {
const name = groupNameOf(record === null || record === undefined ? undefined : record[key], key);
if (groups[name] === undefined) groups[name] = [];
groups[name].push(record);
}
return groups;
}
/** How many records fall in each group, without carrying the records themselves. */
export function countByKey(records: readonly GroupableRecord[], key: string): Readonly<Record<string, number>> {
const counts: { [group: string]: number } = Object.create(null);
for (const [name, group] of Object.entries(groupByKey(records, key))) {
counts[name] = group.length;
}
return counts;
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add collections.group-by-key
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./collections.group-by-key-1.0.0-typescript.fune, or fetch it from a terminal with fune pull collections.group-by-key@1.0.0:typescript.
The whole function, every language, is one file too: collections.group-by-key-1.0.0.fune, 16,841 bytes, sha256 640bae4c083fa730ec305773e5380386cb168bcaf3f4e6d84a52df863327dc20. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
// fune: before collections.group-by-key
after — your function gets the result and the arguments, and returns the final result.
// fune: after collections.group-by-key
replace — it requires no other capability, so there is no dependency to replace.
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show collections.group-by-key --steps.
// fune: step collections.group-by-key after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| records fall into buckets named by the value at the key | records ×3, team | → | core ×2, ops ×1 |
| grouping a sorted list does not unsort it: input order survives inside each group | records ×5, team | → | a ×3, b ×2 |
| one record is one group of one | records ×1, team | → | core ×1 |
| no records is no groups, not a group of nothing | , team | → | |
| every distinct value is its own group | records ×3, team | → | a ×1, b ×1, c ×1 |
| one shared value is one group holding everything | records ×3, team | → | a ×3 |
| a missing key groups under the empty string rather than dropping the record | records ×2, team | → | a ×1, ×1 |
| a null value and a missing key land in the same group, because they mean the same | records ×3, team | → | ×2, a ×1 |
| integers name their group by their decimal digits | records ×3, owner | → | 17 ×2, 42 ×1 |
| a negative integer keeps its sign in the group name | records ×3, delta | → | 0 ×1, -3 ×2 |
Show the other 8 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| booleans render as true and false in every language, not True and False | records ×3, active | → | true ×2, false ×1 |
| records with different shapes group on the one field they share | records ×3, kind | → | invoice ×2, credit ×1 |
| the empty string is an ordinary group name when it is the real value | records ×2, team | → | ×1, a ×1 |
| a fractional number cannot name a group: the three languages print it differently | records ×1, rate | → | error: fractional number |
| a list cannot name a group | records ×1, tags | → | error: list or map |
| a map cannot name a group | records ×1, meta | → | error: list or map |
| an integer too large to survive a JavaScript round trip is rejected | records ×1, id | → | error: out-of-range number |
| an empty key name is a caller bug | records ×1, | → | error: non-empty key name |
More from the author
Group names are derived by an explicit rendering, not by each language's default string conversion, because those disagree: a string is itself, an integer is its decimal digits, true and false are "true" and "false".
A float, a list or a map at the key is an error. A float has no decimal rendering the three languages agree on (Python prints 1.0, JavaScript prints 1), and there is no defensible name for a group identified by a list. Integers outside the +/-2^53 safe range are rejected for the same reason: JavaScript would render them in exponential notation or lose digits.
Key order in the returned map is first-appearance order everywhere except JavaScript, which reorders integer-like object keys ahead of the rest. Do not rely on the order of the groups; sort the keys if you need one. The order WITHIN each group is guaranteed.
Files
| Path | Bytes |
|---|---|
| README.md | 1,506 |
| impl/python.py | 2,433 |
| impl/rust.rs | 3,109 |
| impl/typescript.ts | 2,769 |
| vectors.json | 4,532 |