Book Image

Lo-Dash Essentials

By : Adam Boduch
Book Image

Lo-Dash Essentials

By: Adam Boduch

Overview of this book

Table of Contents (15 chapters)
Lo-Dash Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Generic wrappers and chains


With generic functions under our belts, it's time to turn our attention to Lo-Dash wrapper instances and create generic function call chains. Chains are useful when you're stuck and you need a quick way out of a tricky programming situation, but they're also useful in the generic sense. That is, you can compose chains of functionality that are general enough to apply in a variety of contexts.

Generic filters

Let's start by taking a look at generic filters and how they can be utilized in our functions. Filters are especially suitable for chained function calls since they can be stitched together by applying a filter after a preceding filter. There are often some kinds of sorting or other constraints that take place at the end of a filter, such as limiting the number of results returned, as shown in the following code:

function byName(coll, name, take) {
    return _(coll)
        .filter({ name: name })
        .take(_.isUndefined(take) ? 100 : take)
        .value...