Socket
Book a DemoInstallSign in
Socket

paraphrase

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

paraphrase

🧩 Create flavoured string template interpolation

3.1.2
latest
Source
npmnpm
Version published
Weekly downloads
1.8K
-14.74%
Maintainers
1
Weekly downloads
 
Created
Source

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

optionmeaningtypedefault
recursiveShould continue to resolve result string until replacements have been exhaustedBooleantrue
resolveShould resolve dot notations within the templateBooleantrue
cleanShould remove unmatched template instancesBooleanfalse
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 ]

Keywords

string

FAQs

Package last updated on 24 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.