Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zbo14/reworder
✏️ Replace words and phrases via custom mappings
https://github.com/zbo14/reworder
matching phrases regex subgroups text text-matching text-processing words
Last synced: 18 days ago
JSON representation
✏️ Replace words and phrases via custom mappings
- Host: GitHub
- URL: https://github.com/zbo14/reworder
- Owner: zbo14
- License: mit
- Created: 2022-01-28T03:38:33.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-12T17:47:11.000Z (almost 3 years ago)
- Last Synced: 2024-11-19T20:17:05.055Z (about 1 month ago)
- Topics: matching, phrases, regex, subgroups, text, text-matching, text-processing, words
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/reworder
- Size: 607 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# reworder
[![npm](https://img.shields.io/npm/v/reworder.svg?color=brightgreen)](https://www.npmjs.com/package/reworder)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)Replace words and phrases via custom mappings!
## Install
`npm i reworder`
## Usage
**Replace by strings:**
```js
'use strict'const reworder = require('reword')
const reword = reworder([
{ key: 'foo', value: 'bar' },
{ key: 'bar', value: 'baz' }
])const input = 'abc foo hello world bar baz'
const result = reword(input)console.log(result)
// {
// input: 'abc foo hello world bar baz',
//
// matches: [
// { key: 'foo', index: 4, value: 'bar' },
// { key: 'bar', index: 20, value: 'baz' }
// ],
//
// output: 'abc bar hello world baz baz'
// }
```**Replace by regex:**
```js
const reword = reworder({ key: /ba\w/, value: 'foo' })
const input = 'abc foo hello world bar baz'
const result = reword(input)console.log(result)
// {
// input: 'abc foo hello world bar baz',
//
// matches: [
// { key: 'bar', index: 20, value: 'foo' },
// { key: 'baz', index: 24, value: 'foo' }
// ],
//
// output: 'abc foo hello world foo foo'
// }
```**Replace by transform function:**
```js
const reword = reworder({
key: /ba\w/,
transform: key => key.slice(-1)
})const input = 'abc foo hello world bar baz'
const result = reword(input)console.log(result)
// {
// input: 'abc foo hello world bar baz',
//
// matches: [
// { key: 'bar', index: 20, value: 'r' },
// { key: 'baz', index: 24, value: 'z' }
// ],
//
// output: 'abc foo hello world r z'
// }
````transform` can be `async`, in which case the `reword` function returns a promise:
```js
const result = await reword(input)
```**Replace with options:**
```js
const config = [
{ key: 'foo', value: 'bar' },
{ key: 'hello world', value: 'helloworld' }
]const options = {
caseInsensitive: true,
includePunctuation: ',',
variableSpacing: true
}const reword = reworder(config, options)
const input = 'abc FoO hello, world bar baz'
const result = reword(input)console.log(result)
// {
// input: 'abc FoO hello, world bar baz',
//
// matches: [
// { key: 'FoO', index: 4, value: 'bar' },
// { key: 'hello, world', index: 8, value: 'helloworld' }
// ],
//
// output: 'abc bar helloworld bar baz'
// }
```## Reference
* `config` is an object literal or array of object literals. Each object literal must contain a `key` (string or RegExp) and either `value` (string) or `transform` (function). The `transform` function should accept a single argument- a string matching the `key`, and return a string or a promise that resolves to a string.
* `options` is an object literal with the following properties:
* `caseInsensitive` is a boolean indicating whether regex permits case insensitive matching.
* `includePunctuation` is a boolean or string indicating whether/what punctuation can be included in matches.
* `variableSpacing` is a boolean indicating whether the regex matches variable number of spaces.## Test
`npm test`
## Lint
`npm run lint` or `npm run lint:fix`
## License
Licensed under [MIT](./LICENSE).