From 51acec71c3794a449bcc079abdb00014ffa27394 Mon Sep 17 00:00:00 2001 From: Andrea Sonny Date: Sat, 16 Jun 2018 13:37:13 +0100 Subject: [PATCH] fix(angular2-cookie-law.service): :bug: regular expression Closes #20 --- projects/angular2-cookie-law/README.md | 4 +-- .../lib/angular2-cookie-law.service.spec.ts | 19 ++++++++++- .../src/lib/angular2-cookie-law.service.ts | 33 +++++++------------ 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/projects/angular2-cookie-law/README.md b/projects/angular2-cookie-law/README.md index 42c970c..2497c3b 100644 --- a/projects/angular2-cookie-law/README.md +++ b/projects/angular2-cookie-law/README.md @@ -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 @@ -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 diff --git a/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.spec.ts b/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.spec.ts index c33b742..c1942d8 100644 --- a/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.spec.ts +++ b/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.spec.ts @@ -11,6 +11,7 @@ describe('Angular2CookieLawService', () => { }); document.cookie = 'cookieLawSeen=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; + service = TestBed.get(Angular2CookieLawService); }); @@ -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', () => { diff --git a/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.ts b/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.ts index 5279693..31b888a 100644 --- a/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.ts +++ b/projects/angular2-cookie-law/src/lib/angular2-cookie-law.service.ts @@ -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 = 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 = document.cookie.split(';'); - const caLen: number = ca.length; - const cookieName = name + '='; - let c: string; + private cookieExisits(name: string, cookies: Array): 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; } - }