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

luckystarry-collections

v1.0.0

Published

模仿 .NET 的集合类,对数组类提供了类似于 LINQ 的方法,如 Select Where ToList 等。

Downloads

114

Readme

luckystarry-collections

模仿 .NET 的集合类,对数组类提供了类似于 LINQ 的方法,如 Select Where ToList

Build Status Coverage Status Npm Status install size JSDelivr codebeat badge Known Vulnerabilities License Status

Install

打包安装使用

npm install -S luckystarry-collections

使用此种方式请在项目入口处增加以下代码,否则无法对Array进行扩展

import 'luckystarry-collections'

浏览器引入使用

<script src="https://cdn.jsdelivr.net/npm/luckystarry-collections/dist/index.min.js"></script>

Usage

类或接口

IEnumerable<T>

可枚举的集合

List<T> 及 IList<T>

列表对象 及 列表接口

Dictionary<TKey, TValue> 及 IDictionary<TKey, TValue>

字典对象 及 字典接口

ReadOnlyCollection<T>

只读列表

Array<T>

数组

集合类的公共方法

All(predicate: (item: T) => boolean): boolean

判断集合中是否所有元素均符合 predicate 的要求,是则返回 true 否则 返回 false

[1, 2, 3].All(x => x > 0)
// true

[1, 2, 3].All(x => x > 1)
// false

Any(predicate: (item: T) => boolean): boolean

判断集合中是否存在元素符合 predicate 的要求,是则返回 true 否则 返回 false

[1, 2, 3].Any(x => x > 0)
// true

[1, 2, 3].Any(x => x > 1)
// true

[1, 2, 3].Any(x => x > 3)
// false

AsEnumerable(): IEnumerable<T>

将一个集合对象转为其可枚举接口形式表达

[1, 2, 3].AsEnumerable()
// [1, 2, 3]

Average(selector?: (item: T) => number): number | null

计算集合中数据的平均值,如果集合为非数型集合,则利用 selector 映射出列表中每个元素的数值

[1, 2, 3].Average()
// 2

[1, 2, 3].Average(x => x * 2)
// 4

Concat(second: IEnumerable<T>): IEnumerable<T>

连接两个集合

[1, 2, 3].Concat([3, 4, 5])
// [1, 2, 3, 3, 4, 5]

Contains(value: T, comparer?: IEqualityComparer<T>): boolean

判断集合中是否存在某个元素值,并可通过指定的等值比较器进行比较,如果存在则返回 true

[1, 2, 3].Contains(2)
// true

Count(predicate?: (item: T) => boolean): number

计算集合中符合 predicate 要求的元素的数目,如果没有传入此条件,则返回集合中全部元素的数目

[1, 2, 3].Count()
// 3

[1, 2, 3].Count(x => x > 1)
// 2

DefaultIfEmpty(defaultValue?: IEnumerable<T>): IEnumerable<T>

判断集合是否为空集,如果为空集则返回 defaultValue 作为默认集合,否则返回一个新的包含原有全部元素的集合对象

[1, 2, 3].DefaultIfEmpty()
// [1, 2, 3]

[].DefaultIfEmpty([1])
// [1]

Distinct(comparer?: IEqualityComparer<T>): IEnumerable<T>

将一个集合去重,并可通过 comparer 作为等值判断器来判断元素是否相等

[1, 2, 3].Distinct()
// [1, 2, 3]

[1, 2, 3, 3, 4].Distinct()
// [1, 2, 3, 4]

ElementAt(index: number): T

从前向后取出从 0 开始的第 index 个元素

[1, 2, 3].ElementAt(1)
// 2

ElementAtOrDefault(defaultValue: T, index: number): T

从前向后取出从 0 开始的第 index 个元素,如果超出集合范围,则返回 defaultValue 作为默认值

[1, 2, 3].ElementAtOrDefault(0, 1)
// 2

[1, 2, 3].ElementAtOrDefault(0, 3)
// 0

Except(second: IEnumerable<T>, comparer?: IEqualityComparer<T>): IEnumerable<T>

从集合中排除 second 中存在的元素,并可通过 comparer 作为等值判断器来判断元素是否相等

[1, 2, 3].Except([3, 4])
// [1, 2]

First(predicate?: (item: T) => boolean): T

从集合中取出符合 predicate 要求的第一个元素,如果不使用此条件,则返回集合的第一个元素

[1, 2, 3].First()
// 1

[1, 2, 3].First(x => x > 1)
// 2

FirstOrDefault(defaultValue: T, predicate?: (item: T) => boolean): T

从集合中取出符合 predicate 要求的第一个元素,如果不使用此条件,则返回集合的第一个元素,如果元素不存在,则返回 defaultValue 作为默认值

[1, 2, 3].FirstOrDefault(0)
// 1

[1, 2, 3].FirstOrDefault(0, x => x > 1)
// 2

[1, 2, 3].FirstOrDefault(0, x => x > 3)
// 0

GroupBy<TKey, TElement = T>(keySelector: (item: T) => TKey, elementSelector?: (item: T) => TElement, comparer?: IEqualityComparer): IEnumerable<IGrouping<TKey, TElement>>

将集合的每一个元素按照 keySelector 选择出的值作为分组条件,将每个元素通过 elementSelector 映射后做组合,判断键值相等时可使用 comparer 作为等值比较器进行比较

[1, 2, 3].GroupBy(x => x)
// [{ Key: 1, [1] }, { Key: 2, [2] }, { Key: 3, [3] }]

[{ Value: 1 }, { Value: 1 }, { Value: 2 })].GroupBy(x => x.Value, e => e)
// [{ Key: 1, [{ Value: 1 }, { Value: 1}] }, { Key: 2, [{ Value : 2}] }]

GroupJoin<TInner, TKey, TResult = { Outer: T; Inners: IEnumerable }>(inner: IEnumerable, outerKeySelector: (item: T) => TKey, innerKeySelector: (item: TInner) => TKey, resultSelector?: (item: T, inners: IEnumerable) => TResult, comparer?: IEqualityComparer): IEnumerable

[1, 2, 3].GroupJoin([3, 4, 5], o => o, i => i)
// [{ Outer: 1, Inners: [] }, { Outer: 2, Inners: [] }, { Outer: 3, Inners: [3] }]

Intersect(second: IEnumerable<T>, comparer?: IEqualityComparer<T>): IEnumerable<T>

从集合中取出 second 中也存在的元素集合,并可通过 comparer 作为等值判断器来判断元素是否相等

[1, 2, 3].Intersect([3, 4])
// [3]

Join<TInner, TKey, TResult = { Outer: T; Inner: TInner }>(inner: IEnumerable, outerKeySelector: (item: T) => TKey, innerKeySelector: (item: TInner) => TKey, resultSelector?: (item: T, inners: TInner) => TResult, comparer?: IEqualityComparer): IEnumerable

将集合的每一个元素按照 outerKeySelector 映射出的值,与 inner 通过 innerKeySelector 映射出的值,使用 comparer 作为等值比较器进行比较,将二者都存在的数据通过 resultSelector 映射为新集合的结果

[1, 2, 3].Join([3, 4, 5], o => o, i => i)
// [{ Outer: 3, Inner: [3] }]

Last(predicate?: (item: T) => boolean): T

从集合中取出符合 predicate 要求的最后一个元素,如果不使用此条件,则返回集合的最后一个元素

[1, 2, 3].Last()
// 3

[1, 2, 3].Last(x => x > 1)
// 3

LastOrDefault(defaultValue: T, predicate?: (item: T) => boolean): T

从集合中取出符合 predicate 要求的最后一个元素,如果不使用此条件,则返回集合的最后一个元素,如果元素不存在,则返回 defaultValue 作为默认值

[1, 2, 3].LastOrDefault(0)
// 3

[1, 2, 3].LastOrDefault(0, x => x > 1)
// 3

[1, 2, 3].LastOrDefault(0, x => x < 1)
// 0

Max(selector?: (item: T) => number): number | null

计算集合中数据的最大值,如果集合为非数型集合,则利用 selector 映射出列表中每个元素的数值

[1, 2, 3].Max()
// 3

[1, 2, 3].Max(x => x * 2)
// 6

Min(selector?: (item: T) => number): number | null

计算集合中数据的最小值,如果集合为非数型集合,则利用 selector 映射出列表中每个元素的数值

[1, 2, 3].Min()
// 1

[1, 2, 3].Min(x => x * 2)
// 2

Reverse(): IEnumerable<T>

生成一个与现有集合元素顺序相反的新集合

[1, 2, 3].Select(x => x)
// [1, 2, 3]

[1, 2, 3].Select(x => x * 3)
// [3, 6, 9]

