猴子修补 formsy-react 库中使用的函数

monkey patching a function used in formsy-react library

我使用 formsy-react v1.1.5 进行验证,我有大约 100 个输入字段,由于代码中不必要的 object.assign 函数,它的速度慢得令人难以置信。我知道更高版本修复了这个问题,但我现在不能更新它。

我完全不知道猴子补丁,我不想使用任何补丁库来完成工作。我想知道如何打补丁。

此代码:

getCurrentValues = () => (
 this.inputs.reduce((data, component) => {
   const { name } = component.props;
   const dataCopy = Object.assign({}, data); // avoid param reassignment
  dataCopy[name] = component.state.value;
  return dataCopy;
 }, {})
)

 getPristineValues = () => (
  this.inputs.reduce((data, component) => {
    const { name } = component.props;
    const dataCopy = Object.assign({}, data); // avoid param reassignment
   dataCopy[name] = component.props.value;
  return dataCopy;
 }, {})
)

我想进行以下更改:

getCurrentValues = () => (
this.inputs.reduce((data, component) => {
  const { name } = component.props;
  data[name] = component.state.value;
  return data;
}, {})
)

  getPristineValues = () => (
this.inputs.reduce((data, component) => {
  const { name } = component.props;
  data[name] = component.props.value;
  return data;
}, {})
)

谢谢。

最简单最快的方法:

转到"node_modules/formsy-react/lib",打开您要修补的文件,更改并保存。

下次执行时,将使用更改后的文件。

唯一的问题是每次在每台计算机上执行 npm 安装时都需要重新执行此操作。

更好的方法是创建一个带有补丁文件的 .js 文件,在 package.json 中创建一个脚本,将其复制到正确的位置,并在 README 中添加一个提醒,您需要在之后执行该脚本执行 npm install.

可能是这样的:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js",
    "patch": "mv patchedFile.js ./node_modules/formsy-react/lib/{fileName}.js"
  },

在项目根目录中 patchedFile.js 并且与修改后的 {fileName}.js 具有相同的内容。

并且在"npm install"之后执行"npm run patch"。