npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

before-replace

v1.0.5

Published

Hide or replace text

Downloads

2,511

Readme

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];
});
// ...