├── .gitignore ├── src ├── storage-change-arguments.interface.ts ├── cool-local-storage.ts ├── cool-session-storage.ts ├── cool-server-storage.ts └── cool-storage-base.ts ├── tsconfig.json ├── index.ts ├── LICENSE ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | tmp 3 | compiled 4 | 5 | node_modules 6 | typings 7 | 8 | .vscode 9 | .idea 10 | 11 | npm-debug.log -------------------------------------------------------------------------------- /src/storage-change-arguments.interface.ts: -------------------------------------------------------------------------------- 1 | export interface StorageChangeArguments { 2 | key: string; 3 | 4 | value: any; 5 | } -------------------------------------------------------------------------------- /src/cool-local-storage.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, Inject, PLATFORM_ID } from '@angular/core'; 2 | import { isPlatformBrowser } from '@angular/common'; 3 | 4 | import { CoolStorageBase } from './cool-storage-base'; 5 | import { CoolServerStorage } from './cool-server-storage'; 6 | 7 | @Injectable() 8 | export class CoolLocalStorage extends CoolStorageBase { 9 | constructor(@Inject(PLATFORM_ID) platformId: Object) { 10 | if (isPlatformBrowser(platformId)) { 11 | super(window.localStorage, 'LocalStorage'); 12 | } else { 13 | super(new CoolServerStorage(), 'LocalStorage'); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/cool-session-storage.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, Inject, PLATFORM_ID } from '@angular/core'; 2 | import { isPlatformBrowser } from '@angular/common'; 3 | 4 | import { CoolStorageBase } from './cool-storage-base'; 5 | import { CoolServerStorage } from './cool-server-storage'; 6 | 7 | @Injectable() 8 | export class CoolSessionStorage extends CoolStorageBase { 9 | constructor(@Inject(PLATFORM_ID) platformId: Object) { 10 | if (isPlatformBrowser(platformId)) { 11 | super(window.sessionStorage, 'SessionStorage'); 12 | } else { 13 | super(new CoolServerStorage(), 'SessionStorage'); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "emitDecoratorMetadata": true, 5 | "experimentalDecorators": true, 6 | "lib": [ 7 | "es6", 8 | "dom" 9 | ], 10 | "module": "commonjs", 11 | "moduleResolution": "node", 12 | "noEmitOnError": false, 13 | "noImplicitAny": true, 14 | "outDir": "dist", 15 | "rootDir": ".", 16 | "sourceMap": true, 17 | "target": "es5", 18 | "inlineSources": true 19 | }, 20 | "angularCompilerOptions": { 21 | "strictMetadataEmit": true, 22 | "skipTemplateCodegen": true 23 | }, 24 | "files": [ 25 | "index.ts" 26 | ], 27 | "exclude": [ 28 | "node_modules" 29 | ] 30 | } -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | import { NgModule, ModuleWithProviders } from '@angular/core'; 2 | 3 | import { CoolLocalStorage } from './src/cool-local-storage'; 4 | export { CoolLocalStorage } from './src/cool-local-storage'; 5 | import { CoolSessionStorage } from './src/cool-session-storage'; 6 | export { CoolSessionStorage } from './src/cool-session-storage'; 7 | export { StorageChangeArguments } from './src/storage-change-arguments.interface'; 8 | 9 | @NgModule({ 10 | providers: [CoolLocalStorage, CoolSessionStorage] 11 | }) 12 | export class CoolStorageModule { 13 | /** @deprecated */ 14 | public static forRoot(): ModuleWithProviders { 15 | return { 16 | ngModule: CoolStorageModule, 17 | providers: [] 18 | }; 19 | } 20 | } -------------------------------------------------------------------------------- /src/cool-server-storage.ts: -------------------------------------------------------------------------------- 1 | export class CoolServerStorage { 2 | private _storageObject: any; 3 | 4 | public constructor() { 5 | this._storageObject = {}; 6 | } 7 | 8 | public getItem(key: string): string { 9 | return this._storageObject[key] || null; 10 | } 11 | 12 | public setItem(key: string, value: string): void { 13 | this._storageObject[key] = value; 14 | } 15 | 16 | public removeItem(key: string): void { 17 | this._storageObject[key] = undefined; 18 | } 19 | 20 | public key(index: number): string { 21 | return this._storageObject.key(index); 22 | } 23 | 24 | public clear(): void { 25 | this._storageObject = {}; 26 | } 27 | 28 | public get length(): number { 29 | return Object.keys(this._storageObject).length; 30 | } 31 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Hacklone 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular2-cool-storage", 3 | "version": "3.1.5", 4 | "description": "Cool Storage wrapper for angular2.", 5 | "main": "index.js", 6 | "peerDependencies": { 7 | "@angular/core": "^5.1.0", 8 | "@angular/common": "^5.1.0", 9 | "rxjs": "^5.0.0" 10 | }, 11 | "scripts": { 12 | "build": "rm -rf ./dist && ngc && cp ./README.md ./dist/README.md && cp ./package.json ./dist/package.json" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/Hacklone/angular2-cool-storage.git" 17 | }, 18 | "keywords": [ 19 | "angular2", 20 | "local-storage", 21 | "localStorage", 22 | "session-storage", 23 | "sessionStorage", 24 | "global", 25 | "wrapper", 26 | "cool", 27 | "cool-local-storage", 28 | "cool-session-storage", 29 | "CoolLocalStorage", 30 | "CoolSessionStorage", 31 | "Universal" 32 | ], 33 | "author": "Hacklone ", 34 | "license": "MIT", 35 | "licenses": [ 36 | { 37 | "type": "MIT", 38 | "url": "https://github.com/Hacklone/angular2-cool-storage/raw/master/LICENSE" 39 | } 40 | ], 41 | "bugs": { 42 | "url": "https://github.com/Hacklone/angular2-cool-storage/issues" 43 | }, 44 | "homepage": "https://github.com/Hacklone/angular2-cool-storage#readme", 45 | "devDependencies": { 46 | "@angular/compiler": "^5.0.0", 47 | "@angular/compiler-cli": "^5.0.0", 48 | "@angular/platform-server": "^5.0.0", 49 | "rxjs": "^5.5.5", 50 | "typescript": "2.4.2" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/cool-storage-base.ts: -------------------------------------------------------------------------------- 1 | import { Observable, Subject } from 'rxjs'; 2 | import { StorageChangeArguments } from './storage-change-arguments.interface'; 3 | 4 | export abstract class CoolStorageBase { 5 | private _storageObject: any; 6 | private _itemSetSubject: Subject; 7 | private _itemRemovedSubject: Subject; 8 | 9 | constructor(storageObject: any, storageObjectName: string) { 10 | if (!storageObject) { 11 | throw new Error(`Current browser does not support ${ storageObjectName }`); 12 | } 13 | 14 | this._storageObject = storageObject; 15 | 16 | this._itemSetSubject = new Subject(); 17 | this._itemRemovedSubject = new Subject(); 18 | } 19 | 20 | public get itemSetObservable(): Observable { 21 | return this._itemSetSubject.asObservable(); 22 | } 23 | 24 | public get itemRemovedObservable(): Observable { 25 | return this._itemRemovedSubject.asObservable(); 26 | } 27 | 28 | public getItem(key: string): string { 29 | return this._storageObject.getItem(key) || null; 30 | } 31 | 32 | public setItem(key: string, value: string): void { 33 | this._itemSetSubject.next({ 34 | key, 35 | value 36 | }); 37 | 38 | this._setItemInStorage(key, value); 39 | } 40 | 41 | public removeItem(key: string): void { 42 | let currentValue = this.tryGetObject(key); 43 | 44 | if (!currentValue) { 45 | currentValue = this.getItem(key); 46 | } 47 | 48 | this._itemRemovedSubject.next({ 49 | key, 50 | value: currentValue 51 | }); 52 | 53 | this._storageObject.removeItem(key); 54 | } 55 | 56 | public key(index: number): string { 57 | return this._storageObject.key(index); 58 | } 59 | 60 | public clear(): void { 61 | this._storageObject.clear(); 62 | } 63 | 64 | public get length(): number { 65 | return this._storageObject.length; 66 | } 67 | 68 | public getObject(key: string): T { 69 | let jsonInStorage = this.getItem(key); 70 | 71 | if (jsonInStorage === null) { 72 | return null; 73 | } 74 | 75 | return JSON.parse(jsonInStorage) as T; 76 | } 77 | 78 | public tryGetObject(key: string): T { 79 | try { 80 | return this.getObject(key); 81 | } catch (e) { 82 | return null; 83 | } 84 | } 85 | 86 | public setObject(key: string, value: any): void { 87 | this._itemSetSubject.next({ 88 | key, 89 | value 90 | }); 91 | 92 | this._setItemInStorage(key, JSON.stringify(value)); 93 | } 94 | 95 | private _setItemInStorage(key: string, value: string) { 96 | this._storageObject.setItem(key, value); 97 | } 98 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [npm-url]: https://npmjs.org/package/angular2-cool-storage 2 | [npm-image]: https://img.shields.io/npm/v/angular2-cool-storage.svg 3 | [downloads-image]: https://img.shields.io/npm/dm/angular2-cool-storage.svg 4 | [total-downloads-image]: https://img.shields.io/npm/dt/angular2-cool-storage.svg 5 | 6 | # angular2-cool-storage [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Total Downloads][total-downloads-image]][npm-url] 7 | Cool angular2 wrapper for LocalStorage and SessionStorage 8 | 9 | ## Install 10 | > npm install --save angular2-cool-storage 11 | 12 | ## Setup 13 | ```javascript 14 | import { NgModule } from '@angular/core'; 15 | import { CoolStorageModule } from 'angular2-cool-storage'; 16 | 17 | @NgModule({ 18 | imports: [CoolStorageModule] 19 | }) 20 | export class MyAppModule {} 21 | ``` 22 | 23 | ## Features 24 | 25 | - LocalStorage support 26 | - SessionStorage support 27 | - Universal support 28 | 29 | ## API 30 | - getItem(key) 31 | - setItem(key, value) 32 | - removeItem(key) 33 | - length 34 | - clear() 35 | - key(index) 36 | - itemSetObservable 37 | - itemRemovedObservable 38 | - getObject(key) 39 | - tryGetObject(key) 40 | - setObject(key, value) 41 | 42 | ```javascript 43 | import { Component, OnInit } from '@angular/core'; 44 | 45 | import { CoolLocalStorage } from 'angular2-cool-storage'; 46 | 47 | @Component({ 48 | selector: 'my-app' 49 | }) 50 | export class AppComponent implements OnInit { 51 | localStorage: CoolLocalStorage; 52 | 53 | constructor(localStorage: CoolLocalStorage) { 54 | this.localStorage = localStorage; 55 | } 56 | 57 | ngOnInit() { 58 | this.localStorage.setItem('itemKey', 'itemValue'); 59 | 60 | console.log(this.localStorage.getItem('itemKey')); 61 | 62 | this.localStorage.setObject('itemKey', { 63 | someObject: 3 64 | }); 65 | 66 | console.log(this.localStorage.getObject('itemKey')); 67 | } 68 | } 69 | ``` 70 | 71 | # License 72 | > The MIT License (MIT) 73 | 74 | > Copyright (c) 2016 Hacklone 75 | > https://github.com/Hacklone 76 | 77 | > Permission is hereby granted, free of charge, to any person obtaining a copy 78 | > of this software and associated documentation files (the "Software"), to deal 79 | > in the Software without restriction, including without limitation the rights 80 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 81 | > copies of the Software, and to permit persons to whom the Software is 82 | > furnished to do so, subject to the following conditions: 83 | 84 | > The above copyright notice and this permission notice shall be included in all 85 | > copies or substantial portions of the Software. 86 | 87 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 88 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 89 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 90 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 91 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 92 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 93 | > SOFTWARE. 94 | --------------------------------------------------------------------------------