documentcloud / documentcloud/underscore-contrib

Would an _.if function be useful?

Open
#215 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

after modules enhancement
Dominant language
JavaScript
Stars
622
Forks
114
PR merge metrics
No merged PRs in 30d

Description

_.if would take two arguments, a primary function and a conditional function. It would return a new function that, when called, will call the primary function only if the conditional (called with the same arguments) returns a truthy value. It would look something like this:

_.if = function(func, condition){
    return function(){
        if (condition.apply(this, arguments)){
            return func.apply(this, arguments);
        }
    }
};

This way you can write generic functions and then easily add conditions for execution when passing them to iterators. Something like:

items.forEach(_(processItem).if(isNotProcessed));

Would this be a useful addition?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is named. Start by reviewing existing function utilities and iterator-related APIs in the repository, then assess whether the proposed _.if behavior has clear acceptance criteria. Done would require a maintainer decision plus implementation and tests, none of which the issue specifies.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.