Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including paraphrase with all npm packages installed. Try it out:

var paraphrase = require("paraphrase")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

paraphrase v1.7.3

🧩 Create flavoured string template interpolation

paraphrase

🧩 Create flavoured string template interpolation

npm i paraphrase

Creates new paraphrase method instance

const paraphrase = require('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
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 ${...}

const phrase = require('paraphrase/dollar');

phrase('Hello, ${name}', {name: 'Martin'}); // Hello, Martin

double {{...}}

const phrase = require('paraphrase/double');

phrase('Hello, {{name}}', {name: 'Martin'}); // Hello, Martin

single {...}

const phrase = require('paraphrase/single');

phrase('Hello, {name}', {name: 'Martin'}); // Hello, Martin

percent %{...} (i18n style)

const phrase = require('paraphrase/percent');

phrase('Hello, %{name}', {name: 'Martin'}); // Hello, Martin

hash #{...} (ruby style)

const phrase = require('paraphrase/hash');

phrase('Hello, #{name}', {name: 'Martin'}); // Hello, Martin

patterns

A paraphrase instance exposes access to it's patterns array

const phrase = require('paraphrase/hash');

phrase.patterns // [ /#{([^{}]*)}/gm ]
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free