Skip to content

Commit

Permalink
fix: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Janvier committed Sep 3, 2020
1 parent d55e76e commit 4269867
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/cookiePersister.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/inMemoryPersister.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CookiePersister } from './cookiePersister';
import { InMemoryPersister } from './inMemoryPersister';
import {
config,
create,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export function config(userConfig: Partial<Config> = {}) {
_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);
}
}

Expand Down
41 changes: 0 additions & 41 deletions src/userSessionPersister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4269867

Please sign in to comment.