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

@xeinebiu/ts-iterable

v1.1.0

Published

Enumerable arrays for Typescript

Downloads

4

Readme

Iterable for Typescript

Similar to what we know from C#, Dart or any other language which supports them, we use Iterables to stream over collections.

Why using the Iterable?

Iterables are useful when you want to chain several operations on a collection such as

  • filter
  • filterNotNull
  • group
  • sort
  • map
  • mapNotNull
  • take
  • skip
  • every
  • none
  • some
  • etc ...

For example, lets consider a case. We need to work with a collection to filter the numbers greater than 20, map to the string only the 3'rd value.

  • Without the Iterable
const data = [1, 10, 20, 30, 40, 50, ...];
const filteredData = data.filter(x => x > 20);
const value = filteredData[3];
const mappedValue = value.toString();

// result "50"
  • Using the Iterable
const data = [1, 10, 20, 30, 40, 50, ...];
const mappedValue = asIterable(data)
    .filter(x => x > 20)
    .skip(2)
    .map(x => x.toString())
    .first()

// result "50"

The Iterable code would be similar as

let skipped = 0;
for (let i = 0; i < data.length; i++) {
    const element = data[i];
    if (element > 20 && ++skipped < 2) return element.toString();
}
throw new NoElementError();

Not only the difference stays that we have written it differently, but also how much data was processed.

On the example without using the Iterable

  • All elements of the collection are visited and filtered
  • The third element is retrieved
  • The retrieved element is mapped to a string

Now, if the collection is really huge, this will take time to process.

While, using the Iterable, that is not necessarily as we know we do not need all the elements. Because we call first() at the end, that means that the operation will stop as soon this condition is meet.

  • Find from collection only the first value that is greater than 20
  • Map the value to a string

Installation

npm i @xeinebiu/ts-iterable

Examples

Convert a list to iterable

const data = [1, 2, 3, 4, 5];
const iterable = asIterable(data);

Filter

// without the Iterable
const filtered = data.filter(x => x < 4);

// with iterable
const filtered = asIterable(data)
    .filter(x => x < 4)
    .toList();

// result [1, 2, 3]

Filter Not Null

Filter undefined|null values out

const data = [1, 2, null, 3, undefiend, 4];
const filtered = asIterable(data)
    .filterNotNull();

// result [1, 2, 3, 4]

Take

Take specific amount of elements

// without iterable
const taken = data.slice(0, 3);

// with iterable
const taken = asIterable(data)
    .take(3)
    .toList();

// result [1, 2, 3]

Every

Return true if all elements match the predicate.

const result = asIterable(data)
    .every(x => x.toString() !== "hello world");

// result true

Some

Return true if any of the elements match the predicate

const result = asIterable(data)
    .some(x => x.toString() !== "1");

// result true

None

Return true if all the elements do not match the predicate

const result = asIterable(data)
    .none(x => x <= -1);

// result true

First

Return the first element if available, otherwise throw NoElementError

const result = asIterable(data)
    .filter(x => x > 4)
    .first();

// result 5

First Or Null

Return the first element if available, otherwise null.

const result = asIterable(data)
    .filter(x => x > 100)
    .firstOrNull();

// result null

Map

Map the elements using a mapper

const result = asIterable(data)
    .filter(x => x < 3)
    .map(x => x.toString())
    .toList();

// result ["1", "2"]

Map Not Null

Map the elements using the mapper and avoid inserting null|undefined values in the list

const data = [1, null, 2, undefined, 3];

const result = asIterable(data)
    .filter(x => x < 3)
    .mapNotNull(x => x?.toString())
    .toList();

// result ["1", "2", "3"]

Skip

Offset the elements cursor starting from index 0

const result = asIterable(data)
    .skip(1)
    .toList();

// result ["2", "3", "4", "5"]

Take

Take specific amount of elements

const result = asIterable(data)
    .take(2)
    .toList();

// result ["1", "2"]

Sort

Sort all elements and return new [ExtendedIterable]

const sorted = asIterable(data)
    .sort((a, b) => b - a)
    .toList();

// result [5, 4, 3, 2, 1]

Group

Group all elements and return new [ExtendedIterable]

const data = [-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

const groupedData = asIterable(data)
    .group(x => {
        if (x < 0) return "negative";
        return "positive";
    })
    .toList();

// result
// [
//     ["negative", [-9, -8, -7, -6, -5, -4, -3, -2, -1]],
//     ["positive", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
// ];

To List

Convert the Iterable to a collection.

const list = asIterable(data)
    .toList();

// result [1, 2, 3, 4, 5]

MIT

The MIT License

License: MIT