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
Open
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
30 changes: 17 additions & 13 deletions src/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ export class CookieService {
* @returns existence of the cookie
*/
public check(name: string): boolean {
if (typeof document === "undefined") return false; // Check if document exist avoiding issues on server side prerendering
name = encodeURIComponent(name);
let regexp = new RegExp('(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)', 'g');
let exists = regexp.test(document.cookie);
return exists;
// Check if document exist avoiding issues on server side prerendering
if (typeof document === "undefined") return false;
return this.cookieHandler(name, 'test');
}

/**
Expand All @@ -26,13 +24,9 @@ export class CookieService {
*/
public get(name: string): string {
if (this.check(name)) {
name = encodeURIComponent(name);
let regexp = new RegExp('(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)', 'g');
let result = regexp.exec(document.cookie);
return decodeURIComponent(result[1]);
} else {
return '';
return decodeURIComponent(this.cookieHandler(name, 'exec')[1]);
}
return '';
}

/**
Expand Down Expand Up @@ -88,7 +82,6 @@ export class CookieService {
cookieStr += 'secure;';
}

// console.log(cookieStr);
document.cookie = cookieStr;
}

Expand All @@ -112,9 +105,20 @@ export class CookieService {
for (let cookieName of Object.keys(cookies)) {
this.delete(cookieName, path, domain);
}

}

/**
* @private
* Cookie Handler - Reusable Method
*
* @param {string} name Cookie's identification
* @param {string} methodName Method to be called
*/
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.

}
}

export const Cookie = new CookieService();