Skip to main content
Module

x/text_clipper/mod.ts>default

Fast and correct clip functions for HTML and plain text
Latest
function default
import { default } from "https://deno.land/x/text_clipper@2.1.0/mod.ts";

Clips a string to a maximum length. If the string exceeds the length, it is truncated and an indicator (an ellipsis, by default) is appended.

In detail, the clipping rules are as follows:

  • The resulting clipped string may never contain more than maxLength characters. Examples:
    • clip("foo", 3) => "foo"
    • clip("foo", 2) => "f…"
  • The indicator is inserted if and only if the string is clipped at any place other than a newline. Examples:
    • clip("foo bar", 5) => "foo …"
    • clip("foo\nbar", 5) => "foo"
  • If the html option is true and valid HTML is inserted, the clipped output must also be valid HTML. If the input is not valid HTML, the result is undefined (not to be confused with JS' "undefined" type; some errors might be detected and result in an exception, but this is not guaranteed).

Parameters

string: string

The string to clip.

maxLength: number

The maximum length of the clipped string in number of characters.

optional
options: ClipOptions = [UNSUPPORTED]

Optional options object.

Returns

string

The clipped string.