https://github.com/omrilotan/paraphrase
🧩 Create flavoured string template interpolation
https://github.com/omrilotan/paraphrase
interpolation javascript phrase replace string
Last synced: 3 months ago
JSON representation
🧩 Create flavoured string template interpolation
- Host: GitHub
- URL: https://github.com/omrilotan/paraphrase
- Owner: omrilotan
- License: unlicense
- Created: 2019-10-31T14:34:26.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-24T10:28:37.000Z (8 months ago)
- Last Synced: 2024-10-25T04:08:30.997Z (8 months ago)
- Topics: interpolation, javascript, phrase, replace, string
- Language: TypeScript
- Homepage: https://omrilotan.com/paraphrase
- Size: 36.1 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# paraphrase [](https://www.npmjs.com/package/paraphrase)
## 🧩 Create flavoured string template interpolation
[](https://github.com/omrilotan/paraphrase/actions) [](https://runkit.com/omrilotan/paraphrase) [](https://bundlephobia.com/result?p=paraphrase)
```
npm i paraphrase
```Creates new paraphrase method instance
```js
import { paraphrase } from "paraphrase";
const phrase = paraphrase(/\${([^{}]*)}/gm); // Create a new phrase function using a RegExp matchphrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
```Acceptable replacements (values) are strings and numbers
### Arguments and Options
One or more RegExp replacers, an optional options object at the end
| option | meaning | type | default |
| --------- | ------------------------------------------------------------------------------- | --------- | ------- |
| recursive | Should continue to resolve result string until replacements have been exhausted | `Boolean` | `true` |
| resolve | Should resolve dot notations within the template | `Boolean` | `true` |
| clean | Should remove unmatched template instances | `Boolean` | `false` |##### Multiple replacers
```js
const phrase = paraphrase(/\${([^{}]*)}/gm, /\{{([^{}]*)}}/gm);phrase("Hello, ${firstname} {{lastname}}", {
firstname: "Martin",
lastname: "Prince",
}); // Hello, Martin Prince
```##### Dot notation resolve
Treat dots as part of the key instead of notation marks
```js
const phrase = paraphrase(/\${([^{}]*)}/gm, { resolve: false });phrase("Hello, ${name} ${last.name}", {
name: "Martin",
"last.name": "Prince",
}); // Hello, Martin Prince
```##### Unmatched cleanup
Remove unmatched template instances from the result string
```js
const phrase = paraphrase(/\${([^{}]*)}/gm, { clean: true });phrase("Hello, ${firstname} ${lastname}", { firstname: "Martin" }); // Hello, Martin
```## Examples
### Objects
```js
phrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
```### Objects with dot notation
```js
const user = {
name: { first: "Martin", last: "Prince" },
};
phrase("Hello, ${name.first} ${name.last}", user); // Hello, Martin Prince
```### Arrays
```js
phrase("Hello, ${0} ${1}", ["Martin", "Prince"]); // Hello, Martin Prince
```### Spread arguments
```js
phrase("Hello, ${0} ${1}", "Martin", "Prince"); // Hello, Martin Prince
```## Premade
### dollar `${...}`
```js
import { dollar as phrase } from "paraphrase";phrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
```### double `{{...}}`
```js
import { double as phrase } from "paraphrase";phrase("Hello, {{name}}", { name: "Martin" }); // Hello, Martin
```### single `{...}`
```js
import { single as phrase } from "paraphrase";phrase("Hello, {name}", { name: "Martin" }); // Hello, Martin
```### percent `%{...}` (i18n style)
```js
import { percent as phrase } from "paraphrase";phrase("Hello, %{name}", { name: "Martin" }); // Hello, Martin
```### hash `#{...}` (ruby style)
```js
import { hash as phrase } from "paraphrase";phrase("Hello, #{name}", { name: "Martin" }); // Hello, Martin
```### loose. Accommodate all of the above
```js
import { loose as phrase } from 'paraphrase';phrase('Hello, #{name.first} {name.last}', {name: { first: 'Martin', last: 'Prince' }); // Hello, Martin Prince
```## patterns
A paraphrase instance exposes view to its patterns array (immutable)
```js
import { hash as phrase } from "paraphrase";phrase.patterns; // [ /#{([^{}]*)}/gm ]
```