diff --git a/.travis.yml b/.travis.yml index 4d7aab0..8ee6a7e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: - - "0.10" + - "5.9.0" before_install: - npm install -g grunt-cli diff --git a/Gruntfile.js b/Gruntfile.js index 99af145..9aa2105 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -19,17 +19,33 @@ module.exports = function(grunt) { banner: '<%= meta.banner %>' }, build: { - src: 'src/<%= pkg.name %>.js', + src: 'dist/<%= pkg.name %>.js', dest: 'dist/<%= pkg.name %>.min.js' } }, - concat: { + babel: { + options: { + presets: ['es2015'] + }, + dist: { + files: [ + { + expand: true, + cwd: 'src/', + src: ['**/*.js'], + dest: 'lib/' + } + ] + } + }, + browserify: { options: { banner: '<%= meta.banner %>' }, dist: { - src: ['src/**/*.js'], - dest: 'dist/<%= pkg.name %>.js' + files: { + 'dist/<%= pkg.name %>.js': ['lib/index.js'] + } } }, mochaTest: { @@ -38,15 +54,22 @@ module.exports = function(grunt) { reporter: 'spec', mocha: require('mocha') }, - src: ['spec/**/*.js'] + src: ['spec/index.js'] } }, - clean: ['dist'] + mocha_phantomjs: { + options: { + reporter: 'spec' + }, + all: ['spec/index.html'] + } }); - grunt.loadNpmTasks('grunt-mocha-test'); + grunt.loadNpmTasks('grunt-babel'); + grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-contrib-concat'); + grunt.loadNpmTasks('grunt-mocha-test'); + grunt.loadNpmTasks('grunt-mocha-phantomjs'); - grunt.registerTask('default', ['uglify', 'concat', 'mochaTest']); + grunt.registerTask('default', ['babel', 'browserify', 'uglify', 'mochaTest', 'mocha_phantomjs']); }; diff --git a/README.md b/README.md index c407a48..2bd10ad 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,19 @@ handlebars.nested [![Build Status](https://travis-ci.org/mateusmaso/handlebars.nested.svg?branch=master)](https://travis-ci.org/mateusmaso/handlebars.nested) ================= -This library is an extension for Handlebars which allows nesting helpers and expressions in one level deep. It was first created as a workaround but turned out to be an simple and effective solution for many issues. +This is an extension for Handlebars which allows nesting helpers and expressions in one level deep. For latest versions of Handlebars, please consider using [subexpressions](http://handlebarsjs.com/expressions.html) instead. -## Features +## Install -* Nesting helpers and expressions. - -## Dependencies - -* handlebars.js (>= 1.0.0) +``` +$ npm install --save handlebars.nested +``` -## Node +## Usage ```javascript -var Handlebars = global.Handlebars = require("handlebars"); -require("handlebars.nested"); +var Handlebars = require("handlebars"); +require("handlebars.nested").default(Handlebars); ``` ## Examples @@ -31,4 +29,4 @@ require("handlebars.nested"); ## License -Copyright (c) 2012-2014 Mateus Maso. Released under an MIT license. +MIT © [Mateus Maso](http://www.mateusmaso.com) diff --git a/dist/handlebars.nested.js b/dist/handlebars.nested.js index 9174569..8ba6d0a 100644 --- a/dist/handlebars.nested.js +++ b/dist/handlebars.nested.js @@ -1,61 +1,125 @@ // handlebars.nested // ----------------- -// v0.1.1 +// v0.2.0 // -// Copyright (c) 2012-2014 Mateus Maso +// Copyright (c) 2012-2016 Mateus Maso // Distributed under MIT license // // http://github.com/mateusmaso/handlebars.nested -(function(root, factory) { - if (typeof exports !== 'undefined') { - if (typeof module !== 'undefined' && module.exports) - module.exports = factory(global.Handlebars); - exports = factory(global.Handlebars); - } else { - factory(root.Handlebars); - } +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= 0) { - value = Handlebars.compile(value)(this); - } + _deps2.default.Handlebars._registerHelper.apply(this, [name, nestedFn, inverse]); +}; - return value; - }; +function resolveNested(value) { + if ((0, _deps.getUtils)().isString(value) && value.indexOf('{{') >= 0) { + value = _deps2.default.Handlebars.compile(value)(this); + } + + return value; +}; + +},{"../deps":2}],2:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.getUtils = getUtils; +var deps = {}; + +function getUtils() { + return deps.Handlebars.Utils; +} + +exports.default = deps; + +},{}],3:[function(require,module,exports){ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = HandlebarsNested; + +var _utils = require('./utils'); + +var _core = require('./core'); + +var _deps = require('./deps'); + +var _deps2 = _interopRequireDefault(_deps); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function HandlebarsNested(Handlebars) { + if (!_deps2.default.Handlebars) { + var extend = Handlebars.Utils.extend; + + + extend(_deps2.default, { Handlebars: Handlebars }); + + extend(Handlebars, { + resolveNested: _core.resolveNested, + registerHelper: _core.registerHelper, + _registerHelper: Handlebars.registerHelper + }); + + extend(Handlebars.Utils, { isString: _utils.isString }); + } + + return Handlebars; +} + +if (typeof window !== "undefined" && window.Handlebars) { + HandlebarsNested(window.Handlebars); +} + +},{"./core":1,"./deps":2,"./utils":4}],4:[function(require,module,exports){ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isString = isString; +function isString(object) { + return toString.call(object) == '[object String]'; +} -})); +},{}]},{},[3]); diff --git a/dist/handlebars.nested.min.js b/dist/handlebars.nested.min.js index d804bbf..177a063 100644 --- a/dist/handlebars.nested.min.js +++ b/dist/handlebars.nested.min.js @@ -1,10 +1,10 @@ // handlebars.nested // ----------------- -// v0.1.1 +// v0.2.0 // -// Copyright (c) 2012-2014 Mateus Maso +// Copyright (c) 2012-2016 Mateus Maso // Distributed under MIT license // // http://github.com/mateusmaso/handlebars.nested -!function(a,b){"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(module.exports=b(global.Handlebars)),exports=b(global.Handlebars)):b(a.Handlebars)}(this,function(a){var b=a.Utils,c=a.registerHelper;a.Utils.isString=function(a){return"[object String]"==toString.call(a)},a.registerHelper=function(b,d,e){var f=function(){for(var b=[],c=0;c=0&&(c=a.compile(c)(this)),c}}); \ No newline at end of file +!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g=0&&(a=h["default"].Handlebars.compile(a)(this)),a}Object.defineProperty(c,"__esModule",{value:!0}),c.registerHelper=e,c.resolveNested=f;var g=a("../deps"),h=d(g)},{"../deps":2}],2:[function(a,b,c){"use strict";function d(){return e.Handlebars.Utils}Object.defineProperty(c,"__esModule",{value:!0}),c.getUtils=d;var e={};c["default"]=e},{}],3:[function(a,b,c){"use strict";function d(a){return a&&a.__esModule?a:{"default":a}}function e(a){if(!i["default"].Handlebars){var b=a.Utils.extend;b(i["default"],{Handlebars:a}),b(a,{resolveNested:g.resolveNested,registerHelper:g.registerHelper,_registerHelper:a.registerHelper}),b(a.Utils,{isString:f.isString})}return a}Object.defineProperty(c,"__esModule",{value:!0}),c["default"]=e;var f=a("./utils"),g=a("./core"),h=a("./deps"),i=d(h);"undefined"!=typeof window&&window.Handlebars&&e(window.Handlebars)},{"./core":1,"./deps":2,"./utils":4}],4:[function(a,b,c){"use strict";function d(a){return"[object String]"==toString.call(a)}Object.defineProperty(c,"__esModule",{value:!0}),c.isString=d},{}]},{},[3]); \ No newline at end of file diff --git a/lib/core/index.js b/lib/core/index.js new file mode 100644 index 0000000..777f3d7 --- /dev/null +++ b/lib/core/index.js @@ -0,0 +1,45 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.registerHelper = registerHelper; +exports.resolveNested = resolveNested; + +var _deps = require("../deps"); + +var _deps2 = _interopRequireDefault(_deps); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function registerHelper(name, fn, inverse) { + var nestedFn = function nestedFn() { + var nestedArguments = []; + + for (var index = 0; index < arguments.length; index++) { + var argument = arguments[index]; + + if (argument && argument.hash) { + for (var key in argument.hash) { + argument.hash[key] = resolveNested.apply(this, [argument.hash[key]]); + } + + nestedArguments.push(argument); + } else { + nestedArguments.push(resolveNested.apply(this, [argument])); + } + } + + return fn.apply(this, nestedArguments); + }; + + _deps2.default.Handlebars._registerHelper.apply(this, [name, nestedFn, inverse]); +}; + +function resolveNested(value) { + if ((0, _deps.getUtils)().isString(value) && value.indexOf('{{') >= 0) { + value = _deps2.default.Handlebars.compile(value)(this); + } + + return value; +}; diff --git a/lib/deps/index.js b/lib/deps/index.js new file mode 100644 index 0000000..7ac6023 --- /dev/null +++ b/lib/deps/index.js @@ -0,0 +1,13 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.getUtils = getUtils; +var deps = {}; + +function getUtils() { + return deps.Handlebars.Utils; +} + +exports.default = deps; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..0926142 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,39 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = HandlebarsNested; + +var _utils = require('./utils'); + +var _core = require('./core'); + +var _deps = require('./deps'); + +var _deps2 = _interopRequireDefault(_deps); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function HandlebarsNested(Handlebars) { + if (!_deps2.default.Handlebars) { + var extend = Handlebars.Utils.extend; + + + extend(_deps2.default, { Handlebars: Handlebars }); + + extend(Handlebars, { + resolveNested: _core.resolveNested, + registerHelper: _core.registerHelper, + _registerHelper: Handlebars.registerHelper + }); + + extend(Handlebars.Utils, { isString: _utils.isString }); + } + + return Handlebars; +} + +if (typeof window !== "undefined" && window.Handlebars) { + HandlebarsNested(window.Handlebars); +} diff --git a/lib/utils/index.js b/lib/utils/index.js new file mode 100644 index 0000000..5d9d416 --- /dev/null +++ b/lib/utils/index.js @@ -0,0 +1,9 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isString = isString; +function isString(object) { + return toString.call(object) == '[object String]'; +} diff --git a/package.json b/package.json index 8ed3145..0f2430e 100644 --- a/package.json +++ b/package.json @@ -1,31 +1,37 @@ { "name": "handlebars.nested", "description": "Handlebars plugin for nesting helpers and expressions", - "version": "0.1.1", + "version": "0.2.0", "author": "mateusmaso", - "keywords": ["handlebars", "javascript", "nested"], + "license": "MIT", + "keywords": [ + "handlebars", + "nested", + "subexpressions" + ], "devDependencies": { "chai": "*", "mocha": "*", - "handlebars": "~1.0.0", "grunt": "~0.4.5", + "grunt-babel": "^6.0.0", + "grunt-browserify": "~4.0.1", + "grunt-contrib-uglify": "~0.5.0", "grunt-mocha-test": "~0.11.0", - "grunt-contrib-concat": "~0.5.0", - "grunt-contrib-uglify": "~0.5.0" + "grunt-mocha-phantomjs": "~3.0.0", + "babel-preset-es2015": "^6.5.0", + "babelify": "^7.2.0", + "jsdom": "^9.1.0", + "handlebars": "~4.0.5" }, - "licenses": [{ - "type": "MIT", - "url": "https://github.com/mateusmaso/handlebars.nested/blob/master/LICENSE.txt" - }], "repository": { "type" : "git", "url": "http://github.com/mateusmaso/handlebars.nested" }, - "dependencies": { - "handlebars": "~1.0.0" - }, "scripts": { - "test": "grunt mochaTest" + "test": "grunt mochaTest mocha_phantomjs" + }, + "engines": { + "node" : "~5.9.0" }, - "main": "dist/handlebars.nested.js" + "main": "lib/index.js" } diff --git a/spec/index.html b/spec/index.html new file mode 100644 index 0000000..ca35e4c --- /dev/null +++ b/spec/index.html @@ -0,0 +1,21 @@ + + + + + handlebars.nested + + + + + + + + +
+
+ + + + + + diff --git a/spec/handlebars.nested.spec.js b/spec/index.js similarity index 67% rename from spec/handlebars.nested.spec.js rename to spec/index.js index ceb6397..1290777 100644 --- a/spec/handlebars.nested.spec.js +++ b/spec/index.js @@ -1,6 +1,11 @@ -var chai = require("chai"); -global.Handlebars = require("handlebars"); -require("../src/handlebars.nested"); +if (typeof window === "undefined") { + var jsdom = require("jsdom").jsdom; + var document = global.document = jsdom("test"); + var window = global.window = document.defaultView; + var chai = require("chai"); + var Handlebars = require("handlebars"); + require("../lib").default(Handlebars); +} describe("handlebars.nested", function() { beforeEach(function() { diff --git a/src/core/index.js b/src/core/index.js new file mode 100644 index 0000000..4c7729a --- /dev/null +++ b/src/core/index.js @@ -0,0 +1,33 @@ +import deps, {getUtils} from "../deps"; + +export function registerHelper(name, fn, inverse) { + var nestedFn = function() { + var nestedArguments = []; + + for (var index = 0; index < arguments.length; index++) { + var argument = arguments[index]; + + if (argument && argument.hash) { + for (var key in argument.hash) { + argument.hash[key] = resolveNested.apply(this, [argument.hash[key]]); + } + + nestedArguments.push(argument); + } else { + nestedArguments.push(resolveNested.apply(this, [argument])); + } + } + + return fn.apply(this, nestedArguments); + }; + + deps.Handlebars._registerHelper.apply(this, [name, nestedFn, inverse]); +}; + +export function resolveNested(value) { + if (getUtils().isString(value) && value.indexOf('{{') >= 0) { + value = deps.Handlebars.compile(value)(this); + } + + return value; +}; diff --git a/src/deps/index.js b/src/deps/index.js new file mode 100644 index 0000000..d9e5e45 --- /dev/null +++ b/src/deps/index.js @@ -0,0 +1,7 @@ +let deps = {}; + +export function getUtils() { + return deps.Handlebars.Utils; +} + +export default deps; diff --git a/src/handlebars.nested.js b/src/handlebars.nested.js deleted file mode 100644 index 4d2ce54..0000000 --- a/src/handlebars.nested.js +++ /dev/null @@ -1,52 +0,0 @@ -(function(root, factory) { - - if (typeof exports !== 'undefined') { - if (typeof module !== 'undefined' && module.exports) - module.exports = factory(global.Handlebars); - exports = factory(global.Handlebars); - } else { - factory(root.Handlebars); - } - -}(this, function(Handlebars) { - - var Utils = Handlebars.Utils; - var registerHelper = Handlebars.registerHelper; - - Handlebars.Utils.isString = function(object) { - return toString.call(object) == '[object String]'; - }; - - Handlebars.registerHelper = function(name, fn, inverse) { - var nestedFn = function() { - var nestedArguments = []; - - for (var index = 0; index < arguments.length; index++) { - var argument = arguments[index]; - - if (argument && argument.hash) { - for (key in argument.hash) { - argument.hash[key] = Handlebars.resolveNested.apply(this, [argument.hash[key]]); - } - - nestedArguments.push(argument); - } else { - nestedArguments.push(Handlebars.resolveNested.apply(this, [argument])); - } - } - - return fn.apply(this, nestedArguments); - }; - - registerHelper.apply(this, [name, nestedFn, inverse]); - }; - - Handlebars.resolveNested = function(value) { - if (Utils.isString(value) && value.indexOf('{{') >= 0) { - value = Handlebars.compile(value)(this); - } - - return value; - }; - -})); diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a7b767b --- /dev/null +++ b/src/index.js @@ -0,0 +1,25 @@ +import {isString} from './utils'; +import {registerHelper, resolveNested} from './core'; +import deps from "./deps"; + +export default function HandlebarsNested(Handlebars) { + if (!deps.Handlebars) { + var {extend} = Handlebars.Utils; + + extend(deps, {Handlebars}); + + extend(Handlebars, { + resolveNested, + registerHelper, + _registerHelper: Handlebars.registerHelper + }); + + extend(Handlebars.Utils, {isString}); + } + + return Handlebars; +} + +if (typeof window !== "undefined" && window.Handlebars) { + HandlebarsNested(window.Handlebars); +} diff --git a/src/utils/index.js b/src/utils/index.js new file mode 100644 index 0000000..71e3d87 --- /dev/null +++ b/src/utils/index.js @@ -0,0 +1,3 @@ +export function isString(object) { + return toString.call(object) == '[object String]'; +}