🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

dom-storage

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dom-storage - npm Package Compare versions

Comparing version

to
2.0.1

53

lib/index.js

@@ -1,2 +0,1 @@

/*jshint node:true es5:true laxcomma:true laxbreak:true*/
// http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm

@@ -21,15 +20,21 @@

Object.defineProperty(this, '___priv_bk___', {
value: {
path: path
}
, writable: false
, enumerable: false
value: {
path: path
}
, writable: false
, enumerable: false
});
Object.defineProperty(this, '___priv_strict___', {
value: !!opts.strict
, writable: false
, enumerable: false
value: !!opts.strict
, writable: false
, enumerable: false
});
Object.defineProperty(this, '___priv_ws___', {
value: opts.ws || ' '
, writable: false
, enumerable: false
});
try {

@@ -48,3 +53,7 @@ db = JSON.parse(fs.readFileSync(path));

if (this.hasOwnProperty(key)) {
return this.___priv_strict___ && String(this[key]) || this[key];
if (this.___priv_strict___) {
return String(this[key]);
} else {
return this[key];
}
}

@@ -55,3 +64,9 @@ return null;

Storage.prototype.setItem = function (key, val) {
this[key] = this.___priv_strict___ && String(val) || val;
if (val === undefined) {
this[key] = null;
} else if (this.___priv_strict___) {
this[key] = String(val);
} else {
this[key] = val;
}
this.___save___();

@@ -97,3 +112,7 @@ };

this.___priv_bk___.lock = true;
fs.writeFile(this.___priv_bk___.path, JSON.stringify(this), 'utf8', function (e) {
fs.writeFile(
this.___priv_bk___.path
, JSON.stringify(this, null, this.___priv_ws___)
, 'utf8'
, function (e) {
self.___priv_bk___.lock = false;

@@ -111,7 +130,7 @@ if (e) {

Object.defineProperty(Storage, 'create', {
value: function (path, opts) {
return new Storage(path, opts);
}
, writable: false
, enumerable: false
value: function (path, opts) {
return new Storage(path, opts);
}
, writable: false
, enumerable: false
});

@@ -118,0 +137,0 @@

@@ -5,3 +5,3 @@ {

"description": "W3C DOM Storage (localStorage and sessionStorage) for Node.JS",
"version": "2.0.0",
"version": "2.0.1",
"repository": {

@@ -8,0 +8,0 @@ "type": "git",

@@ -14,19 +14,21 @@ sessionStorage & localStorage for NodeJS

var Storage = require('dom-storage')
```javascript
var Storage = require('dom-storage')
// in-file, doesn't call `String(val)` on values (default)
, localStorage = new Storage('./db.json', { strict: false })
// in-file, doesn't call `String(val)` on values (default)
, localStorage = new Storage('./db.json', { strict: false, ws: ' ' })
// in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`
, sessionStorage = new Storage(null, { strict: true })
// in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`
, sessionStorage = new Storage(null, { strict: true })
, myValue = { foo: 'bar', baz: 'quux' }
;
, myValue = { foo: 'bar', baz: 'quux' }
;
localStorage.setItem('myKey', myValue);
myValue = localStorage.getItem('myKey');
localStorage.setItem('myKey', myValue);
myValue = localStorage.getItem('myKey');
// use JSON to stringify / parse when using strict w3c compliance
sessionStorage.setItem('myKey', JSON.stringify(myValue));
myValue = JSON.parse(localStorage.getItem('myKey'));
// use JSON to stringify / parse when using strict w3c compliance
sessionStorage.setItem('myKey', JSON.stringify(myValue));
myValue = JSON.parse(localStorage.getItem('myKey'));
```

@@ -43,21 +45,28 @@ API

### Options
* strict - whether to stringify strictly as text `[Object object]` or as json `{ foo: bar }`.
* ws - the whitespace to use saving json to disk. Defaults to `' '`.
Tests
---
0 === localStorage.length;
null === localStorage.getItem('doesn't exist');
undefined === localStorage['doesn't exist'];
```javascript
0 === localStorage.length;
null === localStorage.getItem('doesn't exist');
undefined === localStorage['doesn't exist'];
localStorage.setItem('myItem');
"undefined" === localStorage.getItem('myItem');
1 === localStorage.length;
localStorage.setItem('myItem');
"undefined" === localStorage.getItem('myItem');
1 === localStorage.length;
localStorage.setItem('myItem', 0);
"0" === localStorage.getItem('myItem');
localStorage.setItem('myItem', 0);
"0" === localStorage.getItem('myItem');
localStorage.removeItem('myItem', 0);
0 === localStorage.length;
localStorage.removeItem('myItem', 0);
0 === localStorage.length;
localStorage.clear();
0 === localStorage.length;
localStorage.clear();
0 === localStorage.length;
```

@@ -64,0 +73,0 @@ Notes

@@ -1,2 +0,1 @@

/*jshint node:true es5:true laxcomma:true laxbreak:true*/
(function () {

@@ -7,3 +6,3 @@ "use strict";

, fs = require('fs')
, Storage = require('dom-storage')
, Storage = require('../')
, dbPath = './db.json'

@@ -14,3 +13,3 @@ ;

// should not return prototype properties
assert.strictEqual(null, Object.getItem('key'));
assert.strictEqual(null, storage.getItem('key'));

@@ -25,3 +24,3 @@ assert.strictEqual(0, Object.keys(storage).length);

storage.setItem('b', '2');
assert.strictEqual(storage.getItem('a'), '1');
assert.strictEqual(storage.getItem('a'), 1);
assert.strictEqual(storage.getItem('b'), '2');

@@ -34,3 +33,3 @@ assert.strictEqual(storage.length, 2);

storage.setItem('c');
assert.strictEqual(storage.getItem('c'), "undefined");
assert.strictEqual(storage.getItem('c'), null);
assert.strictEqual(storage.length, 3);

@@ -49,4 +48,4 @@

function runAll() {
var localStorage = new Storage(dbPath)
, sessionStorage = new Storage()
var localStorage = new Storage(dbPath, { strict: false, ws: ' ' })
, sessionStorage = new Storage(null, { strict: false })
;

@@ -53,0 +52,0 @@