├── package.json ├── README.md └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "localstorage", 3 | "version": "1.0.1", 4 | "main": "index.js", 5 | "scripts": { 6 | "test": "standard ." 7 | }, 8 | "author": "voltraco", 9 | "license": "MIT", 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/voltraco/localstorage.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/voltraco/localstorage/issues" 16 | }, 17 | "homepage": "https://github.com/voltraco/localstorage#readme", 18 | "description": "serialize, deserialize and namespace JSON" 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SYNOPSIS 2 | Serialize and deserialize and namespace JSON in localstorage. 3 | 4 | # INSTALL 5 | 6 | ```bash 7 | npm install localstorage 8 | ``` 9 | 10 | # USAGE 11 | 12 | ```js 13 | const LocalStorage = require('localstorage') 14 | ``` 15 | 16 | ```js 17 | const foo = new LocalStorage('foo') // create a `foo` namespace 18 | foo.put('quxx', { foo: 100 }) 19 | ``` 20 | 21 | ```js 22 | const [err, value] = foo.get('quxx') 23 | value.foo === 100 // true 24 | ``` 25 | 26 | ```js 27 | foo.get('quxx') // [null, { foo: 100 }] 28 | foo.get('foo') // [ErrorNotFOund] 29 | 30 | foo.has('quxx') // [null, true] 31 | foo.has('foo') // [ErrorNotFound] 32 | 33 | foo.delete('quxx') // [null, true] 34 | foo.delete('foo') // [ErrorNotFound] 35 | 36 | foo.delete() // delete everything in the `foo` namespace 37 | ``` 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | class LocalStorage { 2 | constructor (namespace) { 3 | this._namespace = namespace 4 | this._store = window.localStorage 5 | this._sep = '\x00' 6 | } 7 | 8 | static clear () { 9 | window.localStorage.clear() 10 | } 11 | 12 | _notFound (k) { 13 | const err = new Error(`Not Found [${k}]`) 14 | err.notFound = true 15 | err.key = k 16 | return err 17 | } 18 | 19 | get (key) { 20 | const k = [this._namespace, key].join(this._sep) 21 | if (!this._store[k]) return [this._notFound(k)] 22 | 23 | try { 24 | return [null, JSON.parse(this._store[k])] 25 | } catch (err) { 26 | return [err] 27 | } 28 | } 29 | 30 | put (key, value) { 31 | if (typeof value === 'undefined') { 32 | return [new Error(`Invalid parameters to put, ('${key}', undefined)`)] 33 | } 34 | 35 | try { 36 | const k = [this._namespace, key].join(this._sep) 37 | const v = JSON.stringify(value) 38 | const result = this._store[k] = v 39 | return [null, result] 40 | } catch (err) { 41 | return [err] 42 | } 43 | } 44 | 45 | has (key) { 46 | const k = [this._namespace, key].join(this._sep) 47 | if (!this._store[k]) return [this._notFound(k)] 48 | return [null, true] 49 | } 50 | 51 | del (key) { 52 | if (key) { 53 | const k = [this._namespace, key].join(this._sep) 54 | if (!this._store[k]) return [this._notFound(k)] 55 | 56 | delete this._store[k] 57 | return [null] 58 | } 59 | 60 | Object.keys(window.localStorage).forEach(k => { 61 | const ns = k.split(this._sep)[0] 62 | if (ns === this._namespace) { 63 | delete this._store[k] 64 | } 65 | }) 66 | } 67 | 68 | search (pattern) { 69 | if (!pattern) { 70 | throw new Error('A pattern is required') 71 | } 72 | 73 | const matchKeys = key => { 74 | const [, _key] = key.split(this._sep) 75 | 76 | if (!_key) return 77 | if (!pattern.test(_key)) return 78 | 79 | return key 80 | } 81 | 82 | const makePairs = key => ({ 83 | key: key.split(this._sep)[1], 84 | value: this._store[key] 85 | }) 86 | 87 | return [null, Object 88 | .keys(this._store) 89 | .filter(matchKeys) 90 | .map(makePairs)] 91 | } 92 | } 93 | 94 | module.exports = LocalStorage 95 | --------------------------------------------------------------------------------