Functional Programming in Javascript: each, keys, isObject

Image for post
Image for post

Last time, We made two functions map and filter. We combined them and implemented the user’s logic simply.

In this chapter, we’ll be going to extract their common logic and make it safer. The common concept of them is that it traverses collection and do something. So we can make a function called ‘each’.

function each(collection, iteratee) {

‘each’ function traverse the taken collection and execute the iteratee function for each values.

So we can use the ‘each’ function in ‘map’ and ‘filter’. We should just call the ‘each’ function with the collection and iteratee logic.

// map/index.js

and

// filter/index.js

When the refactoring is over, let’s check the test cases run well. Nothing wrong. ‘map’ & ‘filter’ work rightly. The reason that we refactored like that is we have to more code in ‘each’ function. ‘map’ & ‘filter’ work well now, with correct values. But if some unexpected value is entered, they’ll make trouble.

Let’s check the ‘each’ function’s logic. It implemented under the premise that the ‘collection’ is an iterable object. So if value such as ‘null’, ‘{}’ come in, current ‘each’ can’t support them.

// functions.test.js

You’ll see an error message, ‘ TypeError: collection is not iterable’

Iterable and not iterable objects are can access their own values by ‘key’ like that. Of course, if an object is an array or array-like the key is an ‘index’.

object[key]

So If we have an array of their keys, we can traverse it is iterable or not. If input collection is falsy like ‘null’, ‘undefined’, keys will be an empty array so traversing will not be started.

So we need a function that makes the array of object’s keys.

// functions.test.js

To pass that test case, we can use the ‘Object.keys’ method.

// keys/index.js

Simply writing like this, we can implement ‘keys’ function. Although now the function supports not iterable object, falsy values are not yet.

// functions.test.js

To solve this problem, we have to check the input object’s type.

function keys(object) {

Or we can also separate the checking logic ‘isObject’.

// isObject/index.js

and

// keys/index.js

Once the ‘keys’ function has been implemented, let’s apply to ‘each’.

const keys  = require('../keys');

‘each’ function gets the list of collection keys.

  1. If the collection is an iterable object, the collection keys will be an array of indexes.
  2. If the collection is not an iterable object, the collection keys will be an array of object’s keys.
  3. If the collection is falsy value, the collection keys will be an empty array.

Then it will execute iteratee function with traversing collection keys.

Now, there’s no error if we put any value in ‘map’ &‘filter’ function. they are made more safer and reusable!

Github Source Code

woohyun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store