Ramda 之 whereEq()

栏目: JavaScript · 发布时间: 5年前

内容简介:Ramda 的VS Code 1.31.1Quokka 1.0.136

Ramda 的 where() 可產生多個以 && 串起來的 Predicate,但若針對 Object,且都是 === 的 Predicate,就可以使用 whereEq() 更為精簡。

Version

VS Code 1.31.1

Quokka 1.0.136

Ramda 0.26.1

filter()

import { filter } from 'ramda';

const data = [
  { id: 1, title: 'Impatient JavaScript', year: 2018 },
  { id: 2, title: 'RxJS in Action', year: 2017 },
  { id: 3, title: 'Speaking JavaScript', year: 2014 }
];

const item = { title: 'RxJS in Action', year: 2017 };

const getBooks = filter(
  x => x.title === item.title && x.year === item.year
);

const result = getBooks(data);
console.dir(result);

想要找出 title 等於 RxJS in Actionyear 等於 2017 的 data ,且條件以 object 方式呈現。

11 行

const getBooks = filter(
  x => x.title === item.title && x.year === item.year
);

使用了 Ramda 的 filter() 並搭配 predicate function,由於包含了兩個條件,因此必須使用 && 串起來。

Ramda 之 whereEq()

whereEq()

import { filter, whereEq } from 'ramda';

const data = [
  { id: 1, title: 'Impatient JavaScript', year: 2018 },
  { id: 2, title: 'RxJS in Action', year: 2017 },
  { id: 3, title: 'Speaking JavaScript', year: 2014 }
];

const item = { title: 'RxJS in Action', year: 2017 };

const getBooks = filter(whereEq(item));

const result = getBooks(data);
console.dir(result);

filter() 的 predicate 可由 whereEq() 產生。

whereEq()

{String: *} → {String: *} → Boolean

比對兩個 object 的 property 是否相等

{String: *} :Spec object

{String: *} :Test object

Boolean :若相等傳回 true ,否則傳回 false

Spec object 的 property 可以少於 Test object

Ramda 之 whereEq()

compose()

import { filter, whereEq, compose } from 'ramda';

const data = [
  { id: 1, title: 'Impatient JavaScript', year: 2018 },
  { id: 2, title: 'RxJS in Action', year: 2017 },
  { id: 3, title: 'Speaking JavaScript', year: 2014 }
];

const item = { title: 'RxJS in Action', year: 2017 };

const getBooks = compose(
  filter,
  whereEq(item)
)();

const result = getBooks(data);
console.dir(result);

原本 11 行

const getBooks = filter(whereEq(item));

先執行 whereEq() ,再將結果傳給 filter() 執行,因此也可以將 whereEq()filter() 兩個 function 加以組合。

11 行

const getBooks = compose(
  filter,
  whereEq(item)
)();

與一般 compose() 不同的是:最後還加上了 () ,因為 whereEq(item) 不需要任何參數,所以直接以 () 結束。

whereEq(item) 產生的 predicate 傳給 filter() 後,還留一個參數,這正是要傳給 getBooks() 的 data,因為 Point-free 而省略。

Ramda 之 whereEq()

Conclusion

  • 當 predicate 有多個條件,可使用 where()
  • 當 predicate 有多個條件,且資料來自於 object,並且只要判斷 === 而已,則可使用 whereEq()
  • 實務上不見的要使用 compose() 重構, filter(whereEq(item)) 的寫法可讀性已經很高

Reference

Ramda , filter()

Ramda , whereEq()

Ramda , compose()

Ramda , where()


以上所述就是小编给大家介绍的《Ramda 之 whereEq()》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深入浅出 HTTPS:从原理到实战

深入浅出 HTTPS:从原理到实战

虞卫东 / 电子工业出版社 / 2018-6 / 89

本书是一本专业的HTTPS书籍,全面讲解了HTTPS领域的相关知识,内容包括密码学、OpenSSL命令行、证书、TLS协议、HTTPS网站性能优化、HTTPS网站优秀实践、大型网站HTTPS架构设计等。本书有几个特点:(1)内容全面而新颖,基于RFC文档、国外书籍、社区等一手资料,总结了大部分最新的HTTPS知识;(2)由浅入深,从基础到进阶全面掌握HTTPS,读者能够轻松构建一个HTTPS网站,......一起来看看 《深入浅出 HTTPS:从原理到实战》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换