[Javascript Crocks] Safely Access Object Properties with `prop`
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);
[Javascript Crocks] Safely Access Object Properties with `prop`的更多相关文章
- [Javascript Crocks] Safely Access Nested Object Properties with `propPath`
In this lesson, we’ll look at the propPath utility function. We’ll ask for a property multiple level ...
- How to access the properties of an object in Javascript
Javascript has three different kinds of properties: named data property, named accessor property and ...
- [Javascript] Intercept property access with Javascript Proxy
A Javascript Proxy object is a very interesting es6 feature, that allows you to determine behaviors ...
- javascript中function和object的区别,以及javascript如何实现面向对象的编程思想.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- javascript学习笔记 - 引用类型 Object
引用类型是一种数据结构,也称作对象定义,类似于类的概念. 对象是引用类型的实例. javascript引用类型有:Object, Array, Date, RegExp, Function 使用new ...
- [Ramda] Declaratively Map Predicates to Object Properties Using Ramda where
Sometimes you need to filter an array of objects or perform other conditional logic based on a combi ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- JavaScript Patterns 5.7 Object Constants
Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...
- [设计模式] JavaScript 之 原型模式 : Object.create 与 prototype
原型模式说明 说明:使用原型实例来 拷贝 创建新的可定制的对象:新建的对象,不需要知道原对象创建的具体过程: 过程:Prototype => new ProtoExam => clone ...
随机推荐
- centos的vsftp修改上传下载速度
比如你想限制本地用户的上传速度和下载速度为1MB/s,则在vsftpd.conf中添加以下内容:local_max_rate = 1048576 ※ 默认单位是Byte/s
- bzoj 2152 聪聪可可(点分治模板)
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 3194 Solved: 1647[Submit][Status][Discuss ...
- getField();在TP5里成什么了?
拆分为value和column了 $comps=db("company")->where(array("areaid"=>$areaid))-> ...
- 【LuoguP2210 USACO】 Haywire
这种答案跟序列排列顺序有关的,n比较小的(稍微大一点的也可以),求最优解的,一般都可以随机化过 随机化不一定是模拟退火或是什么遗传蚁群 哪怕只是直接随机化一个序列,只要你随机的次数够多,它都能找到正解 ...
- ACM_来自不给标题的菜鸟出题组(巴什博弈+素数判定)
来自不给标题的菜鸟出题组 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大B和小b合作出一道程序设计月赛的题,他们的想法是给定一 ...
- 5.20 mybatis反向生成的映射文件xml(如果需要自己定义其他sql语句时如下)
解决mybatis-generator 生成的mapper.xml覆盖自定义sql的问题 mybatis-generator是个好工具,一建即可生成基本增删改成功能的mapper.xml.但这些是不够 ...
- c++ 枚举与字符串 比较
读取字符串,然后将这个字符转换为对应的枚举. 如:从屏幕上输入'a',则转换为set枚举中对应的a,源代码如下: //关键函数为char2enum(str,temp); #include using ...
- 西门子Step7中DB块结构导出
Step7 通过变量表可以导出内存M地址和I,Q,T,C地址的变量,以及DB块的名称.怎么导出DB块的内部结构结构呢.即如何导出结构内的定义呢? 可以通过“选择某个DB块”,通过菜单命令“File&g ...
- 【sqli-labs】 less27a GET- Blind based -All you Union&Select Belong to us -Double Quotes(GET型基于盲注的去除了Union和Select的双引号注入)
和less 27一样,单引号换双引号 http://192.168.136.128/sqli-labs-master/Less-27a/?id=0"%a0uNion%a0sElect%a01 ...
- excel 处理方法
//.方法一:采用OleDB读取EXCEL文件: //打开excel 返回指定表中的所有数据 public DataSet ExcelToDS(string Path) { string strCon ...