Socket
Socket
Sign inDemoInstall

ngrx-store-localstorage

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngrx-store-localstorage - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

2

dist/index.d.ts

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

export declare const localStorageSync: (keys: string[], rehydrate?: boolean) => (reducer: any) => (state: {}, action: any) => any;
export declare const localStorageSync: (keys: any[], rehydrate?: boolean) => (reducer: any) => (state: {}, action: any) => any;

@@ -15,5 +15,9 @@ "use strict";

return keys.map(function (key) {
if (typeof (key) !== 'string') {
var attr = key;
if (typeof key == 'object') {
attr = Object.keys(key)[0];
}
if (typeof (attr) !== 'string') {
throw new TypeError("localStorageSync Unknown Parameter Type: "
+ ("Expected type of string, got " + typeof key));
+ ("Expected type of string, got " + typeof attr));
}

@@ -25,2 +29,5 @@ return key;

return keys.reduce(function (acc, curr) {
if (typeof curr == 'object') {
curr = Object.keys(curr)[0];
}
var stateSlice = localStorage.getItem(curr);

@@ -37,5 +44,16 @@ if (stateSlice) {

var stateSlice = state[key];
if (typeof key == 'object') {
var name_1 = Object.keys(key)[0];
stateSlice = state[name_1];
if (key[name_1]) {
stateSlice = key[name_1].reduce(function (memo, attr) {
memo[attr] = stateSlice[attr];
return memo;
}, {});
}
key = name_1;
}
if (typeof (stateSlice) !== 'undefined') {
try {
localStorage.setItem(key, JSON.stringify(state[key]));
localStorage.setItem(key, JSON.stringify(stateSlice));
}

@@ -42,0 +60,0 @@ catch (e) {

{
"name": "ngrx-store-localstorage",
"version": "0.1.3",
"version": "0.1.4",
"description": "State and local storage syncing for @ngrx/store",

@@ -42,7 +42,7 @@ "main": "./dist/index.js",

"typings": "^0.6.6",
"zone.js": "0.5.15",
"zone.js": "^0.6.6",
"@ngrx/store": "^2.0.0",
"rxjs": "^5.0.0-beta.6"
"rxjs": "5.0.0-beta.6"
},
"typings": "./dist/index.d.ts"
}

@@ -12,5 +12,5 @@ # ngrx-store-localstorage

1. Import `compose` and `combineReducers` from `@ngrx/store` and `@ngrx/core/compose`
2. Invoke the `localStorageSync` function after `combineReducers`, specifying the slices of state you would like to keep synced with local storage.
2. Invoke the `localStorageSync` function after `combineReducers`, specifying the slices of state you would like to keep synced with local storage.
3. Optionally specify whether to rehydrate this state from local storage as `initialState` on application bootstrap.
4. Invoke composed function with application reducers as an argument to `provideStore`.
4. Invoke composed function with application reducers as an argument to `provideStore`.

@@ -40,3 +40,3 @@ ```ts

## API
### `localStorageSync(keys : string[], rehydrateState : boolean = false) : Reducer`
### `localStorageSync(keys : any[], rehydrateState : boolean = false) : Reducer`
Provide state (reducer) keys to sync with local storage. Optionally specify whether to rehydrate `initialState` from local storage on bootstrap.

@@ -46,3 +46,7 @@ *Returns a meta-reducer*

#### Arguments
* `keys` \(*string[]*): State keys to sync with local storage
* `keys` State keys to sync with local storage. The keys can be defined in two different formats:
* \(*string[]*): array of strings representing the state (reducer) keys. Full state will be synced (e.g. `localStorageSync(['todos'])`).
* \(*object[]*): Array of objects where for each object the key represents the state key and the value represents an array of properties which should be synced. This allows for the partial state sync (e.g. `localStorageSync([{todos: ['name', 'status'] }, ... ])`)
* `rehydrateState` \(*boolean? = false*): Pull initial state from local storage on startup

@@ -14,8 +14,14 @@ const INIT_ACTION = "@ngrx/store/init";

const validateStateKeys = (keys: string[]) => {
const validateStateKeys = (keys: any[]) => {
return keys.map(key => {
if(typeof(key) !== 'string'){
let attr = key;
if (typeof key == 'object') {
attr = Object.keys(key)[0];
}
if(typeof(attr) !== 'string'){
throw new TypeError(
`localStorageSync Unknown Parameter Type: `
+ `Expected type of string, got ${typeof key}`
+ `Expected type of string, got ${typeof attr}`
);

@@ -29,2 +35,5 @@ }

return keys.reduce((acc, curr) => {
if (typeof curr == 'object') {
curr = Object.keys(curr)[0];
}
let stateSlice = localStorage.getItem(curr);

@@ -40,6 +49,22 @@ if(stateSlice){

keys.forEach(key => {
let stateSlice = state[key];
if (typeof key == 'object') {
let name = Object.keys(key)[0];
stateSlice = state[name];
if (key[name]) {
stateSlice = key[name].reduce((memo, attr) => {
memo[attr] = stateSlice[attr];
return memo;
}, {});
}
key = name;
}
if (typeof(stateSlice) !== 'undefined') {
try{
localStorage.setItem(key, JSON.stringify(state[key]));
localStorage.setItem(key, JSON.stringify(stateSlice));
} catch(e){

@@ -52,3 +77,3 @@ console.warn('Unable to save state to localStorage:', e);

export const localStorageSync = (keys : string[], rehydrate : boolean = false) => (reducer : any) => {
export const localStorageSync = (keys : any[], rehydrate : boolean = false) => (reducer : any) => {
const stateKeys = validateStateKeys(keys);

@@ -55,0 +80,0 @@ const rehydratedState = rehydrate ? rehydrateApplicationState(stateKeys) : undefined;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc