-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It has lazy evaluation of the second argument, so it doesn't even get read if the first argument is already undefined. For now without an option to coerce strings to numbers.
- Loading branch information
Showing
3 changed files
with
177 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Arguments, CapturedArguments, VM } from '@glimmer/runtime'; | ||
import { InternalHelperReference } from '../utils/references'; | ||
|
||
/** | ||
@module ember | ||
*/ | ||
|
||
/** | ||
Compares the two given values with > | ||
Example: | ||
```handlebars | ||
{{gt age 17}} | ||
{{! be true if `age` is > 17}} | ||
``` | ||
@public | ||
@method gt | ||
@for Ember.Templates.helpers | ||
@since 2.7.0 | ||
*/ | ||
function gt({ positional: { references } }: CapturedArguments) { | ||
let left = references[0].value(); | ||
if (left === undefined || left === null) { | ||
return false; | ||
} | ||
let right = references[1].value(); | ||
if (right === undefined || right === null) { | ||
return false; | ||
} | ||
return left > right; | ||
} | ||
|
||
export default function(_vm: VM, args: Arguments) { | ||
return new InternalHelperReference(gt, args.capture()); | ||
} |
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
137 changes: 137 additions & 0 deletions
137
packages/@ember/-internals/glimmer/tests/integration/helpers/gt-test.js
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,137 @@ | ||
import { RenderingTest, moduleFor } from '../../utils/test-case'; | ||
import { set } from '@ember/-internals/metal'; | ||
import { Component } from '../../utils/helpers'; | ||
import { computed } from '@ember/-internals/metal'; | ||
|
||
moduleFor( | ||
'Helpers test: {{gt}}', | ||
class extends RenderingTest { | ||
['@test returns true when the first static argument is bigger than the second one']() { | ||
this.render(`{{gt 2 1}}`); | ||
this.assertText('true'); | ||
} | ||
|
||
['@test returns false when the first static argument is not bigger than the second one']() { | ||
this.render(`{{gt 0 1}}`); | ||
this.assertText('false'); | ||
} | ||
|
||
['@test returns false when both arguments are the same']() { | ||
this.render(`{{gt 1 1}}`); | ||
this.assertText('false'); | ||
} | ||
|
||
['@test it updates for bound arguments']() { | ||
this.render(`{{gt left right}}`, { left: 1, right: 2 }); | ||
|
||
this.assertText('false'); | ||
|
||
this.runTask(() => this.rerender()); | ||
|
||
this.assertText('false'); | ||
|
||
this.runTask(() => set(this.context, 'left', 3)); | ||
|
||
this.assertText('true'); | ||
|
||
this.runTask(() => set(this.context, 'right', 4)); | ||
|
||
this.assertText('false'); | ||
|
||
this.runTask(() => set(this.context, 'left', 5)); | ||
|
||
this.assertText('true'); | ||
|
||
this.runTask(() => set(this.context, 'right', 5)); | ||
|
||
this.assertText('false'); | ||
} | ||
|
||
['@test it can be used as a sub-expression']() { | ||
this.render(`{{if (gt left right) "yes" "no"}}`, { left: 1, right: 2 }); | ||
|
||
this.assertText('no'); | ||
|
||
this.runTask(() => this.rerender()); | ||
|
||
this.assertText('no'); | ||
|
||
this.runTask(() => set(this.context, 'left', 3)); | ||
|
||
this.assertText('yes'); | ||
|
||
this.runTask(() => set(this.context, 'right', 4)); | ||
|
||
this.assertText('no'); | ||
|
||
this.runTask(() => { | ||
set(this.context, 'left', 5); | ||
set(this.context, 'right', 0); | ||
}); | ||
|
||
this.assertText('yes'); | ||
} | ||
|
||
['@test of the first argument is undefined, it never pulls the second argument']() { | ||
let didInvokeLeft = false; | ||
let didInvokeRight = false; | ||
let FooBarComponent = Component.extend({ | ||
left: computed(function() { | ||
didInvokeLeft = true; | ||
return undefined; | ||
}), | ||
right: computed(function() { | ||
didInvokeRight = true; | ||
return 5; | ||
}), | ||
}); | ||
|
||
this.registerComponent('foo-bar', { | ||
ComponentClass: FooBarComponent, | ||
template: `{{gt left right}}`, | ||
}); | ||
|
||
this.render(`{{foo-bar}}`, {}); | ||
|
||
this.assertText('false'); | ||
|
||
this.runTask(() => this.rerender()); | ||
|
||
this.assertText('false'); | ||
|
||
this.assert.ok(didInvokeLeft, 'the `left` property was accessed'); | ||
this.assert.notOk(didInvokeRight, "the `right` property wasn't accessed"); | ||
} | ||
|
||
['@test of the first argument is null, it never pulls the second argument']() { | ||
let didInvokeLeft = false; | ||
let didInvokeRight = false; | ||
let FooBarComponent = Component.extend({ | ||
left: computed(function() { | ||
didInvokeLeft = true; | ||
return null; | ||
}), | ||
right: computed(function() { | ||
didInvokeRight = true; | ||
return 5; | ||
}), | ||
}); | ||
|
||
this.registerComponent('foo-bar', { | ||
ComponentClass: FooBarComponent, | ||
template: `{{gt left right}}`, | ||
}); | ||
|
||
this.render(`{{foo-bar}}`, {}); | ||
|
||
this.assertText('false'); | ||
|
||
this.runTask(() => this.rerender()); | ||
|
||
this.assertText('false'); | ||
|
||
this.assert.ok(didInvokeLeft, 'the `left` property was accessed'); | ||
this.assert.notOk(didInvokeRight, "the `right` property wasn't accessed"); | ||
} | ||
} | ||
); |