Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
246 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import Handlebars from '../src/handlebars' | ||
|
||
const data = { | ||
foo: 'bar', | ||
fu: 'bar', | ||
obj: { | ||
a: 'b', | ||
c: 123, | ||
d: ['x', 'y', 'z'] | ||
}, | ||
text: 'this is a long line of text', | ||
uuid: 'CFE20509-E7CF-4401-9733-7615EF3E8A25', | ||
want: 0, | ||
t: true, | ||
f: false, | ||
empty: [], | ||
commits: ['088fcd5a5bc73a6733edcc58d0d30869ddbe2e2f'], | ||
numbers: [1, 2, 3, 4], | ||
letters: ['abc', 'def', 'ghi'], | ||
foobars: [ | ||
{foo: true, bar: 1}, | ||
{foo: false, bar: 3}, | ||
{foo: true, bar: 3} | ||
], | ||
attendees: ['dave', 'mike', 'jane', 'betty'], | ||
items: [1, 2, 3] | ||
} | ||
|
||
// utilities | ||
test('json stringify', () => { | ||
const template = Handlebars.compile('{{{json obj}}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('{"a":"b","c":123,"d":["x","y","z"]}') | ||
}) | ||
|
||
test('truncate uuid', () => { | ||
const template = Handlebars.compile('{{truncate uuid 8}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('CFE20509') | ||
}) | ||
|
||
test('default is not needed', () => { | ||
const template = Handlebars.compile('{{default want "fallback"}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('0') | ||
}) | ||
|
||
test('default is needed', () => { | ||
const template = Handlebars.compile('{{default fallback "fallback"}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('fallback') | ||
}) | ||
|
||
test('pluralize empty list', () => { | ||
const template = Handlebars.compile('{{pluralize empty}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('no items') | ||
}) | ||
|
||
test('pluralize commits', () => { | ||
const template = Handlebars.compile('{{pluralize commits "commit"}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('1 commit') | ||
}) | ||
|
||
test('pluralize attendees', () => { | ||
const template = Handlebars.compile('{{pluralize attendees "attendee"}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('4 attendees') | ||
}) | ||
|
||
test('pluralize people', () => { | ||
const template = Handlebars.compile('{{pluralize attendees "person" "people"}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('4 people') | ||
}) | ||
|
||
// equality | ||
test('eq is true', () => { | ||
const template = Handlebars.compile('{{#if (eq foo fu)}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('yes') | ||
}) | ||
|
||
test('eq is false', () => { | ||
const template = Handlebars.compile('{{#if (eq foo "foo")}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('no') | ||
}) | ||
|
||
test('neq is true', () => { | ||
const template = Handlebars.compile('{{#if (neq foo fu)}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('no') | ||
}) | ||
|
||
test('neq is false', () => { | ||
const template = Handlebars.compile('{{#if (neq foo "foo")}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('yes') | ||
}) | ||
|
||
// boolean operators | ||
test('not false', () => { | ||
const template = Handlebars.compile('{{#if (not f)}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('yes') | ||
}) | ||
|
||
test('not true', () => { | ||
const template = Handlebars.compile('{{#if (not t)}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('no') | ||
}) | ||
|
||
test('not false and (true or false)', () => { | ||
const template = Handlebars.compile('{{#if (and (not f) (or t f))}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('yes') | ||
}) | ||
|
||
test('or is true', () => { | ||
const template = Handlebars.compile('{{#if (or t f)}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('yes') | ||
}) | ||
|
||
test('and is false', () => { | ||
const template = Handlebars.compile('{{#if (and t f)}}yes{{else}}no{{/if}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('no') | ||
}) | ||
|
||
// conditionals | ||
test('#ifeq is true', () => { | ||
const template = Handlebars.compile('{{#ifeq foo fu}}yes{{else}}no{{/ifeq}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('yes') | ||
}) | ||
|
||
test('#ifeq is false', () => { | ||
const template = Handlebars.compile('{{#ifeq foo "foo"}}yes{{else}}no{{/ifeq}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('no') | ||
}) | ||
|
||
test('#ifneq is true', () => { | ||
const template = Handlebars.compile('{{#ifneq foo fu}}yes{{else}}no{{/ifneq}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('no') | ||
}) | ||
|
||
test('#ifneq is false', () => { | ||
const template = Handlebars.compile('{{#ifneq foo "foo"}}yes{{else}}no{{/ifneq}}') | ||
const text = template(data) | ||
expect(text).toStrictEqual('yes') | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Handlebars from 'handlebars' | ||
|
||
// utilities | ||
Handlebars.registerHelper('json', value => new Handlebars.SafeString(JSON.stringify(value))) | ||
|
||
Handlebars.registerHelper('truncate', (text, size) => text.substring(0, size)) | ||
|
||
Handlebars.registerHelper('default', (want, fallback) => want ?? fallback) | ||
|
||
Handlebars.registerHelper('pluralize', (items, ...args) => { | ||
const count = items.length | ||
const singular = args.length === 1 ? 'item' : args[0] | ||
const plural = args.length === 3 ? args[1] : `${singular}s` | ||
|
||
if (count === 0) return `no ${plural}` | ||
if (count === 1) return `1 ${singular}` | ||
return `${count} ${plural}` | ||
}) | ||
|
||
// equality | ||
Handlebars.registerHelper('eq', (a, b) => a === b) | ||
|
||
Handlebars.registerHelper('neq', (a, b) => a !== b) | ||
|
||
// logical operators | ||
Handlebars.registerHelper('not', a => !a) | ||
|
||
Handlebars.registerHelper('and', (a, b) => a && b) | ||
|
||
Handlebars.registerHelper('or', (a, b) => a || b) | ||
|
||
// conditionals | ||
Handlebars.registerHelper('ifeq', (a, b, opts) => { | ||
return a === b ? opts.fn(this) : opts.inverse(this) // eslint-disable-line no-invalid-this | ||
}) | ||
|
||
Handlebars.registerHelper('ifneq', (a, b, opts) => { | ||
return a !== b ? opts.fn(this) : opts.inverse(this) // eslint-disable-line no-invalid-this | ||
}) | ||
|
||
export default Handlebars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters