Skip to content

Commit

Permalink
Improve RegExp comparisons (#76)
Browse files Browse the repository at this point in the history
* use `flags` property when available (all modern environments)

* I do not actually need to move it to the top of the file
  • Loading branch information
planttheidea authored Apr 29, 2022
1 parent 648b235 commit d3085e0
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { EqualityComparator, InternalEqualityComparator } from './types';

const { keys } = Object;
interface Cache {
add: (value: any) => void;
has: (value: any) => boolean;
}

const HAS_WEAKSET_SUPPORT = typeof WeakSet === 'function';

type Cache = {
add: (value: any) => void;
has: (value: any) => boolean;
};
const { keys } = Object;

/**
* are the values passed strictly equal or both NaN
Expand Down Expand Up @@ -282,17 +282,25 @@ export function areObjectsEqual(
* @param b the regExp to test agains
* @returns are the regExps equal
*/
export function areRegExpsEqual(a: RegExp, b: RegExp) {
return (
a.source === b.source &&
a.global === b.global &&
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline &&
a.unicode === b.unicode &&
a.sticky === b.sticky &&
a.lastIndex === b.lastIndex
);
}
export const areRegExpsEqual = (() => {
if (/foo/g.flags === 'g') {
return function areRegExpsEqual(a: RegExp, b: RegExp) {
return a.source === b.source && a.flags === b.flags;
};
}

return function areRegExpsEqualFallback(a: RegExp, b: RegExp) {
return (
a.source === b.source &&
a.global === b.global &&
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline &&
a.unicode === b.unicode &&
a.sticky === b.sticky &&
a.lastIndex === b.lastIndex
);
};
})();

/**
* are the sets equal in value
Expand Down

0 comments on commit d3085e0

Please sign in to comment.