Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customize open/close tags via 'string-template/custom' #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function compile(string, inline) {
}

var replaceCode = replace.join(" +\n ")
var compiledSource = template(replaceTemplate, replaceCode)
return new Function(compiledSource)
var compiledSource = template(replaceTemplate, replaceCode);
return new Function(compiledSource);
}

return function template() {
Expand Down
47 changes: 47 additions & 0 deletions custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@


var opTag = "{";
var clTag = "}";
var nargs = /\{([0-9a-zA-Z]+)\}/g



function isSpecialRegExpChar( c ) {
return "\^$.|?*+()[]{}".indexOf( c ) != -1;
}


function escapeForRegExp( str ) {
var l = str.length;
var result = "";
for( var i=0; i<l; i++ ) {
result += ( isSpecialRegExpChar( str[i] ) ? "\\" : "" ) + str[i];
}
return result;
}


function setOpenCloseTags( op, cl ) {
if( op == cl ) {
console.log( "ERROR: In setOpenCloseTags, open and close tags must be different to each other." );
return;
}
opTag = op;
clTag = cl;

var op_RegExp, cl_RegExp;
op_RegExp = escapeForRegExp( op );
cl_RegExp = escapeForRegExp( cl );

nargs = new RegExp( op_RegExp+"([0-9a-zA-Z]+)"+cl_RegExp, "g" );
}


module.exports.config = function() {
return {
opTag: opTag,
clTag: clTag,
nargs: nargs
}
}
module.exports.setOpenCloseTags = setOpenCloseTags;
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var nargs = /\{([0-9a-zA-Z]+)\}/g
var slice = Array.prototype.slice

var custom = require('./custom');

module.exports = template

function template(string) {
Expand All @@ -16,11 +17,13 @@ function template(string) {
args = {}
}

return string.replace(nargs, function replaceArg(match, i, index) {
var config = custom.config();

return string.replace(config.nargs, function replaceArg(match, i, index) {
var result

if (string[index - 1] === "{" &&
string[index + match.length] === "}") {
if (string[index - 1] === config.opTag &&
string[index + match.length] === config.clTag) {
return i
} else {
result = args.hasOwnProperty(i) ? args[i] : null
Expand Down
43 changes: 43 additions & 0 deletions test/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var test = require("tape");

var format = require('../index');
var custom = require('../custom');



test("Setting identical open/close tags leads to no action", function (assert) {
var config = custom.config();
custom.setOpenCloseTags( "<==", "<==" );
var newConfig = custom.config();
assert.equal( config.nargs, newConfig.nargs );
assert.equal( config.opTag, newConfig.opTag );
assert.equal( config.clTag, newConfig.clTag );

assert.end();
});


test("Changing open/close tags works", function (assert) {
var objs = {
name: "Nicolas",
age: "31",
city: "Paris" };

custom.setOpenCloseTags( "{", "}" );
var result = format( "Hello {name}, I hear you come from {{city}}?", objs );
assert.equal(result, "Hello Nicolas, I hear you come from {city}?");

custom.setOpenCloseTags( "{{", "}}" );
result = format( "Hello {name}, I hear you come from {{city}}?", objs );
assert.equal(result, "Hello {name}, I hear you come from Paris?");

custom.setOpenCloseTags( "<%=", "%>" );
result = format( "Hello {name}, I hear you come from {{city}}?", objs );
assert.equal(result, "Hello {name}, I hear you come from {{city}}?");

// restoring standard open/close tags for following tests
custom.setOpenCloseTags( "{", "}" );

assert.end();
});

1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require("./string-template")
require("./compile-weak")
require("./compile-strong")
require("./custom")