Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shun-shobon/iter-funcs
Utility functions for iterators. Inspired by Rust's `std::iter::Iterator` trait.
https://github.com/shun-shobon/iter-funcs
deno functional functional-programming iterator javascript typescript utilities
Last synced: about 2 months ago
JSON representation
Utility functions for iterators. Inspired by Rust's `std::iter::Iterator` trait.
- Host: GitHub
- URL: https://github.com/shun-shobon/iter-funcs
- Owner: shun-shobon
- License: mit
- Archived: true
- Created: 2022-09-01T01:42:52.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-01T10:56:05.000Z (10 months ago)
- Last Synced: 2024-09-26T21:58:59.985Z (about 2 months ago)
- Topics: deno, functional, functional-programming, iterator, javascript, typescript, utilities
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@shun-shobon/iter-funcs
- Size: 94.7 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# iter-funcs
[![npm](https://img.shields.io/npm/v/@shun-shobon/iter-funcs?logo=npm)](https://www.npmjs.com/package/@shun-shobon/iter-funcs)
[![deno](https://img.shields.io/github/v/release/shun-shobon/iter-funcs?label=deno&logo=deno)](https://deno.land/x/iter_funcs)
[![test](https://github.com/shun-shobon/iter-funcs/actions/workflows/test.yml/badge.svg)](https://github.com/shun-shobon/iter-funcs/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/shun-shobon/iter-funcs/branch/master/graph/badge.svg?token=gwyScwGdCG)](https://codecov.io/gh/shun-shobon/iter-funcs)## About
Utility functions for iterators. Inspired by Rust's
[`std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html)
trait. This library uses JavaScript native iterators, so it's compatible with
any library that uses them.## Support platforms
- Node.js
- Deno
- Browser## Installation
### Node.js
Install package from npm.
```sh
npm install @shun-shobon/[email protected]
```You can import from the package as `@shun-shobon/iter-funcs`.
```ts
import { filter, map } from "@shun-shobon/iter-funcs";
```### Deno
You can directly import from `deno.land/x`.
```ts
import { filter, map } from "https://deno.land/x/[email protected]/mod.ts";
```### Browser
You can directly import from `unpkg.com`.
```js
import {
filter,
map,
} from "https://unpkg.com/@shun-shobon/[email protected]/esm/mod.js";
```## Example
This is a basic example.
```ts
import {
filter,
forEach,
fromIterable,
map,
pipe,
take,
} from "https://deno.land/x/[email protected]/mod.ts";const array = [1, 2, 3, 4, 5, 6];
pipe(
array,
fromIterable, // make iterator from iterable
filter((x) => x % 2 === 0), // filter even numbers
map((x) => x * 2), // multiply by 2
take(2), // take first 2 elements
forEach(console.log), // => 4, 8
);
```You can also `AsyncIterator`.
```ts
import { expandGlob } from "https://deno.land/std/fs/mod.ts";
import {
asyncFilter,
asyncForEach,
asyncMap,
asyncTake,
asyncToArray,
pipe,
} from "https://deno.land/x/[email protected]/mod.ts";const files: Array = await pipe(
expandGlob("src/*.ts"), // Find all .ts files in src directory
asyncMap((entry) => entry.path), // Get path from entry
asyncFilter((path) => !path.endsWith("_test.ts")), // Exclude test files
asyncTake(2), // Take first 2 elements
asyncMap((path) => Deno.readTextFile(path)), // Read file content
asyncToArray, // Convert to array
);
```## Difference from `Array.prototype` methods
This library's functions are **lazy**. They don't evaluate the whole iterator at
once. They evaluate only the needed elements. This is useful when you have a
large iterator and you only need a few elements from it.## Why not use method chaining?
Method chaining is a common pattern in JavaScript. However, it tends to increase
bundle size. It also makes it difficult to use with other libraries that use
iterators.