Emmanuel Akinfulubi

TypeScriptClasses and OOP

Static utility class in TypeScript

24 A static utility class

fair

Write a utility class with static methods for formatting a date, generating a short random string and capitalising text. Each must work without creating an instance.

Stuck? Three hints, smallest first
  1. 1. Keep the date formatting deterministic so it can be asserted; take the date as an argument.
  2. 2. For the random string, assert its length and character set rather than its value.
  3. 3. Capitalising an empty string must not crash. Check that edge first.

The answer, and proof it works

const same = (a: unknown, b: unknown): boolean =>
  JSON.stringify(a) === JSON.stringify(b);

function test(name: string, got: unknown, want: unknown): void {
  console.log(
    (same(got, want) ? 'ok   ' : 'FAIL ') + name +
    (same(got, want) ? '' : `  got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`)
  );
}

class Utils {
  // A fixed format, and the date comes in as an argument so it is testable.
  static formatDate(date: Date): string {
    const y = date.getUTCFullYear();
    const m = String(date.getUTCMonth() + 1).padStart(2, '0');
    const d = String(date.getUTCDate()).padStart(2, '0');
    return `${y}-${m}-${d}`;
  }

  static generateRandomString(length = 8): string {
    const alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
    let out = '';
    for (let i = 0; i < length; i++) {
      const pick = Math.floor(Math.random() * alphabet.length);
      out += alphabet[pick] ?? 'a';
    }
    return out;
  }

  static capitalizeString(text: string): string {
    if (text.length === 0) return text;         // guard the empty case first
    return text[0]!.toUpperCase() + text.slice(1);
  }

  static titleCase(text: string): string {
    return text.split(' ').map((w) => Utils.capitalizeString(w)).join(' ');
  }
}

const date = new Date('2026-09-02T00:00:00Z');
console.log(Utils.formatDate(date));
console.log(Utils.capitalizeString('lagos'));
console.log(Utils.titleCase('jibowu motor park'));

const random = Utils.generateRandomString(10);
console.log('random:', random);

test('date formatted', Utils.formatDate(date), '2026-09-02');
test('single-digit month and day are padded', Utils.formatDate(new Date('2026-01-05T00:00:00Z')), '2026-01-05');
test('capitalised', Utils.capitalizeString('lagos'), 'Lagos');
test('empty text does not crash', Utils.capitalizeString(''), '');
test('title case', Utils.titleCase('jibowu motor park'), 'Jibowu Motor Park');
test('random string has the asked-for length', random.length, 10);
test('random string uses only the alphabet', /^[a-z0-9]+$/.test(random), true);

Output


      

Two testing habits here. formatDate takes the date rather than calling new Date() inside, which is the only way to assert its output. And the random string is checked by length and shape, not by value, because you cannot assert a random result. Both are how you keep unpredictable code testable.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.