🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →

frep

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

frep - npm Package Compare versions

Comparing version

to
0.2.0

{
"name": "frep",
"version": "0.1.8",
"version": "0.2.0",
"main": [

@@ -5,0 +5,0 @@ "index.js"

@@ -8,5 +8,7 @@ /**!

const utils = require('./lib/utils');
const replace = module.exports = {};
var patternArray = function(str, patterns) {
replace.patternArray = function(str, patterns) {
return patterns.reduce(function(content, pairings) {

@@ -18,3 +20,2 @@ return content.replace(pairings[0], pairings[1]);

/**

@@ -27,6 +28,9 @@ * Transform a string with an array of RegExp or

*/
function strWithArr(str, replacements) {
return patternArray(str, replacements.map(function(match) {
var flags = replacements.flags? replacements.flags : 'g';
match.pattern = utils.buildRegexGroup(match.pattern, flags);
replace.strWithArr = function(str, replacements) {
return replace.patternArray(str, replacements.map(function(match) {
if (match.auto) {
var flags = match.flags ? match.flags : 'g';
match.pattern = utils.buildRegexGroup(match.pattern, flags);
}
return [match.pattern, match.replacement];

@@ -44,6 +48,7 @@ }));

*/
function arrWithArr(arr, replacements) {
replace.arrWithArr = function(arr, replacements) {
return arr.map(function(match) {
return strWithArr(match, replacements);
})
return replace.strWithArr(match, replacements);
});
};

@@ -59,3 +64,4 @@

*/
function strWithObj(str, replacements) {
replace.strWithObj = function(str, replacements) {
var re = new RegExp(Object.keys(replacements).join('|'), 'gi');

@@ -75,20 +81,14 @@ return str.replace(re, function(match) {

*/
function arrWithObj(arr, replacements) {
replace.arrWithObj = function(arr, replacements) {
return arr.map(function(match) {
return strWithObj(match, replacements);
})
return replace.strWithObj(match, replacements);
});
};
module.exports = {
strWithArr: strWithArr,
arrWithArr: arrWithArr,
strWithObj: strWithObj,
arrWithObj: arrWithObj,
// Aliases
replaceStr: strWithArr,
replaceArr: arrWithArr,
replaceObj: strWithObj,
replaceObjArr: arrWithObj
};
// Aliases
replace.replaceStr = replace.strWithArr;
replace.replaceArr = replace.arrWithArr;
replace.replaceObj = replace.strWithObj;
replace.replaceObjArr = replace.arrWithObj;
{
"name": "frep",
"description": "A find and replace utility. Modify strings by passing an array or object of RegExp or string replacement patterns",
"version": "0.1.8",
"version": "0.2.0",
"homepage": "https://github.com/jonschlinkert/frep",

@@ -27,2 +27,5 @@ "author": {

},
"scripts": {
"test": "mocha -R test"
},
"keywords": [

@@ -51,4 +54,5 @@ "find",

"chai": "~1.9.0",
"mocha": "~1.17.1"
"mocha": "~1.17.1",
"verb": "~0.1.20"
}
}
# frep [![NPM version](https://badge.fury.io/js/frep.png)](http://badge.fury.io/js/frep)
> A find and replace utility. Modify strings by passing an array of RegExp or string replacement patterns
> A find and replace utility. Modify strings by passing an array or object of RegExp or string replacement patterns
## Quickstart
Install with [npm](npmjs.org):
```bash
npm i frep --save
npm i frep --save-dev
```
Usage:
```js
var frep = require('frep');
var replace = require('frep');
// Patterns can be strings, regex or arrays.
// Replacements can be strings or functions.
var replacements = [
{
pattern: 'a',
replacement: 'x'
},
{
pattern: /b/,
replacement: 'y'
},
{
pattern: /c[\S]+/,
replacement: function(match) {
return match.toUpperCase();
}
}
];
console.log(replace.strWithArr('abcdefg', replacements));
//=> 'xyCDEFG'
```
## API
```js
// Transform a string with an array of replacement patterns
frep.strWithArr(String, replacements);
replace.strWithArr(String, replacements);
// Transform an array of strings with an array of replacement patterns
frep.arrWithArr(Array, replacements);
replace.arrWithArr(Array, replacements);
// Transform a string with an object of replacement patterns
frep.strWithObj(String, replacements);
replace.strWithObj(String, replacements);
// Transform an array of strings with an object of replacement patterns
frep.arrWithObj(Array, replacements);
replace.arrWithObj(Array, replacements);
```
## Methods
### .strWithArr( string, array )

@@ -33,3 +57,3 @@ Transform a string with an array of replacement patterns.

```js
frep.strWithArr(String, Array)
replace.strWithArr(String, Array)
```

@@ -61,3 +85,3 @@

frep.strWithArr(str, patterns));
replace.strWithArr(str, patterns));
// => #########DEFGHIJKLMNOPQRSTUVW$$$$$$$$$

@@ -94,3 +118,3 @@ ```

frep.strWithArr(str, patterns));
replace.strWithArr(str, patterns));
// => #########DEFGHIJKLMNOPQRSTUVW#########

@@ -103,3 +127,3 @@ ```

```js
frep.arrWithArr( Array, Array )
replace.arrWithArr( Array, Array )
```

@@ -122,2 +146,3 @@

];
var patterns = [

@@ -135,3 +160,3 @@ {

frep.arrWithArr(arr, patterns));
replace.arrWithArr(arr, patterns));
// => ["$$$on ###chlinkert", "###rian $$$oodward"]

@@ -148,3 +173,3 @@ ```

```js
frep.strWithObj( String, Object )
replace.strWithObj( String, Object )
```

@@ -173,3 +198,3 @@

frep.strWithObj(str, replacements));
replace.strWithObj(str, replacements));
// => AAABBBCCC

@@ -183,3 +208,3 @@ ```

```js
frep.arrWithObj(Array, Object)
replace.arrWithObj(Array, Object)
```

@@ -208,3 +233,3 @@

frep.arrWithObj(arr, replacements));
replace.arrWithObj(arr, replacements));
// => ['AAABBBCCC', 'DDDEEEFFF']

@@ -214,5 +239,4 @@ ```

## Usage example
### replace.strWithArray( string, array )
### Uses frep.strWithArray( string, array )
> Slugify URL segments using frep

@@ -223,3 +247,3 @@

```js
var frep = require('frep');
var replace = require('frep');

@@ -269,3 +293,3 @@ // We'll use underscore string's slugify function for the first example

// will be placed. Run frep to see what happens!
console.log(frep.strWithArr(':foo/:bar/:baz', patterns));
console.log(replace.strWithArr(':foo/:bar/:baz', patterns));
```

@@ -276,10 +300,13 @@

**[Jon Schlinkert](http://github/jonschlinkert)**
**Jon Schlinkert**
+ [github/jonschlinkert](http://github/jonschlinkert)
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright (c) 2014 Jon Schlinkert, contributors.
Released under the MIT license
## License
Copyright (c) 2014 [Jon Schlinkert](http://github/jonschlinkert), contributors.
Licensed under the [MIT license](LICENSE-MIT).
***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 30, 2014._

@@ -0,1 +1,2 @@

const path = require('path');
const expect = require('chai').expect;

@@ -7,6 +8,68 @@ const frep = require('../');

// Setup
var name = 'C:/foo/bar/baz/somefile.md';
var dirname = path.dirname(name);
var extname = path.extname(name);
var basename = path.basename(name, path.extname(name));
var p = {
dirname: dirname,
basename: basename,
extname: extname
};
describe('when a string with multiple potential matches is passed in', function () {
it('should only replace exact matches and not replace partial matches.', function () {
var template = '%d';
var replacements = [
{
pattern: /%s\b/g,
replacement: p.dirname
},
{
pattern: /%d\b/g,
replacement: p.basename
},
{
pattern: /%j\b/g,
replacement: p.extname
}
];
var actual = frep.strWithArr(template, replacements);
var expected = 'somefile';
expect(actual).to.eql(expected);
});
});
describe('when a string with multiple potential matches is passed in', function () {
it('should only replace exact matches and not replace partial matches.', function () {
var template = ':d/:dir';
var replacements = [
{
pattern: /:d\b/g,
replacement: p.dirname
},
{
pattern: /:b\b/g,
replacement: p.basename
},
{
pattern: /:e\b/g,
replacement: p.extname
}
];
var actual = frep.strWithArr(template, replacements);
var expected = 'C:/foo/bar/baz/:dir';
expect(actual).to.eql(expected);
});
});
describe('frep', function () {
it('should replace "foo" with "SUCCESS".', function () {
var patterns = [{pattern: /:foo/g, replacement: 'SUCCESS'}];
var actual = frep.strWithArr(':foo/:bar/:baz', patterns);
var replacements = [{pattern: /:foo/g, replacement: 'SUCCESS'}];
var actual = frep.strWithArr(':foo/:bar/:baz', replacements);
var expected = 'SUCCESS/:bar/:baz';

@@ -17,3 +80,3 @@ expect(actual).to.eql(expected);

it('should replace the given strings with the given replacements.', function () {
var patterns = [
var replacements = [
{

@@ -28,3 +91,3 @@ pattern: /[ABC]/g,

];
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', patterns);
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', replacements);
var expected = '@@@@@@@@@DEFGHIJKLMNOPQRSTUVW#########';

@@ -35,3 +98,3 @@ expect(actual).to.eql(expected);

it('should replace the given strings with the given replacements.', function () {
var patterns = [
var replacements = [
{

@@ -46,3 +109,3 @@ pattern: /(ABC)/g,

];
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', patterns);
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', replacements);
var expected = '###DEFGHIJKLMNOPQRSTUVW@@@';

@@ -53,3 +116,3 @@ expect(actual).to.eql(expected);

it('should replace the given strings with the given replacements.', function () {
var patterns = [
var replacements = [
{

@@ -60,10 +123,14 @@ pattern: /[^ABC]/g,

];
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', patterns);
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', replacements);
var expected = 'ABC#######################';
expect(actual).to.eql(expected);
});
});
it('should replace the given string with an array of replacement patterns.', function () {
var patterns = [
describe('when an array of replacements is given, and `auto: true` is defined', function () {
it('should convert each pattern to regex, and transform the given string with the replacement', function () {
var replacements = [
{
auto: true,
flags: 'g',

@@ -74,3 +141,3 @@ pattern: ['ABC', 'XYZ'],

];
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', patterns);
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', replacements);
var expected = '@@@DEFGHIJKLMNOPQRSTUVW@@@';

@@ -80,9 +147,6 @@ expect(actual).to.eql(expected);

});
describe('pattern arrays', function () {
it('should replace the given strings with the given replacements.', function () {
var patterns = [
var replacements = [
{
auto: true,
pattern: ['ABC', 'XYZ'],

@@ -94,3 +158,3 @@ replacement: function(match) {

];
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', patterns);
var actual = frep.strWithArr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', replacements);
var expected = 'abcDEFGHIJKLMNOPQRSTUVWxyz';

@@ -97,0 +161,0 @@ expect(actual).to.eql(expected);