Given a map-like object and a set of keys, this returns a new map with the specified keys. This is similar to mask, but here the values (not the keys) will be used from the keys option. Typically keys will be an array of strings.
Establishing a domain for a function #
One use for withKeys is to establish a domain for a function:
// withKeys.ori
Tree.withKeys((name) => `Hello, ${name}!`, ['Alice', 'Bob', 'Charlie'])
Since the function now has a domain, it can be expanded into a full map.
$ ori withKeys.ori/
Alice: Hello, Alice!
Bob: Hello, Bob!
Charlie: Hello, Charlie!
Here the ori CLI can ask for the keys, and then get the value for each of those keys using the function.
Sorting in arbitrary order #
Another use for withKeys is to manually specify a sort order.
# movies.yaml
kiki:
title: Kiki's Delivery Service
year: 1989
mononoke:
title: Princess Mononoke
year: 1997
spirited:
title: Spirited Away
year: 2001
heron:
title: The Boy and the Heron
year: 2023
You could use Tree.sort to sort this according to data like date, but if you want to specify an arbitrary order for the entries or omit specific keys — say, to list your personal favorites — you can use withKeys:
$ ori withKeys.ori movies.yaml, [«mononoke», «spirited», «kiki»]
mononoke:
title: Princess Mononoke
year: 1997
spirited:
title: Spirited Away
year: 2001
kiki:
title: Kiki's Delivery Service
year: 1989