Skip to content

Commit

Permalink
adds object replacement patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Oct 3, 2013
1 parent d185cd6 commit f5b264c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 28 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Set default behaviour, in case users don't have core.autocrlf set.
* text=lf
* text eol=lf
*.* eol=lf

*.jpg binary
*.gif binary
*.png binary
*.jpeg binary
109 changes: 94 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,33 @@ npm i frep --save
```js
var frep = require('frep');

frep.replaceStr(String, ArrayOfPatterns));
frep.replaceArray(ArrayOfStrings, ArrayOfPatterns));
// 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);
```



## Methods

### replaceStr
### strWithArr
Transform a string with an array of replacement patterns.

`replaceStr(String, Array)`
```js
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).
* A new string is returned with some or all matches replaced by the given replacement patterns.


Given the following:

Expand All @@ -45,23 +58,23 @@ var patterns = [
...
];

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

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

```
#########DEFGHIJKLMNOPQRSTUVW$$$$$$$$$
```

### replaceArray
### arrWithArr
Transform an array of strings with an array of replacement patterns

`replaceArray(Array, Array)`
```js
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.
* A new array of strings is returned with some or all matches replaced by the given replacement patterns.

Given the following:

Expand All @@ -84,13 +97,79 @@ var patterns = [
...
];

frep.replaceArray(arr, patterns));
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.

```json
["$$$on ###chlinkert", "###rian $$$oodward"]


### strWithObj
Transform a string with an object of replacement patterns

```js
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.
* A new string is returned with some or all matches replaced by the given replacement patterns.


Given the following:

```js
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
```


### arrWithObj
Transform an array of strings with an object of replacement patterns

```js
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.
* A new array of strings is returned with some or all matches replaced by the given replacement patterns.


Given the following:

```js
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']
```


Expand Down
70 changes: 57 additions & 13 deletions frep.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,85 @@
* Licensed under the MIT license.
*/

var path = require('path');


'use strict';

var path = require('path');


var reduceArray = function (str, replacements) {
return replacements.reduce(function (content, pairings) {
var patternArray = function(str, patterns) {
return patterns.reduce(function(content, pairings) {
return content.replace(pairings[0], pairings[1]);
}, str);
};



/**
* Modify the given string with an array of RegExp or string replacement patterns
* Transform a string with an array of RegExp or
* string replacement patterns
* @param {String} str The string to modify.
* @param {Array} replacements Array of replacement patterns.
* @return {String} The new string.
*/
exports.replaceStr = function (str, replacements) {
return reduceArray(str, replacements.map(function (rpl) {
return [rpl.pattern, rpl.replacement];
function strWithArr(str, replacements) {
return patternArray(str, replacements.map(function(match) {
return [match.pattern, match.replacement];
}));
};


/**
* Modify the given array of strings with an array of RegExp or string replacement patterns
* Transform an array of strings with an array of
* RegExp or string replacement patterns
* @param {Array} arr The array of strings to modify.
* @param {Array} replacements Array of replacement patterns.
* @return {String} The new string.
*/
function arrWithArr(arr, replacements) {
return arr.map(function(match) {
return strWithArr(match, replacements);
})
};


/**
* Transform a string with an object of RegExp or
* string replacement patterns
* @param {String} str The string to modify.
* @param {Object} patterns Array of replacement patterns.
* @return {String} The new string.
*/
function strWithObj(str, replacements) {
var re = new RegExp(Object.keys(replacements).join('|'), 'gi');
return str.replace(re, function(match) {
return replacements[match];
});
};


/**
* Transform an array of strings with an array of
* objects of RegExp or string replacement patterns
* @param {Array} arr The array of strings to modify.
* @param {Array} replacements Array of replacement patterns.
* @return {String} The new string.
*/
exports.replaceArray = function (arr, replacements) {
return arr.map(function (item) {
return exports.replaceStr(item, replacements);
function arrWithObj(arr, replacements) {
return arr.map(function(match) {
return strWithObj(match, replacements);
})
};


module.exports = {
strWithArr: strWithArr,
arrWithArr: arrWithArr,
strWithObj: strWithObj,
arrWithObj: arrWithObj,

// Aliases
replaceStr: strWithArr,
replaceArr: arrWithArr,
replaceObj: strWithObj,
replaceObjArr: arrWithObj
};

0 comments on commit f5b264c

Please sign in to comment.