Skip to content

Instantly share code, notes, and snippets.

@artisonian
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artisonian/353ec5ec1218822a4052 to your computer and use it in GitHub Desktop.
Save artisonian/353ec5ec1218822a4052 to your computer and use it in GitHub Desktop.
replace-tokens.js
// `replaceTokens` is a modification of Crockford's `String.prototype.supplant`
// which takes works on an object/array (if given as the second argument)
// or a variable number of replacements.
module.exports = function replaceTokens (tmpl/*, replacements... */) {
var args = [].slice.call(arguments, 1);
if (args[0] != null && (Array.isArray(args[0]) || typeof args[0] === 'object')) {
args = args[0];
}
return tmpl.replace(/\{([^{}]*)\}/g, function (match, index) {
var result = args[index];
return typeof result === 'string' || typeof result === 'number' ? result : match;
});
};
{
"name": "replace-tokens",
"version": "0.0.0",
"description": "A .NET-style token replacer for strings",
"main": "index.js",
"scripts": {
"test": "tape test.js"
},
"author": "Leroy Campbell <leroy@artisonian.com>",
"license": "MIT",
"devDependencies": {
"tape": "^2.13.3"
}
}
var test = require('tape');
var replace = require('./index');
test('replacing tokens in numeric order', function (t) {
t.plan(1);
var tmpl = 'The {0} walks {1} to the {2}.';
var result = replace(tmpl, 'fox', 'quickly', 'fence');
t.equal(result, 'The fox walks quickly to the fence.');
});
test('replacing tokens out of numeric order', function (t) {
t.plan(1);
var tmpl = "If you're {2} and you know it, {0} your {1}.";
var result = replace(tmpl, 'happy', 'clap', 'hands');
t.equal(result, "If you're hands and you know it, happy your clap.");
});
test('using only the given replacements', function (t) {
t.plan(4);
var tmpl = 'Do not {0} the {1}.';
t.equal(replace(tmpl), 'Do not {0} the {1}.');
t.equal(replace(tmpl, 'touch'), 'Do not touch the {1}.');
t.equal(replace(tmpl, null, 'cookie'), 'Do not {0} the cookie.');
t.equal(replace(tmpl, 'touch', 'cookie', 'at all'), 'Do not touch the cookie.');
});
test('using a replacement more than once', function (t) {
t.plan(1);
var tmpl = 'You put your {0} {1} in, you put your {0} {1} out...';
var result = replace(tmpl, 'left', 'foot');
t.equal(result, 'You put your left foot in, you put your left foot out...');
});
test('using an array for replacements', function (t) {
t.plan(3);
var tmpl = 'The {0} part of waking up is {1} in your {2}.';
t.equal(replace(tmpl, ['best']), 'The best part of waking up is {1} in your {2}.');
t.equal(replace(tmpl, ['best', 'Folgers']), 'The best part of waking up is Folgers in your {2}.');
t.equal(replace(tmpl, ['best', 'Folgers', 'cup']), 'The best part of waking up is Folgers in your cup.');
});
test('using an object for replacements', function (t) {
t.plan(2);
var tmpl = 'The {thing} on the bus go {action} and {action}.';
var result = replace(tmpl, { thing: 'wheels', action: 'round' });
t.equal(result, 'The wheels on the bus go round and round.');
tmpl = '{name} the {0} has {count} {1}.';
result = replace(tmpl, { 0: 'camel', 1: 'humps', name: 'Sally', count: 'five' });
t.equal(result, 'Sally the camel has five humps.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment