Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add eq and ne helpers #16

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading