From af4afe21af19339e108abe00041fea10f664ac0c Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Mon, 11 Mar 2019 16:12:00 -0300 Subject: [PATCH] fix(docz): merge menus on useMenus --- core/docz/src/hooks/useMenus.ts | 10 +++++----- core/docz/src/utils/helpers.ts | 6 +----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/core/docz/src/hooks/useMenus.ts b/core/docz/src/hooks/useMenus.ts index 0f19d4f35..e1a051c29 100644 --- a/core/docz/src/hooks/useMenus.ts +++ b/core/docz/src/hooks/useMenus.ts @@ -1,10 +1,10 @@ import { useMemo, useContext } from 'react' -import { pipe, get, omit, flattenDepth } from 'lodash/fp' +import { pipe, get, omit, flattenDepth, unionBy } from 'lodash/fp' import { ulid } from 'ulid' import match from 'match-sorter' import sort from 'array-sort' -import { compare, flatArrFromObject, mergeArrBy } from '../utils/helpers' +import { compare, flatArrFromObject } from '../utils/helpers' import { Entry, MenuItem, doczState } from '../state' const noMenu = (entry: Entry) => !entry.menu @@ -25,9 +25,9 @@ const parseMenu = (entries: Entry[]) => (name: string) => ({ type Menus = MenuItem[] const menusFromEntries = (entries: Entry[]) => { - const entriesWithoutMenu = entries.filter(noMenu).map(entryAsMenu) + const entriesWithoutMenu = entries.filter(noMenu).map(entryAsMenu) as any const menus = flatArrFromObject(entries, 'menu').map(parseMenu(entries)) - return [...entriesWithoutMenu, ...menus] + return unionBy('name', menus, entriesWithoutMenu) } const parseItemStr = (item: MenuItem | string) => @@ -56,7 +56,7 @@ const normalizeAndClean = pipe( const mergeMenus = (entriesMenu: Menus, configMenu: Menus): Menus => { const first = entriesMenu.map(normalizeAndClean) const second = configMenu.map(normalizeAndClean) - const merged = mergeArrBy('name', first, second) + const merged = unionBy('name', first, second) as MenuItem[] return merged.map(item => { if (!item.menu) return item diff --git a/core/docz/src/utils/helpers.ts b/core/docz/src/utils/helpers.ts index b6810407a..6a735bd37 100644 --- a/core/docz/src/utils/helpers.ts +++ b/core/docz/src/utils/helpers.ts @@ -1,4 +1,4 @@ -import { get, unionBy } from 'lodash/fp' +import { get } from 'lodash/fp' export const isFn = (value: any): boolean => typeof value === 'function' @@ -16,7 +16,3 @@ export function compare(a: T, b: T, reverse?: boolean): number { if (a > b) return reverse ? -1 : 1 return 0 } - -export function mergeArrBy(prop: string, a: T[], b: T[]): T[] { - return unionBy(prop, a)(b) -}