Getting Started
Basic Usage

Basic Usage

To get started using the @ibnlanre/builder package, it is essential to understand the basic concepts and usage patterns involved in working with the library. This guide will walk you through the fundamental steps required to create a builder object, access values, and retrieve keys using the package.

The @ibnlanre/builder package provides a simple and efficient way to generate and manage keys within your application. It requires a register to be passed as an argument to the createBuilder function, which can be imported from the package. In turn, it generates a builder which serves as a bridge between the register object and the user, providing a convenient way to interact with the data structure.

The following steps outline how to quickly get started using the @ibnlanre/builder package:

  1. Import the createBuilder function from the library:

    import { createBuilder } from '@ibnlanre/builder';
  2. Create a register object with nested keys and values:

    const register = {
      foo: {
        baz: (id: number) => `/bazaar/${id}`,
        bar: 10,
      },
    }
  3. Create a builder object using the createBuilder function:

    const builder = createBuilder(register, { prefix: ["root", "node"] });
  4. Access the register values using the builder object:

    // Accessing the register values
    builder.$use; // { foo: { baz: [Function: baz], bar: 10 } }
     
    // Accessing a nested function value
    builder.$use.foo.baz(12); // "/bazaar/12"
     
    // Accessing a nested primitive value
    builder.$use.foo.bar; // 10
  5. Generate keys using the builder object:

    // Retrieving the root key
    builder.$get(); // ["root", "node"]
     
    // Accessing a nested function key
    builder.foo.baz.$use(12); // ["root", "node", "foo", "baz", 12]
     
    // Accessing a nested primitive key
    builder.foo.bar.$use(); // ["root", "node", "foo", "bar"]