简体   繁体   English

比较日期并将 boolean 值返回到日历的逻辑

[英]Logic to compare dates and return boolean value to calendar

I'm implementing a logic for a calendarpicker component, which has disabledDateCallback event param, which provides date, but need to apply some logic so that it can disable all dates returned from a service, provided return statement should be boolean.我正在为日历选择器组件实现一个逻辑,该组件具有 disabledDateCallback 事件参数,它提供日期,但需要应用一些逻辑,以便它可以禁用从服务返回的所有日期,提供的返回语句应该是 boolean。

<CalendarPicker 
  disabledDateCallback={(date:Date)=>{
    dateService.holidays.map((d)=>{
    return date === d
  })
}}
/>

Problem is the component need boolean, but Im not sure how to compare date and service dates n return boolean value问题是组件需要 boolean,但我不确定如何比较日期和服务日期 n 返回 boolean 值

As others have noted, the following function fails to disable any of the dateService holidays正如其他人所指出的,以下 function 无法禁用任何dateService holidays

(date: Date) => {
    dateService.holidays.map((d)=> {
      return date === d
    })
 })

because the CalendarPicker 's disabledDateCallback property should be a function that returns a truthy value if the date passed to it should be disabled or a falsy value if the date should be not be disabled.因为CalendarPickerdisabledDateCallback属性应该是一个 function 如果应该禁用传递给它的日期,则返回一个真值,如果不应该禁用该日期,则返回一个假值。

However, the function above always returns undefined , a falsy value, because it lacks a return statement.但是,上面的 function 总是返回undefined ,一个虚假值,因为它缺少return语句。

We can begin by modifying it as follows我们可以先修改如下

(date: Date) => {
    return dateService.holidays.map(d => {
      return date === d
    })
 })

but now it always returns a truthy value because Array.prototype.map transforms one array into another array by applying the specified callback to each element.但现在它总是返回一个真实值,因为Array.prototype.map通过对每个元素应用指定的回调将一个数组转换为另一个数组。 Since an array is always truthy, the function will now disable all dates regardless of whether or not they match the input.由于数组总是真实的,因此 function 现在将禁用所有日期,无论它们是否与输入匹配。

Instead, we will leverage the Array.prototype.some which takes a predicate callback and returns true if and only if at least one element in the array satisfies the predicate;相反,我们将利用Array.prototype.some ,它接受一个谓词回调,当且仅当数组中至少有一个元素满足谓词时才返回true otherwise it returns false .否则返回false

Thus the callback we want to pass for disabledDateCallback turns out to be因此,我们想要为disabledDateCallback传递的回调结果是

(date: Date) => {
   return dateService.holidays.some(d => {
      return date === d
    })
 })

This is also highly readable and more efficient as we are using some to clearly express our intent at a high level and the some need not traverse the entire array to answer its question every as it can stop as soon as it encounters a value for which the predicate we provide returns a truthy value.这也是高度可读且更有效的,因为我们使用some来清楚地表达我们的高层次意图,而some不需要遍历整个数组来回答它的问题,因为它可以在遇到一个值时立即停止我们提供的谓词返回一个真实值。

Finally, let's clean the code up a bit since it is unnecessarily verbose given that our function bodies consist of a single statement statement in which case we can remove both the block {} and the return .最后,让我们稍微清理一下代码,因为它不必要地冗长,因为我们的 function 主体由单个语句组成,在这种情况下,我们可以删除块{}return

(date: Date) => dateService.holidays.some(d => date === d)

Additionally, since we are passing our dateDisabledCallback inline as part of creating our <CalendarPicker> element, we can improve even things further by leveraging type inference.此外,由于我们将dateDisabledCallback内联传递作为创建<CalendarPicker>元素的一部分,因此我们可以通过利用类型推断来进一步改进。

<CalendarPicker 
  disabledDateCallback={date => dateService.holidays.some(d => date === d)}

It is highly desirable to allow the TypeScript type inference engine to infer the types of callback arguments, here date , for numerous reasons with type safety being the most significant.非常希望允许 TypeScript 类型的推理引擎推断回调 arguments 的类型,这里date ,原因有很多,其中类型安全是最重要的。 Of course this only applies to inline callbacks.当然,这只适用于内联回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM