@adamburgess/linq 中文文档教程

发布于 3年前 浏览 10 项目主页 更新于 3年前

@adamburgess/linq

一个体面的林克。 有体面的类型。 压缩后不到 2kb。

 npm 版本gzipped sizebrotlied sizenpm 类型定义codecov

Docs/Usage

生成的文档:https: //linq.adam.id.au /

import from from '@adamburgess/linq'
const sequence = from(['an', 'iterable', 'here']);
// now use any methods on sequence!
// e.g. mapping:
const uppercases = sequence.map(x => x.toUpperCase());
// note: the sequence hasn't been mapped yet! toUpperCase hasn't been called!
// you must _run_ the sequence (see Outputs below)
Array.from(uppercases); // or uppercases.toArray()
// => ['AN', 'ITERABLE', 'HERE']

// You can extend already existing transforms:
const reversed = uppercases.reverse();
// still! The sequence hasn't been reversed!
// Again you must run it:
Array.from(reversed);
// => ['HERE', 'ITERABLE', 'AN']
// note! When this reversed array was created, it ran:
// 1. the uppercase sequence (yes, again!)
// 2. the reverse method
// _ALL_ operations are deferred until outputting the sequence!

Features

完全惰性求值。

Inputs

  1. Arrays
  2. Iterables
  3. Generators
  4. Infinite Generators*

Transformations

  1. Map
  2. Where (with narrowing!)
  3. Reversing
  4. Group By
  5. Order By
  6. Order By Descending
  7. Order By …, Then By
  8. Order By …, Then By Descending
  9. Take
  10. Skip
  11. Take While
  12. Skip While
  13. Append
  14. Prepend
  15. Distinct
  16. Flat (with projection to sequence)
  17. Join (an inner join)
  18. GroupJoin

Outputs

  1. Count
  2. toArray
  3. toMap
  4. toObject
  5. First (+ or Default)
  6. Single (+ or Default)
  7. Last (+ or Default)
  8. All
  9. Any
  10. None
  11. Contains
  12. Sum (with projection to number)
  13. Average (with projection to number)
  14. Max (with projection to number)
  15. Min (with projection to number)
  16. Min By
  17. Max By

数字序列的

  1. Sum
  2. Average
  3. Max
  4. Min

特殊添加:可迭代/数组序列

  1. Flat

的特殊添加:字符串序列的特殊添加:

  1. JoinString

* 注意:某些转换/大多数输出​​不适用于无限序列,例如 Group By 和 Order By。

Other libraries

或者:为什么要用这个?

iterare

⚠️ 真的没有足够多的通用方法。 它缺少:Group、Order、Count、First、Last、Distinct。 主观上,我使用所有这些。
⚠️ 支持 ES 迭代器,但不支持可重复/惰性 ES 迭代器
✔️ 非常受欢迎。
✔️ 压缩后的 3,818 字节/压缩后的 1,065 字节✔️

linq.js (on npm: linq)

应有尽有
❌ 除了可迭代的支持。 它支持迭代器但不支持iterables。 大多数时候,您使用可迭代对象。 数组是可迭代的。 您使用可迭代协议将它们转换为迭代器。 从技术上讲,它甚至不支持迭代器。 仅限同时迭代器和可迭代对象的对象。
✔️ 非常受欢迎。
⚠️ 类型可以改进:toObject ~~is~~ was not typed
❌ 35KB 缩小版/6.6KB 压缩版

fromfrom

✔️ 几乎拥有您想要的一切。
✔️ 支持 ES 迭代器,包括惰性/可重复。 (不错!)
⚠️ 不是很受欢迎,但是嘿,这个图书馆很棒。
✔️ 缩小 4,216 字节/压缩 1,330 字节
✅好名字。 import { from } from 'fromfrom'

@adamburgess/linq

✔️ 支持 ES 迭代器,包括 lazy/repeatable。
✔️ 拥有 fromfrom 中的所有内容,iterare 中的所有内容,但 linq.js 中没有所有内容。 考虑添加“扩展”版本。
✔️ 出色的打字,如果我自己这么说的话。 具有其他图书馆所没有的一些功能。
❌ 1 个用户。 哈。
✔️ 缩小 4,457 字节/压缩 1,251 字节

不考虑的:

其他 >不是通用的。~~它现在有一个打字稿版本。 然而,他们已经搞砸了包装——我无法在不更改他们的 package.json 和导入直接路径的情况下导入模块。 出于这个原因,没用。 具有与 fromfrom 类似的功能。

Table comparison to other libraries

