create-reducer-tree
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -10,2 +10,3 @@ export declare function createReducerTree(reducerTree: any): any; | ||
reducer: Function; | ||
initialState: any; | ||
}>; |
@@ -31,6 +31,7 @@ "use strict"; | ||
exports.fetchActionsForTree = fetchActionsForTree; | ||
var states = []; | ||
function generateInitialState(tree) { | ||
function removeActionsAndReducers(state) { | ||
for (var i in state) { | ||
if (i === "actions" || i === "reducer") { | ||
if (i === "actions" || i === "reducer" || i === "initialState") { | ||
delete state[i]; | ||
@@ -43,2 +44,3 @@ } | ||
removeActionsAndReducers(state); | ||
states.push(state); | ||
return state; | ||
@@ -50,3 +52,4 @@ } | ||
var keys = fetchKeysInItem(reducerTree); | ||
var initialState = generateInitialState(reducerTree); | ||
var initialState = reducerTree.initialState || generateInitialState(reducerTree); | ||
console.log(initialState); | ||
return function (state, action) { | ||
@@ -80,2 +83,5 @@ if (state === void 0) { state = clone(initialState); } | ||
} | ||
if (deepestLevel.initialState === undefined) { | ||
throw Error("The deepest level of every reducer branch should have initialData"); | ||
} | ||
}); | ||
@@ -87,3 +93,3 @@ } | ||
var keys = fetchKeysInItem(reducerTree); | ||
var filtered = keys.filter(function (key) { return key !== "actions" && key !== "reducer"; }); | ||
var filtered = keys.filter(function (key) { return key !== "actions" && key !== "reducer" && key !== "initialState"; }); | ||
if (filtered.length === 0) { | ||
@@ -94,3 +100,3 @@ deepestLevels.push(reducerTree); | ||
if (typeof reducerTree[key] === "object" && reducerTree[key]) { | ||
if (key !== "actions" && key !== "reducer") { | ||
if (key !== "actions" && key !== "reducer" && key !== "initialState") { | ||
getDeepestLevels(reducerTree[key], deepestLevels); | ||
@@ -97,0 +103,0 @@ } |
{ | ||
"name": "create-reducer-tree", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A tool to create a redux reducer composition tree", | ||
@@ -10,3 +10,3 @@ "main": "dist/create-reducer-tree.js", | ||
"prebuild": "rimraf dist && typings i", | ||
"prepublish": "npm run build", | ||
"prepublish": "npm run test && npm run build", | ||
"test": "karma start" | ||
@@ -13,0 +13,0 @@ }, |
@@ -23,12 +23,14 @@ # Create-reducer-tree | ||
let reducerComposer: any = { | ||
groceryManagement /* needs reducer*/: { | ||
data /* needs reducer*/: { | ||
groceryManagement: { | ||
data: { | ||
groceries: { | ||
actions: ["ACTION1", "ACTION2"], | ||
initialState: [], | ||
actions: ["GROCERIES"], | ||
reducer: groceriesReducer | ||
} | ||
}, | ||
container /* needs reducer*/: { | ||
container: { | ||
currentList: { | ||
actions: ["ACTION3", "ACTION4"], | ||
initialState: null, | ||
actions: ["CURRENTLIST", "CURRENTLIST2"], | ||
reducer: currentListReducer | ||
@@ -39,11 +41,13 @@ } | ||
listManagement: { | ||
data /* needs reducer*/: { | ||
data: { | ||
lists: { | ||
actions: ["ACTION5"], | ||
initialState: [], | ||
actions: ["LISTS"], | ||
reducer: listsReducer | ||
} | ||
}, | ||
container /* needs reducer*/: { | ||
container: { | ||
groceryListsEdit: { | ||
actions: ["ACTION6"], | ||
initialState: {list: null}, | ||
actions: ["GROCERYLISTSEDIT"], | ||
reducer: groceryListsEditReducer | ||
@@ -54,12 +58,12 @@ } | ||
common: { | ||
container /* needs reducer*/: { | ||
container: { | ||
application: { | ||
actions: ["ACTION7"], | ||
initialState: {isBusy: false}, | ||
actions: ["APPLICATION"], | ||
reducer: applicationReducer | ||
}, | ||
collapsableSidebar /* needs reducer*/: { | ||
isCollapsed: { | ||
actions: ["ACTION8"], | ||
reducer: collapsableSidebarReducer | ||
} | ||
collapsableSidebar: { | ||
initialState: {isCollapsed: false}, | ||
actions: ["COLLAPSABLESIDEBAR"], | ||
reducer: collapsableSidebarReducer | ||
} | ||
@@ -96,3 +100,3 @@ } | ||
```typescript | ||
export function groceryManagementReducer(state: GroceryManagementState = INITIAL_STATE.groceryManagement, action: Action): GroceryManagementState { | ||
export function groceryManagementReducer(state: GroceryManagementState = {data:{...}}, action: Action): GroceryManagementState { | ||
switch (action.type) { | ||
@@ -99,0 +103,0 @@ case "ACTION1": |
@@ -18,2 +18,3 @@ import {createReducerTree} from "./create-reducer-tree"; | ||
groceries: { | ||
initialState: [], | ||
actions: ["GROCERIES"], | ||
@@ -25,2 +26,3 @@ reducer: groceriesReducer | ||
currentList: { | ||
initialState: null, | ||
actions: ["CURRENTLIST", "CURRENTLIST2"], | ||
@@ -34,2 +36,3 @@ reducer: currentListReducer | ||
lists: { | ||
initialState: [], | ||
actions: ["LISTS"], | ||
@@ -41,2 +44,3 @@ reducer: listsReducer | ||
groceryListsEdit: { | ||
initialState: {list: null}, | ||
actions: ["GROCERYLISTSEDIT"], | ||
@@ -50,2 +54,3 @@ reducer: groceryListsEditReducer | ||
application: { | ||
initialState: {isBusy: false}, | ||
actions: ["APPLICATION"], | ||
@@ -55,2 +60,3 @@ reducer: applicationReducer | ||
collapsableSidebar: { | ||
initialState: {isCollapsed: false}, | ||
actions: ["COLLAPSABLESIDEBAR"], | ||
@@ -92,3 +98,15 @@ reducer: collapsableSidebarReducer | ||
}); | ||
describe("when branch does not have any initialState", () => { | ||
it("should throw an error", () => { | ||
let badReducerComposer = clone(reducerComposer); | ||
badReducerComposer.listManagement.data.lists.initialState = undefined; | ||
expect(() => createReducerTree(badReducerComposer)) | ||
.toThrowError("The deepest level of every reducer branch should have initialData"); | ||
delete badReducerComposer.listManagement.data.lists.initialState; | ||
expect(() => createReducerTree(badReducerComposer)) | ||
.toThrowError("The deepest level of every reducer branch should have initialData"); | ||
}); | ||
}); | ||
describe("when the groceryManagement reducer is called", () => { | ||
@@ -102,3 +120,3 @@ describe("and the action is GROCERIES, CURRENTLIST OR CURRENTLIST2", () => { | ||
groceriesReducer.and.returnValue([]); | ||
let result = groceryManagementReducer(initialState, {type: "GROCERIES"}); | ||
@@ -105,0 +123,0 @@ expect(Object.keys(result)).toEqual(Object.keys(reducerComposer.groceryManagement)); |
@@ -31,7 +31,7 @@ declare function require(string: string): any; | ||
} | ||
let states = []; | ||
export function generateInitialState(tree: any): any { | ||
function removeActionsAndReducers(state) { | ||
for (var i in state) { | ||
if (i === "actions" || i === "reducer") { | ||
if (i === "actions" || i === "reducer" || i === "initialState") { | ||
delete state[i]; | ||
@@ -45,2 +45,3 @@ } | ||
removeActionsAndReducers(state); | ||
states.push(state); | ||
return state; | ||
@@ -52,3 +53,4 @@ } | ||
let keys = fetchKeysInItem(reducerTree); | ||
let initialState = generateInitialState(reducerTree); | ||
let initialState = reducerTree.initialState || generateInitialState(reducerTree); | ||
console.log(initialState) | ||
return function (state: any = clone(initialState), action: {type: string, payload: any}) { | ||
@@ -81,9 +83,12 @@ if (actions.indexOf(action.type) > -1) { | ||
} | ||
if (deepestLevel.initialState === undefined) { | ||
throw Error("The deepest level of every reducer branch should have initialData"); | ||
} | ||
}); | ||
} | ||
export function getDeepestLevels(reducerTree: any, deepestLevels: any = []): Array<{actions: Array<string>, reducer: Function}> { | ||
export function getDeepestLevels(reducerTree: any, deepestLevels: any = []): Array<{actions: Array<string>, reducer: Function, initialState: any}> { | ||
let keys: Array<string> = fetchKeysInItem(reducerTree); | ||
// if no keys except maybe "actions" or "reducer" | ||
let filtered = keys.filter((key) => key !== "actions" && key !== "reducer"); | ||
let filtered = keys.filter((key) => key !== "actions" && key !== "reducer" && key !== "initialState"); | ||
if (filtered.length === 0) { | ||
@@ -94,3 +99,3 @@ deepestLevels.push(reducerTree); | ||
if (typeof reducerTree[key] === "object" && reducerTree[key]) { | ||
if (key !== "actions" && key !== "reducer") { | ||
if (key !== "actions" && key !== "reducer" && key !== "initialState") { | ||
getDeepestLevels(reducerTree[key], deepestLevels); | ||
@@ -97,0 +102,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30912
552
110