Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,416 @@
# `handlebars-wax`
[![NPM version][npm-img]][npm-url] [![Downloads][downloads-img]][npm-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Chat][gitter-img]][gitter-url] [![Tip][amazon-img]][amazon-url]
Effortless registration of [Handlebars][handlebars] data, partials, helpers, and decorators.
## Install
$ npm install --save handlebars-wax
## Usage
```
┣━ index.js
┣━ data/
┃ ┣━ site.js
┃ ┗━ locale.json
┣━ decorators/
┃ ┣━ currency.js
┃ ┗━ i18n.js
┣━ helpers/
┃ ┣━ link.js
┃ ┗━ list.js
┗━ partials/
┣━ footer.js
┗━ header.hbs
```
```js
var handlebars = require('handlebars');
var handlebarsWax = require('handlebars-wax');
var wax = handlebarsWax(handlebars)
// Partials
.partials('./partials/**/*.{hbs,js}')
.partials({
boo: '{{#each boo}}{{greet}}{{/each}}',
far: '{{#each far}}{{length}}{{/each}}'
})
// Helpers
.helpers(require('handlebars-layouts'))
.helpers('./helpers/**/*.js')
.helpers({
foo: function () { ... },
bar: function () { ... }
})
// Decorators
.decorators('./decorators/**/*.js')
.decorators({
baz: function () { ... },
qux: function () { ... }
})
// Data
.data('./data/**/*.{js,json}')
.data({
lorem: 'dolor',
ipsum: 'sit amet'
});
console.log(handlebars.partials);
// { footer: fn(), header: fn(), boo: fn(), far: fn() }
console.log(handlebars.helpers);
// { link: fn(), list: fn(), foo: fn(), bar: fn(), extend: fn(), ... }
console.log(handlebars.decorators);
// { currency: fn(), i18n: fn(), baz: fn(), bat: fn() }
console.log(wax.context);
// { site: { ... }, locale: { ... }, lorem: 'dolor', ipsum: 'sit amet' }
var template = wax.compile('{{lorem}} {{ipsum}}');
console.log(template({ ipsum: 'consectetur' }));
// "dolor consectetur"
```
## Registering Partials, Helpers, and Decorators
You may use `handlebars-wax` to require and register any modules that export a `register` factory, an object, or a function as partials, helpers, and decorators.
### Exporting a Factory
In cases where a direct reference to the instance of Handlebars in use is needed, modules may export a `register` factory function. For example, the following module will define a new helper called `foo-bar`:
```js
module.exports.register = function (handlebars) {
handlebars.registerHelper('foo-bar', function (text, url) {
var result = '<a href="' + url + '">' + text + '</a>';
return new handlebars.SafeString(result);
});
};
```
### Exporting an Object
If a module exports an object, that object is registered with Handlebars directly where the object keys are used as names. For example, the following module exports an object that will cause `baz` and `qux` to be registered:
```js
module.exports = {
baz: function () {
// do something
},
qux: function () {
// do something
}
};
```
### Exporting a Function
If a module exports a function, that function is registered based on the globbed portion of a path, ignoring extensions. Handlebars' `require.extensions` hook may be used to load `.handlebars` or `.hbs` files.
```js
module.exports = function () {
// do something
};
```
```
┣━ index.js
┗━ partials/
┣━ components
┃ ┣━ link.js
┃ ┗━ list.js
┗━ layouts
┣━ one-column.hbs
┗━ two-column.hbs
```
```js
handlebarsWax(handlebars)
.partials('./partials/**/*.{hbs,js}');
// registers the partials:
// - `components/link`
// - `components/list`
// - `layouts/one-column`
// - `layouts/two-column`
handlebarsWax(handlebars)
.partials('./partials/components/*.js')
.partials('./partials/layouts/*.hbs');
// registers the partials:
// - `link`
// - `list`
// - `one-column`
// - `two-column`
handlebarsWax(handlebars)
.partials([
'./partials/**/*.{hbs,js}',
'!./partials/layouts/**'
])
.partials('./partials/layouts/*.hbs');
// registers the partials:
// - `components/link`
// - `components/list`
// - `one-column`
// - `two-column`
```
Helpers and decorators are handled similarly to partials, but path separators and non-word characters are replaced with hyphens to avoid having to use [segment-literal notation][square] inside templates.
```
┣━ index.js
┗━ helpers/
┣━ format
┃ ┣━ date.js
┃ ┗━ number.round.js
┗━ list
┣━ group-by.js
┗━ order-by.js
```
```js
handlebarsWax(handlebars)
.helpers('./helpers/**/*.js');
// registers the helpers:
// - `format-date`
// - `format-number-round`
// - `list-group-by`
// - `list-order-by`
```
You may customize how names are generated by using the `base` option, or by specifying a custom `parsePartialName`, `parseHelperName`, or `parseDecoratorName` function.
```js
handlebarsWax(handlebars)
.partials('./partials/components/*.js', {
base: __dirname
})
.partials('./partials/layouts/*.hbs', {
base: path.join(__dirname, 'partials/layouts')
});
// registers the partials:
// - `partials/components/link`
// - `partials/components/list`
// - `one-column`
// - `two-column`
handlebarsWax(handlebars)
.helpers('./helpers/**/*.{hbs,js}', {
// Expect these helpers to export their own name.
parseHelperName: function(options, file) {
// options.handlebars
// file.cwd
// file.base
// file.path
// file.exports
return file.exports.name;
}
});
// registers the helpers:
// - `date`
// - `round`
// - `groupBy`
// - `orderBy`
```
## Registering Data
When data is registered, the resulting object structure is determined according to the default rules of [`require-glob`][reqglob].
```
┣━ index.js
┗━ data/
┣━ foo/
┃ ┣━ hello.js
┃ ┗━ world.json
┗━ bar/
┣━ bye.js
┗━ moon.json
```
```js
handlebarsWax(handlebars)
.data('./data/**/*.{js,json}');
// registers the data:
// {
// foo: {
// hello: require('./data/foo/hello.js'),
// world: require('./data/foo/world.json')
// },
// bar: {
// hello: require('./data/bar/bye.js'),
// world: require('./data/bar/moon.json')
// }
// }
```
You may customize how data is structured by using the `base` option, or by specifying a custom `parseDataName`.
```js
handlebarsWax(handlebars)
.data('./data/**/*.{js,json}', {
base: __dirname,
parseDataName: function(options, file) {
// options.handlebars
// file.cwd
// file.base
// file.path
// file.exports
return file.path
.replace(file.base, '')
.split(/[\/\.]/)
.filter(Boolean)
.reverse()
.join('_')
.toUpperCase();
}
});
// registers the data:
// {
// JS_HELLO_FOO_DATA: require('./data/foo/hello.js'),
// JSON_WORLD_FOO_DATA: require('./data/foo/world.json'),
// JS_BYE_BAR_DATA: require('./data/bar/bye.js'),
// JSON_MOON_BAR_DATA: require('./data/bar/moon.json')
// }
```
## Context and Rendering
Registered data is exposed to templates that are compiled by `handlebars-wax` as the [`@root`][root] context and as a [parent frame][frame] of data passed to the template function. You may compile strings, or recompile template functions.
```js
var compiledTemplate = handlebars.compile('{{foo}} {{bar}}');
var waxedTemplate = wax.compile(compiledTemplate);
// or: var waxedTemplate = wax.compile('{{foo}} {{bar}}');
wax.data({ foo: 'hello', bar: 'world' });
console.log(compiledTemplate());
// " "
console.log(waxedTemplate());
// "hello world"
console.log(waxedTemplate({ bar: 'moon' }));
// "hello moon"
var accessTemplate = wax.compile('{{bar}} {{_parent.bar}} {{@root.bar}} {{@root._parent.bar}}');
console.log(accessTemplate({ bar: 'moon' }, { data: { root: { bar: 'sun'} } }));
// "moon world sun world"
```
## API
### handlebarsWax(handlebars [, options]): HandlebarsWax
- `handlebars` `{Handlebars}` An instance of Handlebars to wax.
- `options` `{Object}` (optional) Passed directly to [`require-glob`][reqglob] so check there for more options.
- `bustCache` `{Boolean}` (default: `true`) Force reload data, partials, helpers, and decorators.
- `cwd` `{String}` (default: `process.cwd()`) Current working directory.
- `compileOptions` `{Object}` Default options to use when compiling templates.
- `templateOptions` `{Object}` Default options to use when rendering templates.
- `parsePartialName` `{Function(options, file): String}` See section on [registering a function](#exporting-a-function).
- `parseHelperName` `{Function(options, file): String}` See section on [registering a function](#exporting-a-function).
- `parseDecoratorName` `{Function(options, file): String}` See section on [registering a function](#exporting-a-function).
- `parseDataName` `{Function(options, file): String}` See section on [registering data](#exporting-data).
Provides a waxed API to augment an instance of Handlebars.
### .handlebars
The instance of Handlebars in use.
### .context
An object containing all [registered data](#data-pattern-options-handlebarswax).
### .partials(pattern [, options]): HandlebarsWax
- `pattern` `{String|Array.<String>|Object|Function(handlebars)}` One or more [`minimatch` glob patterns][minimatch] patterns, an object of partials, or a partial factory.
- `options` `{Object}` Passed directly to [`require-glob`][reqglob] so check there for more options.
- `parsePartialName` `{Function(options, file): String}` See section on [registering a function](#exporting-a-function).
Requires and registers [partials][partials] en-masse from the file-system or an object. May be called more than once. If names collide, newest wins.
### .helpers(pattern [, options]): HandlebarsWax
- `pattern` `{String|Array.<String>|Object|Function(handlebars)}` One or more [`minimatch` glob patterns][minimatch] patterns, an object of helpers, or a helper factory.
- `options` `{Object}` Passed directly to [`require-glob`][reqglob] so check there for more options.
- `parseHelperName` `{Function(options, file): String}` See section on [registering a function](#exporting-a-function).
Requires and registers [helpers][helpers] en-masse from the file-system or an object. May be called more than once. If names collide, newest wins.
### .decorators(pattern [, options]): HandlebarsWax
- `pattern` `{String|Array.<String>|Object|Function(handlebars)}` One or more [`minimatch` glob patterns][minimatch] patterns, an object of decorators, or a decorator factory.
- `options` `{Object}` Passed directly to [`require-glob`][reqglob] so check there for more options.
- `parseDecoratorName` `{Function(options, file): String}` See section on [registering a function](#exporting-a-function).
Requires and registers [decorators][decorators] en-masse from the file-system or an object. May be called more than once. If names collide, newest wins.
### .data(pattern [, options]): HandlebarsWax
- `pattern` `{String|Array.<String>|Object}` One or more [`minimatch` glob patterns][minimatch] patterns, or a data object.
- `options` `{Object}` Passed directly to [`require-glob`][reqglob] so check there for more options.
- `parseDataName` `{Function(options, file): String}` See section on [registering data](#registering-data).
Requires and registers data en-masse from the file-system or an object into the current context. May be called more than once. Results are shallow-merged into a single object. If keys collide, newest wins. See [Context and Rendering](#context-and-rendering).
### .compile(template [, options]): Function(Object)
- `template` `{String|Function(Object)}`
- `options` `{Object}` See the [`Handlebars.compile` documentation][compile].
Compiles a template that can be executed immediately to produce a final result. Data provided to the template function will be a [child frame][frame] of the current [context](#context). See [Context and Rendering](#context-and-rendering).
[compile]: http://handlebarsjs.com/reference.html#base-compile
[decorators]: https://github.com/wycats/handlebars.js/blob/master/docs/decorators-api.md
[frame]: http://handlebarsjs.com/reference.html#base-createFrame
[glob]: https://github.com/isaacs/node-glob#usage
[handlebars]: https://github.com/wycats/handlebars.js#usage
[helpers]: http://handlebarsjs.com/#helpers
[keygen]: https://github.com/shannonmoeller/require-glob#keygen
[minimatch]: https://github.com/isaacs/minimatch#usage
[partials]: http://handlebarsjs.com/#partials
[reqglob]: https://github.com/shannonmoeller/require-glob#usage
[root]: http://handlebarsjs.com/reference.html#data-root
[square]: http://handlebarsjs.com/expressions.html#basic-blocks
## Contribute
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.
### Test
$ npm test
----
© Shannon Moeller <me@shannonmoeller.com> (shannonmoeller.com)
Licensed under [MIT](http://shannonmoeller.com/mit.txt)
[amazon-img]: https://img.shields.io/badge/amazon-tip_jar-yellow.svg?style=flat-square
[amazon-url]: https://www.amazon.com/gp/registry/wishlist/1VQM9ID04YPC5?sort=universal-price
[coveralls-img]: http://img.shields.io/coveralls/shannonmoeller/handlebars-wax/master.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/shannonmoeller/handlebars-wax
[downloads-img]: http://img.shields.io/npm/dm/handlebars-wax.svg?style=flat-square
[gitter-img]: http://img.shields.io/badge/gitter-join_chat-1dce73.svg?style=flat-square
[gitter-url]: https://gitter.im/shannonmoeller/shannonmoeller
[npm-img]: http://img.shields.io/npm/v/handlebars-wax.svg?style=flat-square
[npm-url]: https://npmjs.org/package/handlebars-wax
[travis-img]: http://img.shields.io/travis/shannonmoeller/handlebars-wax.svg?style=flat-square
[travis-url]: https://travis-ci.org/shannonmoeller/handlebars-wax

View File

@ -0,0 +1,103 @@
{
"_from": "handlebars-wax@^4.0.4",
"_id": "handlebars-wax@4.0.4",
"_inBundle": false,
"_integrity": "sha1-4byjtgbvFnvgzvBCRtZvFaBxxYw=",
"_location": "/handlebars-wax",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "handlebars-wax@^4.0.4",
"name": "handlebars-wax",
"escapedName": "handlebars-wax",
"rawSpec": "^4.0.4",
"saveSpec": null,
"fetchSpec": "^4.0.4"
},
"_requiredBy": [
"/gulp-hb"
],
"_resolved": "https://registry.npmjs.org/handlebars-wax/-/handlebars-wax-4.0.4.tgz",
"_shasum": "e1bca3b606ef167be0cef04246d66f15a071c58c",
"_spec": "handlebars-wax@^4.0.4",
"_where": "/home/manfred/enviPath/ketcher2/ketcher/node_modules/gulp-hb",
"author": {
"name": "Shannon Moeller",
"email": "me@shannonmoeller.com",
"url": "http://shannonmoeller.com"
},
"bugs": {
"url": "https://github.com/shannonmoeller/handlebars-wax/issues"
},
"bundleDependencies": false,
"dependencies": {
"object-assign": "^4.1.0",
"require-glob": "^3.2.0"
},
"deprecated": false,
"description": "Effortless wiring of Handlebars data, partials, helpers, and decorators.",
"devDependencies": {
"ava": "^0.16.0",
"coveralls": "^2.11.14",
"handlebars": "^4.0.5",
"nyc": "^8.3.0",
"watch": "^0.19.2",
"xo": "^0.16.0"
},
"engines": {
"node": ">= 0.12"
},
"homepage": "https://github.com/shannonmoeller/handlebars-wax",
"keywords": [
"handlebars",
"hbs",
"hb",
"glob",
"globby",
"register",
"data",
"partials",
"helpers",
"decorators"
],
"license": "MIT",
"main": "src/handlebars-wax.js",
"name": "handlebars-wax",
"repository": {
"type": "git",
"url": "git+https://github.com/shannonmoeller/handlebars-wax.git"
},
"scripts": {
"coveralls": "nyc report -r text-lcov | coveralls",
"pretest": "xo src/*.js test/*.js",
"test": "nyc -r html -r text ava -v test/*.js",
"watch": "watch 'npm test' src test -du --wait=5"
},
"version": "4.0.4",
"xo": {
"rules": {
"brace-style": [
2,
"stroustrup"
],
"eqeqeq": [
2,
"allow-null"
],
"no-eq-null": 0,
"object-curly-spacing": [
2,
"always"
],
"operator-linebreak": [
2,
"before"
],
"babel/object-curly-spacing": [
2,
"always"
]
}
}
}

View File

@ -0,0 +1,222 @@
'use strict';
var fs = require('fs');
var path = require('path');
var assign = require('object-assign');
var requireGlob = require('require-glob');
var toString = Object.prototype.toString;
var ESCAPE_CHARACTERS = /[-\/\\^$*+?.()|[\]{}]/g;
var NON_WORD_CHARACTERS = /\W+/g;
var PATH_SEPARATOR = '/';
var PATH_SEPARATORS = /[\\\/]/g;
var WHITESPACE_CHARACTERS = /\s+/g;
var WORD_SEPARATOR = '-';
var TYPE_FUNCTION = 'fun';
var TYPE_OBJECT = 'obj';
// Utilities
function escapeRx(str) {
return str.replace(ESCAPE_CHARACTERS, '\\$&');
}
function getTypeOf(value) {
return toString
.call(value)
.substr(8, 3)
.toLowerCase();
}
function hookRequire(handlebars) {
var extLong = require.extensions['.handlebars'];
var extShort = require.extensions['.hbs'];
function extensions(module, filename) {
var templateString = fs.readFileSync(filename, 'utf8');
module.exports = handlebars.compile(templateString);
}
require.extensions['.handlebars'] = extensions;
require.extensions['.hbs'] = extensions;
return function unhookRequire() {
require.extensions['.handlebars'] = extLong;
require.extensions['.hbs'] = extShort;
};
}
// Map Reduce
function keygenPartial(options, file) {
var resolvedFilePath = fs.realpathSync(file.path);
var resolvedFileBase = fs.realpathSync(file.base);
var fullPath = resolvedFilePath.replace(PATH_SEPARATORS, PATH_SEPARATOR);
var basePath = resolvedFileBase.replace(PATH_SEPARATORS, PATH_SEPARATOR) + PATH_SEPARATOR;
var shortPath = fullPath.replace(new RegExp('^' + escapeRx(basePath), 'i'), '');
var extension = path.extname(shortPath);
return shortPath
.substr(0, shortPath.length - extension.length)
.replace(WHITESPACE_CHARACTERS, WORD_SEPARATOR);
}
function keygenHelper(options, file) {
return keygenPartial(options, file)
.replace(NON_WORD_CHARACTERS, WORD_SEPARATOR);
}
function keygenDecorator(options, file) {
return keygenHelper(options, file);
}
function reducer(options, obj, fileObj) {
var value = fileObj.exports;
if (!value) {
return obj;
}
if (getTypeOf(value.register) === TYPE_FUNCTION) {
value = value.register(options.handlebars, options);
if (getTypeOf(value) === TYPE_OBJECT) {
return assign(obj, value);
}
return obj;
}
if (getTypeOf(value) === TYPE_OBJECT) {
return assign(obj, value);
}
obj[options.keygen(fileObj)] = value;
return obj;
}
function resolveValue(options, value) {
if (!value) {
return {};
}
if (getTypeOf(value) === TYPE_FUNCTION) {
value = value(options.handlebars, options);
if (getTypeOf(value) === TYPE_OBJECT) {
return value;
}
return {};
}
if (getTypeOf(value) === TYPE_OBJECT) {
return reducer(options, {}, { exports: value });
}
return requireGlob.sync(value, options);
}
// Wax
function HandlebarsWax(handlebars, options) {
var defaults = {
handlebars: handlebars,
bustCache: true,
cwd: process.cwd(),
compileOptions: null,
templateOptions: null,
parsePartialName: keygenPartial,
parseHelperName: keygenHelper,
parseDecoratorName: keygenDecorator,
parseDataName: null
};
this.handlebars = handlebars;
this.config = assign(defaults, options);
this.context = Object.create(null);
}
HandlebarsWax.prototype.partials = function (partials, options) {
options = assign({}, this.config, options);
options.keygen = options.parsePartialName;
options.reducer = options.reducer || reducer;
var unhookRequire = hookRequire(options.handlebars);
options.handlebars.registerPartial(
resolveValue(options, partials)
);
unhookRequire();
return this;
};
HandlebarsWax.prototype.helpers = function (helpers, options) {
options = assign({}, this.config, options);
options.keygen = options.parseHelperName;
options.reducer = options.reducer || reducer;
options.handlebars.registerHelper(
resolveValue(options, helpers)
);
return this;
};
HandlebarsWax.prototype.decorators = function (decorators, options) {
options = assign({}, this.config, options);
options.keygen = options.parseDecoratorName;
options.reducer = options.reducer || reducer;
options.handlebars.registerDecorator(
resolveValue(options, decorators)
);
return this;
};
HandlebarsWax.prototype.data = function (data, options) {
options = assign({}, this.config, options);
options.keygen = options.parseDataName;
assign(this.context, resolveValue(options, data));
return this;
};
HandlebarsWax.prototype.compile = function (template, compileOptions) {
var config = this.config;
var context = this.context;
compileOptions = assign({}, config.compileOptions, compileOptions);
if (getTypeOf(template) !== TYPE_FUNCTION) {
template = this.handlebars.compile(template, compileOptions);
}
return function (data, templateOptions) {
templateOptions = assign({}, config.templateOptions, templateOptions);
templateOptions.data = assign({}, templateOptions.data);
// {{@root.foo}} and {{@root._parent.foo}}
templateOptions.data.root = assign({ _parent: context }, templateOptions.data.root || context);
// {{foo}} and {{_parent.foo}}
return template(assign({ _parent: context }, context, data), templateOptions);
};
};
// API
function handlebarsWax(handlebars, config) {
return new HandlebarsWax(handlebars, config);
}
module.exports = handlebarsWax;
module.exports.HandlebarsWax = HandlebarsWax;

View File

@ -0,0 +1,59 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should pre-fill template-string data', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{foo}} {{bar}} {{baz}}');
wax.data({ foo: 'hello', bar: 'world' });
t.is(waxedTemplate(), 'hello world ');
t.is(waxedTemplate({ foo: 'a' }), 'a world ');
t.is(waxedTemplate({ bar: 'b' }), 'hello b ');
t.is(waxedTemplate({ baz: 'c' }), 'hello world c');
});
test('should pre-fill template-function data', async t => {
const { hb, wax } = setup();
const template = hb.compile('{{foo}} {{bar}} {{baz}}');
const waxedTemplate = wax.compile(template);
wax.data({ foo: 'hello', bar: 'world' });
t.is(template(), ' ');
t.is(template({ foo: 'a' }), 'a ');
t.is(template({ bar: 'b' }), ' b ');
t.is(template({ baz: 'c' }), ' c');
t.is(waxedTemplate(), 'hello world ');
t.is(waxedTemplate({ foo: 'a' }), 'a world ');
t.is(waxedTemplate({ bar: 'b' }), 'hello b ');
t.is(waxedTemplate({ baz: 'c' }), 'hello world c');
});
test('should set registered data as _parent', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{_parent.foo}} {{foo}}');
wax.data({ foo: 'hello' });
t.is(waxedTemplate({ foo: 'world' }), 'hello world');
});
test('should set registered data as @root', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{@root.foo}} {{foo}}');
wax.data({ foo: 'hello' });
t.is(waxedTemplate({ foo: 'world' }), 'hello world');
});
test('should prefer user-specified @root', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{foo}} {{_parent.foo}} {{@root.foo}} {{@root._parent.foo}}');
wax.data({ foo: 'hello' });
t.is(waxedTemplate({ foo: 'world' }, { data: { root: { foo: 'bye' } } }), 'world hello bye hello');
});

View File

@ -0,0 +1,30 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should not modify data', async t => {
const { wax, defaultData } = setup();
wax.data();
t.deepEqual(Object.keys(wax.context), defaultData);
});
test('should register data by object', async t => {
const { wax } = setup();
const foo = 'hello';
const bar = 'world';
wax.data({ foo, bar });
t.is(wax.context.foo, foo);
t.is(wax.context.bar, bar);
});
test('should register data by globbed object', async t => {
const { wax } = setup();
wax.data('./fixtures/data/object/**/*.{js,json}');
t.is(wax.context.hello, 'world');
t.deepEqual(wax.context.good.night, ['chair', 'bear', 'moon']);
});

View File

@ -0,0 +1,65 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should not modify decorators', async t => {
const { hb, wax, defaultDecorators } = setup();
wax.decorators().decorators('./fixtures/decorators/bogu*.js');
t.deepEqual(Object.keys(hb.decorators), defaultDecorators);
});
test('should register decorators by object', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.decorators({ foo, bar });
t.is(hb.decorators.foo, foo);
t.is(hb.decorators.bar, bar);
});
test('should register decorators by globbed factory', async t => {
const { hb, wax } = setup();
wax.decorators('./fixtures/decorators/factory/**/*.js');
t.is(typeof hb.decorators.currencyDecimal, 'function');
t.is(typeof hb.decorators.currencyFormat, 'function');
t.is(typeof hb.decorators.i18nLanguage, 'function');
t.is(typeof hb.decorators.i18nCountry, 'function');
t.is(hb.decorators.empty, undefined);
});
test('should register decorators by globbed function', async t => {
const { hb, wax } = setup();
wax.decorators('./fixtures/decorators/function/**/*.{hbs,js}');
t.is(typeof hb.decorators['currency-decimal'], 'function');
t.is(typeof hb.decorators['currency-format'], 'function');
t.is(typeof hb.decorators.language, 'function');
t.is(typeof hb.decorators.country, 'function');
t.is(hb.decorators.empty, undefined);
});
test('should register decorators by globbed object', async t => {
const { hb, wax } = setup();
wax.decorators('./fixtures/decorators/object/**/*.js');
t.is(typeof hb.decorators.currencyDecimal, 'function');
t.is(typeof hb.decorators.currencyFormat, 'function');
t.is(typeof hb.decorators.i18nLanguage, 'function');
t.is(typeof hb.decorators.i18nCountry, 'function');
t.is(hb.decorators.empty, undefined);
});
test('should raise errors', async t => {
const { wax } = setup();
const template = wax.compile('{{* foo}}');
t.throws(() => template(), /not a function/i);
});

