before-replace

1.0.5 • Public • Published

Description

A tool to hide or replace text from source.



Installation

npm install before-replace


Usage

In Node (CJS)

const {beforeReplace, hideText, restoreText}  = require("before-replace");

In Node (ESM)

import {beforeReplace, hideText, restoreText} from "before-replace";

In the Browser

import {beforeReplace, hideText, restoreText} from "./node_modules/before-replace/index.mjs";

Examples



hideText()


Hiding then restoring text

let text = "John and Sophie hoped that the bus station was open. Great! They didn't miss the bus." +
    "The bus station was not closed till noon.";
console.log("Original           :", text);

// Hide all bus station occurences
text = hideText(/ bus station /gm, text);
console.log("Hidden             :", text);

// Replace part of text with native replace
text = text.replace("John and Sophie", "Louis and Coralie");
console.log("Random replacement :", text);

// Restore bus station occurences 
text = restoreText(text);
console.log("Restored           :", text);
Original           : John and Sophie hoped that the bus station was open. Great! They didn't miss the bus.The bus station was not closed till noon.
Hidden             : John and Sophie hoped that the was open. Great! They didn't miss the bus.The was not closed till noon.
Random replacement : Louis and Coralie hoped that the was open. Great! They didn't miss the bus.The was not closed till noon.
Restored           : Louis and Coralie hoped that the bus station was open. Great! They didn't miss the bus.The bus station was not closed till noon.




replaceText()


Replacing then restoring text

let text = "I looked at my watch when I suddenly realised I didn't have it. Where was it? Is it possible that I left"
    + " it at home? I should watch elsewhere.\n"
console.log(text);

text = replaceText(" watch ", "**phone**", text);
console.log(text);

text = text.replace("I looked at", "I checked");
console.log(text);

text = restoreText(text);
console.log(text);
I looked at my watch when I suddenly realised I didn't have it. Where was it? Is it possible that I left it at home? I should watch elsewhere.

I looked at my **phone** when I suddenly realised I didn't have it. Where was it? Is it possible that I left it at home? I should **phone** elsewhere.

I checked my **phone** when I suddenly realised I didn't have it. Where was it? Is it possible that I left it at home? I should **phone** elsewhere.

I checked my watch when I suddenly realised I didn't have it. Where was it? Is it possible that I left it at home? I should watch elsewhere.




beforeReplace()


Usage

beforeReplace(regex, text, callback, options)
Options Description Expect
restorable Whether replaced patterns can be restored Boolean true
useHiddenChars Use invisible characters Boolean false
resetAll Reset restoration Boolean false

Replacing text via callbacks

let text = "I noticed there was some misunderstanding. What was going on? Why would it be that way? So I proceeded" +
    " to fix all of the misdeeds. Misunderstanding? Never.";
console.log(text);
text = beforeReplace(/misunderstanding/gi, text, function (found, wholeText, index, match)
{
    console.log(found, match, index);
    return "huge misunderstanding";
});
console.log(text);
I noticed there was some misunderstanding. What was going on? Why would it be that way? So I proceeded to fix all of the misdeeds. Misunderstanding? Never.
Misunderstanding [
  'Misunderstanding',
  index: 131,
  input: 'I noticed there was some misunderstanding. What was going on? Why would it be that way? So I proceeded to fix all of the misdeeds. Misunderstanding? Never.',
  groups: undefined
] 0
misunderstanding [
  'misunderstanding',
  index: 25,
  input: 'I noticed there was some misunderstanding. What was going on? Why would it be that way? So I proceeded to fix all of the misdeeds. Misunderstanding? Never.',
  groups: undefined
] 1
I noticed there was some huge misunderstanding. What was going on? Why would it be that way? So I proceeded to fix all of the misdeeds. huge misunderstanding? Never.

🚫 To return the original content, return match[0]

// ...
text = beforeReplace(/.../gi, text, (found, wholeText, index, match)=>{
   return match[0];
});
// ...

Readme

Keywords

none

Package Sidebar

Install

npm i before-replace

Weekly Downloads

669

Version

1.0.5

License

MIT

Unpacked Size

26.5 kB

Total Files

10

Last publish

Collaborators

  • thimpat