How to use the keep-func-props function in keep-func-props

To help you get started, we’ve selected a few keep-func-props examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ehmicky / unix-permissions / src / moize.js View on Github external
import moize from 'moize'
import keepFuncProps from 'keep-func-props'

import { mapValues } from './utils.js'

const kMoize = keepFuncProps(moize)

// Moize a function, or an object containing functions
export const moizeFuncs = function(value) {
  if (typeof value === 'function') {
    return kMoize(value, { isDeepEqual: true })
  }

  return mapValues(value, moizeFuncs)
}
github ehmicky / autoserver / src / perf / helpers.js View on Github external
// eslint-disable-next-line default-param-last
  label = func.name,
  category,
  measuresIndex = 0,
) {
  return function monitoredFunc(...args) {
    const labelA = result(label, ...args)
    const categoryA = result(category, ...args)
    const perf = startPerf(labelA, categoryA)
    const response = func(...args)
    const { measures } = args[measuresIndex]
    return promiseThen(response, recordPerf.bind(null, measures, perf))
  }
}

export const monitor = keepFuncProps(kMonitor)

const recordPerf = function(measures, perf, response) {
  const perfA = stopPerf(perf)
  // We directly mutate the passed argument, because it greatly simplifies
  // the code
  // eslint-disable-next-line fp/no-mutating-methods
  measures.push(perfA)
  return response
}

// Combine monitor() and reduceAsync()
export const monitoredReduce = function({
  funcs,
  initialInput,
  mapInput = identity,
  mapResponse = identity,
github ehmicky / autoserver / src / errors / handler.js View on Github external
import keepFuncProps from 'keep-func-props'

import { result } from '../utils/functional/result.js'

import { throwError, normalizeError, isError } from './main.js'
import { throwPb } from './props.js'

// Wrap a function with a error handler
// Allow passing an empty error handler, i.e. ignoring any error thrown
const kAddErrorHandler = function(func, errorHandler = () => undefined) {
  return errorHandledFunc.bind(null, func, errorHandler)
}

export const addErrorHandler = keepFuncProps(kAddErrorHandler)

const errorHandledFunc = function(func, errorHandler, ...args) {
  try {
    const retVal = func(...args)

    // eslint-disable-next-line promise/prefer-await-to-then
    return retVal && typeof retVal.then === 'function'
      ? retVal.catch(error => errorHandler(error, ...args))
      : retVal
  } catch (error) {
    return errorHandler(error, ...args)
  }
}

// Use `addErrorHandler()` with a generic error handler that rethrows
export const addGenErrorHandler = function(func, { message, reason, extra }) {

keep-func-props

Wrap a function without changing its name and other properties

MIT
Latest version published 6 months ago

Package Health Score

70 / 100
Full package analysis

Popular keep-func-props functions