hbl-comparison

0.1.0 • Public • Published

hbl-comparison

A helper for Handlebars that adds comparison functions. Meant to supercede helpers/handlebars-helpers' comparison helpers.

Usage

const Handlebars = require('handlebars');
require('hbl-comparison').default();

const tpl = Handlebars.compile('{{#lt 2 4}}Tiny number.{{else}}Big number.{{/lt}}');
console.log(tpl()); // result : 'Tiny number.' 

Package documentation

{{#and a b }}

If both a and b are true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

<!-- great: true, magnifice: true -->
{{#and great magnifice }}
A
{{else}}
B
{{/and}}

Result : A

{{#compare a op b }}

Compares a and b with an operator. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

The operator must be a string (enclose in strings). The conventional operators are supported : >, >=, ==, ===, !=, !==, <=, <. typeof is also supported, where a is checked to be the typeof b.

Usage :

{{#compare 2 != 2}}
A
{{else}}
B
{{/compare}}

Result : B

{{#contains array value }}

Checks if an array contains a value. If it does, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#contains ['a','b','c'] "d" }}
A
{{else}}
B
{{/contains}}

Result : B

{{ default value defaultValue }}

Checks if a value is undefined. If it is, the defaultValue is rendered — otherwise, the value is.

Usage : {{ default apple "Whoops, I forgot to define something" }}

Result : Whoops, I forgot to define something

{{#eq a b }}

Checks if a === b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#eq 2 2}}
A
{{else}}
B
{{/eq}}

Result : A

{{#gt a b }}

Checks if a > b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#gt 2 2}}
A
{{else}}
B
{{/gt}}

Result : B

{{#gte a b }}

Checks if a >= b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#gte 2 2}}
A
{{else}}
B
{{/gte}}

Result : A

{{#has "a b" }}

Checks if an array has a pattern. If it does, the block is rendered. Otherwise, the inverse is rendered.

Usage :

<!-- context: 'CCC' -->
{{#has context "C"}}
A
{{else}}
B
{{/has}}

Result : A

{{#ifEven num }}

Checks if num is even. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#ifEven 2 }}
A
{{else}}
B
{{/ifEven}}

Result : A

{{#ifNth a b }}

Checks if a is divisible by b evenly (no remainder). If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#ifNth 4 2 }}
A
{{else}}
B
{{/ifNth}}

Result : A

{{#ifOdd num }}

Checks if num is odd. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#ifOdd 2 }}
A
{{else}}
B
{{/ifOdd}}

Result : B

{{#is a b }}

Checks if a == b. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#is 2 3 }}
A
{{else}}
B
{{/is}}

Result : B

{{ isFalsey val }}

Checks if val is false. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage : {{ isFalsey false }}

Result : true

{{#isnt a b }}

Checks if a != b. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#isnt 2 3 }}
A
{{else}}
B
{{/isnt}}

Result : A

{{ isTruthy val }}

Checks if val is true. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage : {{ isTruthy false }}

Result : false

{{#lt a b }}

Checks if a < b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#lt 2 2}}
A
{{else}}
B
{{/lt}}

Result : B

{{#lte a b }}

Checks if a <= b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#lte 2 2}}
A
{{else}}
B
{{/lte}}

Result : A

{{#neither a b }}

Checks if a and b are false. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#neither false true }}
A
{{else}}
B
{{/neither}}

Result : B

{{ not val }}

Checks if val is false. If it is, the block is rendered. Otherwise, the inverse is rendered.

Usage : {{ not false }}

Result : true

{{#or a b }}

If either a or b are true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

<!-- great: true -->
{{#or great false }}
A
{{else}}
B
{{/or}}

Result : A

{{#unlessEq a b }}

Checks if a !== b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#unlessEq 2 2}}
A
{{else}}
B
{{/unlessEq}}

Result : B

{{#unlessGt a b }}

Checks if a <= b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#unlessGt 2 2}}
A
{{else}}
B
{{/unlessEq}}

Result : B

{{#unlessGteq a b }}

Checks if a < b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#unlessEq 2 2}}
A
{{else}}
B
{{/unlessEq}}

Result : A

{{#unlessLt a b }}

Checks if a >= b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#unlessEq 2 2}}
A
{{else}}
B
{{/unlessEq}}

Result : B

{{#unlessLteq a b }}

Checks if a > b. If the result is true, the block is rendered. Otherwise, the inverse is rendered.

Usage :

{{#unlessEq 2 2}}
A
{{else}}
B
{{/unlessEq}}

Result : A

Acknowledgements

This package is licensed under the 3-Clause BSD licence. A copy of it can be found in the LICENSE file in the root of the source repository and in the root of the package directory.

Readme

Keywords

none

Package Sidebar

Install

npm i hbl-comparison

Weekly Downloads

0

Version

0.1.0

License

BSD-3-Clause

Unpacked Size

14.1 kB

Total Files

5

Last publish

Collaborators

  • doamatto