Skip to content

Commit

Permalink
Merge pull request #28 from andreasonny83/20--cookie-law-service-rege…
Browse files Browse the repository at this point in the history
…x-patch

fix(angular2-cookie-law.service): 🐛 regular expression
  • Loading branch information
andreasonny83 authored Jun 16, 2018
2 parents 089924b + 51acec7 commit b9df8f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions projects/angular2-cookie-law/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ If set to a valid absolute or relative URL, it will render an extra 'learn more'
### awsomeCloseIcon
Font Awsome is required in your header for this feature to work.
[Font Awesome](https://fontawesome.com/) is required in your header for this feature to work.
```html
<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>
Expand All @@ -176,7 +176,7 @@ Font Awsome is required in your header for this feature to work.
| --- | --- |
| string | null |
If set to a Font awsome Icon e.g. "fa-window-close" it will replace the standard SVG with the Font awsome Icon.
If set to a Font awesome Icon e.g. "fa-window-close" it will replace the standard SVG with the Font awesome Icon.
###### Example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Angular2CookieLawService', () => {
});

document.cookie = 'cookieLawSeen=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

service = TestBed.get(Angular2CookieLawService);
});

Expand All @@ -34,7 +35,23 @@ describe('Angular2CookieLawService', () => {

expect(service.seen()).toBe(true);
expect(service.seen('cookieLawSeen')).toBe(true);
expect(document.cookie.match('cookieLawSeen').indexOf('cookieLawSeen')).not.toBe(-1);
expect(document.cookie.match('cookieLawSeen')
.indexOf('cookieLawSeen')).not.toBe(-1);
});

it('seen should return the current stored cookies matching the name passed ' +
'in the argument', () => {
document.cookie = ' myCookie=true; ';
document.cookie = ' yourCookie=true ; ';

expect(service.seen('myCookie')).toEqual(true);
expect(service.seen('yourCookie')).toEqual(true);

document.cookie = 'myCookie=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = 'yourCookie=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

expect(service.seen('myCookie')).toEqual(false);
expect(service.seen('yourCookie')).toEqual(false);
});

it('should stored different cookie names', () => {
Expand Down
33 changes: 12 additions & 21 deletions projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,30 @@ import { Injectable } from '@angular/core';
providedIn: 'root'
})
export class Angular2CookieLawService {

public seen(cookieName: string = 'cookieLawSeen'): boolean {
return this.cookieExisits(cookieName);
const cookies: Array<string> = document.cookie.split(';');

return this.cookieExisits(cookieName, cookies);
}

public storeCookie(cookieName: string, expiration?: number): void {
return this.setCookie(cookieName, expiration);
}

private cookieExisits(name: string): boolean {
const ca: Array<string> = document.cookie.split(';');
const caLen: number = ca.length;
const cookieName = name + '=';
let c: string;
private cookieExisits(name: string, cookies: Array<string>): boolean {
const cookieName = `${name}=`;

for (let i = 0; i < caLen; i += 1) {
c = ca[i].replace(/^\s\+/g, '');
if (c.indexOf(cookieName) !== -1) {
return true;
}
}

return false;
return cookies.reduce((prev, curr) =>
prev || curr.trim().search(cookieName) > -1, false);
}

private setCookie(name: string, expiration?: number): void {
const date = new Date();
let expires;
const now: Date = new Date();
const exp: Date = new Date(now.getTime() + expiration * 86400000);

date.setTime(date.getTime() + expiration * 86400000);
expires = '; expires=' + date.toUTCString();
const cookieString = encodeURIComponent(name) +
`=true;path=/;expires=${exp.toUTCString()};`;

document.cookie = encodeURIComponent(name) + '=true; path=/' + expires;
document.cookie = cookieString;
}

}

0 comments on commit b9df8f2

Please sign in to comment.