Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions src/vault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from '@mparticle/web-sdk';
import { isEmpty } from './utils';
import { isEmpty, isNumber } from './utils';

export interface IVaultOptions {
logger?: Logger;
Expand Down Expand Up @@ -42,9 +42,14 @@ export abstract class BaseVault<StorableItem> {
* @param item {StorableItem}
*/
public store(item: StorableItem): void {
let stringifiedItem: string;
this.contents = item;

const stringifiedItem = !isEmpty(item) ? JSON.stringify(item) : '';
if (isNumber(item)) {
stringifiedItem = JSON.stringify(item);
} else {
stringifiedItem = !isEmpty(item) ? JSON.stringify(item) : '';
}

try {
this.storageObject.setItem(this._storageKey, stringifiedItem);
Expand Down Expand Up @@ -97,4 +102,4 @@ export class SessionStorageVault<StorableItem> extends BaseVault<StorableItem> {
constructor(storageKey: string, options?: IVaultOptions) {
super(storageKey, window.sessionStorage, options);
}
}
}
63 changes: 63 additions & 0 deletions test/src/vault.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const testArray: Dictionary<string>[] = [
{ narf: 'poit' },
];

const testString: string = 'foo';
const testNumber: number = 123;

describe('Vault', () => {
describe('SessionStorageVault', () => {
afterEach(() => {
Expand Down Expand Up @@ -49,6 +52,36 @@ describe('Vault', () => {
JSON.stringify(testArray)
);
});

it('should store a string', () => {
const storageKey = 'test-key-store-array';

const vault = new SessionStorageVault<string>(
storageKey
);

vault.store(testString);

expect(vault.contents).to.equal(testString);
expect(window.sessionStorage.getItem(storageKey)).to.equal(
JSON.stringify(testString)
);
});

it('should store a number', () => {
const storageKey = 'test-key-store-array';

const vault = new SessionStorageVault<number>(
storageKey
);

vault.store(testNumber);

expect(vault.contents).to.equal(testNumber);
expect(window.sessionStorage.getItem(storageKey)).to.equal(
JSON.stringify(testNumber)
);
});
});

describe('#retrieve', () => {
Expand Down Expand Up @@ -168,6 +201,36 @@ describe('Vault', () => {
JSON.stringify(testArray)
);
});

it('should store a string', () => {
const storageKey = 'test-key-store-array';

const vault = new LocalStorageVault<string>(
storageKey
);

vault.store(testString);

expect(vault.contents).to.equal(testString);
expect(window.localStorage.getItem(storageKey)).to.equal(
JSON.stringify(testString)
);
});

it('should store a number', () => {
const storageKey = 'test-key-store-array';

const vault = new LocalStorageVault<number>(
storageKey
);

vault.store(testNumber);

expect(vault.contents).to.equal(testNumber);
expect(window.localStorage.getItem(storageKey)).to.equal(
JSON.stringify(testNumber)
);
});
});

describe('#retrieve', () => {
Expand Down
Loading