Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A familiar set of functions that operate on JavaScript iterables (ES2015+) in a similar way to .NET's LINQ does with enumerables.

License

NotificationsYou must be signed in to change notification settings

tsdotnet/linq

Repository files navigation

alt text tsdotnet / linq

GitHub license100% code coveragenpm-publishnpm version

A familiar set of functions that operate on JavaScript iterables (ES2015+) in a similar way to .NET's LINQ does with enumerables.

Docs

tsdotnet.github.io/linq

Source

GitHub

API

linq<T> vslinqExtended<T>

It is possible to do everything with justlinq butlinqExtended offers more functionality for those expecting to use common resolutions like.count,.first,.last, etc. Usinglinq will save you some bytes when your common use cases do not need resolutions.

Iterating

for(consteoflinq(source).filter(a)){// Iterate filtered results.}
for(consteoflinq(source).filterWith(a,b,c).transform(x)){// Iterate filtered and then transformed results.}
for(consteoflinq(source).where(predicate).skip(10).take(10).select(mapping)){// Iterate filtered and mapped results.}

Resolving

constresult=linq(source).filterWith(a,b,c).transform(x).resolve(r);
constfirstElement=linqExtended(source).where(predicate).select(mapping).first();

Examples

linq<T> with imported filters

importlinqfrom'@tsdotnet/linq/dist/linq';importrangefrom'@tsdotnet/linq/dist/iterables/range';importwherefrom'@tsdotnet/linq/dist/filters/where';importdescendingfrom'@tsdotnet/linq/dist/filters/descending';constsource=range(1,100);// Iterable<number>constfiltered=linq(source).filters(where(n=>n%2===1),descending);for(constooffiltered){// Emit all odd numbers in descending order.console.log(o);// 99, 97, 95 ...}

linq<T> with simplified imports

importlinq,{iterables,resolutions}from'@tsdotnet/linq';constsource=iterables.range(1,100);// Iterable<number>constresult=linq(source).where(n=>n%2===1)// odd numbers only.resolve(resolutions.sum);// 2500

or

importlinqfrom'@tsdotnet/linq';import{range}from'@tsdotnet/linq/dist/iterables';import{sum}from'@tsdotnet/linq/dist/resolutions';constsource=range(1,100);// Iterable<number>constresult=linqExtended(source).where(n=>n%2===1)// odd numbers only.resolve(sum);// 2500

Concepts

Iterables

ES2015 enables full support for theiteration protocol.

Iterables are a significant leap forward in operating with data sequences.Instead of loading entire sets into arrays or other collections, iterables allow for progressive iteration or synchronous streaming of data.

tsdotnet/linq is designed around iterables but also optimized for arrays.

Generators

Iterable<T> helpers are provided as sources. Calling for anIterator<T> should always start from the beginning and iterators are not shared. Same behavior as LINQ in .NET.

empty,range, andrepeat to name a few.See thedocs for a full list.

Filters

linq(source).filter(a,b);linq(source).filter(a).filter(b);linq(source).filter(a).where(predicate);

Any function that receives anIterable<T> and returns anIterable<T> is considered anIterableFilter<T>. A filter may result in a different order or ultimately a completely different set than the input but must be of the same type.

There are an extensive set of filters.See thedocs for a full list.

Transforms

linq(source).transform(x);linq(source).filter(a).transform(x);linq(source).where(predicate).transform(x);linq(source).where(predicate).select(mapping);

Any function that receives anIterable<T> and returns anIterable<TResult> is considered anIterableValueTransform<T, TResult>.

Any filter can be used as a transform, but not every transform can be used as a filter.

notNull,rows,select,selectMany andgroupBy to name a few.See thedocs for a full list.

Resolutions

sequence=linq(source);sequence.resolve(r);sequence.transform(x).resolve(r);sequence.filter(a).transform(x).resolve(r);sequence.where(predicate).resolve(r);sequence.filterWith(a,b).transform(x).resolve(r);
sequence=linqExtended(source);// Examples:sequence.any(predicate);sequence.any();// resolution predicates are optional.sequence.count(predicate);sequence.first(predicate);sequence.last(predicate);sequence.singleOrDefault(defaultValue,predicate);sequence.firstOrUndefined(predicate);sequence.lastOrNull(predicate);

A resolution is a transform that takes anIterable<T> and returnsTResult.Unlike.filter(a) and.transform(x),.resolve(r) does not wrap the result in anotherLinq<T>.

There are an extensive set of resolutions.See thedocs for a full list.

History

Originally this was a port oflinq.js converted to full TypeScript under the nameTypeScript.NET Library and thenTypeScript.NET-Core with full module support but potentially more than a user might want for a simple task. Instead of .NET style extensions,Enumerables incurred a heavy cost of all the extensions under one module.

Modern web standards and practices demanded more granular access to classes and functions. Hencetsdotnet was born.tsdotnet/linq functionally allows for all the features of its predecessor as well as providing type-safety, and most of the features of LINQ in .NET while not forcing the consumer to download unneeded/undesired modules (extensions).


[8]ページ先頭

©2009-2025 Movatter.jp