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

times-iterator

v1.0.0

Published

This package is inspired by the syntax of the Ruby programming language. It adds an iterator that can be called 'n' times with a function that is passed to one of the methods of the iterator.

Downloads

23

Readme

times-iterator

This package is inspired by the syntax of the Ruby programming language and lets you iterate over a number of times and do something with it.

Installation

npm

npm install times-iterator

yarn

yarn add times-iterator

API

The argument that you pass to the times function must be greater than 0

do - this method always returns the instance of the "times" function, so you can chain it

  let n = 0
  // will be called 20 times
  times(20).do((number, index, exitFn) => n++)
  // n = 20

  let k = 0
  // will also be called 20 times
  times(10)
     .do((number, index, exitFn) => k++)
     .do((number, index, exitFn) => k++)
  // k = 20

map

  const arr = times(10).map((number, index, arr, exitFn) => number * 2)
  // arr = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

reduce

  const sum = times(10).reduce((acc, number, index, exitFn) => acc + number, 0)
  // sum = 55

filter or select

  const arr = times(10).filter((number, index, arr, exitFn) => number % 2 === 0)
  // arr = [2, 4, 6, 8, 10]

  const anotherArr = times(10).select((number, index, arr, exitFn) => number % 2 === 0)
  // anotherArr = [2, 4, 6, 8, 10]

takeWhile - takes while the condition is true

  const arr = times(10).takeWhile((number, index, arr, exitFn) => number < 5)
  // arr = [1, 2, 3, 4]

takeUntil - takes till the condition becomes true

  const arr = times(10).takeUntil((number, index, arr, exitFn) => number === 5)
  // arr = [1, 2, 3, 4]

Reverse

Every method has a reverse version, for example: reverseDo, reverseMap, reverseReduce, reverseFilter, reverseSelect, reverseTakeWhile, reverseTakeUntil

  let n = 0
  // will be called 20 times
  times(20).reverseDo((number, index, exitFn) => n++)
  // n = 20

  let k = 0
  // will also be called 20 times
  times(10)
     .reverseDo((number, index, exitFn) => k++)
     .reverseDo((number, index, exitFn) => k++)
  // k = 20

You can also go reverse by passing a second argument as true to any function you use

  let n = 0
  // will be called 20 times
  times(20).do((number, index, exitFn) => n++, true)
  // n = 20
    
  let k = 0
  // will also be called 20 times
  times(10)
    .do((number, index, exitFn) => k++, true)
    .do((number, index, exitFn) => k++, true)
    // k = 20

Exit

You can exit the loop by calling the exit function

  let n = 0
  // will be called 20 times
  times(20).do((number, index, exit) => {
    n++
    if (n === 10) exit()
  })
  // n = 10

It gives you the possibility to create infinite loops since you can pass Infinity as the first argument

  let n = 0
  times(Infinity).do((number, index, exit) => {
    n++
    if (n === 10) exit()
  })
  // n = 10
Exit function is available in all the methods so you can quit the loop at any time you want!

Usage

import { times } from 'times-iterator'

const evenNumbersTill100 = times(100).select(number => number % 2 === 0)

React

import React from 'react'
import { times } from 'times-iterator'

const Dots = props => {
  return (
    <div class={'flex items-center space-x-10'}>
      {times(props.dotsNumber).map(n => <div key={n} className={'w-4 h-4 rounded-full'} />)}
    </div>
  )
}