In this lesson, we’ll use a Maybe to safely operate on properties of an object that could be undefined. We’ll use our initial code as the basis for a prop utility function that can be reused with different objects and various property names. Instead of just blindly asking for a property, this version of prop will drop us into the safe confines of a Maybe, giving us a Just when the property exists and a Nothing for an undefined property. Once we’ve built up our own prop utility, we’ll refactor the code one more time to take advantage of the built-in proputility provided by the crocks library.

When you want to pull out a value from object or array, you might doing like this:

const safe = require('crocks/Maybe/safe');
const { inc } = require('./utils');
const { not, compose, isNil, prop } = require('ramda'); // check the value is not undefined or null
const isNotNil = safe(compose(not, isNil));
// check object's prop value is not undefined or null
const safeProp = propName => obj => isNotNil(prop(propName, obj));
// get 'page' prop from the object as Maybe type
const safePage = safeProp('page');
// data
const qs = { page: 4, pageSize: 10, totalPages: 203 };
//default value is 1
const result = safePage(qs).option(1); console.log(result); //

Actually from the code above we write many code, we use Ramda lib for utils functions and  safe prop method.

const { not, compose, isNil, prop } = require('ramda');

const isNotNil = safe(compose(not, isNil));
const safeProp = propName => obj => isNotNil(prop(propName, obj));

Actually crocks lib provide 'prop' method to simply the code:

const prop = require('crocks/Maybe/prop');
const { inc } = require('./utils'); const safePage = prop('page'); const qs = { page: , pageSize: , totalPages: };
const result = safePage(qs).option(); console.log(result);

最新文章

  1. .NET 实现并行的几种方式(二)
  2. 软件工程(FZU2015)赛季得分榜,第八回合
  3. clientTop、offsetTop和scrollTop的区分
  4. 使用javascript实现html页面直接下载网盘文件
  5. unity3d 关于断点下载和整个下载(用于更新)
  6. Flex Excel下载
  7. html5+php实现文件的断点续传ajax异步上传
  8. swagger-editor 快速REST-API 测试文档编写
  9. js中的数组
  10. 【原创翻译】Reducing Branch Delay to Zero in Pipelined Processors
  11. python的py文件打包成exe
  12. push方法的页面间跳转--
  13. SSAS系列——【04】多维数据(物理体系结构)
  14. SQL Server 2008 允许远程链接,适用于广域网和局域网
  15. 局域网iis添加主机头
  16. Alamofire源码解读系列(八)之安全策略(ServerTrustPolicy)
  17. Android开发之初识MVP模式
  18. Asp.net 程序连接orcle如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,
  19. ajax跨域请求Flask后台
  20. vue 分享知识点

热门文章

  1. Reactive Native开发环境搭建
  2. python3+request接口自动化框架中自动发送邮件
  3. DMARC 介绍
  4. 【贪心】小Y的炮[cannon]题解
  5. Python生成器(yield)
  6. HBase编程 API入门系列之get(客户端而言)(2)
  7. 移动端弹性滑动以及vue记录滑动位置
  8. 数据库 'tempdb' 的事务日志已满。若要查明无法重用日志中的空间的原因
  9. 人工机器:NDC-谷歌机器翻译破世界纪录,仅用Attention模型,无需CNN和RNN
  10. 点云处理软件Pointscene