Bitbybit Docs
    Preparing search index...

    Class TextBitByBit

    Working with text: splitting, joining, searching, trimming, padding, changing case, regular expressions and formatting with placeholders. vectorChar and vectorText turn text into line paths drawn with a simple stroke font, so words can become geometry. Positions in text count from 0.

    Index

    Constructors

    create

    • Passes a text through unchanged, so a value can be given a name and reused.

      Example: 'Hello World' -> 'Hello World'

      Parameters

      Returns string

      The same text

    query

    • Tells whether a text contains a search text.

      Example: 'hello world' includes 'world' -> true

      Parameters

      Returns boolean

      True when the search text occurs in it

      const has = bitbybit.text.includes({ text: "hello world", search: "world" });
      
    • Tells whether a text begins with a search text.

      Example: 'hello world' starts with 'hello' -> true

      Parameters

      Returns boolean

      True when the text begins with it

      const starts = bitbybit.text.startsWith({ text: "hello world", search: "hello" });
      
    • Tells whether a text ends with a search text.

      Example: 'hello world' ends with 'world' -> true

      Parameters

      Returns boolean

      True when the text ends with it

      const ends = bitbybit.text.endsWith({ text: "hello world", search: "world" });
      
    • Finds where a search text first occurs, counting characters from 0, or -1 when it does not occur.

      Example: 'hello world' finding 'world' -> 6

      Parameters

      Returns number

      The position of the first occurrence, or -1

      const at = bitbybit.text.indexOf({ text: "hello world", search: "world" });
      
    • Finds where a search text last occurs, counting characters from 0, or -1 when it does not occur.

      Example: 'hello world hello' finding 'hello' -> 12

      Parameters

      Returns number

      The position of the last occurrence, or -1

      const at = bitbybit.text.lastIndexOf({ text: "hello world hello", search: "hello" });
      
    • Reads the character at a position, counting from 0.

      Example: 'hello' at 1 -> 'e'

      Parameters

      Returns string

      The character, or an empty text when the position is outside the text

      const second = bitbybit.text.charAt({ text: "hello", index: 1 });
      
    • Counts the characters in a text.

      Example: 'hello' -> 5

      Parameters

      Returns number

      The number of characters

    • Tells whether a text is empty or holds only whitespace.

      Example: ' ' -> true, 'a' -> false

      Parameters

      Returns boolean

      True when there is nothing but whitespace

    regex

    • Tells whether a regular expression matches somewhere in a text.

      Example: 'hello123' against '[0-9]+' -> true

      Parameters

      Returns boolean

      True when the pattern matches

      const hasDigits = bitbybit.text.regexTest({ text: "hello123", pattern: "[0-9]+", flags: "" });
      
    • Finds the parts of a text that a regular expression matches.

      With the g flag every match is listed; without it only the first match and its capture groups. No match gives null. Example: 'hello123world456' against '[0-9]+' with 'g' -> ['123', '456']

      Parameters

      Returns string[]

      The matches, or null when there are none

      const numbers = bitbybit.text.regexMatch({ text: "hello123world456", pattern: "[0-9]+", flags: "g" });
      
    • Replaces what a regular expression matches with another text.

      With the g flag every match is replaced; without it only the first. Example: 'hello123world456' against '[0-9]+' with 'g', replaced by 'X' -> 'helloXworldX'

      Parameters

      Returns string

      The text with the replacements made

      const clean = bitbybit.text.regexReplace({ text: "hello123world456", pattern: "[0-9]+", flags: "g", replaceWith: "X" });
      
    • Finds where a regular expression first matches, counting characters from 0, or -1 when it does not match.

      Example: 'hello123' against '[0-9]+' -> 5

      Parameters

      Returns number

      The position of the first match, or -1

      const at = bitbybit.text.regexSearch({ text: "hello123", pattern: "[0-9]+", flags: "" });
      
    • Cuts a text into pieces wherever a regular expression matches; the matches themselves are dropped.

      Example: 'a1b2c3' split by '[0-9]+' -> ['a', 'b', 'c', '']

      Parameters

      Returns string[]

      The pieces, in order

      const letters = bitbybit.text.regexSplit({ text: "a1b2c3", pattern: "[0-9]+", flags: "" });
      

    transform

    • Cuts a text into pieces wherever a separator occurs; the separator itself is dropped.

      Example: 'apple,banana,cherry' split by ',' -> ['apple', 'banana', 'cherry']

      Parameters

      Returns string[]

      The pieces, in order

      const parts = bitbybit.text.split({ text: "apple,banana,cherry", separator: "," });
      
    • Replaces every occurrence of a search text with another text.

      Example: 'hello hello' replacing 'hello' with 'hi' -> 'hi hi'

      Parameters

      • inputs: TextReplaceDto

        The text, what to search for and what to put in its place

      Returns string

      The text with the replacements made

      const greeting = bitbybit.text.replaceAll({ text: "hello hello", search: "hello", replaceWith: "hi" });
      
    • Joins a list of texts into one, with a separator between neighbors.

      Example: ['apple', 'banana', 'cherry'] joined by ', ' -> 'apple, banana, cherry'

      Parameters

      Returns string

      The joined text

      const line = bitbybit.text.join({ list: ["apple", "banana"], separator: ", " });
      
    • Turns any value into text, the way JavaScript prints it.

      Example: 42 -> '42', [1, 2] -> '1,2'

      Type Parameters

      • T

      Parameters

      Returns string

      The value as text

    • Turns every item of a list into text, the way JavaScript prints it.

      Example: [1, 2.5, true] -> ['1', '2.5', 'true']

      Type Parameters

      • T

      Parameters

      Returns string[]

      One text per item, in order

    • Fills numbered placeholders in a text with values: {0} takes the first value, {1} the second, and so on.

      A placeholder without a value is left as it is. Example: 'Point: ({0}, {1})' with [10, 5] -> 'Point: (10, 5)'

      Parameters

      • inputs: TextFormatDto

        The text with placeholders and the values to fill in

      Returns string

      The filled-in text

      const label = bitbybit.text.format({ text: "Point: ({0}, {1})", values: ["10", "5"] });
      
    • Takes the characters from a start position up to, but not including, an end position.

      A start larger than the end swaps the two, and negative positions count as 0. Example: 'hello world' from 0 to 5 -> 'hello'

      Parameters

      Returns string

      The characters in that range

      const word = bitbybit.text.substring({ text: "hello world", start: 0, end: 5 });
      
    • Takes the characters from a start position up to, but not including, an end position.

      Unlike substring, a negative position counts from the end of the text. Example: 'hello world' from 0 to 5 -> 'hello'; from -5 -> 'world'

      Parameters

      Returns string

      The characters in that range

      const tail = bitbybit.text.slice({ text: "hello world", start: 6, end: 11 });
      
    • Removes spaces, tabs and line breaks from both ends of a text.

      Example: ' hello ' -> 'hello'

      Parameters

      Returns string

      The trimmed text

    • Removes spaces, tabs and line breaks from the start of a text.

      Example: ' hello ' -> 'hello '

      Parameters

      Returns string

      The text without leading whitespace

    • Removes spaces, tabs and line breaks from the end of a text.

      Example: ' hello ' -> ' hello'

      Parameters

      Returns string

      The text without trailing whitespace

    • Adds a filler text in front until the text reaches a length; a text already that long is left alone.

      Example: 'x' to length 3 with 'a' -> 'aax'

      Parameters

      • inputs: TextPadDto

        The text, the length to reach and the filler

      Returns string

      The padded text

      const padded = bitbybit.text.padStart({ text: "7", length: 3, padString: "0" });
      
    • Adds a filler text behind until the text reaches a length; a text already that long is left alone.

      Example: 'x' to length 3 with 'a' -> 'xaa'

      Parameters

      • inputs: TextPadDto

        The text, the length to reach and the filler

      Returns string

      The padded text

      const padded = bitbybit.text.padEnd({ text: "x", length: 3, padString: "a" });
      
    • Turns every letter into a capital.

      Example: 'hello' -> 'HELLO'

      Parameters

      Returns string

      The text in capitals

    • Turns every letter into lower case.

      Example: 'HELLO' -> 'hello'

      Parameters

      Returns string

      The text in lower case

    • Turns the first character into a capital and leaves the rest as it is.

      Example: 'hello world' -> 'Hello world'

      Parameters

      Returns string

      The text with its first character capitalized

    • Turns the first character into lower case and leaves the rest as it is.

      Example: 'Hello World' -> 'hello World'

      Parameters

      Returns string

      The text with its first character in lower case

    • Repeats a text a number of times, end to end.

      Example: 'ha' three times -> 'hahaha'

      Parameters

      Returns string

      The repeated text

      const laugh = bitbybit.text.repeat({ text: "ha", count: 3 });
      
    • Reverses the order of the characters.

      Example: 'hello' -> 'olleh'

      Parameters

      Returns string

      The reversed text

    • Joins several texts into one with nothing between them.

      Example: ['hello', ' ', 'world'] -> 'hello world'

      Parameters

      Returns string

      The joined text

      const sentence = bitbybit.text.concat({ texts: ["hello", " ", "world"] });
      

    vector

    • Draws one character as line paths with a simple stroke font.

      The paths lie flat on the XZ plane, scaled so the character is height tall, and are returned with the character's width and height. An unknown character is drawn as a question mark. Example: 'A' at height 10 -> the strokes of an A, 10 units tall

      Parameters

      Returns VectorCharData

      The character's width, height and stroke paths as lists of points

      const letter = bitbybit.text.vectorChar({ char: "A", height: 10, xOffset: 0, yOffset: 0, extrudeOffset: 0 });
      
    • Draws a text, with line breaks, as line paths with a simple stroke font.

      Each line comes back as its characters with their paths, laid out flat on the XZ plane with the given height, spacing and alignment; centerOnOrigin puts the middle of the block at the origin. Example: 'Hello' at height 10 -> five characters with their strokes

      Parameters

      Returns VectorTextData[]

      One entry per line, each with its characters and their stroke paths

      const lines = bitbybit.text.vectorText({
      text: "Hello\nWorld",
      height: 10,
      align: Bit.Inputs.Base.horizontalAlignEnum.center,
      centerOnOrigin: true,
      });