lodash#memoize JavaScript Examples

The following examples show how to use lodash#memoize. 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: maplayout.js    From mapstore2-cadastrapp with GNU General Public License v3.0 6 votes vote down vote up
mapLayoutValuesSelector = memoize((state, attributes = {}, isDock = false) => {
    const layout = mapLayoutSelector(state);
    const boundingSidebarRect = boundingSidebarRectSelector(state);
    return layout && Object.keys(layout).filter(key =>
        attributes[key]).reduce((a, key) => {
        if (isDock) {
            return ({...a, [key]: (boundingSidebarRect[key] ?? layout[key])});
        }
        return ({...a, [key]: layout[key]});
    },
    {}) || {};
}, (state, attributes, isDock) =>
    JSON.stringify(mapLayoutSelector(state)) +
    JSON.stringify(boundingSidebarRectSelector(state)) +
    JSON.stringify(attributes) + (isDock ? '_isDock' : ''))
Example #2
Source File: external.js    From datapass with GNU Affero General Public License v3.0 5 votes vote down vote up
getCachedOrganizationInformation = memoize(
  getOrganizationInformation
)
Example #3
Source File: stats.js    From datapass with GNU Affero General Public License v3.0 5 votes vote down vote up
getCachedMajorityPercentileProcessingTimeInDays = memoize(
  getMajorityPercentileProcessingTimeInDays
)
Example #4
Source File: bootstrap.js    From atomize with MIT License 5 votes vote down vote up
getSortedBreakpoints = memoize(breakpoints =>
  sortBy(
    Object.entries(breakpoints),
    ([, [item]]) => (item.type === 'max-width' ? -1 : 1),
    ([, [item]]) => item.value * (item.type === 'max-width' ? -1 : 1),
  ),
)
Example #5
Source File: useQueryString.jsx    From ResoBin with MIT License 5 votes vote down vote up
useQueryString = () => {
  const location = useLocation()
  const navigate = useNavigate()

  const setQueryString = (key, value) => {
    const queryString = new URLSearchParams(location.search)

    // ? No change
    if (value === undefined || value === queryString.get(key)) return

    // ? update query string (or clear query string if query is empty)
    if (isEmpty(value) && !isFinite(value)) queryString.delete(key)
    else queryString.set(key, value)

    location.search = queryString.toString()
    navigate(location, { replace: true })
  }

  // ? Debouncing is necessary to avoid unnecessary API calls
  const setQueryStringDebounced = useCallback(
    memoize((key) => debounce(setQueryString, 500)),
    [location.search]
  )

  const getQueryString = useCallback(
    (key) => {
      const queryString = new URLSearchParams(location.search)
      if (key) return queryString.get(key)

      return Object.fromEntries(queryString)
    },
    [location.search]
  )

  // ? If keys are passed, removes keys from qs. Else clears qs
  const deleteQueryString = useCallback(
    (...keys) => {
      const queryString = new URLSearchParams(location.search)

      if (isEmpty(keys)) {
        location.search = ''
      } else {
        keys.forEach((key) => {
          queryString.delete(key)
        })
        location.search = queryString.toString()
      }

      navigate(location, { replace: true })
    },
    [location.search]
  )

  return {
    deleteQueryString,
    getQueryString,
    setQueryString: (key, value) => setQueryStringDebounced(key)(key, value),
  }
}