shelving/util/entrymodule

An entry is a readonly [key, value] pair — the shape produced by Map.entries(), Object.entries(), and Array.entries(). These helpers provide a uniform way to iterate, extract, and type entries across all of those sources.

Usage

Iterating entries from mixed sources

ts
import { getEntries, getEntryKeys, getEntryValues } from "shelving/util";

const obj = { a: 1, b: 2 };
const map = new Map([["c", 3], ["d", 4]]);
const arr = [10, 20, 30];
const set = new Set(["x", "y"]);

// getEntries accepts objects, Maps, arrays, Sets, or any Entry iterable — including multiple at once.
for (const [k, v] of getEntries(obj, map)) { /* ... */ }
for (const [i, v] of getEntries(arr)) { /* ... */ }
for (const [k] of getEntries(set)) { /* ... */ } // Sets yield [value, value] entries

for (const k of getEntryKeys(getEntries(obj))) { /* k = "a", "b" */ }
for (const v of getEntryValues(getEntries(map))) { /* v = 3, 4 */ }

Extracting from a single entry

ts
import { getEntryKey, getEntryValue } from "shelving/util";

const entry = ["name", "Alice"] as const;
getEntryKey(entry);   // "name"
getEntryValue(entry); // "Alice"

Using EntryObject to convert entries back to object types

ts
import type { Entry, EntryObject } from "shelving/util";

type E = Entry<"status", "active" | "inactive">;
type O = EntryObject<E>; // { status: "active" | "inactive" }

Functions

Go

getEntryKey()function

Extract the key from an object entry.

getEntryKey([k]: Entry<K, T>): K
Go

getEntryValue()function

Extract the value from an object entry.

getEntryValue([, v]: Entry<K, T>): T
Go

getEntryKeys()function

Yield the keys of an iterable set of entries.

getEntryKeys(input: Iterable<Entry<K, T>>): Iterable<K>
Go

getEntryValues()function

Yield the values of an iterable set of entries.

getEntryValues(input: Iterable<Entry<K, T>>): Iterable<T>
Go

getEntries()function

Yield the entries from one or more entry-yielding sources (sets, objects, maps, arrays, or iterables of entries).

getEntries(...input: (ImmutableSet<K & T> | Partial<ImmutableObject<K, T>> | Iterable<Entry<K, T>>)[]): Iterable<Entry<K, T>>
getEntries(...input: (ImmutableArray<T> | ImmutableSet<K & T> | Iterable<Entry<K, T>>)[]): Iterable<Entry<K, T>>
getEntries(...input: (ImmutableSet<K & T> | Iterable<Entry<K, T>>)[]): Iterable<Entry<K, T>>

Types

Go

Entrytype

Single key/value entry from a map-like object.

readonly [K, T]
Go

EntryKeytype

Extract the key type from an Entry.

X extends Entry<infer Y, unknown> ? Y : never
Go

EntryValuetype

Extract the value type from an Entry.

X extends Entry<unknown, infer Y> ? Y : never
Go

EntryObjecttype

Turn an Entry type back into an object with a single property. i.e. EntryObject<Entry<"a", string>> produces { a: string }

{ readonly [E in T as E[0]]: E[1] }