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

feat(angular.isRegExp): check object type for regular expressions #2729

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
18 changes: 17 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function nextUid() {

/**
* Set or clear the hashkey for an object.
* @param obj object
* @param obj object
* @param h the hashkey (!truthy to delete the hashkey)
*/
function setHashKey(obj, h) {
Expand Down Expand Up @@ -395,6 +395,22 @@ function isDate(value){
}


/**
* @ngdoc function
* @name angular.isRegExp
* @function
*
* @description
* Determines if a value is a regular expression object.
*
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is a `RegExp`.
*/
function isRegExp(value) {
return toString.apply(value) == '[object RegExp]';
}


/**
* @ngdoc function
* @name angular.isArray
Expand Down
1 change: 1 addition & 0 deletions src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function publishExternalAPI(angular){
'isArray': isArray,
'version': version,
'isDate': isDate,
'isRegExp': isRegExp,
'lowercase': lowercase,
'uppercase': uppercase,
'callbacks': {counter: 0},
Expand Down
17 changes: 17 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,23 @@ describe('angular', function() {
});
});


describe('isRegExp', function() {
it('should return true for RegExp object', function() {
expect(isRegExp(/^foobar$/)).toBe(true);
expect(isRegExp(new RegExp('^foobar$/'))).toBe(true);
});

it('should return false for non RegExp objects', function() {
expect(isRegExp([])).toBe(false);
expect(isRegExp('')).toBe(false);
expect(isRegExp(23)).toBe(false);
expect(isRegExp({})).toBe(false);
expect(isRegExp(new Date())).toBe(false);
});
});


describe('compile', function() {
it('should link to existing node and create scope', inject(function($rootScope, $compile) {
var template = angular.element('<div>{{greeting = "hello world"}}</div>');
Expand Down