From a45a30965b543946fc8cfa052672b0c0f610c4ea Mon Sep 17 00:00:00 2001 From: Nate Cavanaugh Date: Thu, 20 Aug 2015 08:06:58 -0700 Subject: [PATCH] Initial commit --- .gitattributes | 1 + .gitignore | 4 ++ .travis.yml | 8 ++++ LICENSE | 21 ++++++++++ README.md | 56 +++++++++++++++++++++++++++ gulpfile.js | 29 ++++++++++++++ index.js | 81 +++++++++++++++++++++++++++++++++++++++ package.json | 42 ++++++++++++++++++++ test/test.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 343 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 gulpfile.js create mode 100644 index.js create mode 100644 package.json create mode 100644 test/test.js diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbc8813 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules + +# Coverage directory used by tools like istanbul +coverage \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1a30bdf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +sudo: false +language: node_js +node_js: + - 'iojs' + - '0.12' + - '0.10' +after_success: + - npm run coveralls \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0485fa2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Nate Cavanaugh (alterform.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..43a7daf --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# content-logger-handlebars-helpers +[![NPM version][npm-image]][npm-url] +[![build status][travis-image]][travis-url] +[![Test coverage][coveralls-image]][coveralls-url] + +> My mathematical module + + +## Install + +``` +$ npm install --save content-logger-handlebars-helpers +``` + + +## Usage + +```js +var contentLoggerHandlebarsHelpers = require('content-logger-handlebars-helpers'); + +contentLoggerHandlebarsHelpers('unicorns'); +//=> unicorns & rainbows +``` + + +## API + +### contentLoggerHandlebarsHelpers(input, [options]) + +#### input + +*Required* +Type: `string` + +Lorem ipsum. + +#### options + +##### foo + +Type: `boolean` +Default: `false` + +Lorem ipsum. + + +## License + +MIT © [Nate Cavanaugh](http://alterform.com) + +[npm-image]: https://img.shields.io/npm/v/content-logger-handlebars-helpers.svg?style=flat-square +[npm-url]: https://npmjs.org/package/content-logger-handlebars-helpers +[travis-image]: https://img.shields.io/travis/natecavanaugh/content-logger-handlebars-helpers/master.svg?style=flat-square +[travis-url]: https://travis-ci.org/natecavanaugh/content-logger-handlebars-helpers +[coveralls-image]: https://img.shields.io/coveralls/natecavanaugh/content-logger-handlebars-helpers/master.svg?style=flat-square +[coveralls-url]: https://coveralls.io/r/natecavanaugh/content-logger-handlebars-helpers?branch=master \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..3df0934 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,29 @@ +var gulp = require('gulp-help')(require('gulp')); +var plugins = require('gulp-load-plugins')(); +var runSequence = require('run-sequence'); + +gulp.task('coveralls', function () { + gulp.src('coverage/**/lcov.info') + .pipe(plugins.coveralls()); +}); + +gulp.task('test', function(done) { + return runSequence('test-unit', done); +}); + +gulp.task('test-unit', function() { + return gulp.src(['test/**/*.js', '!test/fixture/*.js']) + .pipe(plugins.mocha()); +}); + +gulp.task('test-cover', function() { + return gulp.src(['./*.js']) + .pipe(plugins.istanbul()) + .pipe(plugins.istanbul.hookRequire()); +}); + +gulp.task('test-coverage', ['test-cover'], function() { + return gulp.src(['test/**/*.js', '!test/fixture/*.js']) + .pipe(plugins.mocha()) + .pipe(plugins.istanbul.writeReports()); +}); \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..03ca8f7 --- /dev/null +++ b/index.js @@ -0,0 +1,81 @@ +var _ = require('lodash'); + +var colors = require('cli-color-keywords')(); + +module.exports = function(hb) { + hb = hb || require('handlebars'); + + Object.keys(colors.styles).forEach( + function(item, index) { + hb.registerHelper( + item, + function(options) { + return colors[item](options.fn(this)); + } + ); + } + ); + + hb.registerHelper( + 'color', + function(options) { + var colorStyle = this.type; + + if (!_.isFunction(colors[colorStyle])) { + colorStyle = 'warn'; + } + + return colors[colorStyle](options.fn(this)); + } + ); + + hb.registerHelper( + 'line', + function(options) { + var line = this.line; + var text = 'Line'; + + if (Array.isArray(line)) { + line = line.join('-'); + text = 'Lines'; + } + + if (this.column && options.data.root.showColumns) { + line += ', Column ' + this.column; + } + + return text + ' ' + line; + } + ); + + hb.registerHelper( + 'and', + function(a, b, options) { + var retVal; + + if (a && b) { + retVal = options.fn(this); + } + else { + retVal = options.inverse(this); + } + + return retVal; + } + ); + + hb.registerHelper( + 'banner', + function(options) { + var content = options.fn(this); + + if (!this.showBanner) { + content = ''; + } + + return content; + } + ); + + return hb; +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..cd79940 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "content-logger-handlebars-helpers", + "version": "0.0.1", + "description": "My mathematical module", + "license": "MIT", + "repository": "natecavanaugh/content-logger-handlebars-helpers", + "author": { + "name": "Nate Cavanaugh", + "email": "nate@shift22.com", + "url": "alterform.com" + }, + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "gulp test-coverage", + "coveralls": "gulp coveralls" + }, + "keywords": [ + "" + ], + "dependencies": { + "cli-color-keywords": "0.0.1", + "handlebars": "^3.0.3", + "lodash": "^3.10.1" + }, + "devDependencies": { + "mocha": "*", + "chai": "*", + "chai-string": "*", + "coveralls": "*", + "gulp-coveralls": "*", + "gulp-istanbul": "*", + "istanbul": "*", + "sinon": "*", + "gulp": "*", + "gulp-load-plugins": "*", + "gulp-help": "*", + "run-sequence": "*", + "gulp-mocha": "*" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..f2df59a --- /dev/null +++ b/test/test.js @@ -0,0 +1,101 @@ +'use strict'; +var Handlebars = require('../')(); + +var colors = require('cli-color-keywords')(); + +var sinon = require('sinon'); +var chai = require('chai'); + +chai.use(require('chai-string')); + +var assert = chai.assert; + +var colorStyles = Object.keys(colors.styles); + +it( + 'should have explicitly defined helpers available', + function() { + 'banner and line color'.split(' ').forEach( + function(item, index) { + assert.isTrue(item in Handlebars.helpers); + } + ); + } +); + +it( + 'should have dynamically defined helpers available', + function() { + colorStyles.forEach( + function(item, index) { + assert.isTrue(item in Handlebars.helpers); + } + ); + } +); + +it( + 'handle banner', + function() { + var banner = '{{#banner}}Hello{{/banner}}'; + + var tpl = Handlebars.compile(banner); + + assert.equal(tpl({showBanner: true}), 'Hello'); + assert.equal(tpl({showBanner: false}), ''); + } +); + +it( + 'handle and', + function() { + var and = '{{#and foo bar}}Hello{{/and}}'; + + var tpl = Handlebars.compile(and); + + assert.equal(tpl({bar: true, foo: true}), 'Hello'); + assert.equal(tpl({bar: false, foo: true}), ''); + assert.equal(tpl({bar: true, foo: false}), ''); + } +); + +it( + 'handle line', + function() { + var line = '{{line}}'; + + var tpl = Handlebars.compile(line); + + assert.equal(tpl({line: 1}), 'Line 1'); + assert.equal(tpl({line: [1, 5]}), 'Lines 1-5'); + assert.equal(tpl({line: 1, column: 1, showColumns: true}), 'Line 1, Column 1'); + } +); + +it( + 'handle color', + function() { + var color = '{{#color}}Hello{{/color}}'; + + var tpl = Handlebars.compile(color); + + assert.equal(tpl({type: 'warn'}), colors.warn('Hello')); + assert.equal(tpl({type: 'subtle'}), colors.subtle('Hello')); + assert.equal(tpl({type: 'foo'}), colors.warn('Hello')); + } +); + +it( + 'handle dynamically defined color', + function() { + colorStyles.forEach( + function(item, index) { + var color = '{{#' + item + '}}Hello{{/' + item + '}}'; + + var tpl = Handlebars.compile(color); + + assert.equal(tpl(), colors[item]('Hello')); + } + ); + } +); \ No newline at end of file