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

wordlist-iterator

v1.0.0

Published

Iterate throw wordlists using breaks, returns and throws ( without memory leaks )

Downloads

5

Readme

wordlist-iterator

Supports only ES6 or higher.

wordlist-iterator's only export is async generator function which gives you ability to continue iteration of wordlist after breaks, returns and throws in for await...of loop from where you left off, and thus get around the Do not reuse generators problem.

Usage

The only export of wordlist-iterator is wordlistIterator async generator function which accepts two arguments - wordlistPath, WordListIteratorOptions, and returns async Iterable to iterate through the words of wordlist.

Arguments:

  1. wordlistPath - absolute path to wordlist
  2. WordListIteratorOptions? - Optional object which takes only property highWaterMark, which will be passed to openReadStream function under the hood, for more details see fs.createReadStream options.

This examples below are very self explanatory:

const { wordlistIterator, testWordlist } = require('wordlist-iterator')


const execute = async () => {
    const wordlist = wordlistIterator(testWordlist)

    let index = 0
    for await (const word of wordlist) {
        console.log(`word: ${word}`)
        // do some async stuff...
        if (index++ === 2) break // stop on 3rd word if reached.
    }
    // do stuff between iterations...
    // ... await stuff1()
    // ... stuff2()
    // do some logs...
    console.log('First Iteration Finished.')

    // continue iteration from where you left off ( where you break, return or throw ).
    // in this case from 4th word.
    for await (const word of wordlist) {
        console.log(`word: ${word}`)
        if (index++ === 4) break // break on 5th word if reached.
    }
    console.log('Second Iteration finished.')
    // and so on.
}

execute()

If you need you can pass iterable to function and after it returns, or throws, you can continue to use your iterable of words from outside. e.g.

const { wordlistIterator, testWordlist } = require('wordlist-iterator')


const execute = async () => {
    const wordlist = wordlistIterator(testWordlist)

    let currentIterationState

    let index = 0
    // 1. First Iteration.
    for await (const word of wordlist) {
        console.log(`word: ${word}`)
        // do some async stuff...
        if (index++ === 2) break // break on 3-rd word.
    }
    console.log('First Iteration Finished.')

    // 2. Second Iteration.
    currentIterationState = { wordlist, index }
    try {
        const result = await innerFunctionFail(currentIterationState)
        console.log(result)
    } catch (error) {
        console.log(error.message)
    }

    // 3. Third Iteration
    currentIterationState = { wordlist, index }
    const result = await innerFunctionSuccess(currentIterationState)
    console.log(result)
    // continue to do something...

}

const innerFunctionSuccess = async ({ wordlist, index }) => {
    for await (const word of wordlist) {
        console.log(`word: ${word}`)
        if (index++ === 4) break // break on 5-th word if reached.
    }
    console.log('innerFunctionSuccess: Iteration Finished.')
    return 'Success'
}

const innerFunctionFail = async ({ wordlist, index }) => {
    for await (const word of wordlist) {
        console.log(`word: ${word}`)
        throw new Error('innerFunctionFail: Error occure') // throw error on 1st iteration.
    }
    // This will be reached only if theres no words left to iterate through.
    console.log('innerFunctionFail: Iteration Finished.')
    return 'Success'
}

execute()

Important

To close underlying ReadableStream of wordlist before iteration is finished ensure to call one of the generator methods - generator.next(), generator.return() with truthy value, or generator.throw() with any value ( including case without value ), e.g.

wordlist.next(true)
// or
wordlist.return(1)
// or as an exception for wordlist.throw().
wordlist.throw('error') // truthy
wordlist.throw(null) // falsy
wordlist.throw() // without value

This will ensure that all underlying resources used by generator are freed.

NOTE: If Iteration is finished this will be done automatically.