Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump deps & move API back to README #9

Merged
merged 1 commit into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"profile": "https://robertwpearce.com",
"contributions": [
"code",
"ideas"
"ideas",
"test",
"example",
"doc"
]
}
],
Expand Down
71 changes: 0 additions & 71 deletions API.md

This file was deleted.

89 changes: 86 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,103 @@
[![npm version](https://img.shields.io/npm/v/@rpearce/flexible-string-replace.svg)](https://www.npmjs.com/package/@rpearce/flexible-string-replace) [![npm downloads](https://img.shields.io/npm/dm/@rpearce/flexible-string-replace.svg)](https://www.npmjs.com/package/@rpearce/flexible-string-replace) [![Build Status](https://travis-ci.org/rpearce/flexible-string-replace.svg?branch=master)](https://travis-ci.org/rpearce/flexible-string-replace) [![Coverage Status](https://coveralls.io/repos/github/rpearce/flexible-string-replace/badge.svg?branch=master)](https://coveralls.io/github/rpearce/flexible-string-replace?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/ca092c1e9dfe10b455d7/maintainability)](https://codeclimate.com/github/rpearce/flexible-string-replace/maintainability)

## Links
* [`API Documentation`](./API.md)
* [`Installation`](#installation)
* [`Usage`](#usage)
* [`All Contributors`](#contributors)
* [`Authors`](./AUTHORS)
* [`Changelog`](./CHANGELOG.md)
* [`Contributing`](./CONTRIBUTING.md)
* [`Code of Conduct`](./CODE_OF_CONDUCT.md)

## 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`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/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`](https://ramdajs.com/docs/#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.

```js
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](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
<table><tr><td align="center"><a href="https://robertwpearce.com"><img src="https://avatars2.githubusercontent.com/u/592876?v=4" width="100px;" alt="Robert Pearce"/><br /><sub><b>Robert Pearce</b></sub></a><br /><a href="https://github.com/rpearce/flexible-string-replace/commits?author=rpearce" title="Code">💻</a> <a href="#ideas-rpearce" title="Ideas, Planning, & Feedback">🤔</a></td></tr></table>
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://robertwpearce.com"><img src="https://avatars2.githubusercontent.com/u/592876?v=4" width="100px;" alt=""/><br /><sub><b>Robert Pearce</b></sub></a><br /><a href="https://github.com/rpearce/flexible-string-replace/commits?author=rpearce" title="Code">💻</a> <a href="#ideas-rpearce" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/rpearce/flexible-string-replace/commits?author=rpearce" title="Tests">⚠️</a> <a href="#example-rpearce" title="Examples">💡</a> <a href="https://github.com/rpearce/flexible-string-replace/commits?author=rpearce" title="Documentation">📖</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
2 changes: 1 addition & 1 deletion husky.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
hooks: {
'pre-commit': 'lint-staged'
'pre-push': 'lint-staged'
}
}
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@
"test": "nyc ava --verbose"
},
"devDependencies": {
"@babel/preset-react": "^7.0.0",
"all-contributors-cli": "^6.9.0",
"ava": "^2.3.0",
"coveralls": "^3.0.6",
"eslint": "^6.3.0",
"eslint-config-prettier": "^6.3.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-react": "^7.14.3",
"husky": "^3.0.5",
"lint-staged": "^9.2.5",
"@babel/preset-react": "^7.7.4",
"all-contributors-cli": "^6.11.2",
"ava": "^2.4.0",
"coveralls": "^3.0.9",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.17.0",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"npm-run-all": "^4.1.5",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"react": "^16.9.0"
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"react": "^16.12.0"
}
}
Loading