Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($parse): handle Infinity #9504

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ forEach({
'null': function() { return null; },
'true': function() { return true; },
'false': function() { return false; },
'undefined': function() {}
'undefined': function() {},
'Infinity': function() { return Infinity; },
'NaN': function() { return NaN; }
}, function(constantGetter, name) {
constantGetter.constant = constantGetter.literal = constantGetter.sharedGetter = true;
CONSTANTS[name] = constantGetter;
Expand Down
32 changes: 32 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,38 @@ describe('parser', function() {
expect(scope.a).not.toBeDefined();
});

it('should evaluate Infinity', function() {
expect(scope.$eval("Infinity")).toBe(Infinity);
expect(scope.$eval("a=Infinity")).toBe(Infinity);
expect(scope.a).toBe(Infinity);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test for -Infinity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

});

it('should evaluate -Infinity', function() {
expect(scope.$eval("-Infinity")).toBe(-Infinity);
expect(scope.$eval("a=-Infinity")).toBe(-Infinity);
expect(scope.a).toBe(-Infinity);
});

it('should dereference object properties named "Infinity"', function() {
scope.obj = { 'Infinity': 42 };
expect(scope.$eval("obj.Infinity")).toBe(42);
expect(scope.$eval("obj.Infinity = 43")).toBe(43);
expect(scope.obj.Infinity).toBe(43);
});

it('should evaluate NaN', function() {
expect(scope.$eval("NaN")).toBeNaN();
expect(scope.$eval("a=NaN")).toBeNaN();
expect(scope.a).toBeNaN();
});

it('should dereference object properties named "NaN"', function() {
scope.obj = { 'NaN': 42 };
expect(scope.$eval("obj.NaN")).toBe(42);
expect(scope.$eval("obj.NaN = 43")).toBe(43);
expect(scope.obj.NaN).toBe(43);
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last thing, can we add a test that

$eval('Infinity = x'); will actually assign $scope.Infinity rather than global Infinity? (and maybe, if $scope.Infinity is defined, it should shadow window.Infinity? --- maybe that's more complicated than people want)

it('should allow assignment after array dereference', function() {
scope.obj = [{}];
scope.$eval('obj[0].name=1');
Expand Down