Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Items with store API key names are considered by key() #3

Merged
merged 2 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
node_modules

# OS generated files #
.DS_Store
.DS_Store?
.Trashes

# PhpStorm project files #
.idea
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"browser": true,
"shadow": true,
"devel": true,
"esnext": true,

"globals": {
"define": false,
Expand Down
4 changes: 2 additions & 2 deletions dist/memorystorage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/memorystorage.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 30 additions & 5 deletions dist/memorystorage.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
// API methods and properties will be cloaked
var API = {'clear':1, 'getItem':1, 'id':1, 'key':1, 'length':1, 'removeItem':1, 'setItem':1},
API_LENGTH = Object.keys(API).length,
CLOAK = '__memorystorage_cloaked_items__';

// Used to store all memorystorage objects
Expand Down Expand Up @@ -53,18 +52,30 @@ function MemoryStorage(id) {// jshint ignore:line
var cloaked = {};
Object.defineProperty(result, CLOAK, {
enumerable: false,
configurable: true,
get: function(){return cloaked;}
});
/**
* private method to find all enumerable keys
* @returns {Array.<String>}
*/
function enumerableKeys(){
var keys = Object.keys(result).filter(function(x){return !(x in API);});
return keys.concat(Object.keys(cloaked));
}

// Allow client code to read the id
Object.defineProperty(result, 'id', {
enumerable: true,
configurable: true,
get: function(){return id;}
});
// Create the length property
Object.defineProperty(result, 'length', {
enumerable: true,
configurable: true,
get: function(){
return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH;
return enumerableKeys().length;
}
});
// Create API methods
Expand All @@ -79,9 +90,13 @@ function MemoryStorage(id) {// jshint ignore:line
if (key in API) {delete this[CLOAK][key];}
else {delete this[key];}
};
/**
* Needed to enumerate over all items in the collection
* @param {Number} idx - the index
* @returns {null|string} - the name of the nth key in the storage
*/
result.key = function MemoryStorage_key(idx) {
var keys = Object.keys(this).concat(Object.keys(this[CLOAK]));
keys = keys.filter(function(x){return !(x in API);});
var keys = enumerableKeys();
return idx >= 0 && idx < keys.length ? keys[idx] : null;
};
result.clear = function MemoryStorage_clear() {
Expand All @@ -94,7 +109,17 @@ function MemoryStorage(id) {// jshint ignore:line
delete this[CLOAK][key];
}
};
return result;

if (typeof Proxy === 'undefined')
{
return result;
}
// ES6 Proxy to support Object.keys() on a MemoryStorage object
return new Proxy(result, {
ownKeys: function() {
return enumerableKeys();
}
});
}


Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "memorystorage",
"version": "0.9.10",
"version": "0.9.12",
"description": "Memory-backed implementation of the Web Storage API",
"src": "src/memorystorage.js",
"main": "dist/memorystorage.umd.js",
Expand Down Expand Up @@ -40,7 +40,7 @@
},
"homepage": "http://download.github.io/memorystorage",
"devDependencies": {
"grunt": "~0.4.5",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-uglify": "~0.6.0",
"grunt-jsdoc": "~0.6.7",
Expand Down
35 changes: 30 additions & 5 deletions src/memorystorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
// API methods and properties will be cloaked
var API = {'clear':1, 'getItem':1, 'id':1, 'key':1, 'length':1, 'removeItem':1, 'setItem':1},
API_LENGTH = Object.keys(API).length,
CLOAK = '__memorystorage_cloaked_items__';

// Used to store all memorystorage objects
Expand Down Expand Up @@ -47,18 +46,30 @@ function MemoryStorage(id) {// jshint ignore:line
var cloaked = {};
Object.defineProperty(result, CLOAK, {
enumerable: false,
configurable: true,
get: function(){return cloaked;}
});
/**
* private method to find all enumerable keys
* @returns {Array.<String>}
*/
function enumerableKeys(){
var keys = Object.keys(result).filter(function(x){return !(x in API);});
return keys.concat(Object.keys(cloaked));
}

// Allow client code to read the id
Object.defineProperty(result, 'id', {
enumerable: true,
configurable: true,
get: function(){return id;}
});
// Create the length property
Object.defineProperty(result, 'length', {
enumerable: true,
configurable: true,
get: function(){
return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH;
return enumerableKeys().length;
}
});
// Create API methods
Expand All @@ -73,9 +84,13 @@ function MemoryStorage(id) {// jshint ignore:line
if (key in API) {delete this[CLOAK][key];}
else {delete this[key];}
};
/**
* Needed to enumerate over all items in the collection
* @param {Number} idx - the index
* @returns {null|string} - the name of the nth key in the storage
*/
result.key = function MemoryStorage_key(idx) {
var keys = Object.keys(this).concat(Object.keys(this[CLOAK]));
keys = keys.filter(function(x){return !(x in API);});
var keys = enumerableKeys();
return idx >= 0 && idx < keys.length ? keys[idx] : null;
};
result.clear = function MemoryStorage_clear() {
Expand All @@ -88,5 +103,15 @@ function MemoryStorage(id) {// jshint ignore:line
delete this[CLOAK][key];
}
};
return result;

if (typeof Proxy === 'undefined')
{
return result;
}
// ES6 Proxy to support Object.keys() on a MemoryStorage object
return new Proxy(result, {
ownKeys: function() {
return enumerableKeys();
}
});
}
1 change: 1 addition & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MemoryStorage Tests</title>
<script src="../dist/memorystorage.min.js"></script>
<!--<script src="../src/memorystorage.js"></script>-->
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css">
<body>
<a href="index.html">Restart</a> | <a href="test-amd.html">Next Test</a>
Expand Down
18 changes: 16 additions & 2 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ QUnit.test("W3C Web Storage API Compliance Test", function( assert ) {
assert.ok(store.length===2, 'value added correctly with index operators');
store.setItem('test2', 'data2');
assert.ok(store.length===3, 'three items added to store');
assert.ok(Object.keys(store).length == (7+3), "store has 10 enumerable properties (id, 6 api methods + 3 stored items)");
if (typeof Proxy === "undefined") {
assert.ok(Object.keys(store).length == (7+3), "store has 10 enumerable properties (id, 6 api methods + 3 stored items)");
}
else {
assert.ok(Object.keys(store).length === 3, "store has 3 enumerable properties (no api methods + 3 stored items)");
assert.ok(Object.keys(store).sort().join(',') === "test0,test1,test2",
"keys are enumerable with Object.keys()");
}
assert.ok(store.getItem('test1')==='data1' && store.getItem('test2')==='data2', "retrieved values matches stored values");
var keyOrderBefore = '';
for (var i=0; i<store.length; i++) {
Expand All @@ -34,9 +41,17 @@ QUnit.test("W3C Web Storage API Compliance Test", function( assert ) {
assert.ok(store.length===2, "double removal has no effect");
assert.ok(store.getItem('test1')===undefined, "get removed item returns undefined");

store.clear();
store.setItem('getItem', 'test');
assert.ok(typeof store.getItem === 'function', "store API methods cannot be overwritten.");
assert.ok(store.getItem('getItem') === 'test', "getItem successfully retrieves item with API name.");
assert.ok(store.length===1, 'items with store API key names should be counted in the length property');
assert.ok(store.key(0)==='getItem', 'items with store API key names should be enumerable with key()');
if (typeof Proxy !== "undefined") {
assert.ok(Object.keys(store).length === 1, "store API key names are counted in the key enumeration");
assert.ok(Object.keys(store).join('') === "getItem",
"store API key names are included in the key enumeration");
}
store.removeItem('getItem');
assert.ok(store.getItem('getItem') === undefined, "After removal of item with API name, getItem returns undefined.");

Expand All @@ -50,7 +65,6 @@ QUnit.test("W3C Web Storage API Compliance Test", function( assert ) {
assert.ok(glob.key(0)===null, "global keys are removed correctly");
assert.ok(glob.getItem('glob0')===undefined, "global values are removed correctly");

store.clear();
assert.ok(store.length===0, "store is cleared");
assert.ok(store.key(0)===null, "no keys in cleared store");
assert.ok(store.getItem('test0')===undefined, "no values in cleared store");
Expand Down