Skip to content

Commit

Permalink
Use mimic-fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 26, 2019
1 parent 8f35f98 commit b2b90f2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"json-schema-faker": "0.5.0-rc15",
"lodash": "^4.17.10",
"methods": "^1.1.2",
"mimic-fn": "^1.2.0",
"moize": "^5.4.2",
"node-notifier": "^5.4.0",
"openapi-schema-to-json-schema": "^2.2.0",
Expand Down
18 changes: 3 additions & 15 deletions src/utils/keep_props.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
'use strict'

const mimicFn = require('mimic-fn')

// Wraps a functor so it does not modify a function `name`, `length`, etc.
const keepProps = function(functor) {
return (originalFunc, ...args) => {
const wrappedFunc = functor(originalFunc, ...args)

copyProperties({ originalFunc, wrappedFunc })

mimicFn(wrappedFunc, originalFunc)
return wrappedFunc
}
}

const copyProperties = function({ originalFunc, wrappedFunc }) {
Reflect.ownKeys(originalFunc).forEach(propName =>
copyProperty({ originalFunc, wrappedFunc, propName }),
)
}

const copyProperty = function({ originalFunc, wrappedFunc, propName }) {
const prop = Object.getOwnPropertyDescriptor(originalFunc, propName)
// eslint-disable-next-line fp/no-mutating-methods
Object.defineProperty(wrappedFunc, propName, prop)
}

module.exports = {
keepProps,
}

0 comments on commit b2b90f2

Please sign in to comment.