replacements

0.2.1 • Public • Published

replacements NPM version Build Status

Transform a string with an array of replacement patterns.

(TOC generated by verb)

Install

Install with npm

$ npm i replacements --save

Usage

var replace = require('replacements');

object of replacement patterns

var transformers = {
  pattern: /a/g,
  replacement: 'bbb'
};
console.log(replace('aaa', transformers));
//=> 'bbbbbbbbb'

key-value replacement patterns

For basic string transformations, you can pass the string to replace as the key, and the replacement as a value:

var transformers = {
  'a': 'b'
  'c': 'd'
};
console.log(replace('aaabbbccc', transformers));
//=> 'bbbbbbddd'

Note that only the first matching string will only be replaced using this format. If you need more flexibility, use the pattern-replacement regex syntax..

array of replacement patterns

var transformers = [
  {pattern: /a/g, replacement: 'bbb'},
  {pattern: /b/g, replacement: 'ccc'},
  {pattern: /c/g, replacement: 'ddd'},
  {pattern: /d/g, replacement: 'eee'},
  {pattern: /[e]+/g, replacement: '_DONE_'}
];
console.log(replace('aaa', transformers));
//=> '__DONE__'

functions as replacements

var transformers = {
  pattern: /a+/g,
  replacement: function(match) {
    return match.split('').map(function(str, i) {
      if (=== 0) {return 'b'; }
      if (=== 1) {return str.toUpperCase();}
      return str;
    }).join('');
  }
};
console.log(replace('aaa', transformers));
//=> 'bAa'

Related projects

easy-renamer: Easily rename files using custom rename functions that are automatically used against any filepaths that… more | homepage

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jon Schlinkert

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb on December 20, 2015.

Package Sidebar

Install

npm i replacements

Weekly Downloads

606

Version

0.2.1

License

MIT

Last publish

Collaborators

  • jonschlinkert