Skip to content

Home > @idealeap/pipeline > replaceSlots

replaceSlots() function ​

Replaces all slots in the given object with the provided replacements. If a slot matches a key in the replacements, it will be substituted. If the replacement value is an object, and the slot covers the entire string, the entire value will be replaced by the object. Otherwise, the object will be stringified.

Signature:

typescript
export declare function replaceSlots(obj: Record<string, any>, replacements: Record<string, any>): any;
export declare function replaceSlots(obj: Record<string, any>, replacements: Record<string, any>): any;

Parameters ​

ParameterTypeDescription
objRecord<string, any>The object containing slots to be replaced.
replacementsRecord<string, any>The replacements to use for the slot substitution.

Returns:

any

  • A new object with all the slots replaced.

Example ​

typescript
const testObj = {
  text: "Hello {{name}}",
  info: {
    age: "{{age}} years old",
    address: "{{addressObj.city}}124453"
  }
};

const replacements = {
  name: "Alice",
  age: "25",
  addressObj: {
    street: "123 Main St",
    city: "Example City"
  }
};

console.log(replaceSlots(testObj, replacements));
const testObj = {
  text: "Hello {{name}}",
  info: {
    age: "{{age}} years old",
    address: "{{addressObj.city}}124453"
  }
};

const replacements = {
  name: "Alice",
  age: "25",
  addressObj: {
    street: "123 Main St",
    city: "Example City"
  }
};

console.log(replaceSlots(testObj, replacements));

Released under the MIT License.