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

str-replace

v0.0.5

Published

Replace Strings like a boss

Downloads

980

Readme

JavaScript String Replace

A simple, lightweight, functional JavaScript API for replacing a String like a boss

  • No dependency
  • Fluent interface

Why?

Code is read much more often than it is written, so plan accordingly, any experienced developer knows that.

The above principle has been ignored for some trivial operations, such as replacing a String. Not anymore.

Try to understand the expected output of the code below:

replaceAll( "bus", "road", "Get on the road", true );

You can't understand the order and meaning of the arguments without looking into the documentation first to make sure it does what you want.

  • Does the guy who wrote this is aware of the Principle_of_least_astonishment to replace the first argument from the second? Is it replacing all "bus" to "road"? Or all "road" to "bus"?
  • What does that Boolean Trap means?

If you need to look elsewhere to be able to understand something, then something is wrong.

What if you could tell the computer to "Replace all occurrences ignoring the case from target with replacement"?

const occurrences = "road";
const target = "Get on the Road";
const replacement = "bus";
const result = replace.all( occurrences ).ignoringCase().from( target ).with( replacement );
console.log(result); // => Get on the bus

Or, if you don't want to use variables:

const result = replace.all( "road" ).ignoringCase().from( "Get on the Road" ).with( "bus" );
console.log(result); // => Get on the bus

Tcharam! This changes how you replace strings.

Stop being imperative and start being functional, the next developer say "Thanks".

Installation

Install via npm:

$ npm install str-replace --save

Require in the file you want to use it:

var replace = require("str-replace");

Basic Usage

Replace the first dot to space:

replace(".").from("John.Doe.Company").with(" "); // => "John Doe.Company"

Replace the first characters ignoring the case when matching:

replace("hey").from("HEY, DON'T SAY HEY!").with("YO"); // => "YO, DON'T SAY HEY!"

Replace all dots to spaces:

replace.all(".").from("John.Doe.Company").with(" "); // => John Doe Company

Replace all characters ignoring the case when matching:

replace.all("hey").from("HEY, DON'T SAY HEY!").with("YO"); // => "YO, DON'T SAY YO!"

API

replace( occurrences )

Creates a ReplaceDefinition that will replace the first substring that matches the occurrences.

Receives an occurrences, which is a String representing what is going to be replaced.

Example:

replace( "e" ); // => ReplaceDefinition

replace.all( occurrences )

Creates a ReplaceDefinition that will replace all substrings that matches the occurrences.

Receives an occurrences, which is a String representing what is going to be replaced.

Example:

replace.all( "dreaming" ); // => ReplaceDefinition

ReplaceDefinition

Contains the strategy for the replace.

ignoringCase()

Creates a ReplaceDefinition that will ignore the case when matching the occurrences.

Example:

replace( "java" ).ignoringCase(); // => ReplaceDefinition

from( target )

Creates a ReplaceOperation that will replace the given target.

Receives a target, which is a String representing from where it is going to be replaced.

Example:

replace( "Thunder" ).from( "Thunderstorm" ); // => ReplaceOperation

ReplaceOperation

Contains the algorithm representing what should be replaced.

with( replacement )

Creates a String replacing with the given replacement according to the rules of the ReplaceDefinition.

Receives a replacement, which is a String representing the new substring to be replaced.

Example:

var result = replace( "Java" ).from( "Java is not JavaScript" ).with( "Type" );
console.log( result ); // => Type is not TypeScript

Manual release steps

  • Increment the "version" attribute of package.json
  • Commit with the message "Release version x.x.x"
  • Create version tag in git
  • Create a github release
  • Release on npm

Authors