1.7.3 • Published 3 years ago

sharp-collections v1.7.3

Weekly downloads
21
License
MIT
Repository
-
Last release
3 years ago

Sharp Collections - TypeScript LINQ library

Sharp Collections is new and modern LINQ library for TypeScript (can be used with JavaScript also). This is the library that you want to use for collections in TypeScript. It implements all of .NET Linq methods and some more (first, groupBy, groupJoin, orderBy, select, singleOrDefault, join, where etc.).

github version npm version build status dependencies dev dependencies vulnerabilities

Features

  • All .NET LINQ methods available and some more
  • Deferred (lazy) execution
  • Dictionary and HashSet with EqualityComparer support (same as in .NET)
  • Intellisense friendly
  • Implemented using generators and iterators
  • ForOf cycle compatible
  • Supports ES5 targeting

Playground

CodeSandbox playground

How to install

npm install sharp-collections

Example

import { Enumerable } from 'sharp-collections';

const data = [
  { name: 'Martin', sex: 'M', age: 28 },
  { name: 'Jane', sex: 'F', age: 27 },
  { name: 'Thomas', sex: 'M', age: 15 },
  { name: 'William', sex: 'M', age: 78 },
  { name: 'Kelly', sex: 'F', age: 30 },
];

const enumerable = Enumerable.from(data); // or List.from(data)

const adults = enumerable.where(x => x.age >= 18);
const adultsGroupedBySex = adults.groupBy(x => x.sex); // nothing is executed so far

// use any collection (Enumerable, List, Dictionary, ...) in ForOf cycle
for (const group of adultsGroupedBySex) {
  console.debug(`Sex: ${group.key}, count: ${group.count()}`);
}

// converts Enumerable to List
const adultsGroupedBySexList = adultsGroupedBySex.toList(); // or toReadOnlyList()

// converts Enumerable to JS Array
const adultsGroupedBySexArray = adultsGroupedBySex.toArray();

Collections

CollectionRead-Only alternativeDescription
Enumerable-Represents a collection which supports a simple iteration.
ListReadOnlyListRepresents a list of objects that can be accessed by index.
DictionaryReadOnlyDictionaryRepresents a collection of keys and values. Values can be accessed by keys.
HashSetReadOnlyHashSetRepresents a set of values.
LinkedList-Represents a linked list of objects.
Stack-Represents a stack of objects.
Queue-Represents a queue of objects.

Implemented Linq methods

MethodDescription
aggregateApplies an accumulator function over a sequence.
allDetermines whether all elements of a sequence satisfy a condition.
anyDetermines whether a sequence contains any elements.
appendAppends a value to the end of the sequence.
asEnumerableReturns the sequence typed as an Enumerable.
asIndexedReturns indexed sequence.
averageComputes the average of a sequence that are obtained by invoking a transform function on each element of the input sequence.
castCasts the elements to the specified type.
concatConcatenates two sequences.
containsDetermines whether a sequence contains a specified element by using an equality comparer.
countReturns a number that represents how many elements in the specified sequence satisfy a condition.
distinctReturns distinct elements from a sequence.
distinctByReturns distinct elements from a sequence by using key selector to compare values.
elementAtReturns the element at a specified index in a sequence.
elementAtOrDefaultReturns the element at a specified index in a sequence or a default value if the index is out of range.
elementsAtReturns elements at a specified indexes in a sequence.
elementsAtRestReturns elements at a specified indexes in a rest sequence.
exceptProduces the set difference of two sequences.
firstReturns the first element of a sequence.
firstOrDefaultReturns the first element of a sequence, or undefined if the sequence contains no elements.
fullJoinPerforms a full outer join on two homogeneous sequences.
groupByGroups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.
groupJoinCorrelates the elements of two sequences based on equality of keys and groups the results.
intersectProduces the set intersection of two sequences.
isEmptyDetermines whether the sequence is empty.
joinCorrelates the elements of two sequences based on matching keys.
lastReturns the last element of a sequence that satisfies a specified condition.
lastOrDefaultReturns the last element of a sequence that satisfies a condition or undefined if no such element is found.
leftGroupJoinCorrelates the elements of two sequences based on equality of keys and groups the results.
leftJoinPerforms a left outer join on two homogeneous sequences.
maxInvokes a transform function on each element of a sequence and returns the maximum value.
maxByReturns the maximal elements of the given sequence, based on the given projection.
minInvokes a transform function on each element of a sequence and returns the minimum value.
minByReturns the minimal elements of the given sequence, based on the given projection.
noDetermines whether no elements of a sequence satisfy a condition.
ofTypeFilters the elements based on a specified type.
orderBySorts the elements of a sequence according to a key.
orderByDescendingSorts the elements of a sequence in descending order according to a key.
prependAdds a value to the beginning of the sequence.
reverseInverts the order of the elements in a sequence.
rightJoinPerforms a right outer join on two homogeneous sequences.
selectProjects each element of a sequence into a new form.
selectManyProjects each element of a sequence and flattens the resulting sequences into one sequence.
sequenceEqualDetermines whether two sequences are equal by comparing the elements.
singleReturns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
singleOrDefaultReturns the only element of a sequence that satisfies a specified condition or undefined if no such element exists; this method throws an exception if more than one element satisfies the condition.
skipBypasses a specified number of elements in a sequence and then returns the remaining elements.
skipWhileBypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
sliceReturns subsequence of a sequence.
sumComputes the sum of the sequence that are obtained by invoking a transform function on each element of the input sequence.
takeReturns a specified number of contiguous elements from the start of a sequence.
takeWhileReturns elements from a sequence as long as a specified condition is true.
thenByPerforms a subsequent ordering of the elements in a sequence according to a key.
thenByDescendingPerforms a subsequent ordering of the elements in a sequence in descending order according to a key.
toArrayConverts sequence to an Array.
toReadOnlyDictionaryConverts sequence to a ReadOnlyDictionary.
toDictionaryConverts sequence to a Dictionary.
toHashSetConverts sequence to a HashSet.
toLinkedListConverts sequence to a LinkedList.
toListConverts sequence to a List.
toLookupConverts sequence to a Lookup.
toMapConverts sequence to a Map.
toReadOnlyHashSetConverts sequence to a ReadOnlyHashSet.
toReadOnlyListConverts sequence to a ReadOnlyList.
toReadOnlyMapConverts sequence to a ReadOnlyMap.
toReadOnlySetConverts sequence to a ReadOnlySet.
toSetConverts sequence to a Set.
unionProduces the set union of two sequences.
whereFilters a sequence of values based on a predicate.
zipApplies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

License

MIT © Martin Volek

1.7.2-next.0

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.7.2-preview.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.3.0-0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

1.1.2

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago