JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 618
  • Score
    100M100P100Q98815F
  • License

Transform a string with an array of replacement patterns.

Package Exports

  • replacements

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (replacements) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

replacements NPM version Build Status

Transform a string with an array of replacement patterns.

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 (i === 0) {return 'b'; }
      if (i === 1) {return str.toUpperCase();}
      return str;
    }).join('');
  }
};
console.log(replace('aaa', transformers));
//=> 'bAa'

Author

Jon Schlinkert

License

Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license


This file was generated by verb-cli on March 13, 2015.