Validate Object Properties with Curried Functions

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the checkProp function in JavaScript. This function takes in a predicate function and a property name, and returns a curried function that checks if the property value satisfies the predicate for a given object. We will learn how to use this function to efficiently validate object properties and create flexible and reusable code.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/BasicConceptsGroup -.-> javascript/obj_manip("`Object Manipulation`") subgraph Lab Skills javascript/variables -.-> lab-28194{{"`Validate Object Properties with Curried Functions`"}} javascript/data_types -.-> lab-28194{{"`Validate Object Properties with Curried Functions`"}} javascript/arith_ops -.-> lab-28194{{"`Validate Object Properties with Curried Functions`"}} javascript/comp_ops -.-> lab-28194{{"`Validate Object Properties with Curried Functions`"}} javascript/array_methods -.-> lab-28194{{"`Validate Object Properties with Curried Functions`"}} javascript/obj_manip -.-> lab-28194{{"`Validate Object Properties with Curried Functions`"}} end

Property Checker

To check if a specific property of an object meets a certain condition, use the checkProp function. This function creates a curried function that takes a predicate function and a property name as arguments. The returned function then takes an object and returns true or false based on whether the specified property meets the condition specified by the predicate function.

Here's an example implementation of checkProp:

const checkProp = (predicate, prop) => (obj) => !!predicate(obj[prop]);

And here are some examples of how you might use it:

const lengthIs4 = checkProp((l) => l === 4, "length");
lengthIs4([]); // false
lengthIs4([1, 2, 3, 4]); // true
lengthIs4(new Set([1, 2, 3, 4])); // false (Set uses Size, not length)

const session = { user: {} };
const validUserSession = checkProp((u) => u.active && !u.disabled, "user");

validUserSession(session); // false

session.user.active = true;
validUserSession(session); // true

const noLength = checkProp((l) => l === undefined, "length");
noLength([]); // false
noLength({}); // true
noLength(new Set()); // true

In summary, the checkProp function allows you to easily check the value of a specific property on an object by specifying a predicate function for that property.

Summary

Congratulations! You have completed the Check Property lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like