How to use keep-func-props - 4 common examples

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 }) {
github ehmicky / autoserver / src / errors / handler.js View on Github external
'use strict'

const { keepFuncProps } = require('keep-func-props')

const { result } = require('../utils')

const { throwError, normalizeError, isError } = require('./main')
const { throwPb } = require('./props')

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

const kAddErrorHandler = keepFuncProps(addErrorHandler)

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
const addGenErrorHandler = function(func, { message, reason, extra }) {

keep-func-props

Wrap a function without changing its name and other properties

MIT
Latest version published 1 year ago

Package Health Score

59 / 100
Full package analysis

Popular keep-func-props functions