-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
148 lines (143 loc) · 4.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*!
* es6-template <https://github.com/tunnckoCore/es6-template>
*
* Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var gana = require('gana')
var tryCatch = require('try-catch-callback')
/**
* > Render `template` with `locals` and optionally
* pass a `cb` callback. If no callback is passed,
* the rendered `string` is returned. It is alias
* of and acts like `.render` method.
*
* **Example**
*
* ```js
* var es6template = require('es6-template')
*
* var template = 'Hello ${ucfirst(author.name)} and have a ${mood} day!'
* var locals = {
* author: {
* name: 'charlike'
* },
* mood: 'nice',
* ucfirst: function ucfirst (val) {
* return val.charAt(0).toUpperCase() + val.slice(1)
* }
* }
*
* // synchronous
* var str = es6template(template, locals)
* console.log(str)
* // => 'Hello Charlike and have a nice day!'
*
* // async
* es6template(template, locals, function cb (err, res) {
* if (err) return console.error(err)
*
* console.log(res)
* // => 'Hello Charlike and have a nice day!'
* })
* ```
*
* @name es6template
* @param {String} `<template>` string to be rendered.
* @param {Object} `<locals>` data to be used in `template`.
* @param {Function} `[cb]` callback with `cb(err, res)` signature.
* @return {String} if no `cb` is passed.
* @api public
*/
var es6template = module.exports = function es6template (template, locals, cb) {
return es6template.render(template, locals, cb)
}
/**
* > Compile a `template` to a function that
* can accept `locals` object to render a string.
* If `cb` is passed, it pass a `compileFn` as result.
* It's a [gana][] mirror, so if there's a problem,
* so please report it in the [gana][]'s issue tracker.
*
* **Example**
*
* ```js
* var es6template = require('es6-template')
*
* var template = 'You, ${uppercase(name)}, are awesome ${person}!'
* var locals = {
* name: 'charlike',
* person: 'developer',
* uppercase: function uppercase (val) {
* return val.toUpperCase()
* }
* }
*
* // sync
* var compileFn = es6template.compile(template)
* var result = compileFn(locals)
* console.log(result)
* // => 'You, CHARLIKE, are awesome developer!'
*
* // asynchronous, gives you `compileFn` in the callback
* es6template.compile(template, function cb (err, compileFn) {
* if (err) return console.error(err)
*
* var result = compileFn(locals)
* console.log(result)
* // => 'You, CHARLIKE, are awesome developer!'
* })
* ```
*
* @name .compile
* @param {String} `<template>` string to be compile to a function.
* @param {Function} `[cb]` callback with `cb(err, compileFn)` signature.
* @return {Function} if no `cb` is passed.
* @api public
*/
es6template.compile = function compile (template, cb) {
return gana(template, cb)
}
/**
* > Renders a `template` with `locals`.
* If no `cb` is passed, returns a rendered string,
* otherwise pass the result to `cb` callback function.
* Acts like a `es6template()` which is mirror of this one.
* If there are some problems, please report them to
* the [gana][] or [gana-compile][] issue trackers, because
* this basically is `gana(template)(locals)`.
*
* **Example**
*
* ```js
* var es6template = require('es6-template')
*
* var str = es6template.render('Hello ${name}.', { name: 'Charlike' })
* console.log(str)
* // => 'Hello Charlike.'
* ```
*
* @name .render
* @param {String} `<template>` string to be rendered.
* @param {Object} `<locals>` data to be used in `template`.
* @param {Function} `[cb]` callback with `cb(err, res)` signature.
* @return {String} if no `cb` is passed.
* @api public
*/
es6template.render = function render (template, locals, cb) {
if (typeof locals === 'function') {
cb = locals
locals = false
}
if (typeof cb === 'function') {
es6template.compile(template, function (err, fn) {
if (err) return cb(err)
tryCatch(function () {
return fn(locals)
}, cb)
})
return
}
return gana(template)(locals)
}