Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
daaku committed Aug 8, 2011
0 parents commit 8ed5bfd
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.tags
/lib-cov/
/node_modules/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test/
/.tags
/lib-cov/
/node_modules/
17 changes: 17 additions & 0 deletions lib/tmpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var INTERPOLATE = /{([\s\S]+?)}/g

module.exports = function(str, data) {
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
'with(obj||{}){__p.push(\'' +
str.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(INTERPOLATE, function(match, code) {
return "'," + code.replace(/\\'/g, "'") + ",'"
})
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
+ "');}return __p.join('');"
var func = new Function('obj', tmpl)
return data ? func(data) : func
}
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "tmpl",
"description": "JavaScript micro templates.",
"version": "1.0.0",
"homepage": "https://github.com/nshah/nodejs-tmpl",
"author": "Naitik Shah <[email protected]>",
"main": "lib/tmpl",
"repository": {
"type": "git",
"url": "https://github.com/nshah/nodejs-tmpl"
},
"scripts": { "test": "./node_modules/.bin/expresso -c" },
"devDependencies": {
"expresso": ">= 0.8.1"
},
"engines": { "node": ">= 0.4.1" }
}
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tmpl
====

Simple string formatting using `{}`.

```javascript
assert.equal(
tmpl('the answer is {answer}', { answer: 42 }),
'the answer is 42')
```
8 changes: 8 additions & 0 deletions test/tmpl-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var tmpl = require('tmpl')
, assert = require('assert')

exports['basic name substitution'] = function(beforeExit) {
assert.equal(
tmpl('the answer is {answer}', { answer: 42 }),
'the answer is 42')
}

0 comments on commit 8ed5bfd

Please sign in to comment.