Skip to content

Commit

Permalink
Core: Improve regular expression comparisons
Browse files Browse the repository at this point in the history
Use ES2015 `flags` when available.

Fixes gh-909
Closes gh-916
  • Loading branch information
mixed authored and gibson042 committed Jan 10, 2016
1 parent eb27714 commit 7c94f6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/equiv.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ QUnit.equiv = (function() {
return false;
}

function getRegExpFlags( regexp ) {
return "flags" in regexp ? regexp.flags : regexp.toString().match( /[gimuy]*$/ )[ 0 ];
}

var callbacks = {
"string": useStrictEquality,
"boolean": useStrictEquality,
Expand All @@ -76,10 +80,7 @@ QUnit.equiv = (function() {
return a.source === b.source &&

// Include flags in the comparison
a.global === b.global &&
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline &&
a.sticky === b.sticky;
getRegExpFlags( a ) === getRegExpFlags( b );
},

// - skip when the property is a method of an instance (OOP)
Expand Down
16 changes: 15 additions & 1 deletion test/main/deepEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ QUnit.test( "RegExp", function( assert ) {
// typeof a === "function"; // Oops, false in IE and Opera, true in FF and Safari ("object")

// Tests same regex with same modifiers in different order
var regex1, regex2, regex3, r3a, r3b,
var regex1, regex2, regex3, r3a, r3b, ru1, ru2,
r1 = /foo/,
r2 = /foo/gim,
r3 = /foo/gmi,
Expand Down Expand Up @@ -484,6 +484,20 @@ QUnit.test( "RegExp", function( assert ) {
assert.equal( QUnit.equiv( rm1, rg1 ), false, "Modifier" );
assert.equal( QUnit.equiv( rg1, rg2 ), true, "Modifier" );

// Compare unicode modifier
try {
r2 = new RegExp( "foo", "umig" );
r3 = new RegExp( "foo", "mgiu" );
assert.equal( QUnit.equiv( r2, r3 ), true, "Modifier order" );
assert.equal( QUnit.equiv( r1, r2 ), false, "Modifier" );

ru1 = new RegExp( "/u{1D306}", "u" );
ru2 = new RegExp( "/u{1D306}", "u" );
assert.equal( QUnit.equiv( ru1, rg1 ), false, "Modifier" );
assert.equal( QUnit.equiv( rg1, ru1 ), false, "Modifier" );
assert.equal( QUnit.equiv( ru1, ru2 ), true, "Modifier" );
} catch ( e ) {}

// Different regex, same modifiers
r1 = /[a-z]/gi;
r2 = /[0-9]/gi; // oops! different
Expand Down

0 comments on commit 7c94f6f

Please sign in to comment.