Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RegExp comparisons #76

Merged
merged 2 commits into from
Apr 29, 2022
Merged
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
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;

Choose a reason for hiding this comment

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

Could the flags be in a different order? 'gm' vs 'mg'

Copy link
Owner Author

Choose a reason for hiding this comment

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

Based on the description in MDN, which is based on the spec, the flags are ordered alphabetically. Are you seeing different results?

Choose a reason for hiding this comment

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

Nope, I was just wondering. Sorry for the noise

Copy link
Owner Author

Choose a reason for hiding this comment

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

No worries!

};
}

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