简介
在本实验中,我们将探索如何自定义一个合并函数,该函数根据给定的验证器返回第一个为 true 的参数。我们将学习使用 Array.prototype.find() 从提供的参数验证函数 valid 中返回第一个返回 true 的参数。在本实验结束时,你将能够创建一个自定义合并函数,该函数可用于从参数列表中返回第一个有效参数。
参数合并工厂代码
要开始编码,请打开终端/SSH 并输入 node。此函数根据作为参数传递的验证器返回第一个计算结果为 true 的参数。
const coalesceFactory =
(validator) =>
(...args) =>
args.find(validator);
使用 Array.prototype.find() 从提供的参数验证函数 valid 中返回第一个返回 true 的参数。例如:
const customCoalesce = coalesceFactory(
(v) => ![null, undefined, "", NaN].includes(v)
);
customCoalesce(undefined, null, NaN, "", "Waldo"); // 'Waldo'
在这里,coalesceFactory 函数被定制以创建 customCoalesce 函数。customCoalesce 函数从提供的参数中过滤掉 null、undefined、NaN 和空字符串,并返回第一个未被过滤掉的参数。customCoalesce(undefined, null, NaN, '', 'Waldo') 的输出是 'Waldo'。
总结
恭喜你!你已经完成了参数合并工厂实验。你可以在 LabEx 中练习更多实验来提升你的技能。