Skip to content

Commit

Permalink
feat(async): rewrite to use gana-compile and try-catch-core; now …
Browse files Browse the repository at this point in the history
…have async compile
  • Loading branch information
tunnckoCore committed Sep 22, 2016
1 parent 54d1873 commit dbdd828
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 62 deletions.
63 changes: 9 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,17 @@
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'

var isObject = require('isobject')

/**
* > Compiles a `template` to a function, which
* accepts `locals` object to populate the template.
*
* **Example**
*
* ```js
* var gana = require('gana')
*
* var template = 'Welcome here, ${ucfirst(name)}! And have fun!'
* var locals = {
* name: 'charlike',
* ucfirst: function ucfirst (val) {
* return val.charAt(0).toUpperCase() + val.slice(1)
* }
* }
*
* var fn = gana(template)
* var str = fn(locals)
*
* console.log(str)
* // => 'Welcome here, Charlike! And have fun!'
* ```
*
* @param {String} `template` string to compile to a function
* @return {Function} like `compileFn(locals)`, where `locals` must be `object`
* @throws {TypeError} if `template` not a string
* @throws {TypeError} if `locals` not an object
* @throws {ReferenceError} if key not exists in `locals` object
* @api public
*/

module.exports = function gana (template) {
if (typeof template !== 'string') {
throw new TypeError('gana: expect `template` to be a string')
}

return function compileFn (locals) {
if (!isObject(locals)) {
throw new TypeError('gana: expect `locals` to be an object')
}

var keys = []
var vals = []
'use strict'

for (var key in locals) {
keys.push(key)
vals.push(locals[key])
}
var utils = require('./utils')

/* eslint-disable no-new-func */
var fn = new Function(keys, 'return `' + template + '`')
return fn.apply(locals, vals)
module.exports = function gana (template, cb) {
if (typeof cb === 'function') {
utils.tryCatchCore(function () {
return utils.ganaCompile(template)
}, cb)
return
}
return utils.ganaCompile(template)
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"postbundle": "uglifyjs dist/gana.js -m -c -o dist/gana.min.js --source-map dist/gana.min.js.map",
"standalone": "browserify -ds gana index.js -o dist/gana.standalone.js",
"poststandalone": "uglifyjs dist/gana.standalone.js -m -c -o dist/gana.standalone.min.js --source-map dist/gana.standalone.min.js.map",
"prelint": "npm install",
"lint": "standard --verbose",
"pretest": "npm run lint",
"test": "npm run coverage",
Expand All @@ -28,7 +27,8 @@
"commit": "git-cz"
},
"dependencies": {
"isobject": "^2.1.0"
"gana-compile": "^1.0.1",
"try-catch-core": "^1.0.1"
},
"devDependencies": {
"browserify": "^13.1.0",
Expand All @@ -39,13 +39,14 @@
"mukla": "^0.4.1",
"nyc": "^8.1.0",
"pre-commit": "^1.1.3",
"standard": "^8.0.0",
"standard": "^8.1.0",
"standard-version": "^2.4.0",
"uglify-js": "^2.7.3"
},
"files": [
"dist/",
"index.js",
"dist/"
"utils.js"
],
"keywords": [
"gana"
Expand Down Expand Up @@ -91,4 +92,4 @@
"reflinks": true
}
}
}
}
14 changes: 11 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test('should throw TypeError if `template` not a string', function (done) {
gana(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /gana: expect `template` to be a string/)
test.throws(fixture, /expect `template` to be a string/)
done()
})

Expand All @@ -25,7 +25,7 @@ test('should throw TypeError if `locals` not an object', function (done) {
gana('foo ${bar} baz')(5555)
}
test.throws(fixture, TypeError)
test.throws(fixture, /gana: expect `locals` to be an object/)
test.throws(fixture, /expect `locals` to be an object/)
done()
})

Expand All @@ -37,7 +37,7 @@ test('should throw TypeError if `locals` is an array', function (done) {
])
}
test.throws(fixture, TypeError)
test.throws(fixture, /gana: expect `locals` to be an object/)
test.throws(fixture, /expect `locals` to be an object/)
done()
})

Expand Down Expand Up @@ -130,3 +130,11 @@ test('should have support for partials', function (done) {
test.strictEqual(str, '<p>layout including home</p><p><h1>Home Page!</h1></p>')
done()
})

test('should be able to accept callback function', function (done) {
gana('Hello, ${name}!', function (err, fn) {
test.ifError(err)
test.strictEqual(fn({ name: 'Hero' }), 'Hello, Hero!')
done()
})
})
19 changes: 19 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

var utils = require('lazy-cache')(require)
var fn = require
require = utils // eslint-disable-line no-undef, no-native-reassign, no-global-assign

/**
* Lazily required module dependencies
*/

require('gana-compile')
require('try-catch-core')
require = fn // eslint-disable-line no-undef, no-native-reassign, no-global-assign

/**
* Expose `utils` modules
*/

module.exports = utils

0 comments on commit dbdd828

Please sign in to comment.