Select(selector: (item: T, index?: number) => TResult): IEnumerable

通过 selector 将集合中的每一个元素映射成新的元素并组成新的集合

[1, 2, 3].Reverse()
// [3, 2, 1]

SelectMany<TCollection, TResult = TCollection>(collectionSelector: (item: T, index?: number) => IEnumerable, resultSelector?: (item: T, collection: TCollection) => TResult): IEnumerable

通过 collectionSelector 将集合中的每一个元素映射成集合对象,并将全部的集合对象的值通过 resultSelector 映射出新的元素集合

[[1, 2], [2, 3], [3, 4]].SelectMany(x => x)
// [1, 2, 2, 3, 3, 4]

SequenceEqual(second: Iterable<T>, comparer?: IEqualityComparer<T>): boolean

判断集合与 second 是否每一个元素都相等且顺序一致,可通过 comparer 判断两个元素是否相等

[1, 2, 3].SequenceEqual([1, 2, 3])
// true

[1, 2, 3, 4].SequenceEqual([1, 2, 3])
// false

[1, 2, 3].SequenceEqual([3, 2, 1])
// false

Single(predicate?: (item: T) => boolean): T

从集合中取出符合 predicate 要求的唯一元素,如果不使用此条件,则返回集合的唯一元素,如果集合中符合条件的元素不唯一,则会抛出异常

[1].Single()
// 1

[1, 2, 3].Single(x => x > 2)
// 3

SingleOrDefault(defaultValue: T, predicate?: (item: T) => boolean): T

从集合中取出符合 predicate 要求的唯一元素,如果不使用此条件,则返回集合的唯一元素,如果集合中符合条件的元素不唯一,则会抛出异常,如果元素不存在,则返回 defaultValue 作为默认值

[1].SingleOrDefault(0)
// 1

[1, 2, 3].SingleOrDefault(0, x => x > 2)
// 3

[1, 2, 3].SingleOrDefault(0, x => x > 3)
// 0

Skip(count: number): IEnumerable<T>

从集合中跳过 count 个元素,并将剩余元素组成新的集合

[1, 2, 3].Skip(1)
// [2, 3]

SkipWhile(predicate: (item: T, index?: number) => boolean): IEnumerable<T>

从集合中跳过符合 predicate 要求的元素,并将剩余元素组成新的集合

[1, 2, 3].SkipWhile(x => x < 2)
// [2, 3]

Sum(selector?: (item: T) => number): number | null

计算集合中数据的总和,如果集合为非数型集合,则利用 selector 映射出列表中每个元素的数值

[1, 2, 3].Sum()
// 6

[1, 2, 3].Sum(x => x * 2)
// 12

Take(count: number): IEnumerable<T>

从集合中选取 count 个元素组成新的集合

[1, 2, 3].Take(2)
// [1, 2]

TakeWhile(predicate: (item: T, index?: number) => boolean): IEnumerable<T>

从集合中选取符合 predicate 要求的元素,并将这些元素组成新的集合

[1, 2, 3].TakeWhile(x => x < 2)
// [1]

ToArray(): Array<T>

将集合中的所有元素放置在一个新的数组对象中

[1, 2, 3].ToArray()
// [1, 2, 3]

ToDictionary<TKey, TElement = T>(keySelector: (item: T) => TKey, elementSelector?: (item: T) => TElement, comparer?: IEqualityComparer): Dictionary<T>

将集合中的所有元素放置在一个新的字典对象中,其中每一个元素通过 keySelector 映射成键,通过 elementSelector 映射为值

[1, 2, 3].ToDictionary(x => x)
// [{1, 1}, {2, 2}, {3, 3}]

ToList(): List<T>

将集合中的所有元素放置在一个新的列表对象中

[1, 2, 3].ToList()
// [1, 2, 3]

Union(second: Iterable<T>, comparer?: IEqualityComparer<T>): IEnumerable<T>

将两个集合连接起来,并去除重复的元素,可通过 comparer 判断两个元素是否相等

[1, 2, 3].Union([3, 4, 5])
// [1, 2, 3, 4, 5]

Where(predicate: (item: T, index?: number) => boolean): IEnumerable<T>

从集合中筛选出符合 predicate 要求的元素,并组成一个新的集合

[1, 2, 3].Where(x => x > 1)
// [2, 3]

LICENSE

MIT License

Copyright (c) 2018 SUN BO [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.