text

Plain text, put in the right place

A text from a person can break the HTML, the pattern, the log line or the screen that gets it. @mark1russell7/text has a small function for each place. Each demo on this page runs the real functions of the package in your browser.

pnpm add @mark1russell7/text

1. Put a text into HTML

A page puts a name into an element and into an attribute. The browser reads a raw < as a tag and a raw " as the end of the attribute. The demo gives each version to the HTML parser of your browser, with no script, and reads what it made.

Each escape of your text

escapeHtml
escapeAttribute
escapeRegExp
escapeMarkdown
escapeWorkflowData
escapeWorkflowProperty

2. Find a text with a regular expression

A search box makes a pattern from the text of a person. In a pattern, +, ?, . and ( are operators. escapeRegExp makes a pattern that finds only the text.

3. Write a warning for GitHub Actions

A step writes ::warning title=…::… and the runner shows a warning. A line end ends the command, and a comma or :: ends the title. Type a title and a message with these characters.

4. Count, cut and pad by grapheme

A grapheme is one character as a person sees it. A flag, a family emoji or a letter with an accent has more than one code unit. String.prototype.length and slice count code units, so a cut can break a character.

    5. Search with words

    wordSearch finds each word of the search in any order. It folds the case as Unicode does, so ß agrees with ss. It also reads a ligature or a full-width letter as the plain letters. The second column is toLowerCase with includes.

    The key of the search:

    RowwordSearch includes

    6. Remove the secrets from a log

    A secret goes into a log in more than one form: raw, encoded in a URL, in the body of a form and in a Basic header. redactSecrets removes each form. replaceAll of the raw secrets leaves the other forms.

    7. The text of a thrown value

    A catch gets any value, not only an Error. String(value) throws for some values, and instanceof Error is false for an error of another window. errorMessage gives a text for each value and never throws. Pick a value to throw.

    Use it

    import { errorMessage, escapeHtml, wordSearch } from "@mark1russell7/text";
    import { truncate } from "@mark1russell7/text/length";
    import { redactSecrets } from "@mark1russell7/text/redact";
    
    cell.innerHTML = `<b>${escapeHtml(name)}</b>`;
    const title = truncate(name, 20, { unit: "grapheme" });
    const rows = all.filter(wordSearch(query));
    log(redactSecrets(errorMessage(error), [token]));

    Releases shows each version, and Status shows the open work.