A simple, lightweight JavaScript API to replace a String
MIT License
A simple, lightweight, functional JavaScript API for replacing a String like a boss
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 );
It is impossible to understand the order and meaning of the arguments by looking at the code:
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 using functions that are impossible to understand, the next developer say "Thanks".
Install via npm:
$ npm install str-replace --save
Require in the file you want to use it:
var replace = require("str-replace");
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").ignoringCase().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").ignoringCase().from("HEY, DON'T SAY HEY!").with("YO"); // => "YO, DON'T SAY YO!"
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
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
Contains the strategy for the replace.
Creates a ReplaceDefinition
that will ignore the case when matching the
occurrences
.
Example:
replace( "java" ).ignoringCase(); // => ReplaceDefinition
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
Contains the algorithm representing what should be replaced.
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 JavaScript
package.json