Skip to content

Commit

Permalink
Add getAllKeys Method to SyncStorage (#5)
Browse files Browse the repository at this point in the history
* Add getAllKeys method

* Add update method

* Add push method

* Add update to readme

* Fix small grammatical problem in error message of update method

* Add test for push update and getAllKeys methods

* Add test for push error for non Array value

* Refactoring
Make handleError work with single argument

* Fix flow for second argument in handleError

* Remove push and update functions

* Update readme, fixing small issue with tests.
  • Loading branch information
iamsoorena authored and raphaelpor committed Oct 29, 2018
1 parent 6d174b3 commit 25ef251
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,13 @@ SyncStorage.remove('foo')
console.log(error);
});
```

#### getAllKeys()

returns an array from all the keys.

```js
SyncStorage.set('foo', 'bar');
SyncStorage.set('boo', 'baz');
console.log(SyncStorage.getAllKeys()) // ['foo', 'boo']
```
8 changes: 8 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ test("Can update an item", () => {
SyncStorage.saveItem(['bar', 'baz']);
expect(SyncStorage.get('bar')).toBe('baz');
});

test('Can get all keys from storage', () => {
SyncStorage.set('foo', 'bar');
const all_keys = SyncStorage.getAllKeys();
expect(all_keys)
.toEqual(expect.arrayContaining(['foo']));
});

9 changes: 7 additions & 2 deletions src/helpers/handleError.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// @flow

function handleError(func: string, param: string): Promise<string> {
const message = `${func}() requires at least ${param} as its first parameter.`;
function handleError(func: string, param: ?string): Promise<string> {
let message;
if (!param) {
message = func;
} else {
message = `${func}() requires at least ${param} as its first parameter.`;
}
console.warn(message); // eslint-disable-line no-console
return Promise.reject(message);
}
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class SyncStorage {
this.data.set(item[0], value);
this.loading = false;
}

getAllKeys(): Array<*> {
return Array.from(this.data.keys());
}
}

const syncStorage = new SyncStorage();
Expand Down

0 comments on commit 25ef251

Please sign in to comment.