redux-state-branch
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -51,8 +51,13 @@ import { Reducer, Middleware, StoreEnhancer } from 'redux'; | ||
/** Get an item by id */ | ||
byId: <T extends string | string[] | null>(state: any, { id }: { | ||
byId: (state: any, { id }: { | ||
/** The ID of your item */ | ||
id?: T | undefined; | ||
}) => T extends string[] ? { | ||
id?: string | null | undefined; | ||
}) => ItemT | undefined; | ||
/** Get multiple items by their id */ | ||
byIds: (state: any, { ids }: { | ||
/** The ID of your item */ | ||
ids: (string | null | undefined)[]; | ||
}) => { | ||
[itemId: string]: ItemT | undefined; | ||
} : ItemT | undefined; | ||
}; | ||
/** Gets an object map of unique ids to items {itemId: ItemT} */ | ||
@@ -207,8 +212,13 @@ mapById: (state: any) => { | ||
/** Get an item by id */ | ||
byId: <T extends string | string[] | null>(state: any, { id }: { | ||
byId: (state: any, { id }: { | ||
/** The ID of your item */ | ||
id?: T | undefined; | ||
}) => T extends string[] ? { | ||
id?: string | null | undefined; | ||
}) => ItemT | undefined; | ||
/** Get multiple items by their id */ | ||
byIds: (state: any, { ids }: { | ||
/** The ID of your item */ | ||
ids: (string | null | undefined)[]; | ||
}) => { | ||
[itemId: string]: ItemT | undefined; | ||
} : ItemT | undefined; | ||
}; | ||
/** Gets an object map of unique ids to items {itemId: ItemT} */ | ||
@@ -215,0 +225,0 @@ mapById: (state: any) => { |
@@ -80,12 +80,16 @@ "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 || '']; | ||
}, | ||
/** Get multiple items by their id */ | ||
byIds: function ( | ||
/** Your store's state object. */ | ||
state, _a) { | ||
var ids = _a.ids; | ||
return ids.reduce(function (acc, id) { | ||
if (id) { | ||
acc[id] = state[name].items[id || '']; | ||
} | ||
return acc; | ||
}, {}); | ||
}, | ||
/** Gets an object map of unique ids to items {itemId: ItemT} */ | ||
@@ -92,0 +96,0 @@ mapById: function ( |
@@ -212,7 +212,8 @@ "use strict"; | ||
it('byId', function () { | ||
expect(branch.select.byId(state, { id: 'testUser' })).toBe(DEFAULT_STATE.items.testUser); | ||
var res = branch.select.byId(state, { id: 'testUser' }); | ||
expect(res).toBe(DEFAULT_STATE.items.testUser); | ||
}); | ||
it('byId multiple', function () { | ||
it('byIds multiple', function () { | ||
var _a; | ||
var res = branch.select.byId(state, { id: ['testUser', 'testUser2'] }); | ||
var res = branch.select.byIds(state, { ids: ['testUser', 'testUser2'] }); | ||
expect(res).toEqual((_a = {}, | ||
@@ -219,0 +220,0 @@ _a[DEFAULT_STATE.items.testUser.id] = DEFAULT_STATE.items.testUser, |
{ | ||
"name": "redux-state-branch", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "A library for simplified Redux development.", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
55521
925