diff --git a/src/config.ts b/src/config.ts index 8c24f67..a98c98d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,7 +1,8 @@ import { Condition } from './condition'; +import { CookiePersister } from './cookiePersister'; import { removeAbTestParameter } from './query'; import { ConsoleTracking, Tracking } from './tracking'; -import { CookiePersister, UserSessionPersister } from './userSessionPersister'; +import { UserSessionPersister } from './userSessionPersister'; export interface Config { cookieName: string; diff --git a/src/cookiePersister.ts b/src/cookiePersister.ts new file mode 100644 index 0000000..8ff6587 --- /dev/null +++ b/src/cookiePersister.ts @@ -0,0 +1,33 @@ +import config from './config'; +import { UserSessionPersister } from './userSessionPersister'; + +export class CookiePersister implements UserSessionPersister { + private static createCookie(name: string, value: string, days: number): void { + let expires = ''; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = '; expires=' + date.toUTCString(); + } + document.cookie = name + '=' + value + expires + '; path=/'; + } + private static readCookie(name: string): string | null { + const nameEq = name + '='; + const ca = document.cookie.split(';'); + for (let c of ca) { + while (c.charAt(0) === ' ') { + c = c.substring(1, c.length); + } + if (c.indexOf(nameEq) === 0) { + return c.substring(nameEq.length, c.length); + } + } + return null; + } + public loadUserSession() { + return CookiePersister.readCookie(config.cookieName); + } + public saveUserSession(userSession: string, daysToLive: number) { + CookiePersister.createCookie(config.cookieName, userSession, daysToLive); + } +} diff --git a/src/inMemoryPersister.ts b/src/inMemoryPersister.ts new file mode 100644 index 0000000..b0a388d --- /dev/null +++ b/src/inMemoryPersister.ts @@ -0,0 +1,13 @@ +import { UserSessionPersister } from './userSessionPersister'; + +export class InMemoryPersister implements UserSessionPersister { + private endOfLife: number = 0; + private storage: string = ''; + public loadUserSession() { + return !this.endOfLife || (Date.now() > this.endOfLife) ? '' : this.storage; + } + public saveUserSession(userSession: string, daysToLive: number) { + this.storage = userSession; + this.endOfLife = Date.now() + (daysToLive * 24 * 60 * 60 * 1000); + } +} diff --git a/src/index.ts b/src/index.ts index 0e8ceac..5f36f41 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +import { CookiePersister } from './cookiePersister'; +import { InMemoryPersister } from './inMemoryPersister'; import { config, create, @@ -12,7 +14,7 @@ import { } from './main'; import { SplitTest } from './splitTest'; import { uiFactory } from './ui'; -import { UserSessionPersister, InMemoryPersister, CookiePersister } from './userSessionPersister'; +import { UserSessionPersister } from './userSessionPersister'; const ui = uiFactory( testsObservable, diff --git a/src/main.ts b/src/main.ts index 0c44bd0..aeec0c6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,9 +31,9 @@ export function config(userConfig: Partial = {}) { _config.onVariationChange = userConfig.onVariationChange; } if (userConfig.sessionPersister) { - const userSession = _config.sessionPersister.loadUserSession() || ''; + const session = _config.sessionPersister.loadUserSession() || ''; _config.sessionPersister = userConfig.sessionPersister; - _config.sessionPersister.saveUserSession(userSession, _config.userSessionDaysToLive); + _config.sessionPersister.saveUserSession(session, _config.userSessionDaysToLive); } } diff --git a/src/userSessionPersister.ts b/src/userSessionPersister.ts index 679503a..bbdf19b 100644 --- a/src/userSessionPersister.ts +++ b/src/userSessionPersister.ts @@ -3,44 +3,3 @@ export interface UserSessionPersister { loadUserSession(): string | null; saveUserSession(userSession: string, daysToLive: number): void; } -export class CookiePersister implements UserSessionPersister { - public loadUserSession() { - return CookiePersister.readCookie(config.cookieName); - } - public saveUserSession(userSession: string, daysToLive: number) { - CookiePersister.createCookie(config.cookieName, userSession, daysToLive); - } - private static createCookie(name: string, value: string, days: number): void { - let expires = ''; - if (days) { - const date = new Date(); - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - expires = '; expires=' + date.toUTCString(); - } - document.cookie = name + '=' + value + expires + '; path=/'; - } - private static readCookie(name: string): string | null { - const nameEq = name + '='; - const ca = document.cookie.split(';'); - for (let c of ca) { - while (c.charAt(0) === ' ') { - c = c.substring(1, c.length); - } - if (c.indexOf(nameEq) === 0) { - return c.substring(nameEq.length, c.length); - } - } - return null; - } -} -export class InMemoryPersister implements UserSessionPersister { - private endOfLife: number = 0; - private storage: string = ''; - public loadUserSession() { - return !this.endOfLife || (Date.now() > this.endOfLife) ? '' : this.storage; - } - public saveUserSession(userSession: string, daysToLive: number) { - this.storage = userSession; - this.endOfLife = Date.now() + (daysToLive*24*60*60*1000); - } -} \ No newline at end of file