diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..87bee69 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,26 @@ +# .editorconfig +# +# Copyright (c) 2015 Charlike Mike Reagent, contributors. +# Released under the MIT license. +# + +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space + +[*.js] +insert_final_newline = true +trim_trailing_whitespace = true + +[*.php] +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +insert_final_newline = false +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ecfaf8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,57 @@ +# .gitignore +# +# Copyright (c) 2015 Charlike Mike Reagent, contributors. +# Released under the MIT license. +# + +# Always-ignore dirs # +# #################### +_gh_pages +node_modules +bower_components +components +vendor +build +dest +dist +src +lib-cov +coverage +nbproject +cache +temp +tmp +es6-template + +# Packages # +# ########## +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# OS, Logs and databases # +# ######################### +*.pid +*.dat +*.log +*.sql +*.sqlite +*~ +~* + +# Another files # +# ############### +Icon? +.DS_Store* +Thumbs.db +ehthumbs.db +Desktop.ini +npm-debug.log +.directory +._* +lcov.info \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..969f268 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,29 @@ +language: "node_js" +sudo: false + +node_js: + - "0.10" + - "0.12" + - "4" + +notifications: + email: + on_success: never + on_failure: never + +before_script: + - npm install standard + - standard + +script: + - npm install istanbul-harmony + - node --harmony node_modules/.bin/istanbul cover test.js + +after_success: + - npm install coveralls + - cat coverage/lcov.info | coveralls + - mv coverage/lcov.info . + +matrix: + allow_failures: + - node_js: "0.10" \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..45751ea --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ + + +## 0.0.0 - 2015-09-20 +- Initial commit \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..5312165 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,32 @@ +# Contributing Guidelines + +Contributions are always welcome! + +**Before spending lots of time on something, ask for feedback on your idea first!** + +Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations. + + +## Installing + +Fork and clone the repo, then `npm install` to install all dependencies and `npm test` to ensure all is okey before you start anything. + + +## Testing + +Tests are run with `npm test`. Please ensure all tests are passing before submitting a pull request (unless you're creating a failing test to increase test coverage or show a problem). + +## Code Style + +[![standard][standard-image]][standard-url] + +This repository uses [`standard`][standard-url] to maintain code style and consistency, and to avoid style arguments. You are encouraged to install it globally. `npm test` runs `standard` so you don't have to! + +``` +npm i standard -g +``` + +It is intentional to don't have `standard`, `istanbul` and `coveralls` in the devDependencies. Travis will handle all that stuffs. That approach will save bandwidth also installing and development time. + +[standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg +[standard-url]: https://github.com/feross/standard \ No newline at end of file diff --git a/LICENSE b/LICENSE index 4599c7b..53eb8e4 100644 --- a/LICENSE +++ b/LICENSE @@ -18,5 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 8dea8c6..cdfc03d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,74 @@ -# es6-template -Easy and small template engine for the browser and nodejs. +# [es6-template][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url] + +> Easy and small template engine for the browser and nodejs. + +[![code climate][codeclimate-img]][codeclimate-url] [![standard code style][standard-img]][standard-url] [![travis build status][travis-img]][travis-url] [![coverage status][coveralls-img]][coveralls-url] [![dependency status][david-img]][david-url] + + +## Install +``` +npm i es6-template --save +``` + + +## Usage +> For more use-cases see the [tests](./test.js) + +```js +var es6Template = require('es6-template') +``` + + +## Contributing +Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/es6-template/issues/new). +But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines. + + +## [Charlike Make Reagent](http://j.mp/1stW47C) [![new message to charlike][new-message-img]][new-message-url] [![freenode #charlike][freenode-img]][freenode-url] + +[![tunnckocore.tk][author-www-img]][author-www-url] [![keybase tunnckocore][keybase-img]][keybase-url] [![tunnckoCore npm][author-npm-img]][author-npm-url] [![tunnckoCore twitter][author-twitter-img]][author-twitter-url] [![tunnckoCore github][author-github-img]][author-github-url] + + +[npmjs-url]: https://www.npmjs.com/package/es6-template +[npmjs-img]: https://img.shields.io/npm/v/es6-template.svg?label=es6-template + +[license-url]: https://github.com/tunnckoCore/es6-template/blob/master/LICENSE.md +[license-img]: https://img.shields.io/badge/license-MIT-blue.svg + + +[codeclimate-url]: https://codeclimate.com/github/tunnckoCore/es6-template +[codeclimate-img]: https://img.shields.io/codeclimate/github/tunnckoCore/es6-template.svg + +[travis-url]: https://travis-ci.org/tunnckoCore/es6-template +[travis-img]: https://img.shields.io/travis/tunnckoCore/es6-template.svg + +[coveralls-url]: https://coveralls.io/r/tunnckoCore/es6-template +[coveralls-img]: https://img.shields.io/coveralls/tunnckoCore/es6-template.svg + +[david-url]: https://david-dm.org/tunnckoCore/es6-template +[david-img]: https://img.shields.io/david/tunnckoCore/es6-template.svg + +[standard-url]: https://github.com/feross/standard +[standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg + + +[author-www-url]: http://www.tunnckocore.tk +[author-www-img]: https://img.shields.io/badge/www-tunnckocore.tk-fe7d37.svg + +[keybase-url]: https://keybase.io/tunnckocore +[keybase-img]: https://img.shields.io/badge/keybase-tunnckocore-8a7967.svg + +[author-npm-url]: https://www.npmjs.com/~tunnckocore +[author-npm-img]: https://img.shields.io/badge/npm-~tunnckocore-cb3837.svg + +[author-twitter-url]: https://twitter.com/tunnckoCore +[author-twitter-img]: https://img.shields.io/badge/twitter-@tunnckoCore-55acee.svg + +[author-github-url]: https://github.com/tunnckoCore +[author-github-img]: https://img.shields.io/badge/github-@tunnckoCore-4183c4.svg + +[freenode-url]: http://webchat.freenode.net/?channels=charlike +[freenode-img]: https://img.shields.io/badge/freenode-%23charlike-5654a4.svg + +[new-message-url]: https://github.com/tunnckoCore/ama +[new-message-img]: https://img.shields.io/badge/ask%20me-anything-green.svg \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..ca7d897 --- /dev/null +++ b/index.js @@ -0,0 +1,38 @@ +/*! + * es6-template + * + * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) + * Released under the MIT license. + */ + +'use strict' + +var sliced = require('sliced') +var extend = require('extend-shallow') +var get = require('get-value') +var re = require('es6-template-regex') + +function template (str) { + var data = {} + sliced(arguments, 1).forEach(function (obj) { + data = extend(data, obj) + }) + return str.replace(re(), function (m, prop) { + if (prop && prop.indexOf('.') !== -1) { + return get(data, prop) + } + return data[prop] || prop + }) +} + +template.render = template +template.compile = function compile (str) { + return function (data) { + sliced(arguments).forEach(function (obj) { + data = extend(data, obj) + }) + return template(str, data) + } +} + +module.exports = template diff --git a/package.json b/package.json new file mode 100644 index 0000000..78685c4 --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "es6-template", + "version": "0.0.0", + "description": "Easy and small template engine for the browser and nodejs.", + "repository": "tunnckoCore/es6-template", + "author": "Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)", + "main": "index.js", + "license": "MIT", + "scripts": { + "test": "standard && node test.js" + }, + "dependencies": { + "es6-template-regex": "^0.1.1", + "extend-shallow": "^2.0.1", + "get-value": "^1.2.1", + "sliced": "^1.0.1" + }, + "devDependencies": { + "assertit": "^0.1.0" + }, + "files": [ + "index.js" + ], + "keywords": [ + "easy", + "engine", + "es", + "regex", + "simple", + "template" + ] +} \ No newline at end of file diff --git a/test.js b/test.js new file mode 100644 index 0000000..66d7e5b --- /dev/null +++ b/test.js @@ -0,0 +1,56 @@ +/*! + * es6-template + * + * Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) + * Released under the MIT license. + */ + +/* jshint asi:true */ + +'use strict' + +var test = require('assertit') +var template = require('./index') + +test('should match es6 template delimiters in a string', function (done) { + var str = 'foo ${bar} baz ${quux}' + var data = {bar: 'AAA', quux: 'BBB'} + test.strictEqual(template(str, data), 'foo AAA baz BBB') + done() +}) + +test('should accept and merge multiple `data` arguments', function (done) { + var str = 'foo ${bar} baz ${quux}' + test.strictEqual(template(str, {bar: 'AAA'}, {quux: 'BBB'}), 'foo AAA baz BBB') + done() +}) + +test('should returned fn from .compile() accept multiple `data` arguments', function (done) { + var fn = template.compile('foo ${bar} baz ${qux.name}') + test.strictEqual(fn({bar: 'bar'}, {qux: {name: 'Charlike'}}), 'foo bar baz Charlike') + done() +}) + +test('should work with dot property paths like `a.b.c`', function (done) { + var str = 'Hello ${foo} and ${user.name}!' + var data = { + foo: 'bar', + user: { + name: 'Charlike' + } + } + test.strictEqual(template(str, data), 'Hello bar and Charlike!') + done() +}) + +test('should es6template.compile() return function', function (done) { + var fn = template.compile('foo ${bar} baz') + test.strictEqual(fn({bar: 'BAR'}), 'foo BAR baz') + done() +}) + +test('should es6template.render() be same as es6template()', function (done) { + var actual = template.render('foo ${bar} baz', {bar: 'QUUX'}) + test.strictEqual(actual, 'foo QUUX baz') + done() +})