lodash#keyBy JavaScript Examples
The following examples show how to use
lodash#keyBy.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: favorites.js From climatescape.org with MIT License | 6 votes |
// Accepts raw data from the GetFavorites query and returns an object indexed
// by record_id that has both count and the user's favorite ID
function indexFavoritesData(data) {
if (!data) return {}
const counts = keyBy(data.favoritesCount, "recordId")
const favorites = keyBy(data.favorites, "recordId")
return mapValues(counts, ({ recordId, count }) => ({
count, // The total count of favorites
id: favorites[recordId]?.id, // The user's favorite
}))
}
Example #2
Source File: index.js From covid19-dashboard with MIT License | 6 votes |
BigPicture = props => {
const {isMobileDevice} = useContext(AppContext)
const [selectedMapId, setSelectedMapId] = useState('Carte des hospitalisations')
const [selectedStat, setSelectedStat] = useState(null)
const Component = isMobileDevice ? MobileBigPicture : DesktopBigPicture
useEffect(() => {
const mapProperties = keyBy(bigPictureMaps, 'property')
if (mapProperties[selectedStat]) {
setSelectedMapId(mapProperties[selectedStat].name)
} else if (selectedStat === 'mixed') {
setSelectedMapId(mapProperties.hospitalises.name)
}
}, [selectedStat])
return (
<BigPictureContext.Provider value={{selectedMapId, setSelectedMapId, selectedStat, setSelectedStat}}>
<Component {...props} />
</BigPictureContext.Provider>
)
}
Example #3
Source File: index.js From covid19-dashboard with MIT License | 6 votes |
Vaccinations = props => {
const {isMobileDevice} = useContext(AppContext)
const [selectedMapId, setSelectedMapId] = useState('Nombre de premières doses injectées (au total)')
const [selectedStat, setSelectedStat] = useState(null)
const Component = isMobileDevice ? MobileVaccinations : DesktopVaccinations
useEffect(() => {
const mapProperties = keyBy(VaccinationsMaps, 'property')
if (mapProperties[selectedStat]) {
setSelectedMapId(mapProperties[selectedStat].name)
} else if (selectedStat === 'mixed') {
setSelectedMapId(mapProperties.hospitalises.name)
}
}, [selectedStat])
return (
<VaccinationsContext.Provider value={{selectedMapId, setSelectedMapId, selectedStat, setSelectedStat}}>
<Component {...props} />
</VaccinationsContext.Provider>
)
}
Example #4
Source File: index.js From covid19-dashboard with MIT License | 6 votes |
Vaccins = props => {
const {isMobileDevice} = useContext(AppContext)
const [selectedMapId, setSelectedMapId] = useState('Nombre de doses en stock dans les établissements de santé pivots du flux B')
const [selectedStat, setSelectedStat] = useState(null)
const Component = isMobileDevice ? MobileVaccins : DesktopVaccins
useEffect(() => {
const mapProperties = keyBy(VaccinsMaps, 'property')
if (mapProperties[selectedStat]) {
setSelectedMapId(mapProperties[selectedStat].name)
} else if (selectedStat === 'mixed') {
setSelectedMapId(mapProperties.hospitalises.name)
}
}, [selectedStat])
return (
<VaccinsContext.Provider value={{selectedMapId, setSelectedMapId, selectedStat, setSelectedStat}}>
<Component {...props} />
</VaccinsContext.Provider>
)
}
Example #5
Source File: favorites.js From goodhere with MIT License | 6 votes |
// Accepts raw data from the GetFavorites query and returns an object indexed
// by record_id that has both count and the user's favorite ID
function indexFavoritesData(data) {
if (!data) return {}
const counts = keyBy(data.favoritesCount, "recordId")
const favorites = keyBy(data.favorites, "recordId")
return mapValues(counts, ({ recordId, count }) => ({
count, // The total count of favorites
id: favorites[recordId]?.id, // The user's favorite
}))
}
Example #6
Source File: depositReducer.js From web-wallet with Apache License 2.0 | 6 votes |
function depositReducer (state = initialState, action) {
switch (action.type) {
case 'DEPOSIT/CREATE/SUCCESS':
const isEth = action.payload.isEth;
if (isEth) {
return {
...state,
eth: {
...state.eth,
[action.payload.transactionHash]: action.payload
}
};
}
return {
...state,
erc20: {
...state.erc20,
[action.payload.transactionHash]: action.payload
}
};
case 'DEPOSIT/CHECKALL/SUCCESS':
case 'DEPOSIT/GETALL/SUCCESS':
const { eth, erc20 } = action.payload;
return {
...state,
eth: {
...state.eth,
...keyBy(eth, 'transactionHash')
},
erc20: {
...state.erc20,
...keyBy(erc20, 'transactionHash')
}
};
default:
return state;
}
}
Example #7
Source File: exitReducer.js From web-wallet with Apache License 2.0 | 6 votes |
function exitReducer (state = initialState, action) {
switch (action.type) {
case 'EXIT/GETALL/SUCCESS':
// action.payload will be null on an event timeout, so return old state
if (!action.payload) {
return state;
}
return { ...state, ...action.payload };
case 'EXIT/CREATE/SUCCESS':
return {
...state,
pending: {
...state.pending,
[action.payload.transactionHash]: action.payload
}
};
case 'EXIT/CHECKALL/SUCCESS':
return {
...state,
pending: {
...state.pending,
...keyBy(action.payload, 'transactionHash')
}
};
default:
return state;
}
}
Example #8
Source File: transactionReducer.js From web-wallet with Apache License 2.0 | 6 votes |
function transactionReducer (state = initialState, action) {
switch (action.type) {
case 'TRANSACTION/GETALL/SUCCESS':
return { ...state, ...keyBy(action.payload, 'txhash') };
case 'TRANSFER/CREATE/SUCCESS':
return { ...state, [action.payload.txhash]: action.payload };
default:
return state;
}
}
Example #9
Source File: updateSymbols.js From bonded-stablecoin-ui with MIT License | 5 votes |
updateSymbols = () => async (dispatch, getState, socket) => {
const store = getState();
let assets = [];
const list = store.list.data;
const now = Date.now();
for (const address in list) {
const coin = list[address];
if ("fund" in coin) {
const newAssets = []
if(!(coin.params.reserve_asset in store.symbols) || (store.symbols[coin.params.reserve_asset].updated_at + (expireIn * 86400 * 1000)) < now){
newAssets.push(coin.params.reserve_asset)
}
if(!(coin.asset_2 in store.symbols) || (store.symbols[coin.asset_2].updated_at + (expireIn * 86400 * 1000)) < now){
newAssets.push(coin.asset_2)
}
if(!(coin.asset_fund in store.symbols) || (store.symbols[coin.asset_fund].updated_at + (expireIn * 86400 * 1000)) < now){
newAssets.push(coin.asset_fund)
}
if(!(coin.asset_stable in store.symbols) || (store.symbols[coin.asset_stable].updated_at + (expireIn * 86400 * 1000)) < now){
newAssets.push(coin.asset_stable)
}
assets = [...assets, ...newAssets];
}
}
assets = uniq(assets);
const getSymbols = assets.map((asset) => {
return socket.api.getSymbolByAsset(
config.TOKEN_REGISTRY,
asset
).then((symbol) => {
return { asset, symbol, "updated_at": Date.now() }
});
});
const symbols = await Promise.all(getSymbols);
const symbolsByAsset = keyBy(symbols, "asset");
dispatch({
type: UPDATE_SYMBOLS_LIST,
payload: symbolsByAsset
})
}
Example #10
Source File: index.js From covid19-dashboard with MIT License | 5 votes |
CovidTests = props => {
const {date, setForcedDate, selectedLocation, isMobileDevice} = useContext(AppContext)
const [selectedMapId, setSelectedMapId] = useState('Tests positifs - ce jour')
const [selectedStat, setSelectedStat] = useState(null)
const Component = isMobileDevice ? MobileCovidTests : DesktopCovidTests
useEffect(() => {
let isCancelled = false
async function fetchMostRecentDateForData() {
const report = await getReport(date, selectedLocation)
const mostRecentDate = findMostRecentDateForData(report, 'testsRealises')
if (!isCancelled) {
setForcedDate(mostRecentDate === date ? null : mostRecentDate)
}
}
fetchMostRecentDateForData()
return () => {
isCancelled = true
}
}, [date, selectedLocation, setForcedDate])
useEffect(() => {
const mapProperties = keyBy(CovidTestsMaps, 'property')
if (mapProperties[selectedStat]) {
setSelectedMapId(mapProperties[selectedStat].name)
} else if (selectedStat === 'mixed') {
setSelectedMapId(mapProperties.testsPositifs.name)
}
}, [selectedStat])
return (
<CovidTestsContext.Provider value={{selectedMapId, setSelectedMapId, selectedStat, setSelectedStat}}>
<Component {...props} />
</CovidTestsContext.Provider>
)
}
Example #11
Source File: index.js From covid19-dashboard with MIT License | 5 votes |
Indicators = props => {
const {date, forcedDate, selectedLocation, isMobileDevice, setForcedDate} = useContext(AppContext)
const selectedDate = forcedDate || date
const [selectedMapId, setSelectedMapId] = useState('Taux d’occupation des lits en réanimation')
const [selectedStat, setSelectedStat] = useState('tauxOccupationRea')
const [report, setReport] = useState(null)
const Component = isMobileDevice ? MobileIndicators : DesktopIndicators
useEffect(() => {
let isCancelled = false
async function fetchMostRecentDateForData() {
const report = await getReport(date, selectedLocation)
const mostRecentDate = findMostRecentDateForData(report, selectedStat)
if (!isCancelled) {
setForcedDate(mostRecentDate === date ? null : mostRecentDate)
}
}
fetchMostRecentDateForData()
return () => {
isCancelled = true
}
}, [date, selectedLocation, selectedStat, setForcedDate])
useEffect(() => {
const mapProperties = keyBy(IndicatorsMaps, 'property')
if (mapProperties[selectedStat]) {
setSelectedMapId(mapProperties[selectedStat].name)
}
}, [selectedStat])
useEffect(() => {
async function fetchReport() {
setReport(await getReport(selectedDate, selectedLocation))
}
fetchReport()
}, [selectedDate, selectedLocation])
return (
<IndicatorsContext.Provider value={{report, selectedDate, selectedMapId, setSelectedMapId, selectedStat, setSelectedStat}}>
<Component {...props} />
</IndicatorsContext.Provider>
)
}
Example #12
Source File: vaccinations-statistics.js From covid19-dashboard with MIT License | 5 votes |
charts = keyBy(indicateurs.map(indicateur => ({
type: 'indicateur',
options: {
label: indicateur.label,
metricName: indicateur.name,
color: indicateur.color
}
})), i => i.options.metricName)
Example #13
Source File: vaccins-statistics.js From covid19-dashboard with MIT License | 5 votes |
charts = keyBy(indicateurs.map(indicateur => ({
type: 'indicateur',
options: {
label: indicateur.label,
metricName: indicateur.name,
color: indicateur.color
}
})), i => i.options.metricName)
Example #14
Source File: feeReducer.js From web-wallet with Apache License 2.0 | 5 votes |
function feeReducer (state = initialState, action) {
switch (action.type) {
case 'FEE/GET/SUCCESS':
return { ...state, ...keyBy(action.payload, 'currency') };
default:
return state;
}
}
Example #15
Source File: networkService.js From web-wallet with Apache License 2.0 | 5 votes |
async getExits () {
try {
const { contract } = await this.rootChain.getPaymentExitGame();
let allExits = [];
try {
allExits = await contract.getPastEvents('ExitStarted', {
filter: { owner: this.account },
fromBlock: 0
});
} catch (error) {
console.log('Getting past ExitStarted events timed out: ', error.message);
return null;
}
const exitedExits = [];
for (const exit of allExits) {
let isFinalized = [];
try {
isFinalized = await contract.getPastEvents('ExitFinalized', {
filter: { exitId: exit.returnValues.exitId.toString() },
fromBlock: 0
});
} catch (error) {
console.log('Getting past ExitFinalized events timed out: ', error.message);
return null;
}
if (isFinalized.length) {
exitedExits.push(exit);
}
}
const pendingExits = allExits
.filter(i => {
const foundMatch = exitedExits.find(x => x.blockNumber === i.blockNumber);
return !foundMatch;
})
.map(this.getExitStatus);
return {
pending: { ...keyBy(pendingExits, 'transactionHash') },
exited: { ...keyBy(exitedExits, 'transactionHash') }
};
} catch (error) {
throw new WebWalletError({
originalError: error,
customErrorMessage: 'Could not fetch past exit information. Please try restarting the application.',
reportToSentry: false,
reportToUi: true
});
}
}