View File

@ -0,0 +1 @@
["chair", "bear", "moon"]

View File

@ -0,0 +1 @@
module.exports = 'world';

View File

@ -0,0 +1,12 @@
'use strict';
module.exports.register = function (hb) {
hb.registerDecorator({
currencyDecimal: function (str) {
// do stuff
},
currencyFormat: function (str) {
// do stuff
}
});
};

View File

@ -0,0 +1,12 @@
'use strict';
module.exports.register = function (hb) {
hb.registerDecorator({
i18nLanguage: function (lang) {
// do stuff
},
i18nCountry: function (str) {
// do stuff
}
});
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,2 @@
'use strict';
module.exports = null;

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
currencyDecimal: function (str) {
// do stuff
},
currencyFormat: function (str) {
// do stuff
}
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
i18nLanguage: function (lang) {
// do stuff
},
i18nCountry: function (str) {
// do stuff
}
};

View File

@ -0,0 +1,12 @@
'use strict';
module.exports.register = function (hb) {
hb.registerHelper({
lower: function (str) {
return str.toLowerCase();
},
upper: function (str) {
return str.toUpperCase();
}
});
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports.register = function (hb) {
hb.registerHelper('lest', function (a, b) {
return a !== b;
});
hb.registerHelper('when', function (a, b) {
return a === b;
});
};

View File

@ -0,0 +1,2 @@
'use strict';
module.exports = null;

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function lest(a, b) {
return a !== b;
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function when(a, b) {
return a === b;
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function lower(str) {
return str.toLowerCase();
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function upper(str) {
return str.toUpperCase();
};

View File

@ -0,0 +1 @@
module.exports = true;

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
lower: function (str) {
return str.toLowerCase();
},
upper: function (str) {
return str.toUpperCase();
}
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
lest: function (a, b) {
return a !== b;
},
when: function (a, b) {
return a === b;
}
};

View File

@ -0,0 +1,8 @@
'use strict';
module.exports.register = function (hb) {
hb.registerPartial({
item: '<li>{{label}}</li>',
link: '<a href="{{url}}">{{label}}</a>'
});
};

View File

@ -0,0 +1,6 @@
'use strict';
module.exports.register = function (hb) {
hb.registerPartial('layout', '<!doctype html>\n<html>\n<head>\n <title>{{{block "title"}}}</title>\n</head>\n<body>\n {{{block "body"}}}\n</body>\n</html>');
hb.registerPartial('layout-2col', '{{#extend "layout"}}\n {{#content "body"}}\n {{{block "left"}}}\n {{{block "right"}}}\n {{/content}}\n{{/extend}}');
};

View File

@ -0,0 +1 @@
<li>{{label}}</li>

View File

@ -0,0 +1 @@
<a href="{{url}}">{{label}}</a>

View File

@ -0,0 +1,6 @@
{{#extend "layout"}}
{{#content "body"}}
{{{block "left"}}}
{{{block "right"}}}
{{/content}}
{{/extend}}

View File

@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<title>{{{block "title"}}}</title>
</head>
<body>
{{{block "body"}}}
</body>
</html>

View File

@ -0,0 +1,6 @@
'use strict';
module.exports = {
item: '<li>{{label}}</li>',
link: '<a href="{{url}}">{{label}}</a>'
};

View File

@ -0,0 +1,6 @@
'use strict';
module.exports = {
layout: '<!doctype html>\n<html>\n<head>\n <title>{{{block "title"}}}</title>\n</head>\n<body>\n {{{block "body"}}}\n</body>\n</html>',
'layout-2col': '{{#extend "layout"}}\n {{#content "body"}}\n {{{block "left"}}}\n {{{block "right"}}}\n {{/content}}\n{{/extend}}'
};

View File

@ -0,0 +1,65 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should not modify helpers', async t => {
const { hb, wax, defaultHelpers } = setup();
wax.helpers().helpers('./fixtures/helpers/bogu*.js');
t.deepEqual(Object.keys(hb.helpers), defaultHelpers);
});
test('should register helpers by object', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.helpers({ foo, bar });
t.is(hb.helpers.foo, foo);
t.is(hb.helpers.bar, bar);
});
test('should register helpers by globbed factory', async t => {
const { hb, wax } = setup();
wax.helpers('./fixtures/helpers/factory/**/*.js');
t.is(typeof hb.helpers.lower, 'function');
t.is(typeof hb.helpers.upper, 'function');
t.is(typeof hb.helpers.lest, 'function');
t.is(typeof hb.helpers.when, 'function');
t.is(hb.helpers.empty, undefined);
});
test('should register helpers by globbed function', async t => {
const { hb, wax } = setup();
wax.helpers('./fixtures/helpers/function/**/*.{hbs,js}');
t.is(typeof hb.helpers.lower, 'function');
t.is(typeof hb.helpers.upper, 'function');
t.is(typeof hb.helpers['flow-lest'], 'function');
t.is(typeof hb.helpers['flow-when'], 'function');
t.is(hb.helpers.empty, undefined);
});
test('should register helpers by globbed object', async t => {
const { hb, wax } = setup();
wax.helpers('./fixtures/helpers/object/**/*.js');
t.is(typeof hb.helpers.lower, 'function');
t.is(typeof hb.helpers.upper, 'function');
t.is(typeof hb.helpers.lest, 'function');
t.is(typeof hb.helpers.when, 'function');
t.is(hb.helpers.empty, undefined);
});
test('should raise errors', async t => {
const { wax } = setup();
const template = wax.compile('{{{foo "bar"}}}');
t.throws(() => template(), /missing helper/i);
});

View File

@ -0,0 +1,21 @@
var handlebars = require('handlebars');
var handlebarsWax = require('../../src/handlebars-wax');
exports.setup = function setup() {
var hb = handlebars.create();
var defaultPartials = Object.keys(hb.partials);
var defaultHelpers = Object.keys(hb.helpers);
var defaultDecorators = Object.keys(hb.decorators);
var wax = handlebarsWax(hb);
var defaultData = Object.keys(wax.context);
return {
hb: hb,
wax: wax,
defaultPartials: defaultPartials,
defaultHelpers: defaultHelpers,
defaultDecorators: defaultDecorators,
defaultData: defaultData
};
};

View File

@ -0,0 +1,111 @@
import test from 'ava';
import handlebars from 'handlebars';
import { setup } from './helpers/setup';
test('should not modify partials', async t => {
const { hb, wax, defaultPartials } = setup();
wax.partials().partials('./fixtures/helpers/bogu*.js');
t.deepEqual(Object.keys(hb.partials), defaultPartials);
});
test('should register partials by factory', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.partials({
register: function (handlebars) {
t.is(handlebars, hb);
handlebars.registerPartial('foo', foo);
}
});
wax.partials({
register: function (handlebars) {
t.is(handlebars, hb);
return { bar };
}
});
t.is(hb.partials.foo, foo);
t.is(hb.partials.bar, bar);
});
test('should register partials by function', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.partials(function (handlebars) {
t.is(handlebars, hb);
handlebars.registerPartial('foo', foo);
});
wax.partials(function (handlebars) {
t.is(handlebars, hb);
return { bar };
});
t.is(hb.partials.foo, foo);
t.is(hb.partials.bar, bar);
});
test('should register partials by object', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.partials({ foo, bar });
t.is(hb.partials.foo, foo);
t.is(hb.partials.bar, bar);
});
test('should register partials by globbed factory', async t => {
const { hb, wax } = setup();
wax.partials('./fixtures/partials/factory/**/*.js');
t.is(typeof hb.partials.item, 'string');
t.is(typeof hb.partials.link, 'string');
t.is(typeof hb.partials.layout, 'string');
t.is(typeof hb.partials['layout-2col'], 'string');
});
test('should register partials by globbed function', async t => {
const { hb, wax } = setup();
wax.partials('./fixtures/partials/function/**/*.{hbs,js}');
t.is(typeof hb.partials['components/item'], 'function');
t.is(typeof hb.partials['components/link'], 'function');
t.is(typeof hb.partials.layout, 'function');
t.is(typeof hb.partials['layout-2col'], 'function');
});
test('should register partials by globbed object', async t => {
const { hb, wax } = setup();
wax.partials('./fixtures/partials/object/**/*.js');
t.is(typeof hb.partials.item, 'string');
t.is(typeof hb.partials.link, 'string');
t.is(typeof hb.partials.layout, 'string');
t.is(typeof hb.partials['layout-2col'], 'string');
});
test('should raise errors', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{> foo}}');
t.throws(() => waxedTemplate(), /could not be found/i);
});
test.after('should not cause cross-contamination', async t => {
t.is(Object.keys(handlebars.partials).length, 0);
});