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`的更多相关文章

  1. [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 ...

  2. How to access the properties of an object in Javascript

    Javascript has three different kinds of properties: named data property, named accessor property and ...

  3. [Javascript] Intercept property access with Javascript Proxy

    A Javascript Proxy object is a very interesting es6 feature, that allows you to determine behaviors ...

  4. javascript中function和object的区别,以及javascript如何实现面向对象的编程思想.

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. javascript学习笔记 - 引用类型 Object

    引用类型是一种数据结构,也称作对象定义,类似于类的概念. 对象是引用类型的实例. javascript引用类型有:Object, Array, Date, RegExp, Function 使用new ...

  6. [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 ...

  7. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  8. JavaScript Patterns 5.7 Object Constants

    Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...

  9. [设计模式] JavaScript 之 原型模式 : Object.create 与 prototype

    原型模式说明 说明:使用原型实例来 拷贝 创建新的可定制的对象:新建的对象,不需要知道原对象创建的具体过程: 过程:Prototype => new ProtoExam => clone ...

随机推荐

  1. 90.商城登录页面Extjs

    1. Ext.onReady(function(){ Ext.BLANK_IMAGE_URL = "Ext/resources/images/default/s.gif"; Ext ...

  2. aspectc中this可以获取的东西

    this->kind  操作类型 this->targetName 被调用函数名称 this->funcName 调用函数名称 this->argsCount 参数个数 thi ...

  3. iOS开发之判断手机号和邮箱 正则表达式

    #pragma mark --判断手机号合法性 + (BOOL)checkPhone:(NSString *)phoneNumber { NSString *regex = @"^((13[ ...

  4. Python定制容器

    Python 中,像序列类型(如列表.元祖.字符串)或映射类型(如字典)都是属于容器类型,容器是可定制的.要想成功地实现容器的定制,我们需要先谈一谈协议.协议是什么呢?协议(Protocols)与其他 ...

  5. C#接入第三方支付一些小问题

    13年第一次接入支付宝的时候,支付宝的api还不是很好用,费了些劲才完成,本月再次接入的时候发现已经很好用了,接入过程非常顺畅,只出现了一个小问题,我的金额默认是保留了4位小数,支付宝api只接受最多 ...

  6. Serializable资料整理

    1. 序列化 简单的说就是为了保存 内存中各种对象的状态(是实例变量,不是方法),并且可以把保存的对象读取出来. 虽然保存 object states的方法很多,但是Java提供了一种保存对象状态的机 ...

  7. MyEclipse创建SSH项目(Java web由maven管理)

    JavaEE后台开发,MyEclipse创建SSH项目,MyEclipse创建Java web 由maven管理的SSH项目. Demo工程源码github地址 1.创建SSH项目 1.创建web工程 ...

  8. MatLab之HDL coder

    1 Workflow The workflow for applying HDL code generation to the hardware design process requires the ...

  9. theano和keras安装

    最近在学深度学习框架,要用到keras库,keras可以搭建在tensorflow和theano上,我电脑装的是Windows,因此决定在电脑上搭建theano框架 下面回顾我的安装过程: 1.安装a ...

  10. Spark中Task,Partition,RDD、节点数、Executor数、core数目的关系和Application,Driver,Job,Task,Stage理解

    梳理一下Spark中关于并发度涉及的几个概念File,Block,Split,Task,Partition,RDD以及节点数.Executor数.core数目的关系. 输入可能以多个文件的形式存储在H ...