包详细信息

@rpearce/flexible-string-replace

rpearce770BSD-31.0.0

🧶 Safely replace any part of a string with anything. Example: useful for replacing substrings with JSX in React

string-replace, string-manipulation, string-interpolation, replace-string

自述文件

flexible-string-replace

All Contributors npm version npm downloads Build Status Coverage Status Maintainability

Links

Installation

$ npm i @rpearce/flexible-string-replace

or

$ yarn add @rpearce/flexible-string-replace

Usage

flexible-string-replace mirrors the functionality of String.prototype.replace with the following exceptions:

  • you can transform your match however you see fit
  • the return value is [ * ] – an Array of whatever type your Replacement is or returns. For example, if you pass it a function, then you'll get back a list of strings as well as your match transformations, whereas if you pass a string, you'll get back simply a list of strings with your replacement applied
  • the argument order is (Pattern, Replacement, String) so that if you'd like to curry the function and partially apply the first two arguments, you can then reuse those over and over again with different strings

Note: while these examples use some JSX, your matching function can return whatever you like.

import flexibleStringReplace from '@rpearce/flexible-string-replace'

const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
const searchText = 'spain'
const replacement = (match, offset) => <mark key={offset}>{match}</mark>


// usage with RegExp pattern
const pattern = new RegExp(searchText, 'igm')
flexibleStringReplace(pattern, replacement, str)
// [
//   'The rain in ',
//   <mark>Spain</mark>,
//   ' falls mainly on the plain. ',
//   <mark>Spain</mark>,
//   ' is nice.'
// ]


// usage with RegExp pattern and string Replacement
const pattern = 'Spain'
flexibleStringReplace(pattern, 'foobar', str)
// [
//   'The rain in ',
//   'foobar',
//   ' falls mainly on the plain. ',
//   'foobar',
//   ' is nice.'
// ]


// usage with String pattern (no match)
const pattern = 'spain'
flexibleStringReplace(pattern, replacement, str)
// ["The rain in Spain falls mainly on the plain. Spain is nice."]


// usage with String pattern (match)
const pattern = 'Spain'
flexibleStringReplace(pattern, replacement, str)
// [
//   'The rain in ',
//   <mark>Spain</mark>,
//   ' falls mainly on the plain. Spain is nice.',
// ]

Contributors

Thanks goes to these wonderful people (emoji key):


Robert Pearce

💻 🤔 ⚠️ 💡 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[1.0.0] - 2020-03-15

Added

  • TypeScript support

Changed

  • License from ISC to BSD-3

Fixed

  • security issues in dependencies

[0.1.3] - 2019-12-28

Changed

  • bumped a number of dev dependencies to resolve security issue
  • moved API docs back into README

[0.1.2] - 2019-09-11

Changed

  • bumped lodash.merge to resolve security issue

[0.1.1] - 2019-09-11

Fixed

  • make sure index.js is present in files key in package.json

[0.1.0] - 2019-03-04

Added

  • Added all the things