string-template
Advanced tools
Comparing version 0.2.0 to 0.2.1
var template = require("./index") | ||
var escape = require("js-string-escape") | ||
var whitespaceRegex = /["'\\\n\r\u2028\u2029]/g | ||
var nargs = /\{[0-9a-zA-Z]+\}/g | ||
@@ -51,3 +51,7 @@ | ||
replace = replace.reduce(function (prev, curr) { | ||
var prev = [""] | ||
for (var j = 0; j < replace.length; j++) { | ||
var curr = replace[j] | ||
if (String(curr) === curr) { | ||
@@ -64,8 +68,18 @@ var top = prev[prev.length - 1] | ||
} | ||
} | ||
return prev | ||
}, [""]) | ||
replace = prev | ||
if (inline) { | ||
var replaceCode = replace.map(inlineConcat).join(" +\n ") | ||
for (var k = 0; k < replace.length; k++) { | ||
var token = replace[k] | ||
if (String(token) === token) { | ||
replace[k] = template(literalTemplate, escape(token)) | ||
} else { | ||
replace[k] = template(argTemplate, escape(token.name)) | ||
} | ||
} | ||
var replaceCode = replace.join(" +\n ") | ||
var compiledSource = template(replaceTemplate, replaceCode) | ||
@@ -106,8 +120,27 @@ return new Function(compiledSource) | ||
function inlineConcat(token) { | ||
if (String(token) === token) { | ||
return template(literalTemplate, escape(token)) | ||
} else { | ||
return template(argTemplate, escape(token.name)) | ||
function escape(string) { | ||
string = '' + string; | ||
return string.replace(whitespaceRegex, escapedWhitespace); | ||
} | ||
function escapedWhitespace(character) { | ||
// Escape all characters not included in SingleStringCharacters and | ||
// DoubleStringCharacters on | ||
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4 | ||
switch (character) { | ||
case '"': | ||
case "'": | ||
case '\\': | ||
return '\\' + character | ||
// Four possible LineTerminator characters need to be escaped: | ||
case '\n': | ||
return '\\n' | ||
case '\r': | ||
return '\\r' | ||
case '\u2028': | ||
return '\\u2028' | ||
case '\u2029': | ||
return '\\u2029' | ||
} | ||
} |
{ | ||
"name": "string-template", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "A simple string template function based on named or indexed arguments", | ||
@@ -25,5 +25,3 @@ "keywords": [ | ||
}, | ||
"dependencies": { | ||
"js-string-escape": "^1.0.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -30,0 +28,0 @@ "tape": "~1.1.1" |
@@ -47,2 +47,32 @@ # string-template | ||
## Compiling templates | ||
`string-template` exposes two template compiling options for when you need the | ||
additional performance. Arguments passed to the compiled template are of the | ||
same structure as the main `string-template` function, so either a single | ||
object/array or a list of arguments. | ||
```js | ||
var compile = require("string-template/compile") | ||
var greetingTemplate = compile("Hello {0}, you have {1} unread messages") | ||
var greeting = greetingTemplate("Robert", 12) | ||
// -> "Hello Robert, you have 12 unread messages" | ||
``` | ||
Passing a truthy second argument to `compile` will opt into using `new Function` | ||
to generate a function. The function returned contains a literal string | ||
concatenation statement, interleaving the correct arguments you have passed in. | ||
```js | ||
var compile = require("string-template/compile") | ||
var greetingTemplate = compile("Hello {0}, you have {1} unread messages", true) | ||
// -> greetingTemplate generated using new Function | ||
var greeting = greetingTemplate(["Robert", 12]) | ||
// -> "Hello Robert, you have 12 unread messages" | ||
``` | ||
## Installation | ||
@@ -49,0 +79,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10468
0
145
104
- Removedjs-string-escape@^1.0.0
- Removedjs-string-escape@1.0.1(transitive)