@s-libs/js-core
    Preparing search index...

    Function createBuilder

    • Creates a factory function for building objects of a given type. Commonly used to create test objects for use in specs.

      interface Message {
      id: number;
      text: string;
      }

      interface Options {
      cypherDistance: number;
      }

      declare function shiftCharacters(text: string, distance: number): string;

      const buildMessage = createBuilder<Message, Options>(
      (seq) => ({ id: seq, text: `message ${seq}` }),
      (message, _seq, options) => {
      message.text = shiftCharacters(message.text, options.cypherDistance || 0);
      },
      );

      buildMessage(); // { id: 1, text: "message 1" }
      buildMessage({ text: "Hello world!" }); // { id: 2, text: "Hello world!" }
      buildMessage({ text: "abc" }, { cypherDistance: 3 }); // { id: 3, text: "def" }

      Type Parameters

      • T extends object
      • OptionsType = EmptyObject

      Parameters

      • buildDefaults: (seq: number, options: Partial<OptionsType>) => T
      • OptionalafterBuild: (obj: T, seq: number, options: Partial<OptionsType>) => void

      Returns (attributes?: Partial<T>, options?: Partial<OptionsType>) => T