✔️ - 有它
⚠️ - 没有,但有一个衬垫可以解决
❌ - 必须自己重新实现,如果多次实现,重新实现会很烦人

this onefromfromiterarelinq.js
Size in bytes (minified)4,4574,2163,81835,451 (+800% ❌)
Size in bytes (brotlied)1,2511,3301,0656,516 (+500% ❌)
Arrays✔️✔️✔️✔️
Iterables✔️✔️✔️
Generators✔️✔️✔️✔️
Infinite Iterables✔️✔️✔️
Lazy Iterables✔️✔️
Map✔️✔️✔️✔️
Where✔️✔️✔️✔️
Reverse✔️✔️✔️
Group By✔️✔️✔️
Order By✔️✔️✔️
Then By✔️✔️✔️
Take✔️✔️✔️✔️
Skip✔️✔️⚠️[5]✔️
Take While✔️✔️✔️
Skip While✔️✔️✔️
Append✔️✔️✔️
Prepend✔️✔️
Distinct✔️✔️✔️
Flat✔️⚠️[1]✔️✔️
Join✔️✔️
Group Join✔️✔️
Count✔️⚠️[2]⚠️[2]✔️
to Array✔️✔️✔️✔️
To Map✔️✔️✔️
to Object✔️✔️✔️
to Set✔️✔️✔️⚠️[8]
First✔️✔️✔️✔️
Single✔️✔️
Last✔️✔️✔️
All✔️✔️✔️✔️
Any✔️✔️✔️✔️
None✔️⚠️[3]⚠️[3]⚠️[3]
Contains✔️✔️⚠️[6]✔️
Sum✔️✔️✔️
Average✔️✔️
Max✔️✔️
Min✔️✔️
Min By✔️✔️
Max By✔️✔️
Sum/Avg/Max/Min fail on non-numbers✔️[4]ⁿ/ₐ
Flatten fails on non-iterables✔️ⁿ/ₐ⚠️[7]⚠️[7]

注意:
1。 使用具有标识的平面图。
2。 使用带有计数的 forEach。
3。 使用 !any
4。 有一些输入可以防止对非数字求和,但实际上没有效果。< /一个
5。 使用切片
6。 使用查找,检查 !== undefined
7. 如果用于不可迭代对象,它会返回未更改的元素。 这遵循 JS 的 .flat() 是如何工作的。 我的意见:为什么要展平不是数组的数组? 不要。
8。 它是无类型的!

Performance

它可能很慢。
它对所有内容都使用迭代器。
如果你想要性能,也许使用 iterare。 他们的自述文件将性能放在首位和中心位置。

@adamburgess/linq

A decent linq. With decent types. Less than 2kb gzipped.

npm versiongzipped sizebrotlied sizenpm type definitionscodecov

Docs/Usage

Generated documentation: https://linq.adam.id.au/

import from from '@adamburgess/linq'
const sequence = from(['an', 'iterable', 'here']);
// now use any methods on sequence!
// e.g. mapping:
const uppercases = sequence.map(x => x.toUpperCase());
// note: the sequence hasn't been mapped yet! toUpperCase hasn't been called!
// you must _run_ the sequence (see Outputs below)
Array.from(uppercases); // or uppercases.toArray()
// => ['AN', 'ITERABLE', 'HERE']

// You can extend already existing transforms:
const reversed = uppercases.reverse();
// still! The sequence hasn't been reversed!
// Again you must run it:
Array.from(reversed);
// => ['HERE', 'ITERABLE', 'AN']
// note! When this reversed array was created, it ran:
// 1. the uppercase sequence (yes, again!)
// 2. the reverse method
// _ALL_ operations are deferred until outputting the sequence!

Features

Completely lazy evaluation.

Inputs

  1. Arrays
  2. Iterables
  3. Generators
  4. Infinite Generators*

Transformations

  1. Map
  2. Where (with narrowing!)
  3. Reversing
  4. Group By
  5. Order By
  6. Order By Descending
  7. Order By …, Then By
  8. Order By …, Then By Descending
  9. Take
  10. Skip
  11. Take While
  12. Skip While
  13. Append
  14. Prepend
  15. Distinct
  16. Flat (with projection to sequence)
  17. Join (an inner join)
  18. GroupJoin

Outputs

  1. Count
  2. toArray
  3. toMap
  4. toObject
  5. First (+ or Default)
  6. Single (+ or Default)
  7. Last (+ or Default)
  8. All
  9. Any
  10. None
  11. Contains
  12. Sum (with projection to number)
  13. Average (with projection to number)
  14. Max (with projection to number)
  15. Min (with projection to number)
  16. Min By
  17. Max By

Special additions for number sequences:

  1. Sum
  2. Average
  3. Max
  4. Min

Special additions for iterable/array sequences:

  1. Flat

Special additions for string sequences:

  1. JoinString

* Note: Some transformations/most outputs do not work with infinite sequences, such as Group By and Order By.

Other libraries

or: why use this one?

iterare

⚠️ Really doesn't have enough methods to be general purpose. It is missing: Group, Order, Count, First, Last, Distinct. Subjectively, I use all of these.
⚠️ Supports ES iterators, but doesn't support repeatable/lazy ES iterators
✔️ Extremely popular.
✔️ 3,818 bytes minified/1,065 bytes brotlied

linq.js (on npm: linq)

✔️ Has everything.
❌ Except iterable support. It supports iterators but not iterables. Most of the time, you use iterables. Arrays are iterables. You use the iterable protocol to convert them to iterators. Technically, it doesn't even support iterators. Only objects that are both iterators and iterables.
✔️ Very popular.
⚠️ Types could be improved: toObject ~~is~~ was not typed
❌ 35KB minified/6.6KB brotlied

fromfrom

✔️ Has nearly everything you'd like.
✔️ Supports ES iterators, including lazy/repeatable. (Nice!)
⚠️ Not very popular, but hey, this library is awesome.
✔️ 4,216 bytes minified/1,330 bytes brotlied
✅ Great name. import { from } from 'fromfrom'

@adamburgess/linq

✔️ Supports ES iterators, including lazy/repeatable.
✔️ Has everything in fromfrom, everything in iterare, but not everything in linq.js. Thinking about adding an "extended" version.
✔️ Excellent typing, if I do say so myself. Has a couple features that other libraries don't have.
❌ 1 user. Hah.
✔️ 4,457 bytes minified/1,251 bytes brotlied

Others not considered:

@siderite/linqer: 5kb brotlied, ~~and the typings aren't generic.~~ It now has a typescript version. Yet, they've borked the packaging -- I can't import the module without changing their package.json and importing the direct path. For that reason, useless. Has similar features to fromfrom.

Table comparison to other libraries

✔️ - has it
⚠️ - doesn't have it, but has a one liner work around
❌ - have to reimplement yourself, and reimplementing would be annoying if done multiple times

this onefromfromiterarelinq.js
Size in bytes (minified)4,4574,2163,81835,451 (+800% ❌)
Size in bytes (brotlied)1,2511,3301,0656,516 (+500% ❌)
Arrays✔️✔️✔️✔️
Iterables✔️✔️✔️
Generators✔️✔️✔️✔️
Infinite Iterables✔️✔️✔️
Lazy Iterables✔️✔️
Map✔️✔️✔️✔️
Where✔️✔️✔️✔️
Reverse✔️✔️✔️
Group By✔️✔️✔️
Order By✔️✔️✔️
Then By✔️✔️✔️
Take✔️✔️✔️✔️
Skip✔️✔️⚠️[5]✔️
Take While✔️✔️✔️
Skip While✔️✔️✔️
Append✔️✔️✔️
Prepend✔️✔️
Distinct✔️✔️✔️
Flat✔️⚠️[1]✔️✔️
Join✔️✔️
Group Join✔️✔️
Count✔️⚠️[2]⚠️[2]✔️
to Array✔️✔️✔️✔️
To Map✔️✔️✔️
to Object✔️✔️✔️
to Set✔️✔️✔️⚠️[8]
First✔️✔️✔️✔️
Single✔️✔️
Last✔️✔️✔️
All✔️✔️✔️✔️
Any✔️✔️✔️✔️
None✔️⚠️[3]⚠️[3]⚠️[3]
Contains✔️✔️⚠️[6]✔️
Sum✔️✔️✔️
Average✔️✔️
Max✔️✔️
Min✔️✔️
Min By✔️✔️
Max By✔️✔️
Sum/Avg/Max/Min fail on non-numbers✔️[4]ⁿ/ₐ
Flatten fails on non-iterables✔️ⁿ/ₐ⚠️[7]⚠️[7]

notes:
1. Use flatmap with identity.
2. Use forEach with a count.
3. Use !any
4. There is some typing to prevent Sum on non-numbers, but it actually has no effect.
5. Use slice
6. Use find, check for !== undefined
7. If used on non-iterables, it returns the element unchanged. This follows how JS's .flat() works. My opinion: Why are you flattening an array of things that aren't arrays? Don't.
8. It's untyped!

Performance

It's probably slow.
It uses iterators for everything.
If you want performance, maybe use iterare. Their readme puts performance front and center.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文