Skip to main content

String Utils

StringUtil provides helper for formatting messages and other strings.

format

Formats placeholders in the source string by substituting values from either an object or an array. Missing parameters are coerced to empty strings.

static format(source:string, params?:OneOrArray):string
  • Accepts either an array or a plain object as params.
  • Array parameters use numeric placeholders such as {0}, {1}, {2}.
  • Object parameters use property placeholders such as {name}, {level}.
  • Returns the original string when source is empty or when params is not provided.
  • Missing values resolve to empty strings instead of leaving placeholders untouched.
  • Values are coerced with toString() before substitution.

Object Parameters Example

Resolves the {feature} placeholder using the object property, returning "Feature Triggered: Free Spins".

const params = { feature: 'Free Spins' };
const message = StringUtil.format('Feature Triggered: {feature}', params);
console.log(message);
// Output: Feature Triggered: Free Spins

Array Parameters Example

Uses array indices for each placeholder, returning "Game Chess: Player vs Computer".

const params = [ 5, 10, '$10.50' ];
const progress = StringUtil.format('Free Games Progress: {0}/{1} with {2} bet', params);
console.log(progress);
// Free Games Progress: 5/10 with $10.50 bet

Wrong Parameters Example

const params = { left: 5, total: 10, bet: '$10.50' };
const progress = StringUtil.format('Free Games Progress: {0}/{1} with {2} bet', params);
console.log(progress);
// Output: Free Games Progress: / with bet