API reference@evolu/commonObject › objectFrom

function objectFrom<K, V>(
  keys: readonly K[],
  getValue: (key: K) => V,
): ReadonlyRecord<K, V>;

Defined in: packages/common/src/Object.ts:221

Creates an object by mapping keys to values.

The inverse of Object.keys — instead of extracting keys from an object, builds an object from keys with a mapper function.

Example

import {
  assertEqual,
  assertType,
  objectFrom,
  type ReadonlyRecord,
} from "@evolu/common";

const translations = objectFrom(
  ["en", "fr"] as const,
  (locale): string => `Hello in ${locale}`,
);

assertType<ReadonlyRecord<"en" | "fr", string>, typeof translations>();
assertEqual(translations, {
  en: "Hello in en",
  fr: "Hello in fr",
});