1. paraphrase
🧩 Create flavoured string template interpolation
paraphrase
Package: paraphrase
Created by: omrilotan
Last modified: Thu, 26 Jan 2023 10:02:05 GMT
Version: 3.1.1
License: Unlicense
Downloads: 16,997
Repository: https://github.com/omrilotan/paraphrase

Install

npm install paraphrase
yarn add paraphrase

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 ]

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code