3.1.1 • Published 1 year ago
paraphrase v3.1.1
paraphrase
🧩 Create flavoured string template interpolation
npm i paraphrase
Creates new paraphrase method instance
import { paraphrase } from "paraphrase";
const phrase = paraphrase(/\${([^{}]*)}/gm); // Create a new phrase function using a RegExp match
phrase("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
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
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
const phrase = paraphrase(/\${([^{}]*)}/gm, { clean: true });
phrase("Hello, ${firstname} ${lastname}", { firstname: "Martin" }); // Hello, Martin
Examples
Objects
phrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
Objects with dot notation
const user = {
name: { first: "Martin", last: "Prince" },
};
phrase("Hello, ${name.first} ${name.last}", user); // Hello, Martin Prince
Arrays
phrase("Hello, ${0} ${1}", ["Martin", "Prince"]); // Hello, Martin Prince
Spread arguments
phrase("Hello, ${0} ${1}", "Martin", "Prince"); // Hello, Martin Prince
Premade
dollar ${...}
import { dollar as phrase } from "paraphrase";
phrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
double {{...}}
import { double as phrase } from "paraphrase";
phrase("Hello, {{name}}", { name: "Martin" }); // Hello, Martin
single {...}
import { single as phrase } from "paraphrase";
phrase("Hello, {name}", { name: "Martin" }); // Hello, Martin
percent %{...}
(i18n style)
import { percent as phrase } from "paraphrase";
phrase("Hello, %{name}", { name: "Martin" }); // Hello, Martin
hash #{...}
(ruby style)
import { hash as phrase } from "paraphrase";
phrase("Hello, #{name}", { name: "Martin" }); // Hello, Martin
loose. Accommodate all of the above
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)
import { hash as phrase } from "paraphrase";
phrase.patterns; // [ /#{([^{}]*)}/gm ]
3.1.1
1 year ago
3.1.0
1 year ago
3.1.0-rc-dcd4648
1 year ago
3.0.0
3 years ago
2.0.0
3 years ago
2.0.0-rc.1-42d32f3
3 years ago
1.8.1
3 years ago
1.8.0
4 years ago
1.7.3
5 years ago
1.7.2
5 years ago
1.7.1
5 years ago
1.7.0
5 years ago
1.6.0
5 years ago
1.5.0
5 years ago
1.4.3
5 years ago
1.4.2
5 years ago
1.4.1
6 years ago
1.4.0
6 years ago
1.3.1
6 years ago
1.3.0
6 years ago
1.3.0-rc-8b91626
6 years ago
1.3.0-rc-065c799
6 years ago
1.2.2
6 years ago
1.2.1
6 years ago
1.2.0
6 years ago
1.1.0
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago