This commit is contained in:
37
node_modules/localstorage-polyfill/.npmignore
generated
vendored
Normal file
37
node_modules/localstorage-polyfill/.npmignore
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules
|
||||
jspm_packages
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
21
node_modules/localstorage-polyfill/LICENSE
generated
vendored
Normal file
21
node_modules/localstorage-polyfill/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2016 Jiri Spac
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
29
node_modules/localstorage-polyfill/README.md
generated
vendored
Normal file
29
node_modules/localstorage-polyfill/README.md
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# localstorage-polyfill
|
||||
|
||||
in memory localStorage polyfill for node.js utilizing ES6 proxies
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm i localstorage-polyfill -D
|
||||
```
|
||||
Saving for development, because primarily this package is meant for unit testing browser in node.js.
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
require('localstorage-polyfill')
|
||||
// or
|
||||
import 'localstorage-polyfill'
|
||||
global.localStorage // now has your in memory localStorage
|
||||
```
|
||||
|
||||
For API doc, refer to MDN.
|
||||
|
||||
There are other packages like node-localStorage, but none of them work properly when you do
|
||||
|
||||
```javascript
|
||||
localStorage.c = 1
|
||||
```
|
||||
|
||||
They only shim the methods of localStorage object, they don't imitate it's behaviour fully.
|
||||
56
node_modules/localstorage-polyfill/localStorage.js
generated
vendored
Normal file
56
node_modules/localstorage-polyfill/localStorage.js
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
'use strict'
|
||||
const valuesMap = new Map()
|
||||
|
||||
class LocalStorage {
|
||||
getItem (key) {
|
||||
const stringKey = String(key)
|
||||
if (valuesMap.has(key)) {
|
||||
return String(valuesMap.get(stringKey))
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
setItem (key, val) {
|
||||
valuesMap.set(String(key), String(val))
|
||||
}
|
||||
|
||||
removeItem (key) {
|
||||
valuesMap.delete(key)
|
||||
}
|
||||
|
||||
clear () {
|
||||
valuesMap.clear()
|
||||
}
|
||||
|
||||
key (i) {
|
||||
if (arguments.length === 0) {
|
||||
throw new TypeError("Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present.") // this is a TypeError implemented on Chrome, Firefox throws Not enough arguments to Storage.key.
|
||||
}
|
||||
var arr = Array.from(valuesMap.keys())
|
||||
return arr[i]
|
||||
}
|
||||
|
||||
get length () {
|
||||
return valuesMap.size
|
||||
}
|
||||
}
|
||||
const instance = new LocalStorage()
|
||||
|
||||
global.localStorage = new Proxy(instance, {
|
||||
set: function (obj, prop, value) {
|
||||
if (LocalStorage.prototype.hasOwnProperty(prop)) {
|
||||
instance[prop] = value
|
||||
} else {
|
||||
instance.setItem(prop, value)
|
||||
}
|
||||
return true
|
||||
},
|
||||
get: function (target, name) {
|
||||
if (LocalStorage.prototype.hasOwnProperty(name)) {
|
||||
return instance[name]
|
||||
}
|
||||
if (valuesMap.has(name)) {
|
||||
return instance.getItem(name)
|
||||
}
|
||||
}
|
||||
})
|
||||
54
node_modules/localstorage-polyfill/localStorage.spec.js
generated
vendored
Normal file
54
node_modules/localstorage-polyfill/localStorage.spec.js
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
/* global localStorage */
|
||||
import test from 'ava'
|
||||
import './localStorage'
|
||||
|
||||
test('methods should work', (t) => {
|
||||
// can't make assuptions about key positioning
|
||||
localStorage.setItem('a', 1)
|
||||
t.is(localStorage.key(0), 'a')
|
||||
|
||||
localStorage.setItem('b', '2')
|
||||
t.is(localStorage.getItem('a'), '1')
|
||||
t.is(localStorage.getItem('b'), '2')
|
||||
t.is(localStorage.length, 2)
|
||||
|
||||
t.is(localStorage['c'], undefined)
|
||||
t.is(localStorage.getItem('c'), null)
|
||||
|
||||
localStorage.setItem('c')
|
||||
t.is(localStorage.getItem('c'), 'undefined')
|
||||
t.is(localStorage.length, 3)
|
||||
|
||||
localStorage.removeItem('c')
|
||||
t.is(localStorage.getItem('c'), null)
|
||||
t.is(localStorage.length, 2)
|
||||
|
||||
localStorage.clear()
|
||||
t.is(localStorage.getItem('a'), null)
|
||||
t.is(localStorage.getItem('b'), null)
|
||||
t.is(localStorage.length, 0)
|
||||
})
|
||||
|
||||
test('error throwing', (t) => {
|
||||
t.throws(() => {
|
||||
localStorage.key()
|
||||
}, /Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present\./)
|
||||
})
|
||||
|
||||
test('proxy should work', (t) => {
|
||||
t.is(localStorage.length, 0)
|
||||
|
||||
localStorage.a = {}
|
||||
t.is(localStorage.a, '[object Object]')
|
||||
localStorage.c = 1
|
||||
const obj = {}
|
||||
localStorage[obj] = 'key gets stringified'
|
||||
t.is(localStorage['[object Object]'], 'key gets stringified')
|
||||
t.is(localStorage.c, '1')
|
||||
t.is(localStorage.length, 3)
|
||||
localStorage.length = 0
|
||||
t.is(localStorage.length, 3)
|
||||
localStorage.key = 'only an ass**** would do this'
|
||||
t.is(localStorage.length, 3)
|
||||
t.is(localStorage.key, 'only an ass**** would do this')
|
||||
})
|
||||
41
node_modules/localstorage-polyfill/package.json
generated
vendored
Normal file
41
node_modules/localstorage-polyfill/package.json
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "localstorage-polyfill",
|
||||
"version": "1.0.1",
|
||||
"description": "in memory localStorage polyfill for node.js utilizing ES6 proxies",
|
||||
"main": "localStorage.js",
|
||||
"scripts": {
|
||||
"test": "ava",
|
||||
"dev": "ava -w"
|
||||
},
|
||||
"engines" : {
|
||||
"node" : ">=6"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/capaj/localstorage-polyfill.git"
|
||||
},
|
||||
"keywords": [
|
||||
"html5",
|
||||
"local",
|
||||
"storage",
|
||||
"browser",
|
||||
"testing",
|
||||
"unit",
|
||||
"testing"
|
||||
],
|
||||
"author": "capajj@gmail.com",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/capaj/localstorage-polyfill/issues"
|
||||
},
|
||||
"homepage": "https://github.com/capaj/localstorage-polyfill#readme",
|
||||
"devDependencies": {
|
||||
"ava": "^0.16.0",
|
||||
"standard": "^8.0.0"
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"localStorage.spec.js"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user