ArraySearch with Chained Functions

After working with some large data sets and developing code to processes them, I created a utility implementing a pattern referred to as “chained functions”.

The Problem

I recently discovered the array.prototype.some and array.prototype.every methods, and saw their utility for searching arrays. I became determined there was a way to streamline their functionality after an initial usage in a case similar to this (although in a far more complex situation):

This block of code finds a single person object with an age of “17” in the people array, and throws an error if one is not found.

However, this takes up a lot of room, and I enjoy the challenge of condensing things, so ended up with this (roughly) equivalent block:

Now I can certainly imagine at least a few programmers would cringe at what could could be considered a confusing and abusive usage of some, among other offenses. And I admit a first time viewer would probably need a few seconds to decipher what’s going on here. However, the alternative is verbose and unwieldy, adding it’s own layer of complexity, So I took some time to write an (rather robust) alternative.

The Solution

Based on my experience, the situation of needing to find a particular object in a array with a particular property (or properties) is pretty common. The ArraySearch utility aims to provide an elegant solution. You can skip to the end and see the result, but here’s a bit of explanation behind the solution.

Recently, I’ve discovered the concept of what I like to call “natural language functions”. These are functions that chain arguments into successive calls, rather than a single call accepting a large group of arguments or breaking functionality into multiple distinct functions. This proves to be extremely effective at reducing both complexity and development time, and vastly increasing readability. My initial exposure to this was through the Chai assertion library, which uses the following syntax:

This style of abstraction/structure allows code to be quickly deciphered and written. Wherever possible, I’ve tried to implement it in my coding. Here’s an example based around ArraySearch:

Traditional function calls with multiple arguments are inherently confusing and ambiguous. Natural language removes the question of what goes where and awkward Boolean values or flags; the function call reads like a sentence and takes advantage of intellisense/auto-complete functionality.

Natural language functions are definitely trickier to write than traditional functions, and use some relatively advanced JavaScript constructs, but they provide immense implementation benefits.

How do they work?

Natural language functions take advantage of “chaining” through custom getter functions. Here’s an example of a couple pieces of ArrayFinder:

This packages the return type implementation into two clear values “one” or “all”. Next the arraySetter accepts a value for the array and stores it, returning another object that contains the actual executing functions:

findBy is a search function internal to ArraySearch that would normally have to be called with multiple explicit arguments, but that is instead provided with the proper context. setSearchPath continues the chain for more complex functionality.

ArraySearch

The final result:

What’s going on here takes no deciphering abilities (and doesn’t need any comments), is extremely quick to write, and takes up very little space. The ArraySearch utility includes the ability to search by any number of properties and at any depth, and to search arrays within an object, and is available on my GitHub.

A few more examples:

The with function accepts an predicate to search for within an array inside of the object, defined by the object path (admittedly this could take the whole natural language thing a little farther, but coming soon).

Please comment with any feedback or bugs/issues you find in my code!

 

Leave a Reply

Your email address will not be published. Required fields are marked *