https://github.com/jedwards1211/preserve-case
very comprehensive case-preserving string.replace
https://github.com/jedwards1211/preserve-case
case-preserving preserve-case refactoring replace replace-text string-manipulation string-replace string-substitution
Last synced: 10 months ago
JSON representation
very comprehensive case-preserving string.replace
- Host: GitHub
- URL: https://github.com/jedwards1211/preserve-case
- Owner: jedwards1211
- License: mit
- Created: 2017-06-16T14:54:18.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-08-13T17:09:33.000Z (almost 8 years ago)
- Last Synced: 2025-01-12T02:02:26.252Z (over 1 year ago)
- Topics: case-preserving, preserve-case, refactoring, replace, replace-text, string-manipulation, string-replace, string-substitution
- Language: JavaScript
- Homepage:
- Size: 156 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# preserve-case
[](https://travis-ci.org/jedwards1211/preserve-case)
[](https://codecov.io/gh/jedwards1211/preserve-case)
[](https://github.com/semantic-release/semantic-release)
[](http://commitizen.github.io/cz-cli/)
case-preserving `string.replace`
## Usage
```
npm install --save preserve-case
```
```js
import replace from 'preserve-case'
console.log(replace.all('foo bar FOO_BAR foo-bar fooBar foo-Bar', 'foo bar', 'baz qux'))
// baz qux BAZ_QUX baz-qux bazQux baz-Qux
```
## API
### `replace(str, query, replacement, [options])`
```js
replace(
str: string,
query: string | RegExp,
replacement: string | Function,
options?: {
caseTypes?: Array
}
): string`
```
Works exactly like `str.replace(query, replacement)`, except that:
* it preserves the case of what it replaces (using the marvelous `case` package)
* if `query` is a string, it matches `query` in any case. (Under the hood, it creates and uses
a `RegExp` with all `caseTypes` of `query` joined together, e.g.
`str.replace(/foo bar|FOO BAR|fooBar|FooBar|.../, ...)`)
`options.caseTypes` defaults to all types built into the [`case`](https://github.com/nbubna/Case) package.
This may be more than you want, so look into it. For instance, the other cases of 'foo bar' include, but may not be
limited to:
```
'Foo Bar'
'Foo bar'
'FOO BAR'
'fooBar'
'FooBar'
'foo-bar'
'Foo-Bar'
'foo_bar'
'FOO_BAR'
```
### `replace.all(str, query, replacement, [options])`
Unlike `replace`, this replaces *all* occurrences of `query`, not just the first one, even if `query` is a `string` or
a `RegExp` without the `g` (global) flag.
## Acknowledgements
Thanks to Nathan Bubna for his [`case`](https://github.com/nbubna/Case) package, which powers `preserve-case`!
### `replace.createSearchRegExp(query, [options])`
Creates a `RegExp` that matches for any case of the given `query` string.
Like the other functions, `options.caseTypes` defaults to all types built into
the [`case`](https://github.com/nbubna/Case) package.
You may also pass a `flags` option for the `RegExp` flags (e.g. `flags: 'g'`)