paraphrase
TypeScript icon, indicating that this package has built-in type declarations

3.1.1 • Public • Published

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 ]

Package Sidebar

Install

npm i paraphrase

Weekly Downloads

3,730

Version

3.1.1

License

Unlicense

Unpacked Size

29.4 kB

Total Files

8

Last publish

Collaborators

  • omrilotan