
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
A find and replace utility. Modify strings by passing an array or object of RegExp or string replacement patterns
A find and replace utility. Modify strings by passing an array of RegExp or string replacement patterns
npm i frep --save
var frep = require('frep');
// Transform a string with an array of replacement patterns
frep.strWithArr(String, replacements);
// Transform an array of strings with an array of replacement patterns
frep.arrWithArr(Array, replacements);
// Transform a string with an object of replacement patterns
frep.strWithObj(String, replacements);
// Transform an array of strings with an object of replacement patterns
frep.arrWithObj(Array, replacements);
Transform a string with an array of replacement patterns.
frep.strWithArr(String, Array)
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).Given the following:
var frep = require('frep');
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var patterns = [
{
pattern: /(A|B|C)/g,
replacement: '###'
},
{
pattern: /(X|Y|Z)/g,
replacement: '$$$'
},
...
];
frep.strWithArr(str, patterns));
// => #########DEFGHIJKLMNOPQRSTUVW$$$$$$$$$
Transform an array of strings with an array of replacement patterns
frep.arrWithArr(Array, Array)
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.Given the following:
var frep = require('frep');
var arr = [
'Jon Schlinkert',
'Brian Woodward'
];
var patterns = [
{
pattern: /(B|S)/g,
replacement: '###'
},
{
pattern: /(J|W)/g,
replacement: '$$$'
},
...
];
frep.arrWithArr(arr, patterns));
// => ["$$$on ###chlinkert", "###rian $$$oodward"]
An array of new strings is returned, with some or all matches in each string replaced by the given replacement strings.
Transform a string with an object of replacement patterns
frep.strWithObj(String, Object)
Parameters:
String
: The string to modify with the given replacement patterns.Object
: Object of replacement patterns, where each key is a string or a RegExp pattern
, and each value is the replacement
string or function to be called for each match.Given the following:
var frep = require('frep');
var str = 'ABC'
var replacements = {
'A': 'AAA',
'B': 'BBB',
'C': 'CCC',
'D': 'DDD',
'E': 'EEE',
'F': 'FFF'
};
frep.strWithObj(str, replacements));
// => AAABBBCCC
Transform an array of strings with an object of replacement patterns
frep.arrWithObj(Array, Object)
Parameters:
Array
: The array of strings to modify with the given replacement patterns.Object
: Object of replacement patterns, where each key is a string or a RegExp pattern
, and each value is the replacement
string or function to be called for each match.Given the following:
var frep = require('frep');
var arr = ['ABC', 'DEF'];
var replacements = {
'A': 'AAA',
'B': 'BBB',
'C': 'CCC',
'D': 'DDD',
'E': 'EEE',
'F': 'FFF'
};
frep.arrWithObj(arr, replacements));
// => ['AAABBBCCC', 'DDDEEEFFF']
Slugify URL segments using frep
To run the example, first do: npm install frep underscore.string
var frep = require('frep');
// We'll use underscore string's slugify function for the first example
var _str = require('underscore.string');
// A custom slugification function for the second
var slugger = function(str) {
return str.replace(/( |-|\.)/g, '_').toLowerCase();
};
// And a third slugification function for the last example
var sluggifier = 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 property
var 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
}
];
// Our "structure", which determines where the
// values from each property will be placed.
var str = ':foo/:bar/:baz';
// Frep will now process each of the replacement
// patterns in sequence.
console.log(frep.strWithArr(str, patterns));
Copyright (c) 2013 Jon Schlinkert Licensed under the MIT license.
Project created by Jon Schlinkert.
This file was generated on Wed Sep 18 2013 00:03:54.
FAQs
Find and replace utility for node.js. Transform strings by running multiple RegExp or string find-and-replace patterns on a string in sequence, reducing the final string to the accumulated result of each transformation. Patterns can be strings (or arrays
The npm package frep receives a total of 360 weekly downloads. As such, frep popularity was classified as not popular.
We found that frep demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.