Skip to content

Commit

Permalink
fix: use pure empty object as storage to prevent key conflits
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Mar 24, 2018
1 parent bec1fd4 commit 48be6ea
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/localstorage.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
export class LocalStorage {
constructor(jest) {
Object.defineProperty(this, 'store', {
enumerable: false,
value: {},
}),
Object.defineProperty(this, 'getItem', {
enumerable: false,
value: jest.fn(key => this[key] || null),
value: jest.fn(key => this.store[key] || null),
});
Object.defineProperty(this, 'setItem', {
enumerable: false,
// not mentioned in the spec, but we must always coerce to a string
value: jest.fn((key, val = '') => {
this[key] = val + '';
this.store[key] = val + '';
}),
});
Object.defineProperty(this, 'removeItem', {
enumerable: false,
value: jest.fn(key => {
delete this[key];
delete this.store[key];
}),
});
Object.defineProperty(this, 'clear', {
enumerable: false,
value: jest.fn(() => {
Object.keys(this).map(key => delete this[key]);
Object.keys(this.store).map(key => delete this.store[key]);
}),
});
Object.defineProperty(this, 'toString', {
Expand All @@ -31,15 +35,15 @@ export class LocalStorage {
});
Object.defineProperty(this, 'key', {
enumerable: false,
value: jest.fn(idx => Object.keys(this)[idx] || null),
value: jest.fn(idx => Object.keys(this.store)[idx] || null),
});
} // end constructor

get length() {
return Object.keys(this).length;
return Object.keys(this.store).length;
}
// for backwards compatibility
get __STORE__() {
return this;
return this.store;
}
}

0 comments on commit 48be6ea

Please sign in to comment.