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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fp-recursion

v1.1.2

Published

Recursion implemented for functional programming for replacement of loop

Readme

fp-recursion

Functional programming requires recursion instead of loop. Writing recursion destracts sometimes developers from focusing more on business logic. So, to make use of recursion instead of loop and implement it easily the library can be used.

Install

npm install --save fp-recursion

Usage

import { recurEach, recurTill, recurWhile } from 'fp-recursion';

OR

const { recurEach, recurTill, recurWhile } = require('fp-recursion');

recurEach

Iterates for each element of source array and generates new value based on initial value.

  • Systax
recurEach(
    srcArray, // oprFunc will be called for each element of the source array
    idx, // Default value is 0. Index from which evaluation starts.
    incr, // Default value is 1. Increment value to evaluate next element
    till // Used if it passed and oprFunc will be called for each element till the value or source array length whichever is less
).initVal( //The function accepts initial value
    value, // The value will be passed to oprFunc and the function should return new computed value which again will be passed to oprFunc on next iteration. The process continues untill recursion ends. The value can be any of type e.g. Array, Object, Primitive
).opr(
    oprFunc, // The function will be executed for each element of the source array, with initial value / computed value
);
  • oprFunc
oprFunc(
    ele, // Element of source array
    value, // To be used to compute a new value and the new value should be returned from the function. The returned value will be passed here in next iteration
    idx, // Current index
    srcArray //Whole source array for handling some unexpected scenario
) // The function should return a new computed value
  • Example
const srcArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const resArr = recurEach(srcArr)
    .initVal([])
    .opr((val, arr) => [...arr, val + 1]);
console.log(resArr); // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

There are two ways to pass initial value.

  1. You can pass it into initVal.
  2. You can use default argument of oprFunc as shown below.
recurEach(srcArr)
    .opr((val, arr = []) => [...arr, val + 1]);

But, if source array is empty in that case second way will return undefined if you dont handle the case in oprFunc. So, first way is recommended to pass initial value.

recurTill

Iterates for no. of till value and generates new value based on initial value.

  • Syntax
recurTill(
    till, // No. of times the oprFunc be called
    idx, // Default value is 0. Index from which evaluation starts.
    incr // Default value is 1. Increment value to evaluate next element
).initVal( //The function accepts initial value
    value, // The value will be passed to oprFunc and the function should return new computed value which again will be passed to next iteration of oprFunc. The process continues untill recursion ends. The value can be any of type e.g. Array, Object, Primitive
).opr(
    oprFunc, // The function will be executed for each index till `till` value
)
  • oprFunc
oprFunc(
    idx, // Currennt index
    value, // To be used to compute a new value and the new value should be returned from the function. The returned value will be passed here in next iteration
    till // No. of times of recursion
) // The function should return a new computed value
  • Example
const resArr = recurTill(10)
    .initVal([])
    .opr((idx, arr) => [...arr, idx * 2]);
console.log(resArr); //[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

There are two ways to pass initial value.

  1. You can pass it into initVal.
  2. You can use default argument of oprFunc as shown below.
recurTill(10)
    .opr((val, arr = []) => [...arr, val + 1]);

But, if till value is 0 in that case second way will return undefined if you dont handle the case in oprFunc. So, first way is recommended to pass initial value.

recurWhile

Iterates till condition isn't be false and generates new value based on initial value.

  • Syntax
recurWhile(
    conFunc, // The function has initial value as parameter and returns `true` or `false`. If it returns `true` recursion continues otherwise starts returning
).initVal( //The function accepts initial value
    value, // The value will be passed to oprFunc and conFunc. conFunc should return boolean and oprFunc should return new computed value which again will be passed to next iteration of oprFunc and conFunc. The process continues untill recursion ends. The value can be any of type e.g. Array, Object, Primitive
).opr(
    oprFunc, // The function will be executed untill conFunc returns `false`
)
  • conFunc
conFunc(
    value //Based on the value the function should return either `true` or `false`
)
  • oprFunc
oprFunc(
    value, // To be used to compute a new value and the new value should be returned from the function. The returned value will be passed here and in conFunc in next iteration
) // The function should return a new computed value
  • Example
const res = recurWhile((val) => val !== 10)
        .initVal(0)
        .opr((val) => val + 1);

console.log(res); //10

We cannot pass initial value as default argument with recurWhile.

Please check test cases for more examples.