0.1.0 • Published 7 months ago

@drew887121/iteratorfuncs v0.1.0

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
7 months ago

IteratorFuncs

A tiny package to simplify working with iterators

tldr: Provides a small set of tools for adding map/filter/reduce functionality to iterators without having to yield their values.

const set = new Set([1, 2, 3, 4, 5]);

const mappedAndFiltered = mapIterator(set, (item) => item * 2).filter((item) => item > 5);

console.log(Array.from(mappedAndFiltered)); // logs out [6, 8, 10]

These functions take an IterableIterator or an Iterator, a function of the appropriate type and will return an AugmentedIterator:

The AugmentedIterator class also provides .map, .filter and .reduce helper functions that are chainable.

Greedy Versions

We also provide small utility functions for if you want to just immediately yield all values from the iterator at the same time but don't want to bother doing an Array.from first:

const set = new Set([1, 2, 3, 4, 5]);

const mappedValues = mapIteratorToArray(set, (item: number): number => {
  return item * 2;
});

console.log(mappedValues); // logs out [2, 4, 6, 8, 10]

For these "greedy" utilities there are:

Docs

Docs are auto generated and live at https://drew887.github.io/iteratorFuncs

Why?

It has become very common for me in places that I work in to need to be able to do the classic map/reduce/filter/etc with iterators (but don't want to have to convert them into arrays first since that is bad for perf, or I need to pass the iterator to another package); while also not being allowed to bring in a package to do so, say lodash or equivalent, due to company/security policy of not allowing new outside packages without having to go through a formal review process.

So here is an MPL-2.0 licensed single file package that gives the ability to do just that.

MPL-2.0 was used so that if need be you can copy the src/index.ts or dist/index.js files as need be to use in your projects. Since MPL-2.0 has so-called "file based copyleft" as long as you don't remove the license call out at the top of the file and don't make changes to the file itself then you're fine to copy the file into your project as needed.

Why not MIT?

TLDR, I Wanted to try out MPL-2.0 based things, and while the MIT license would also fit my use case quite well I want to keep some level of copyleft to my work. This way anyone can use the file untouched so to speak but if they make any changes to the file directly, they're required to distribute said changes.

0.1.0

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago