ts-data-forge
    Preparing search index...

    Function filterMap

    • Transforms and drops entries in one pass: the callback returns Optional.some(value) to keep an entry with a new value, or Optional.none to drop it.

      Type Behavior: as with filter, the surviving keys are the keys of the source record made optional, and a record keyed by an index signature stays total. Unlike filter, every kept value takes the type the callback returns.

      Optional rather than undefined marks the dropped entries, so a record whose values are legitimately undefined stays expressible.

      Key handling: keys reach the callback the way Object.entries yields them, so a numeric key 1 arrives as '1'. Symbol-keyed properties are not enumerated by Object.entries; they are dropped from both the result and its type.

      Type Parameters

      • const R extends UnknownRecord

        The type of the source record

      • V2

        The type the callback returns for the entries it keeps

      Parameters

      • record: R

        The record whose entries are transformed and filtered

      • mapFn: (
            value: R[keyof R],
            key: ToString<RelaxedExclude<keyof R, symbol>>,
        ) => Optional<V2>

        Called once per own enumerable string key, with the value and the key

      Returns FilteredRecord<
          Readonly<
              { [K in string
              | number
              | symbol as K extends symbol ? never : K]: V2 },
          >,
      >

      A new record holding only the kept entries, with mapped values

      const scores = { alice: 1, bob: 2 } as const;

      // `Optional.some` keeps an entry with a new value, `Optional.none` drops it.
      const labelled = Obj.filterMap(scores, (value, key) =>
      value > 1 ? Optional.some(`${key}=${value}`) : Optional.none,
      );

      assert.deepStrictEqual(labelled, { bob: 'bob=2' });