micromustache
Advanced tools
Comparing version 3.0.3 to 3.0.4
{ | ||
"name": "micromustache", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"homepage": "https://github.com/userpixel/micromustache", | ||
@@ -5,0 +5,0 @@ "description": "A faster and smaller subimplementation of the {{mustache}} template engine for JavaScript", |
@@ -1,81 +0,117 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.micromustache = f()}})(function(){var define,module,exports;module={exports:(exports={})}; | ||
/** | ||
* Replaces every {{variable}} inside the template with values provided by view | ||
* If the value is a function, call it passing the name of the variable as the only argument. | ||
* | ||
* @param template {string} the template containing one or more {{variableNames}} | ||
* @param [view={}] {object} an optional object containing values for every variable names | ||
* that is used in the template | ||
* @return {string} template where its variable names replaced with corresponding values. | ||
* If a value is not found or is invalid, it will be assumed empty string ''. | ||
* If the value is an object itself, it'll be stringified by JSON. In case of a JSON error the result will look like "{JSON_ERROR: ... }". | ||
*/ | ||
function render(template, view) { | ||
//don't touch the template if it is not a string | ||
if (typeof template !== 'string') { | ||
return template; | ||
(function(f) { | ||
if (typeof exports === "object" && typeof module !== "undefined") { | ||
module.exports = f() | ||
} else if (typeof define === "function" && define.amd) { | ||
define([], f) | ||
} else { | ||
var g; | ||
if (typeof window !== "undefined") { | ||
g = window | ||
} else if (typeof global !== "undefined") { | ||
g = global | ||
} else if (typeof self !== "undefined") { | ||
g = self | ||
} else { | ||
g = this | ||
} | ||
g.micromustache = f() | ||
} | ||
//if view is not a valid object, assume it is an empty object | ||
//which effectively removes all variable interpolations | ||
if (typeof view !== 'object' || view === null) { | ||
view = {}; | ||
} | ||
return template.replace(/\{\{\s*(.*?)\s*\}\}/g, function(match, varName) { | ||
var path = varName.split('.'); | ||
})(function() { | ||
var define, module, exports; | ||
module = { | ||
exports: (exports = {}) | ||
}; | ||
/** | ||
* Replaces every {{variable}} inside the template with values provided by view | ||
* If the value is a function, call it passing the name of the variable as the only argument. | ||
* | ||
* @param template {string} the template containing one or more {{variableNames}} | ||
* @param [view={}] {object} an optional object containing values for every variable names | ||
* that is used in the template | ||
* @return {string} template where its variable names replaced with corresponding values. | ||
* If a value is not found or is invalid, it will be assumed empty string ''. | ||
* If the value is an object itself, it'll be stringified by JSON. In case of a JSON error the result will look like "{JSON_ERROR: ... }". | ||
*/ | ||
function render(template, view) { | ||
//don't touch the template if it is not a string | ||
if (typeof template !== 'string') { | ||
return template; | ||
} | ||
//if view is not a valid object, assume it is an empty object | ||
//which effectively removes all variable interpolations | ||
if (typeof view !== 'object' || view === null) { | ||
view = {}; | ||
} | ||
return template.replace(/\{\{\s*(.*?)\s*\}\}/g, function(match, varName) { | ||
var path = varName.split('.'); | ||
function resolve(currentScope, pathIndex) { | ||
if (currentScope === null) { | ||
return ''; | ||
} | ||
var key = path[pathIndex]; | ||
var value = currentScope[key]; | ||
switch (typeof value) { | ||
case 'string': | ||
case 'number': | ||
case 'boolean': | ||
return value; | ||
case 'function': | ||
function resolve(currentScope, pathIndex) { | ||
if (currentScope === null) { | ||
return ''; | ||
} | ||
var key = path[pathIndex]; | ||
var value = currentScope[key]; | ||
var typeofValue = typeof value; | ||
if (typeofValue === 'function') { | ||
//if the value is a function, call it passing the variable name | ||
return value.call(view, key, currentScope, path, pathIndex); | ||
case 'object': | ||
if (value === null) { | ||
return ''; | ||
} | ||
return valueFnResultToString(value.call(view, key, currentScope, | ||
path, pathIndex)); | ||
} else if (typeofValue === 'object') { | ||
pathIndex++; | ||
if ( pathIndex < path.length ) { | ||
return resolve(value, pathIndex); | ||
} else { | ||
try { | ||
return JSON.stringify(value); | ||
} catch (jsonError) { | ||
return '{...}'; | ||
} | ||
} | ||
default: | ||
//anything else will be replaced with an empty string (date, regexp, etc). | ||
return ''; | ||
// If it's a leaf and still an object, just stringify it | ||
return pathIndex < path.length ? resolve(value, pathIndex) : | ||
valueFnResultToString(value); | ||
} else { | ||
return valueFnResultToString(value); | ||
} | ||
} | ||
return resolve(view, 0); | ||
}); | ||
} | ||
/** | ||
* This function makes repeated calls shorter by returning a compiler function | ||
* for a particular template that accepts data and returnes the rendered string. | ||
* It doesn't make the code faster since the compiler still uses render internally. | ||
* | ||
* @param template {string} same as the template parameter to render() | ||
* @return compiler(view) {function} a function that accepts a view and returns a rendered template | ||
*/ | ||
function compile(template) { | ||
//create and return a function that will always apply this template under the hood | ||
return function compiler(view) { | ||
return render(template, view); | ||
}; | ||
} | ||
function valueFnResultToString(value) { | ||
switch (typeof value) { | ||
case 'string': | ||
case 'number': | ||
case 'boolean': | ||
return value; | ||
case 'object': | ||
// null is an object but is falsy. Swallow null | ||
return value ? toJsonPolitely(value) : ''; | ||
default: | ||
// Anything else will be replaced with an empty string | ||
// For example: undefined, date, regexp, etc). | ||
return ''; | ||
} | ||
return resolve(view, 0); | ||
}); | ||
} | ||
} | ||
/** | ||
* This function makes repeated calls shorter by returning a compiler function | ||
* for a particular template that accepts data and returnes the rendered string. | ||
* It doesn't make the code faster since the compiler still uses render internally. | ||
* | ||
* @param template {string} same as the template parameter to render() | ||
* @return compiler(view) {function} a function that accepts a view and returns a rendered template | ||
*/ | ||
function compile (template) { | ||
//create and return a function that will always apply this template under the hood | ||
return function compiler (view) { | ||
return render (template, view); | ||
}; | ||
} | ||
// Converts an object to json without throwing | ||
function toJsonPolitely(obj) { | ||
try { | ||
return JSON.stringify(obj); | ||
} catch (jsonError) { | ||
return '{...}'; | ||
} | ||
} | ||
exports.to_html = exports.render = render; | ||
exports.compile = compile; | ||
exports.to_html = exports.render = render; | ||
exports.compile = compile; | ||
return module.exports;}); | ||
return module.exports; | ||
}); |
@@ -1,1 +0,1 @@ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.micromustache=f()}})(function(){var define,module,exports;module={exports:exports={}};function render(template,view){if(typeof template!=="string"){return template}if(typeof view!=="object"||view===null){view={}}return template.replace(/\{\{\s*(.*?)\s*\}\}/g,function(match,varName){var path=varName.split(".");function resolve(currentScope,pathIndex){if(currentScope===null){return""}var key=path[pathIndex];var value=currentScope[key];switch(typeof value){case"string":case"number":case"boolean":return value;case"function":return value.call(view,key,currentScope,path,pathIndex);case"object":if(value===null){return""}pathIndex++;if(pathIndex<path.length){return resolve(value,pathIndex)}else{try{return JSON.stringify(value)}catch(jsonError){return"{...}"}}default:return""}}return resolve(view,0)})}function compile(template){return function compiler(view){return render(template,view)}}exports.to_html=exports.render=render;exports.compile=compile;return module.exports}); | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.micromustache=e()}}(function(){function e(e,n){return"string"!=typeof e?e:("object"==typeof n&&null!==n||(n={}),e.replace(/\{\{\s*(.*?)\s*\}\}/g,function(e,r){function o(e,r){if(null===e)return"";var u=f[r],i=e[u],c=typeof i;return"function"===c?t(i.call(n,u,e,f,r)):"object"===c?(r++,r<f.length?o(i,r):t(i)):t(i)}var f=r.split(".");return o(n,0)}))}function n(n){return function(t){return e(n,t)}}function t(e){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":return e?r(e):"";default:return""}}function r(e){try{return JSON.stringify(e)}catch(e){return"{...}"}}var o,f;return o={exports:f={}},f.to_html=f.render=e,f.compile=n,o.exports}); |
{ | ||
"name": "micromustache", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "A faster and smaller subimplementation of the {{mustache}} template engine for JavaScript", | ||
@@ -22,2 +22,3 @@ "keywords": [ | ||
"mustache": "^2.2.1", | ||
"uglify-js": "^2.7.0", | ||
"umd": "^3.0.1" | ||
@@ -27,5 +28,5 @@ }, | ||
"build:umd": "umd micromustache src/micromustache.js browser/micromustache.js --commonJS", | ||
"build:min": "uglifyjs browser/micromustache.js -o browser/micromustache.min.js", | ||
"build:min": "uglifyjs browser/micromustache.js -o browser/micromustache.min.js --compress --mangle", | ||
"build": "npm run build:umd && npm run build:min", | ||
"test": "mocha" | ||
"test": "mocha test/**/*.spec.js" | ||
}, | ||
@@ -32,0 +33,0 @@ "repository": { |
@@ -21,20 +21,4 @@ [![GitHub issues](https://img.shields.io/github/issues/userpixel/micromustache.svg?style=flat-square)](https://github.com/userpixel/micromustache/issues) | ||
# Installation | ||
**Tradeoff** | ||
Download from [browser directory](https://github.com/userpixel/micromustache/tree/master/browser) | ||
[npm](https://npmjs.org/package/micromustache): | ||
```bash | ||
npm install micromustache | ||
``` | ||
[Bower](http://bower.io/): | ||
````bash | ||
bower install micromustache | ||
```` | ||
# Limitations | ||
Micromustache achieves faster speed and smaller size by dropping: | ||
@@ -49,2 +33,3 @@ | ||
* Custom delimiters: *No support for <% ... %>. We just have {{ ... }}* | ||
* It does no support IE 6-8 | ||
@@ -57,3 +42,3 @@ If you can live with this, read on... | ||
Function signature: | ||
Signature: | ||
@@ -94,4 +79,3 @@ ```js | ||
You can easily reference deep object hierarchies. | ||
For example given the [package.json](https://github.com/userpixel/micromustache/blob/master/package.json) file of this project: | ||
You can easily reference deep object hierarchies: | ||
@@ -132,12 +116,28 @@ ```js | ||
````js | ||
micromustache.render('{{var1}}', { | ||
var1: function (key, currentScope, path, currentPointer) { | ||
// "this" inside the function refers to the current object | ||
return key.toUpperCase(); | ||
} | ||
/** | ||
* @param key {String} variable name for the current scope. | ||
* For hierarchical names like {{a.b.c}} the key can be 'a' or 'b' or 'c' | ||
* @param currentScope {Object} the current object that the variable is | ||
* supposed to resolved from | ||
* @param path {String[]} useful for hierarchical objects. | ||
* for example a variable name like {{a.b.c}} sets the | ||
* path to ['a', 'b', 'c'] | ||
* @param currentPointer {Number} the array index to where in the path we are at the | ||
* moment. This is usually path.length - 1 | ||
* @return {String|Number|Boolean|Object} the value to be interpolated | ||
*/ | ||
function toUpper (key, currentScope, path, currentPointer) { | ||
// key is the variable name | ||
// By the way: "this" inside the function refers to the current object | ||
return key.toUpperCase(); | ||
} | ||
micromustache.render('I bought a {{screaming}} {{dog}}!!!', { | ||
screaming: toUpper, | ||
dog: toUpper | ||
}); | ||
//output = 'VAR1' | ||
//output = 'I bought a SCREAMING DOG!!!' | ||
```` | ||
The function runs synchronously in the context of the view object (i.e. `this` refers to the view object). A more complex example: | ||
The function runs **synchronously** in the context of the view object (i.e. `this` refers to the view object). A more complex example: | ||
@@ -192,2 +192,18 @@ ````js | ||
# Installation | ||
Download from [browser directory](https://github.com/userpixel/micromustache/tree/master/browser) | ||
[npm](https://npmjs.org/package/micromustache): | ||
```bash | ||
npm install micromustache | ||
``` | ||
[Bower](http://bower.io/): | ||
````bash | ||
bower install micromustache | ||
```` | ||
# Tests | ||
@@ -201,2 +217,8 @@ | ||
The browser module loading tests ( | ||
[AMD](https://github.com/userpixel/micromustache/blob/master/test/amd.html) | ||
and | ||
[global](https://github.com/userpixel/micromustache/blob/master/test/global.html) | ||
) need to be loaded in the browser. | ||
# TODO | ||
@@ -203,0 +225,0 @@ |
@@ -31,29 +31,15 @@ /** | ||
var value = currentScope[key]; | ||
switch (typeof value) { | ||
case 'string': | ||
case 'number': | ||
case 'boolean': | ||
return value; | ||
case 'function': | ||
//if the value is a function, call it passing the variable name | ||
return value.call(view, key, currentScope, path, pathIndex); | ||
case 'object': | ||
if (value === null) { | ||
return ''; | ||
} | ||
pathIndex++; | ||
if ( pathIndex < path.length ) { | ||
return resolve(value, pathIndex); | ||
} else { | ||
try { | ||
return JSON.stringify(value); | ||
} catch (jsonError) { | ||
return '{...}'; | ||
} | ||
} | ||
default: | ||
//anything else will be replaced with an empty string (date, regexp, etc). | ||
return ''; | ||
var typeofValue = typeof value; | ||
if (typeofValue === 'function') { | ||
//if the value is a function, call it passing the variable name | ||
return valueFnResultToString(value.call(view, key, currentScope, path, pathIndex)); | ||
} else if (typeofValue === 'object') { | ||
pathIndex++; | ||
// If it's a leaf and still an object, just stringify it | ||
return pathIndex < path.length ? resolve(value, pathIndex) : valueFnResultToString(value); | ||
} else { | ||
return valueFnResultToString(value); | ||
} | ||
} | ||
return resolve(view, 0); | ||
@@ -78,3 +64,28 @@ }); | ||
function valueFnResultToString (value) { | ||
switch (typeof value) { | ||
case 'string': | ||
case 'number': | ||
case 'boolean': | ||
return value; | ||
case 'object': | ||
// null is an object but is falsy. Swallow null | ||
return value ? toJsonPolitely(value) : ''; | ||
default: | ||
// Anything else will be replaced with an empty string | ||
// For example: undefined, date, regexp, etc). | ||
return ''; | ||
} | ||
} | ||
// Converts an object to json without throwing | ||
function toJsonPolitely (obj) { | ||
try { | ||
return JSON.stringify(obj); | ||
} catch (jsonError) { | ||
return '{...}'; | ||
} | ||
} | ||
exports.to_html = exports.render = render; | ||
exports.compile = compile; |
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
73707
214
221
5