Ramda:在 R.ifElse() 函数调用中访问输入对象的属性

Ramda: access input object's properties inside R.ifElse() function call

我有这个现有功能:

  const inferProcessingError = R.ifElse(
    R.propEq('conversionJobStatus', 3),
    R.always('Last Process failed with error; please contact DevOps'),
    R.always(null)
  );

这样称呼:

const msg = inferProcessingError(jobStruct || {});

这个 jobStruct:

{"id":9,"mediaGroupId":1000000,"conversionJobStatus":3,
  "errorDetails": {
     "Cause": {
        "errorMessage": "MediaConvert Job Failed with ERROR status: ERROR Video codec [indeo4] is not a supported input video codec",
     },
     "Error": "Error",
   }
}

我需要创建一个错误消息字符串,其中包含来自 Cause.errorMessage 元素的数据。

如果使用本机 JavaScript 函数,这将非常简单,但我正在学习 Ramda,只想修改现有代码以包含在错误消息中。

R.prop('Cause')['errorMessage'] 可以工作,只是我不知道如何引用传递给 inferProcessingError 语句的 jobStruct。

我可以看到 R.ifElse 和后续的 Ramda 函数能够获取该引用,但是当我在错误消息字符串中嵌入 R.prop('Cause') 时,它解析为一个函数而不是Cause 元素的值,因为它似乎在等待数据结构。

那么...我如何获得对 jobStruct 引用的访问权限? (这里没有定义arguments)。

更新: 我可以通过引用 R.Prop('ErrorDetails', jobStruct)['Cause']['errorMessage'] 中的原始 jobStruct 来实现它,但这对我来说似乎很笨拙......

但是如果对 inferProcessingError 的调用实际上是在 map 语句中并且引用了更大结构中的元素,则映射索引不可用于引用 R.prop.

也许您可以使用 pipe and path 方法来实现这种“ramda 方式”。

首先使用 ramda 的 path() 函数从输入 jobStruct 对象中提取嵌套的 errorMessage 值。接下来,将其包含在 pipe() 中,将提取的消息转换为使用自定义错误前缀格式化的字符串:

const incCount = R.ifElse(
    R.propEq('conversionJobStatus', 3),
    
    /* Evaluate this pipe if the error case is satisfied */
    R.pipe(
        /* Path to extract message from input object */
        R.path(["errorDetails", "Cause", "errorMessage"]), 
        /* Prefix string to extracted error message */
        R.concat('Custom error prefix:')),
    
    R.always('')
);

incCount({"id":9,"mediaGroupId":1000000,"conversionJobStatus":3,
  "errorDetails": {
     "Cause": {
        "errorMessage": "MediaConvert Job Failed with ERROR etc etc",
     },
     "Error": "Error",
   }
});

这是一个 working example - 希望对您有所帮助!

更新

感谢@customcommander 建议使用 concat 作为字符串前缀,并为第二个分支返回一个空字符串值