Skip to content

Commit

Permalink
feat: add eq and ne helpers (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjuanes authored Jul 13, 2024
1 parent 7c0d0a1 commit 9d37fd3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ const data = {
console.log(m("{{#unless isAdmin}}Hello guest{{/unless}}", data)); // --> 'Hello guest'
```

#### eq

> Added in `v0.9.0`.
The `eq` helper renders the blocks only if the two values provided as argument are equal. Example:

```javascript
console.log(m(`{{#eq name "bob"}}Hello bob{{/eq}}`, {name: "bob"})); // --> 'Hello bob'
```

#### ne

> Added in `v0.9.0`.
The `ne` helper renders the block only if the two values provided as argument are not equal. Example:

```javascript
console.log(m(`{{#ne name "bob"}}Not bob{{/ne}}`, {name: "John"})); // --> 'Not bob'
```

### Custom Helpers

> Added in `v0.5.0`.
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const defaultHelpers = {
},
"if": (value, opt) => !!value ? opt.fn(opt.context) : "",
"unless": (value, opt) => !!!value ? opt.fn(opt.context) : "",
"eq": (a, b, opt) => a === b ? opt.fn(opt.context) : "",
"ne": (a, b, opt) => a !== b ? opt.fn(opt.context) : "",
};

const compile = (tokens, output, context, partials, helpers, vars, fn = {}, index = 0, section = "") => {
Expand Down
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ describe("{{#unless }}", () => {
});
});

describe("{{#eq }}", () => {
it("should render block if two values are equal", () => {
assert.equal(m(`{{#eq value "a"}}yes!{{/eq}}`, {value: "a"}), "yes!");
assert.equal(m(`{{#eq value true}}yes!{{/eq}}`, {value: true}), "yes!");
assert.equal(m(`{{#eq value 0}}yes!{{/eq}}`, {value: 0}), "yes!");
});

it("should not render block if two values are not equal", () => {
assert.equal(m(`{{#eq value "a"}}yes!{{/eq}}`, {value: "b"}), "");
assert.equal(m(`{{#eq value true}}yes!{{/eq}}`, {value: false}), "");
assert.equal(m(`{{#eq value 0}}yes!{{/eq}}`, {value: 1}), "");
});
});

describe("{{#ne }}", () => {
it("should not render block if two values are equal", () => {
assert.equal(m(`{{#ne value "a"}}yes!{{/ne}}`, {value: "a"}), "");
assert.equal(m(`{{#ne value true}}yes!{{/ne}}`, {value: true}), "");
assert.equal(m(`{{#ne value 0}}yes!{{/ne}}`, {value: 0}), "");
});

it("should render block if two values are not equal", () => {
assert.equal(m(`{{#ne value "a"}}yes!{{/ne}}`, {value: "b"}), "yes!");
assert.equal(m(`{{#ne value true}}yes!{{/ne}}`, {value: false}), "yes!");
assert.equal(m(`{{#ne value 0}}yes!{{/ne}}`, {value: 1}), "yes!");
});
});

describe("{{#customHelper }}", () => {
it("should allow to execute a simple custom helper", () => {
const options = {
Expand Down

0 comments on commit 9d37fd3

Please sign in to comment.