Skip to content

Commit 8968429

Browse files
committed
release(v0.6.0): add config previousPrefix for backward compatibility and safe config changes, update README, provide better functions accessibility, minor fixes and code improvements
1 parent 2067f93 commit 8968429

File tree

6 files changed

+99
-39
lines changed

6 files changed

+99
-39
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ This library adds decorators that make it super easy to *automagically* save and
99
+ `get(key: string)`: gets JSON-parsed data from HTML5 Storage
1010
+ `set(key: string, value: any)`: sets data in HTML5 Storage
1111
+ `remove(key: string)` removes variable with given key
12-
+ `clear()`: clears Storage out of variables with set prefix
13-
+ `keys`: getter for keys of stored values
12+
+ `clear(clearType: 'decorators' | 'prefix' | 'all')`: clears Storage out of variables with set prefix
1413
+ `config`: getter for module config
14+
+ `keys`: getter for keys of values stored by ngx-store (determined by prefix and decorators)
15+
+ `utility`: access to [WebStorageUtility](https://github.com/zoomsphere/angular2-localstorage/blob/aot/src/utility/webstorage-utility.ts) class for advanced stuff
1516
- Objects read from Storage have added `.save()` method to easily force save of made changes (configurable by `mutateObjects`)
1617
- saving support for all `Array` methods that change array object's value (configurable by `mutateObjects`)
1718
- Easy configuration of what you want (see [#configuration](#configuration) section)
19+
- Compatibility: either with AoT compiler and previous versions including `angular2-localstorage`
1820

1921
## Upcoming (TODO)
20-
- Handle data saved with previous prefix after its change
2122
- Encoding of saved data
2223
- Tests coverage
2324
- Cookies fallback
@@ -47,9 +48,10 @@ As this project uses decorating functions, it's important to provide custom conf
4748
```html
4849
<script>
4950
var NGXSTORE_CONFIG = {
50-
prefix: 'myApp.', // default: ngx_
51-
clearType: 'prefix', // possible values: decorators, prefix, all
52-
mutateObjects: true // defines whether Array methods shall be modified to handle changes automatically and .save() method shall be added to stored objects
51+
prefix: 'myApp.', // default: ngx_, you can set it to '', however using prefix is recommended
52+
clearType: 'prefix', // defines default clear() method behavior, possible values are: decorators, prefix, all
53+
mutateObjects: true // defines whether Array methods shall be modified to handle changes automatically and .save() method shall be added to stored objects (can be troublesome for object comparisons)
54+
previousPrefix: 'angular2ws_' // you have to set it only if you were using custom prefix in old version ('angular2ws_' is a default value)
5355
};
5456
</script>
5557
```
@@ -58,7 +60,7 @@ As this project uses decorating functions, it's important to provide custom conf
5860
plugins: [
5961
new webpack.DefinePlugin({
6062
NGXSTORE_CONFIG: JSON.stringify({
61-
prefix: '' // etc
63+
prefix: '', // etc
6264
})
6365
}),
6466
]
@@ -149,5 +151,6 @@ As this project uses decorating functions, it's important to provide custom conf
149151

150152
**Note**: Please don't store circular structures as this library uses JSON.stringify to encode before using LocalStorage.
151153

154+
**Note**: When you change prefix from '' (empty string) old values won't be removed automatically to avoid deleting necessary data. You should handle it manually or set clearType to 'all'.
152155

153156
**Contributions are welcome!**

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-store",
3-
"version": "0.5.4",
3+
"version": "0.6.0",
44
"main": "./dist/index",
55
"typings": "./dist/index",
66
"description": "Angular decorator to save and restore class properties automatically from LocalStorage and SessionStorage.",
@@ -12,20 +12,20 @@
1212
},
1313
"repository": {
1414
"type": "git",
15-
"url": "git@github.com:zoomsphere/angular2-localstorage.git"
15+
"url": "git@github.com:zoomsphere/ngx-store.git"
1616
},
1717
"keywords": [
1818
"Angular2",
1919
"LocalStorage",
2020
"SessionStorage",
2121
"Storage"
2222
],
23-
"author": "Marc J. Schmidt",
23+
"author": "Daniel Kucal & Marc J. Schmidt",
2424
"license": "ISC",
2525
"bugs": {
26-
"url": "https://github.com/zoomsphere/angular2-localstorage/issues"
26+
"url": "https://github.com/zoomsphere/ngx-store/issues"
2727
},
28-
"homepage": "https://github.com/zoomsphere/angular2-localstorage",
28+
"homepage": "https://github.com/zoomsphere/ngx-store",
2929
"peerDependencies": {
3030
"@angular/core": ">=2.3.0",
3131
"core-js": "^2.4.1",

src/config/config.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
import { WebStorageConfigInterface } from './config.interface';
2+
import { ConfigHelper } from './config.helper';
3+
4+
// TODO allow to set different config for local and session storage
5+
// TODO check if NGXSTORE_CONFIG implements WebStorageConfigInterface
6+
// TODO allow to set configuration in node-config (`config` on npm)
27

38
const DefaultConfig: WebStorageConfigInterface = {
4-
prefix: 'angular2_ws', // TODO: change default to 'ngx_'
5-
clearType: 'decorators', // TODO: change default to 'prefix'
6-
mutateObjects: true
9+
prefix: 'ngx_',
10+
previousPrefix: 'angular2ws_',
11+
clearType: 'prefix',
12+
mutateObjects: true,
713
};
814

9-
// TODO allow to set configuration in node-config (`config` on npm)
1015
// take configuration provided as a global variable
1116
declare const NGXSTORE_CONFIG: WebStorageConfigInterface;
1217

18+
let ConfigFills: WebStorageConfigInterface = {};
19+
let localStoragePrefix = ConfigHelper.getItem('prefix');
20+
if (localStoragePrefix) {
21+
ConfigFills.previousPrefix = localStoragePrefix;
22+
} else if (NGXSTORE_CONFIG && NGXSTORE_CONFIG.previousPrefix !== undefined) {
23+
ConfigFills.previousPrefix = NGXSTORE_CONFIG.previousPrefix;
24+
} else {
25+
ConfigFills.previousPrefix = DefaultConfig.previousPrefix;
26+
}
27+
1328
/**
1429
* @deprecated define global variable `NGXSTORE_CONFIG` instead
1530
*/
16-
export const WEBSTORAGE_CONFIG = DefaultConfig;
31+
export let WEBSTORAGE_CONFIG = DefaultConfig;
1732

1833
// merge default config, deprecated config and global config all together
19-
export const Config = Object.assign({}, DefaultConfig, WEBSTORAGE_CONFIG, NGXSTORE_CONFIG);
34+
export const Config: WebStorageConfigInterface =
35+
Object.assign({}, DefaultConfig, WEBSTORAGE_CONFIG, NGXSTORE_CONFIG, ConfigFills);
36+
37+
ConfigHelper.setItem('prefix', Config.prefix);

src/decorator/webstorage.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { localStorageUtility, sessionStorageUtility } from '../utility';
21
import { LocalStorageService, SessionStorageService, WebStorageServiceInterface } from '../service/webstorage.service';
2+
import { localStorageUtility, sessionStorageUtility } from '../utility';
3+
import { WebStorageUtility } from '../utility/webstorage-utility';
34
import * as isEmpty from 'is-empty';
45
import { Config } from '../config';
5-
import { WebStorageUtility } from '../utility/webstorage-utility';
66

77
export function LocalStorage(key?: string) {
88
return WebStorage(localStorageUtility, LocalStorageService, key);
@@ -29,7 +29,6 @@ function WebStorage(webStorageUtility: WebStorageUtility, service: WebStorageSer
2929
};
3030
}
3131

32-
// let proxy = WebStorageUtility.get(webStorageUtility, key);
3332
let proxy = webStorageUtility.get(key);
3433
service.keys.push(key);
3534

src/service/webstorage.service.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,39 @@ export interface WebStorageServiceInterface {
1717
}
1818

1919
export abstract class WebStorageService {
20-
public static keys: Array<string>;
20+
public static decoratorKeys: Array<string>;
2121

22-
protected _webStorageUtility: WebStorageUtility;
23-
24-
public constructor(protected webStorageUtility: WebStorageUtility) {
25-
this._webStorageUtility = webStorageUtility;
26-
}
22+
public constructor(public utility: WebStorageUtility) { }
2723

2824
/**
29-
* Gets keys from child class
25+
* Gets keys for stored variables created by ngx-store,
26+
* ignores keys that have not been created by decorators and have no prefix at once
3027
* @returns {Array<string>}
3128
*/
3229
public get keys(): Array<string> {
33-
return (<WebStorageServiceInterface>this.constructor).keys;
30+
// get prefixed key if prefix is defined
31+
let prefixKeys = this.utility.keys.filter(key => {
32+
return this.utility.prefix && key.startsWith(this.utility.prefix);
33+
});
34+
let decoratorKeys = (<WebStorageServiceInterface>this.constructor).keys;
35+
return prefixKeys.concat(decoratorKeys);
3436
}
3537

3638
public get config(): WebStorageConfigInterface {
3739
return Config;
3840
}
3941

4042
public get(key: string): any {
41-
return this._webStorageUtility.get(key);
43+
return this.utility.get(key);
4244
}
4345

4446
public set(key: string, value: any): void {
45-
return this._webStorageUtility.set(key, value);
47+
return this.utility.set(key, value);
4648
}
4749

4850
// TODO return true if item existed and false otherwise (?)
4951
public remove(key: string): void {
50-
return this._webStorageUtility.remove(key);
52+
return this.utility.remove(key);
5153
}
5254

5355
/**
@@ -59,17 +61,17 @@ export abstract class WebStorageService {
5961
clearType = clearType || Config.clearType;
6062
if (clearType === 'decorators') {
6163
for (let key of this.keys) {
62-
this._webStorageUtility.remove(key);
64+
this.remove(key);
6365
}
6466
} else if (clearType === 'prefix') {
6567
prefix = prefix || Config.prefix;
66-
this._webStorageUtility.forEach((key) => {
68+
this.utility.forEach((key) => {
6769
if (key.startsWith(prefix)) {
68-
this._webStorageUtility.remove(key);
70+
this.remove(this.utility.trimPrefix(key));
6971
}
7072
});
7173
} else if (clearType === 'all') {
72-
this._webStorageUtility.clear();
74+
this.utility.clear();
7375
}
7476
}
7577
}

src/utility/webstorage-utility.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class WebStorageUtility {
33
protected _storage: Storage;
44

55
public static getSettable(value: any): string {
6-
return typeof value === "string" ? value : JSON.stringify(value);
6+
return typeof value === 'string' ? value : JSON.stringify(value);
77
}
88

99
public static getGettable(value: string): any {
@@ -18,6 +18,32 @@ export class WebStorageUtility {
1818
public constructor(storage: Storage, prefix: string, previousPrefix?: string) {
1919
this._storage = storage;
2020
this._prefix = prefix;
21+
22+
// handle previousPrefix for backward-compatibility and safe config changes below
23+
if (prefix === previousPrefix) return;
24+
if (previousPrefix === null) return;
25+
if (previousPrefix === undefined) return;
26+
this.forEach(key => {
27+
// ignore config settings when previousPrefix = ''
28+
if (key.startsWith(previousPrefix) && !key.startsWith('NGX-STORE_')) {
29+
let nameWithoutPrefix = this.trimPrefix(key);
30+
this.set(nameWithoutPrefix, this._storage.getItem(key));
31+
32+
if (previousPrefix !== '') {
33+
this._storage.removeItem(key);
34+
}
35+
}
36+
});
37+
}
38+
39+
public get prefix(): string {
40+
return this._prefix;
41+
}
42+
43+
public get keys(): Array<string> {
44+
let keys = [];
45+
this.forEach(key => keys.push(key));
46+
return keys;
2147
}
2248

2349
public getStorageKey(key: string): string {
@@ -47,9 +73,21 @@ export class WebStorageUtility {
4773
this._storage.clear();
4874
}
4975

50-
public forEach(func: Function) {
76+
public forEach(func: (key: string, value: any) => any): void {
5177
for (let key in this._storage) {
52-
func(key, this._storage[key]);
78+
func(key, this.getGettable(this._storage[key]));
5379
}
5480
}
81+
82+
public getSettable(value: any): string {
83+
return WebStorageUtility.getSettable(value);
84+
}
85+
86+
public getGettable(value: string): any {
87+
return WebStorageUtility.getGettable(value);
88+
}
89+
90+
public trimPrefix(key: string): string {
91+
return key.replace(this.prefix, '');
92+
}
5593
}

0 commit comments

Comments
 (0)