npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@guj/rif-js

v0.0.3

Published

Simplify filter,map and such when their use should be conditionnal

Downloads

17

Readme

Rif.js

Rif is a "simple" lib with a "simple" syntax meant to simplify certain operations.

contains (as of today) :

filterIf ; mapIf ; concatIf ; sliceIf

TL;DR

Turn :

myArray
    .filter(elem => {
        if(aCondition){
           return true;
        }
        return checkSomethingOnElement(elem);
    })
    .forEach(doSomethingToElement);

Into:

Rif.of(myArray)
    .filterIf(aCondition)(checkSomethingOnElement)
    .forEach(doSomethingToElement);

An example from the everyday life

State of the art :

Let's say you have a huge bunch of kids but just lost your job, the most reasonable thing to do would be to sell them all for pharmaceutical research wouldn't it ?

Now, let's assume that you plan this using JS (and why not) this might look like this:

arrayOfKids.forEach(sendToLab); // Haaa ... so nice and concise :D

But, suddenly, you receive a call for a part-time job, your new salary (quite conveniently for my point) allows you to provide (exactly) for you, your life-partner and those of your kids that haven't been naughty.

This may end up looking like this:

arrayOfKids
    .filter(isThisKidNaughty)
    .forEach(sendToLab);
/* That, actually, is still pretty nice */

Then you remember that:

  • given your lack of access to planned parenthood
    • you're bound to have too many kids again
  • given the current economy/state of the labor-protection
    • you're bound to loose thy job again

So you prepare the code so that it works every time :

arrayOfKids
    .filter(kid => isThisKidNaughty(kid)||!iHaveEnoughIncomeToKeepNiceKids) // ewww !
    .forEach(sendToLab);

That's a oneliner but that is not "first glance" clear to everyone so you try to make it more expressive:

arrayOfKids
    .filter(kid => {
        if(!iHaveEnoughIncomeToKeepNiceKids){
            return true;
        }
        return isThisKidNaughty(kid);
    })
    .forEach(sendToLab);

Not that much better.

Now with Rif :

Rif.of(arrayOfKids)
    .filterIf(!iHaveEnoughIncomeToKeepNiceKids)(isThisKidNaughty)
    .forEach(sendToLab);

Bonus :

Rif.js is dynamically built so any addition to Array.prototype in the future will be "If-ed" automatically. For example, if tomorrow, you have a method respectfulReverse() that would be like reverse but instead of modifying the original, it returns a new array, that method will have it's if without changing code or lib version.

Other Bonus :

I have some ideas about things I could do next but am too lazy to put up a roadmap. And if YOU have an idea, put an issue or (even better) a PR.

Remarkable feedbacks :

That's brilliant ! I wish I'd have had that years ago.

-- me