1.0.5 • Published 2 years ago

before-replace v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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)
OptionsDescriptionExpect
restorableWhether replaced patterns can be restoredBooleantrue
useHiddenCharsUse invisible charactersBooleanfalse
resetAllReset restorationBooleanfalse

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 match0

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

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago