@utilx/iterator
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

iterator-utils

npm install @typeshell/iterator-utils --save

Now it could work

核心方法 ix

通过iter,将实现了Iterable接口的对象转换成了我们内部定义接口更强大的IterableIteratorX接口。

生成列表

import ix from "iterator-utils"

const t = ix(10).take(e => e%2===0)
console.log(t.toArray())

对迭代器(Iterable)的增强

import ix from "iterator-utils"

const t = ix([1,2,3]).take("2").take(/^\d+[68]$/).map(e => e * 10)
console.log(t.toArray())

// python用法: 
// [i * 12 for i in range(10) if i % 2 == 0]
ix(10).fm((v, ds) => i % 2 === 0 ? v * 12 : ds)

对 异步迭代器(AsyncIterable)的增强

import ix from "iterator-utils"

const t = ix([1,2,3]).take("2").take(/^\d+[68]$/).map(e => e * 10)
console.log(t.toArray())

对 Map 的增强

import ix from "iterator-utils"

const map: Map<string, string> = ix(10).toMap(e=>[e, e*2]) // 生成Map,内容为 { 0:0, 1: 2, 3:4, ..., 9:18}
map.map((k, v) => [k, v+1])

const m1 = new Map<number, number>()
m1.set(1,2)
m1.set(2,4)
ix(m1).map((k, v) => [k, v+1])
ix(m1).fm((k, v, ds) => k % 2 === 0 ? ds : [k, v+1])

为什么不对 Array 和 Object 进行增强?

增强的成员方法,会在Object.keys(source) 调用中作为成员返回,一定程度改变了source 这个对象的内部,因此,并不提供这种实现。

支持glob/regex的take/drop函数

take函数可以接受字符串(glob模式),RegExp(正则表达式),函数(此时,take功能等同于filter) drop函数亦然,与take不同的是,drop函数是条件成立时丢弃数据

另外,支持glob/regex/word的takeBy/dropBy函数族

chain函数

支持惰性列表以及无限序列

Readme

Keywords

none

Package Sidebar

Install

npm i @utilx/iterator

Weekly Downloads

8

Version

0.0.3

License

ISC

Unpacked Size

48.7 kB

Total Files

24

Last publish

Collaborators

  • lteam