format-text
Advanced tools
Comparing version 0.0.2 to 0.0.3
29
index.js
module.exports = format; | ||
function findContext(args){ | ||
if(typeof args[1] == 'object' && args[1]) | ||
return args[1]; | ||
function format(text) { | ||
var context; | ||
return Array.prototype.slice.call(args, 1); | ||
} | ||
if (typeof arguments[1] == 'object' && arguments[1]) { | ||
context = arguments[1]; | ||
} else { | ||
context = Array.prototype.slice.call(arguments, 1); | ||
} | ||
function format(text) { | ||
var context = findContext(arguments); | ||
return String(text).replace(/\{?\{([^{}]+)}}?/g, replace(context)); | ||
}; | ||
function replace(context, nil){ | ||
return function(tag, name) { | ||
if(tag.substring(0, 2) == '{{' && tag.substring(tag.length - 2) == '}}'){ | ||
function replace (context, nil){ | ||
return function (tag, name) { | ||
if (tag.substring(0, 2) == '{{' && tag.substring(tag.length - 2) == '}}') { | ||
return '{' + name + '}'; | ||
} | ||
if( !context.hasOwnProperty(name) ){ | ||
if (!context.hasOwnProperty(name)) { | ||
return tag; | ||
} | ||
if( typeof context[name] == 'function' ){ | ||
if (typeof context[name] == 'function') { | ||
return context[name](); | ||
@@ -33,5 +30,3 @@ } | ||
return context[name]; | ||
} | ||
} |
{ | ||
"name": "format-text", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "String formatting library inspired from Python", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
1667