Capstone: utils library

The spec

The utils library ships three pure helpers with exported types: a slug maker, a result wrapper, and a list formatter. No file access and no network calls are allowed inside.

Pure functions make the perfect first library because every test is just input and output. The spec fits on an index card, yet it teaches exports, types, and tests end to end.

Pure functions

A pure function returns the same output for the same input and touches nothing outside itself. Same input, same output, every run.

Build slug from trim, lower case, split, and join in one chain. Each step is a small documented string operation, so the whole pipeline reads aloud. The program turns one title into its slug and prints it.

export function slug(title: string): string { return title.trim().toLowerCase().split(" ").join("-"); } const out: string = slug("Hello World"); console.log(out);

hello-world

Exported types

Export the types next to the functions so consumers can annotate their own code. A Result type with an ok flag and a message covers most helper outcomes.

Keep exported types small and exact, then let callers compose them. A library with three precise types beats one with a bag of loose options. The program builds one result with the helper and prints its flag and message.

export type Result = { ok: boolean; message: string; }; export function done(message: string): Result { return { ok: true, message }; } const first: Result = done("ok"); console.log(first.ok, first.message);

true ok

Assert tests

Cover every helper with strictEqual assertions from node:assert. Pin the slug output, the result flag, and one edge case such as an empty title.

For runner based suites, read mocha testing background after these asserts pass. Start with assert, then graduate to a runner when the suite earns it. The program pins both helpers and prints its pass line.

import assert from "node:assert"; function slug(title: string): string { return title.trim().toLowerCase().split(" ").join("-"); } function done(message: string): { ok: boolean; message: string } { return { ok: true, message }; } assert.strictEqual(slug("Hello World"), "hello-world"); assert.strictEqual(done("ok").ok, true); console.log("utils tests pass");

utils tests pass

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What makes a function pure?

    Answer

    The same input gives the same output and nothing outside changes

    No file, network or module state is touched, so a test is just input and output.

  2. Why export the types next to the functions?

    Answer

    Consumers can annotate their own code with the library shapes

    Exported types are part of the contract, exactly like the function signatures.

  3. What does a library keep out of itself on purpose?

    Answer

    File access and network calls

    Side effects make a library hard to test and reuse, so this one stays pure.

  4. Why start with assert before a test runner?

    Answer

    A few strict equal checks prove the helpers without any dependency

    The runner earns its place once the suite grows; the first checks cost one import.

  5. What does this program print? function slug(title: string): string { return title.trim().toLowerCase().split(" ").join("-"); } console.log(slug(" Hello World "));

    Answer

    hello-world

    The trim drops the spaces, the letters go to lower case, and the space between the words becomes a dash.

  6. What does this program print? function slug(title: string): string { return title.trim().toLowerCase().split(" ").join("-"); } console.log(slug("Two Words Here").split("-").length);

    Answer

    3

    Three words become three parts, and the split counts them.

  7. What does this program print? type Result = { ok: boolean; message: string }; function done(message: string): Result { return { ok: true, message }; } const first = done("saved"); console.log(first.ok, first.message);

    Answer

    true saved

    The helper fills both fields, and the log prints the flag before the message.

  8. What does this program print? function slug(title: string): string { return title.trim().toLowerCase().split(" ").join("-"); } console.log(slug("").length, slug("A"));

    Answer

    0 a

    The empty title gives an empty slug of length zero, and the single letter goes to lower case; the edge case is worth a test.

  9. What does the compiler say about this program? type Result = { ok: boolean; message: string }; function done(message: string): Result { return { ok: true }; } console.log(done("ok"));

    Answer

    error TS2741: Property 'message' is missing in type '{ ok: true; }' but required in type 'Result'.

    The type requires both fields, and the returned object leaves the message out.

  10. What does the compiler say about this program? function slug(title: string): string { return title.trim().split(" ").join("-"); } const out: number = slug("Hello World"); console.log(out);

    Answer

    error TS2322: Type 'string' is not assignable to type 'number'.

    The helper returns text, and the annotation asks for a number.

Article author: Arthur Isaev