BiStringBuilder

class BiStringBuilder(original)

exported from builder

Bidirectionally transformed string builder.

A BistrBuilder builds a transformed version of a source string iteratively. Each builder has an immutable original string, a current string, and the in-progress modified string, with alignments between each. For example:

original: |The| |quick,| |brown| |🦊| |jumps| |over| |the| |lazy| |🐶|
          |   | |      | |     | |  \ \     \ \    \ \   \ \    \ \   \
current:  |The| |quick,| |brown| |fox| |jumps| |over| |the| |lazy| |dog|
          |   | |      / /     /
modified: |the| |quick| |brown| ...

The modified string is built in pieces by calling replace() to change n characters of the current string into new ones in the modified string. Convenience methods like skip(), insert(), and discard() are implemented on top of this basic primitive.

Construct a BiStringBuilder.

Arguments
  • original (bistring.AnyString) – Either an original string or a BiString to start from.

BiStringBuilder.alignment

type: alignment.Alignment

The alignment as built so far from this.current to this.modified.

BiStringBuilder.append(bs)

Append a BiString. The original value of the BiString must match the current string being processed.

Arguments
  • bs (bistring.BiString) –

BiStringBuilder.build()

Build the BiString().

Returns

bistring.BiString

BiStringBuilder.current

type: string

The current string before modifications.

BiStringBuilder.discard(n)

Discard a portion of the original string.

Arguments
  • n (number) –

BiStringBuilder.discardMatch(pattern)

Discard a substring that matches a regex.

Arguments
  • pattern (RegExp) – The pattern to match. Must have either the sticky flag, forcing it to match at the current position, or the global flag, finding the next match.

Returns

boolean – Whether a match was found.

BiStringBuilder.discardRest()

Discard the rest of the original string.

BiStringBuilder.insert(str)

Insert a substring into the string.

Arguments
  • str (string) –

BiStringBuilder.isComplete

type: boolean

Whether we’ve completely processed the string. In other words, whether the modified string aligns with the end of the current string.

BiStringBuilder.modified

type: string

The modified string as built so far.

BiStringBuilder.original

type: string

The original string being modified.

BiStringBuilder.peek(n)

Peek at the next few characters.

Arguments
  • n (number) – The number of characters to peek at.

Returns

string

BiStringBuilder.position

type: number

The position of the builder in this.current.

BiStringBuilder.remaining

type: number

The number of characters of the current string left to process.

BiStringBuilder.replace(n, str)

Replace the next n characters with a new string.

Arguments
  • n (number) –

  • str (bistring.AnyString) –

BiStringBuilder.replaceAll(pattern, replacement)

Replace all occurences of a regex, like String.prototype.replace().

Arguments
  • pattern (RegExp) – The pattern to match. The global flag (/g) must be set to get multiple matches.

  • replacement (string|regex.Replacer) – The replacement string or function, as in String.prototype.replace().

BiStringBuilder.replaceMatch(pattern, replacement)

Replace a substring that matches a regex.

Arguments
  • pattern (RegExp) – The pattern to match. Must have either the sticky flag, forcing it to match at the current position, or the global flag, finding the next match.

  • replacement (string|regex.Replacer) – The replacement string or function, as in String.prototype.replace().

Returns

boolean – Whether a match was found.

BiStringBuilder.rewind()

Reset this builder to apply another transformation.

BiStringBuilder.skip(n)

Skip the next n characters, copying them unchanged.

Arguments
  • n (number) –

BiStringBuilder.skipMatch(pattern)

Skip a substring matching a regex, copying it unchanged.

Arguments
  • pattern (RegExp) – The pattern to match. Must have either the sticky flag, forcing it to match at the current position, or the global flag, finding the next match.

Returns

boolean – Whether a match was found.

BiStringBuilder.skipRest()

Skip the rest of the string, copying it unchanged.