README.md

frep

Find, replace and string tranformation utility for node.js. Modify strings by passing an array or object of RegExp or string replacement patterns. Patterns can be strings, arrays of strings or regex, replacements can be strings or functions.

API

// Transform a string with an array of replacement patterns
replace.strWithArr(String, replacements);
// Transform an array of strings with an array of replacement patterns
replace.arrWithArr(Array, replacements);
// Transform a string with an object of replacement patterns
replace.strWithObj(String, replacements);
// Transform an array of strings with an object of replacement patterns
replace.arrWithObj(Array, replacements);

.strWithArr( string, array )

Transform a string with an array of replacement patterns.

Parameters:

String: The string to modify with the given replacement patterns.

Array: Array of objects containing the replacement patterns, each including a pattern property (which can be a string or a RegExp), and a replacement property (which can be a string or a function to be called for each match).

A new string is returned with some or all matches replaced by the given replacement patterns.

.arrWithArr( array, array )

Transform an array of strings with an array of replacement patterns

Parameters:

Array: The string to modify with the given replacement patterns.

Array: Same as replacStr, this is an an array of objects containing the replacement patterns, each including a pattern property, which can be a string or a RegExp, and a replacement property, which can be a string or a function to be called for each match.

A new array of strings is returned with some or all matches replaced by the given replacement patterns.

Usage example

replace.strWithArray( string, array )

Slugify URL segments using frep

To run the example, first do: npm install frep underscore.string

var replace =require('frep');
// We'll use underscore string's slugify function for the first examplevar _str =require('underscore.string');
// A custom slugification function for the secondvarslugger=function(str) {
return str.replace(/( |-|\.)/g, '_').toLowerCase();
};
// And a third slugification function for the last examplevarsluggifier=function(str) {
return str.replace(/( |\.)/g, '-');
};
// This is an object of data, where each property will be used// to build up a URL that needs to be slugified. e.g.// => /foo/bar/baz// (in reality, you would probably have an array of objects like this)var obj = {
foo:'This is foo.',
bar:'ThIs iS bAr.',
baz:'THIS is BAZ.',
};
// Our custom replacement patterns. These are used to// transform the data from each propertyvar patterns = [
{
pattern:/:foo/g,
replacement: _str.slugify(obj.foo) // underscore.string
},
{
pattern:/:bar/g,
replacement: slugger(obj.bar) // custom function #1
},
{
pattern:/:baz/g,
replacement: sluggifier(obj.baz) // custom function #2
}
];
// The first argument, a string, will be our "structure",// which will determine where the values from each property// will be placed. Run frep to see what happens!console.log(replace.strWithArr(':foo/:bar/:baz', patterns));