Skip to content

Commit

Permalink
add docs comment
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Sep 20, 2016
1 parent 99f9f82 commit f9a03a2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .verb.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

**Uses the "bad" `new Function` thing**

I don't think that's a problem, because other template engines out there also uses some kind of `eval` and used massively. Most of them uses `eval`, most of them uses `with`, others of them uses `RegExp`s and etc. They all are with custom non-standard delimiters. They do too much to accomplish same results as `gana`. They requires too big codebase - and finally what, they still uses some of the "bad" things in JS.
I don't think that's a problem, because other template engines out there also uses some kind of `eval` and it is used massively, believe. Most of them uses `eval`, most of them uses `with`, others of them uses `RegExp`s and etc. They all are with custom non-standard delimiters. They do too much to accomplish same results as `gana`. They requires too big codebase - and finally what, they still uses some of the "bad" things in JS.

**Biggest names uses "bad" things too**

Names such [verb][], [update][], [templates][], [generate][], [assemble][] in our community uses [engine][] - respectively [engine-base][] and/or [engine-cache][]. Not to mention some of the most famous "real" template engines with features like partials, helpers and etc. You can have partials and helpers here in `gana` too.

**Tricking magic**

Behind the scenes `gana` uses [ES2015 (ES6) template strings](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/template_strings) inside the bad `new Function` which seems to **work even in `node@0.10`** which don't have support for Template Strings! That's strange, but it works and give us that awesome and small codebase (1.39kb, minified and not gzipped) - without any costs.
Behind the scenes `gana` uses [ES2015 (ES6) template strings](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/template_strings) inside the bad `new Function` which seems to **work even in `node@0.10`** which don't have support for Template Strings! That's strange, but it works and give us that awesome and small codebase (**1.39kb, minified and not gzipped**) - without any costs.
You just pass normal string `'foo ${bar} and baz'` and then `{ bar: 'bar' }` in the returned function.

**Note about [standard][] (>= v8) users**
Expand Down
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

**Uses the "bad" `new Function` thing**

I don't think that's a problem, because other template engines out there also uses some kind of `eval` and used massively. Most of them uses `eval`, most of them uses `with`, others of them uses `RegExp`s and etc. They all are with custom non-standard delimiters. They do too much to accomplish same results as `gana`. They requires too big codebase - and finally what, they still uses some of the "bad" things in JS.
I don't think that's a problem, because other template engines out there also uses some kind of `eval` and it is used massively, believe. Most of them uses `eval`, most of them uses `with`, others of them uses `RegExp`s and etc. They all are with custom non-standard delimiters. They do too much to accomplish same results as `gana`. They requires too big codebase - and finally what, they still uses some of the "bad" things in JS.

**Biggest names uses "bad" things too**

Names such [verb][], [update][], [templates][], [generate][], [assemble][] in our community uses [engine][] - respectively [engine-base][] and/or [engine-cache][]. Not to mention some of the most famous "real" template engines with features like partials, helpers and etc. You can have partials and helpers here in `gana` too.

**Tricking magic**

Behind the scenes `gana` uses [ES2015 (ES6) template strings](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/template_strings) inside the bad `new Function` which seems to **work even in `node@0.10`** which don't have support for Template Strings! That's strange, but it works and give us that awesome and small codebase (1.39kb, minified and not gzipped) - without any costs.
Behind the scenes `gana` uses [ES2015 (ES6) template strings](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/template_strings) inside the bad `new Function` which seems to **work even in `node@0.10`** which don't have support for Template Strings! That's strange, but it works and give us that awesome and small codebase (**1.39kb, minified and not gzipped**) - without any costs.
You just pass normal string `'foo ${bar} and baz'` and then `{ bar: 'bar' }` in the returned function.

**Note about [standard][] (>= v8) users**
Expand All @@ -35,6 +35,34 @@ npm i gana --save
const gana = require('gana')
```

### [gana](index.js#L40)
> Compiles a `template` to a function, which accepts `locals` object to populate the template.
**Params**

* `template` **{String}**: string to compile to a function
* `returns` **{Function}**: like `compileFn(locals)`, where `locals` must be `object`

**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!'
```

## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/gana/issues/new).
But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines.
Expand Down
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@

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`
* @api public
*/

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

0 comments on commit f9a03a2

Please sign in to comment.