0.1.4 • Published 7 years ago

replace-any v0.1.4

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

replace-any Version Build Status

Flexible string replacement.

Installation

Add the dependency to your project:

$ npm install --save-dev replace-any

Usage

You can use replaceAny to replace parts of a string with anything you want: other strings, objects, even JSX elements!

You just need to pass two arguments:

  1. An array of replacers, where each replacer specifies a regular expression to execute and a function that produces the replacement
  2. The string you want to replace

You'll get back an array with the results of applying all your replacers:

import { replaceAny } from 'replace-any';

const lineBreakReplacer = {
  regexp: new RegExp('\\n', 'gi'),
  replace: (key) => <br key={key} />,
};

const linkReplacer = {
  regexp: new RegExp('https?://[^\\s]+', 'gi'),
  replace: (key, text) => <a key={key} href={text}>{text}</a>
};

const replacers = [lineBreakReplacer, linkReplacer]
const processMessage = (body) => replaceAny(replacers, body);

processMessage('Hi there!\n\nGo to https://www.github.com and star this project!')
// [
//   "Hi there!",
//   <br />,
//   <br />,
//   "Go to ",
//   <a href="https://www.github.com">https://www.github.com</a>,
//   " and star this project!"
// ]

You can then use this in your React components:

import React, { Component } from 'react';

class Message extends Component {
  render() {
    return (
      <div className="Message">
        {processMessage(this.props.body)}
      </div>
    )
  }
}

Meta

Contributors

License

Copyright (c) 2018 Daniel Perez Alvarez (unindented.org). This is free software, and may be redistributed under the terms specified in the LICENSE file.

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago