redux-state-branch
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -51,6 +51,8 @@ import { Reducer, Middleware, StoreEnhancer } from 'redux'; | ||
/** Get an item by id */ | ||
byId: (state: any, { id }: { | ||
byId: <T extends string | string[] | null>(state: any, { id }: { | ||
/** The ID of your item */ | ||
id?: string | null | undefined; | ||
}) => ItemT | undefined; | ||
id?: T | undefined; | ||
}) => T extends string[] ? { | ||
[itemId: string]: ItemT | undefined; | ||
} : ItemT | undefined; | ||
/** Gets an object map of unique ids to items {itemId: ItemT} */ | ||
@@ -205,6 +207,8 @@ mapById: (state: any) => { | ||
/** Get an item by id */ | ||
byId: (state: any, { id }: { | ||
byId: <T extends string | string[] | null>(state: any, { id }: { | ||
/** The ID of your item */ | ||
id?: string | null | undefined; | ||
}) => ItemT | undefined; | ||
id?: T | undefined; | ||
}) => T extends string[] ? { | ||
[itemId: string]: ItemT | undefined; | ||
} : ItemT | undefined; | ||
/** Gets an object map of unique ids to items {itemId: ItemT} */ | ||
@@ -211,0 +215,0 @@ mapById: (state: any) => { |
@@ -80,2 +80,10 @@ "use strict"; | ||
var id = _a.id; | ||
if (Array.isArray(id)) { | ||
var objects = id.reduce(function (acc, aId) { | ||
acc[aId] = state[name].items[aId || '']; | ||
return acc; | ||
}, {}); | ||
// @ts-ignore This is a valid return, but Typescript can't return Array.isArray ahead of time | ||
return objects; | ||
} | ||
return state[name].items[id || '']; | ||
@@ -82,0 +90,0 @@ }, |
@@ -22,2 +22,6 @@ "use strict"; | ||
name: 'James Friedman' | ||
}, | ||
testUser2: { | ||
id: 'testUser2', | ||
name: 'James Friedman 2' | ||
} | ||
@@ -211,2 +215,10 @@ } | ||
}); | ||
it('byId multiple', function () { | ||
var _a; | ||
var res = branch.select.byId(state, { id: ['testUser', 'testUser2'] }); | ||
expect(res).toEqual((_a = {}, | ||
_a[DEFAULT_STATE.items.testUser.id] = DEFAULT_STATE.items.testUser, | ||
_a[DEFAULT_STATE.items.testUser2.id] = DEFAULT_STATE.items.testUser2, | ||
_a)); | ||
}); | ||
it('byId undefined', function () { | ||
@@ -229,2 +241,3 @@ expect(branch.select.byId(state, { id: 'nonExistant' })).toBeUndefined(); | ||
_a['James Friedman'] = [DEFAULT_STATE.items.testUser], | ||
_a['James Friedman 2'] = [DEFAULT_STATE.items.testUser2], | ||
_a)); | ||
@@ -231,0 +244,0 @@ }); |
{ | ||
"name": "redux-state-branch", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"private": false, | ||
@@ -22,5 +22,6 @@ "description": "A library for simplified Redux development.", | ||
"devDependencies": { | ||
"@types/jest": "^24.0.23", | ||
"@types/node": "^12.12.14", | ||
"@types/react": "^16.9.13", | ||
"@babel/runtime": "^7.7.7", | ||
"@types/jest": "^24.0.25", | ||
"@types/node": "^12.12.24", | ||
"@types/react": "^16.9.17", | ||
"@types/react-dom": "^16.9.4", | ||
@@ -35,3 +36,3 @@ "@types/react-redux": "^7.1.5", | ||
"prettier-tslint": "^0.4.2", | ||
"prismjs": "^1.17.1", | ||
"prismjs": "^1.18.0", | ||
"react": "^16.12.0", | ||
@@ -43,5 +44,5 @@ "react-app-rewired": "^2.1.5", | ||
"react-router-dom": "^5.1.2", | ||
"react-scripts": "^3.2.0", | ||
"rmwc": "^5.7.1", | ||
"typescript": "^3.7.2" | ||
"react-scripts": "^3.3.0", | ||
"rmwc": "^5.7.2", | ||
"typescript": "^3.7.4" | ||
}, | ||
@@ -59,3 +60,4 @@ "browserslist": { | ||
] | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
55186
910
23