Skip to content

Commit

Permalink
Added as optional parameter in .getItem(key, noParse) method
Browse files Browse the repository at this point in the history
  • Loading branch information
jherax committed Sep 20, 2017
1 parent 55d943a commit bf56499
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

<!-- markdownlint-disable MD024 MD033 -->

## 2.3.2

### Improvements

1. Added an optional parameter to `getItem(key, noParse)` method
to determine if the value shouldn't be parsed with `JSON.parse`

## 2.3.0

### Improvements
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ $ yarn add proxy-storage
<script src="https://unpkg.com/proxy-storage/dist/proxy-storage.min.js"></script>

<!-- or from rawgit.com -->
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.3.0/dist/proxy-storage.min.js"></script>
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.3.2/dist/proxy-storage.min.js"></script>
```

In the above case, [`proxyStorage`](#api) is included as a global object
Expand Down Expand Up @@ -153,7 +153,8 @@ the prototype:
- **`setItem`**`(key, value [,options])`: stores a `value` given a `key` name.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
- **`getItem`**`(key)`: retrieves a value by its `key` name.
- **`getItem`**`(key [, noParse])`: retrieves a value by its `key` name.
<br>If `noParse` is `true` then the value retrieved is not parsed with `JSON.parse`.
- **`removeItem`**`(key [,options])`: deletes an item from the storage.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
Expand Down Expand Up @@ -190,6 +191,9 @@ storage.setItem('qwerty', [{ garbage: true, some: 'object' }]);
console.log(storage.getItem('qwerty'));
// [{ garbage: true, some: 'object' }]

console.log(storage.getItem('qwerty', true));
// '[{ "garbage": true, "some": "object" }]'

storage.setItem('persisted', true);
storage.setItem('o-really', { status: 'saved' });
console.log(`items: ${storage.length}`);
Expand Down Expand Up @@ -237,7 +241,8 @@ Each instance inherits the following properties:
- **`setItem`**`(key, value [,options])`: stores a `value` given a `key` name.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
- **`getItem`**`(key)`: retrieves a value by its `key` name.
- **`getItem`**`(key [, noParse])`: retrieves a value by its `key` name.
<br>If `noParse` is `true` then the value retrieved is not parsed with `JSON.parse`.
- **`removeItem`**`(key [,options])`: deletes an item from the storage.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
Expand Down
7 changes: 4 additions & 3 deletions dist/proxy-storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! proxyStorage@v2.3.1. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
/*! proxyStorage@v2.3.2. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand Down Expand Up @@ -468,22 +468,23 @@ var WebStorage = function () {
* Retrieves a value by its key name.
*
* @param {string} key: keyname of the storage
* @param {boolean} noParse: if the value shoudn't be parsed with `JSON.parse`
* @return {void}
*
* @memberOf WebStorage
*/

}, {
key: 'getItem',
value: function getItem(key) {
value: function getItem(key, noParse) {
(0, _utils.checkEmpty)(key);
var value = _proxyMechanism.proxy[this.__storage__].getItem(key);
if (value == null) {
// null or undefined
delete this[key];
value = null;
} else {
value = (0, _utils.tryParse)(value);
if (noParse !== true) value = (0, _utils.tryParse)(value);
this[key] = value;
}
var v = (0, _interceptors2.default)('getItem', key, value);
Expand Down
4 changes: 2 additions & 2 deletions dist/proxy-storage.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/proxy-storage.min.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proxy-storage",
"version": "2.3.1",
"version": "2.3.2",
"description": "Normalizes the API for cookies, and localStorage/sessionStorage",
"author": "David Rivera <jherax@gmail.com>",
"main": "src/proxy-storage.js",
Expand Down
5 changes: 3 additions & 2 deletions src/web-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,19 @@ class WebStorage {
* Retrieves a value by its key name.
*
* @param {string} key: keyname of the storage
* @param {boolean} noParse: if the value shoudn't be parsed with `JSON.parse`
* @return {void}
*
* @memberOf WebStorage
*/
getItem(key) {
getItem(key, noParse) {
checkEmpty(key);
let value = proxy[this.__storage__].getItem(key);
if (value == null) { // null or undefined
delete this[key];
value = null;
} else {
value = tryParse(value);
if (noParse !== true) value = tryParse(value);
this[key] = value;
}
const v = executeInterceptors('getItem', key, value);
Expand Down
32 changes: 19 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,12 @@ debug@^2.2.0, debug@^2.6.8:
dependencies:
ms "2.0.0"

debug@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.0.1.tgz#0564c612b521dc92d9f2988f0549e34f9c98db64"
dependencies:
ms "2.0.0"

debug@~2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.2.0.tgz#f87057e995b1a1f6ae6a4960664137bc56f039da"
Expand Down Expand Up @@ -1453,18 +1459,18 @@ eslint-scope@^3.7.1:
estraverse "^4.1.1"

eslint@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.5.0.tgz#bb75d3b8bde97fb5e13efcd539744677feb019c3"
version "4.7.1"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.7.1.tgz#849804136953ebe366782f9f8611e2cbd1b54681"
dependencies:
ajv "^5.2.0"
babel-code-frame "^6.22.0"
chalk "^2.1.0"
concat-stream "^1.6.0"
cross-spawn "^5.1.0"
debug "^2.6.8"
debug "^3.0.1"
doctrine "^2.0.0"
eslint-scope "^3.7.1"
espree "^3.5.0"
espree "^3.5.1"
esquery "^1.0.0"
estraverse "^4.2.0"
esutils "^2.0.2"
Expand All @@ -1485,7 +1491,7 @@ eslint@^4.5.0:
natural-compare "^1.4.0"
optionator "^0.8.2"
path-is-inside "^1.0.2"
pluralize "^4.0.0"
pluralize "^7.0.0"
progress "^2.0.0"
require-uncached "^1.0.3"
semver "^5.3.0"
Expand All @@ -1494,9 +1500,9 @@ eslint@^4.5.0:
table "^4.0.1"
text-table "~0.2.0"

espree@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.0.tgz#98358625bdd055861ea27e2867ea729faf463d8d"
espree@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.1.tgz#0c988b8ab46db53100a1954ae4ba995ddd27d87e"
dependencies:
acorn "^5.1.1"
acorn-jsx "^3.0.0"
Expand Down Expand Up @@ -2655,9 +2661,9 @@ pkg-dir@^2.0.0:
dependencies:
find-up "^2.1.0"

pluralize@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-4.0.0.tgz#59b708c1c0190a2f692f1c7618c446b052fd1762"
pluralize@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-7.0.0.tgz#298b89df8b93b0221dbf421ad2b1b1ea23fc6777"

prelude-ls@~1.1.2:
version "1.1.2"
Expand Down Expand Up @@ -3311,8 +3317,8 @@ webpack-sources@^1.0.1:
source-map "~0.5.3"

webpack@^3.5.5:
version "3.5.5"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-3.5.5.tgz#3226f09fc8b3e435ff781e7af34f82b68b26996c"
version "3.6.0"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-3.6.0.tgz#a89a929fbee205d35a4fa2cc487be9cbec8898bc"
dependencies:
acorn "^5.0.0"
acorn-dynamic-import "^2.0.0"
Expand Down

0 comments on commit bf56499

Please sign in to comment.