Skip to content

Commit 90ee87f

Browse files
author
Yann 'Aaunel' Eves-Hollis
committed
Merge pull request #64 from hppycoder/master-getStorageKeys
Master get storage keys
2 parents e6991b8 + 0347cf9 commit 90ee87f

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.DS_Store
3-
*.log
3+
*.log
4+
.idea

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The simpliest localStorage module you will ever use. Allowing you to set, get, a
4747

4848
// clear all localStorage values
4949
storage.clearAll();
50+
51+
// gets all the keys in the storage system returned as an array
52+
storage.getKeys(); // ['key','key2']
5053
```
5154

5255
## Bower

src/angularLocalStorage.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Global Vars
1212
*/
1313
var storage = (typeof $window.localStorage === 'undefined') ? undefined : $window.localStorage;
14-
var supported = !(typeof storage === 'undefined');
14+
var supported = (typeof storage !== 'undefined');
1515
var watchers = {};
1616

1717
if (supported) {
@@ -200,6 +200,30 @@
200200
*/
201201
isCookieFallbackActive: function() {
202202
return !supported;
203+
},
204+
205+
/**
206+
* Allows the caller to obtain all the keys that are saved in Cookies or LocalStorage. Gets all the keys
207+
* that are saved in LocalStorage or Cookie.
208+
*
209+
* Uses: String.trim() - ECMAScript 1.5+
210+
*
211+
* @returns array
212+
*/
213+
getKeys: function() {
214+
var keys = [];
215+
216+
if(!supported) {
217+
var cookieArr = document.cookie.split(';');
218+
for( var cnt = 0, cntLen = cookieArr.length; cnt < cntLen; ++cnt) {
219+
keys.push(cookieArr[cnt].split('=')[0].trim());
220+
}
221+
} else {
222+
for (var i = 0, len = localStorage.length; i < len; ++i) {
223+
keys.push(localStorage.key(i));
224+
}
225+
}
226+
return keys;
203227
}
204228
};
205229

test/angularLocalStorage.spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,25 @@ describe('angularLocalStorage module', function () {
146146
expect(testValue).toBeNull();
147147
});
148148
});
149-
});
149+
150+
describe('when use getKeys() method all should be returned', function () {
151+
152+
beforeEach(function () {
153+
storage.set('abcKey', 'some test string');
154+
storage.set('key123', 'test 123');
155+
});
156+
157+
beforeEach(function () {
158+
array = storage.getKeys();
159+
});
160+
161+
it('should return an array equaling the length of two', function () {
162+
expect(array.length).toEqual(2);
163+
});
164+
165+
it('should return the same keys that were put into storage', function() {
166+
expect(array.indexOf("key123")).toBeGreaterThan(-1);
167+
expect(array.indexOf("abcKey")).toBeGreaterThan(-1);
168+
});
169+
});
170+
});

0 commit comments

Comments
 (0)