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

refactor(*): Removed duplicate code. #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Mohammed9531
Copy link

No description provided.

@Mohammed9531
Copy link
Author

@carcamano @Bigous Can you please review & merge this PR?

private cookieHandler(name: string, methodName: string): any {
name = encodeURIComponent(name);
return (new RegExp('(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)', 'g') as any)
[methodName](document.cookie);
Copy link
Member

Choose a reason for hiding this comment

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

What's this line for?

Copy link
Author

Choose a reason for hiding this comment

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

I've created a reusable method, the line above is a substitution for the code below:

  1. check method:
    let regexp = new RegExp('(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)', 'g'); let exists = regexp.test(document.cookie);

  2. get method:
    let regexp = new RegExp('(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)', 'g'); let result = regexp.exec(document.cookie);

As you can see from the code above, both has the same regex pattern but calling different methods test & exec. I merged both patterns into a common method by passing the method name as an argument [methodName].

Let me know if you have any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants