TextMasker

TextMasker

Class for replacing parts of a text that shouldn't be modified, with a placeholder, in order to ignore it when performing certain text replacement operations and then replace the placeholders back with the original text.

After creating an instance and masking some text using TextMasker#mask or its derivative methods like TextMasker#maskTemplatesRecursively, there are two ways to use this class based on your needs:

  1. Use TextMasker#withText to make further transformations to the text, unmask it using TextMasker#unmask (as opposed to TextMasker#unmaskText), and get the result using TextMasker#getText.
  2. Get the text using TextMasker#getText, work with it, and in the end, unmask it using TextMasker#unmaskText.

Note that the methods support chaining, so you can sometimes successfully fit all transformations in one chain.

Constructor

new TextMasker(text, maskedTextsopt)

Source:

Create a text masker.

Parameters:
Name Type Attributes Description
text string
maskedTexts Array.<string> <optional>

Array of masked texts to reuse. Use this when you are using the class with a string that already has masked parts, or you will run into problems.

Members

maskedTexts :Array.<string>

Source:

Array of masked texts. Its indexes correspond to marker indexes.

Type:
  • Array.<string>

text :string

Source:

Text parts of which are masked.

Type:
  • string

Methods

getMaskedTexts() → {Array.<string>}

Source:

Get the masked texts.

Returns:
Type
Array.<string>

getText() → {string}

Source:

Get the text in its current (masked/unmasked) state.

Returns:
Type
string

mask(regexp, typeopt, useGroupsopt) → {TextMasker}

Source:

Replace text matched by a regexp with placeholders.

Parameters:
Name Type Attributes Default Description
regexp RegExp
type string <optional>

Should consist only of alphanumeric characters.

useGroups boolean <optional>
false

Use the first two capturing groups in the regexp as the preText and textToMask parameters. (Used for processing table code.)

Returns:
Type
TextMasker

maskSensitiveCode(templateHandleropt) → {TextMasker}

Source:

Replace code, that should not be modified when processing it, with placeholders.

Parameters:
Name Type Attributes Description
templateHandler function <optional>
Returns:
Type
TextMasker

maskTags(tags, type) → {TextMasker}

Source:

Mask HTML tags in the text.

Parameters:
Name Type Description
tags Array.<string>
type string
Returns:
Type
TextMasker

maskTemplatesRecursively(handleropt, addLengthsopt) → {TextMasker}

Source:
Author:
  • Putnik
  • Jack who built the house

Mask templates taking into account nested ones.

Borrowed from https://ru.wikipedia.org/w/index.php?title=MediaWiki:Gadget-wikificator.js&oldid=102530721

Parameters:
Name Type Attributes Default Description
handler function <optional>

Function that processes the template code.

addLengths boolean <optional>
false

Add lengths of the masked templates to markers.

Returns:
Type
TextMasker

unmask(type) → {TextMasker}

Source:

Replace placeholders added by TextMasker#mask with their text.

Parameters:
Name Type Description
type string
Returns:
Type
TextMasker

unmaskText(text, typeopt) → {string}

Source:

In a provided string, replace placeholders added by TextMasker#mask with their text.

Parameters:
Name Type Attributes Description
text string
type string <optional>
Returns:
Type
string

withText(func) → {TextMasker}

Source:

Run a certain function for the text.

Parameters:
Name Type Description
func function

Function that should accept and return a string. It can also accept the TextMasker object as a second parameter.

Returns:
Type
TextMasker