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

If helper #138

Merged
merged 1 commit into from Aug 14, 2018
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
8 changes: 8 additions & 0 deletions helpers/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ function helper(paper) {
result = (lvalue >= rvalue);
break;

case 'gtnum':
if ((typeof lvalue === 'string') && (typeof(rvalue) === 'string') && (!isNaN(lvalue)) && (!isNaN(rvalue))) {
result = (parseInt(lvalue) > parseInt(rvalue));
break;
} else {
throw new Error("Handlerbars Helper if gtnum accepts ONLY valid number string");
}

case 'typeof':
result = (typeof lvalue === rvalue);
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bigcommerce/stencil-paper",
"version": "2.0.8",
"version": "2.0.9",
"description": "A stencil plugin to register partials and helpers from handlebars and returns the compiled version for the stencil platform.",
"main": "index.js",
"author": "Bigcommerce",
Expand Down
49 changes: 49 additions & 0 deletions test/helpers/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ describe('if helper', () => {
expect(c('{{#if 0}}big{{else}}small{{/if}}', context))
.to.be.equal('small');

expect(c('{{#if "2" "gtnum" "1"}}big{{/if}}', context))
.to.be.equal('big');

expect(c('{{#if num2}}big{{else}}small{{/if}}', context))
.to.be.equal('big');

Expand Down Expand Up @@ -75,6 +78,9 @@ describe('if helper', () => {
expect(c('{{#if num1 "<" num2}}big{{/if}}', context))
.to.be.equal('big');

expect(c('{{#if "2" "gtnum" "1"}}big{{/if}}', context))
.to.be.equal('big');

expect(c('{{#if num2 ">=" num1}}big{{/if}}', context))
.to.be.equal('big');

Expand Down Expand Up @@ -116,6 +122,12 @@ describe('if helper', () => {
expect(c('{{#if num2 "<=" num1}}big{{/if}}', context))
.to.be.equal('');

expect(c('{{#if "1" "gtnum" "2"}}big{{/if}}', context))
.to.be.equal('');

expect(c('{{#if "1" "gtnum" "1"}}big{{/if}}', context))
.to.be.equal('');

expect(c('{{#if product "typeof" "string"}}big{{/if}}', context))
.to.be.equal('');

Expand Down Expand Up @@ -229,6 +241,43 @@ describe('if helper', () => {

done();
});

it('should throw an exeption when non string value sent to gtnum', function (done) {
try {
c('{{#if num1 "gtnum" "2"}}big{{/if}}');
} catch(e) {
expect(e.message).to.equal('Handlerbars Helper if gtnum accepts ONLY valid number string');
}
try {
c('{{#if "2" "gtnum" num2}}big{{/if}}');
} catch(e) {
expect(e.message).to.equal('Handlerbars Helper if gtnum accepts ONLY valid number string');
}
try {
c('{{#if num1 "gtnum" num2}}big{{/if}}');
} catch(e) {
expect(e.message).to.equal('Handlerbars Helper if gtnum accepts ONLY valid number string');
}
done();
});
it('should throw an exeption when NaN value sent to gtnum', function (done) {
try {
c('{{#if "aaaa" "gtnum" "2"}}big{{/if}}');
} catch(e) {
expect(e.message).to.equal('Handlerbars Helper if gtnum accepts ONLY valid number string');
}
try {
c('{{#if "2" "gtnum" "bbbb"}}big{{/if}}');
} catch(e) {
expect(e.message).to.equal('Handlerbars Helper if gtnum accepts ONLY valid number string');
}
try {
c('{{#if "aaaa" "gtnum" "bbbb"}}big{{/if}}');
} catch(e) {
expect(e.message).to.equal('Handlerbars Helper if gtnum accepts ONLY valid number string');
}
done();
});
});

describe('unless helper', () => {
Expand Down