[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 ...
随机推荐
- JavaI/O 系统
1.JavaI/O 系统概述 A. 输入输出(I/O):指的是计算机与外部世界,或者一个程序与计算机的其余部分之间的接口 B. 流的概念(流:Stream) 流的基本特性:有数据.有方向 2. 流的 ...
- java jdk 管理工具
官网:http://www.jenv.be/ 安装: Linux / OS X $ git clone https://github.com/gcuisinier/jenv.git ~/.jenv M ...
- selenium3+python-多窗口、句柄(handle)
一.获取当前窗口句柄 1.元素有属性,浏览器的窗口其实也有属性的,只是你看不到,浏览器窗口的属性用句柄(handle)来识别. 2.人为操作的话,可以通过眼睛看,识别不同的窗口点击切换.但是脚本没长眼 ...
- go之切片
一.概念 关于切片 1.切片是对数组一个连续片段的引用,所以切片是一个引用类型 2.切片是数组一样可以索引,可以通过len函数获取切片的数据长度.(数组也可以通过len获取) 3.切片是一个长度可变的 ...
- C - Haiku
Problem description Haiku is a genre of Japanese traditional poetry. A haiku poem consists of 17 syl ...
- AFN请求后台返回数据为NSInlineData类型的处理
在利用AFN进行数据解析时出现返回数据为 <7b227374 61747573 223a302c 226d6573 73616765 223a22e6 82a8e79a 84e6898b e69 ...
- Android 复制文本内容到系统剪贴板(自由复制)
直接上代码:(对应的类:android.content.ClipboardManager) //获取剪贴板管理器: ClipboardManager cm = (ClipboardManager) g ...
- 联想笋尖S90(S90-t 、S90-u)解锁BootLoader
工具下载链接: http://pan.baidu.com/s/1eSgZuka 备用下载链接: http://pan.baidu.com/s/1dFKqSId 本篇教程,仅限于联想笋尖S90(S90- ...
- 【VB】时间戳转日期
DateAdd("s", TimeStamp / 1000, "1970-01-01 00:00:00")
- SQL Server实现用户注册
用SQL Server注册用户,通过页面输入注册信息,存储到数据库. <form action="zhuChe.jsp" method="post" on ...