linqify
Version:
Javascript LINQ library based on C# LINQ
334 lines (292 loc) • 13.3 kB
Markdown
//travis-ci.org/goranhrovat/linqify.svg?branch=master)](https://travis-ci.org/goranhrovat/linqify)
[](https://coveralls.io/github/goranhrovat/linqify)
[](https://snyk.io/test/github/goranhrovat/linqify?targetFile=package.json)
[](https://david-dm.org/goranhrovat/linqify)
[](https://david-dm.org/goranhrovat/linqify?type=dev)



[](https://www.npmjs.com/package/linqify)
[](https://gitter.im/linqify/community?utm_source=share-link&utm_medium=link&utm_campaign=share-link)




JavaScript library for lazy querying Arrays, Maps, Sets, and Strings based on [LINQ (C
Provides List, Dictionary, HashSet, and Lookup with comparer for complex types.
Features:
- lazy evaluation with generator functions,
- extends native javascript Array, Set, Map, and String,
- C
- includes typescript support in code editors.
````shell
npm install linqify
````
```typescript
var {Enumerable, List, Dictionary, HashSet, EqualityComparers,
SortComparers, linqify} = require('linqify');
```
```typescript
import {Enumerable, List, Dictionary, HashSet, EqualityComparers,
SortComparers, linqify} from 'linqify';
```
```html
<script src="https://unpkg.com/linqify"></script>
```
```html
<script src="https://unpkg.com/linqify@1.2.1"></script>
```
```html
<script src="https://unpkg.com/linqify@1"></script>
```
[ ](https://linqify.github.io "https://linqify.github.io") is located on GitHub Pages.
<br>
[ ](https://goranhrovat.blogspot.com/search/label/linqify "https://goranhrovat.blogspot.com/search/label/linqifyy") is located on Blogger.
<br>
[ ](https://github.com/goranhrovat/linqify "https://github.com/goranhrovat/linqify") is available on GitHub.
```javascript
[ ].Select(t => t*2).Where(t => t>5).Skip(1).Take(3).ToArray();
// [8,10,8]
[ ].Select(t => t*2).TakeWhile(t => t<5).ToArray();
// [2,4]
[ ].GroupBy(t => t%2).Select(t => ({Key:t.Key, Vals:[...t]})).ToArray();
// [{"Key":1,"Vals":[1,3,5,3]},{"Key":0,"Vals":[2,4,4,2]}]
[ ].GroupBy(t => t%2).Select(t => ({Key:t.Key, Sum:t.Sum()})).ToArray();
// [{"Key":1,"Sum":12},{"Key":0,"Sum":12}]
[ ].Distinct().OrderBy(t=>t).ToArray();
// [1,3,4,5,6,8,9]
[{Name:"Jack", Age:18},
{Name:"Joe", Age:22},
{Name:"Jack", Age:20}].OrderBy(t=>t.Name).ThenByDescending(t=>t.Age).ToArray();
// [{"Name":"Jack","Age":20},{"Name":"Jack","Age":18},{"Name":"Joe","Age":22}]
```
```javascript
function ageComparer (a, b) {
if (a.Age > b.Age) return 1;
if (a.Age < b.Age) return -1;
return 0;
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}];
people.OrderBy(t=>t, ageComparer).ToArray();
// [{"Name":"Jack","Age":18},{"Name":"Jack","Age":20},{"Name":"Joe","Age":22}]
people.OrderByDescending(t=>t, ageComparer).ToArray();
// [{"Name":"Joe","Age":22},{"Name":"Jack","Age":20},{"Name":"Jack","Age":18}]
```
```javascript
let nameEqualityComparer = {
Equals: (x, y) => x.Name===y.Name,
GetHashCode: obj => obj.Name
}
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22},{Name:"Jack", Age:20}];
people.Distinct(nameEqualityComparer).ToArray();
// [{"Name":"Jack","Age":18},{"Name":"Joe","Age":22}]
// or use DeepComparer
let nameEqComparer = EqualityComparers.DeepComparer(t=>({Name:t.Name}));
people.Distinct(nameEqComparer).ToArray();
// [{"Name":"Jack","Age":18},{"Name":"Joe","Age":22}]
let myhashset = people.ToHashSet(nameEqualityComparer);
[...myhashset]
// [{"Name":"Jack","Age":18},{"Name":"Joe","Age":22}]
myhashset.Add({Name:"Jack", Age:25});
// false
myhashset.Add({Name:"Jane", Age:25});
// true
```
```javascript
Enumerable.setMethod("EvenElements", function*() {
let ind = 0;
for (let t of this)
if (ind++%2 === 0) yield t;
});
["a","b","c","d"].EvenElements().ToArray();
// ["a", "c"]
```
```javascript
Enumerable.setMethod("Variance", function() {
let avg = this.Average();
return this.Select(t => Math.pow(t - avg, 2)).Average();
});
[ ].Variance();
// 15.440000000000001
```
```javascript
let people = [{Name:"Jack", Age:3}, {Name:"Joe", Age:2}, {Name:"Jane", Age:1}]
people.Custom(function* () {
for (let t of this)
yield* Enumerable.Repeat(t.Name, t.Age)
}).Select(t=>t.toUpperCase()).ToArray()
// ["JACK", "JACK", "JACK", "JOE", "JOE", "JANE"]
```
```javascript
let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
let oldest = people.Custom((source)=>{
let currOldest = source.FirstOrDefault();
for (let t of source) if (t.Age > currOldest.Age) currOldest = t;
return currOldest;
})
oldest
// {Name:"Joe", Age:22}
```
```javascript
let mylist = [1,3,4,8,12].ToList();
mylist.Add(15);
[...mylist]
// [1, 3, 4, 8, 12, 15]
let mydict = [1,3,4,8,12].ToDictionary(t=>t, t=>t*2);
[...mydict]
// [{"Key":1,"Value":2},{"Key":3,"Value":6},
// {"Key":4,"Value":8},{"Key":8,"Value":16},
// {"Key":12,"Value":24}]
let myhashset = [1,3,4,8,12].ToHashSet();
myhashset.IsSupersetOf([1,4]);
// true
let groups = [1,3,4,8,12].ToLookup(t=>t>3 ? "big" : "small", t=>t);
for (let group of groups)
console.log(group.Key, [...group]);
// small [1, 3]
// big [4, 8, 12]
```
```javascript
[ ].ToSet();
// Set {1, 3, 4, 8, 12}
[ ].ToMap(t=>t, t=>t*2);
// Map {1 => 2, 3 => 6, 4 => 8, 8 => 16, 12 => 24}
[ ].ToMap(t=>t, t=>t*2).ToArray();
// [[1,2],[3,6],[4,8],[8,16],[12,24]]
let upperNames = ["Jane", "Joe", "Jack"].Select(t=>t.toUpperCase());
upperNames.ToArray();
// or
[...upperNames];
// ["JANE", "JOE", "JACK"]
```
```javascript
"Jane Doe".Distinct().Reverse().ToArray();
// ["o", "D", " ", "e", "n", "a", "J"]
new Map([[1,"Jane"],[2,"Joe"],[3,"Jack"]]).Select(t=>`${t[0]}: ${t[1]}`).ToArray();
// ["1: Jane", "2: Joe", "3: Jack"]
new Set([1,4,6,4,7]).Average();
// 4.5
```
```javascript
function isPrime(num) {
for(let i = 2, s = Math.sqrt(num); i <= s; i++)
if(num % i === 0) return false;
return true;
}
let primeNumbers = Enumerable.From(function* () {
let i = 2;
while (true) {
if (isPrime(i)) yield i;
i++;
}
});
primeNumbers.Take(10).ToArray();
// [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
```
```javascript
let fibonacciNumbers = Enumerable.From(function* () {
let current = 0, next = 1;
while (true) {
yield current;
[ ] = [next, current + next];
}
});
fibonacciNumbers.Take(10).ToArray();
// [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
```

```javascript
import {linqify} from 'linqify';
var lq = linqify.noConflict();
[ ].Select(t=>t*t).ToArray();
// Uncaught TypeError: [1,2,3].Select is not a function
lq.Enumerable.From([1,2,3]).Select(t=>t*t).ToArray();
// or
lq([1,2,3]).Select(t=>t*t).ToArray();
// [1, 4, 9]
```
[ ](https://linqify.github.io/classes/ienumerable.html#aggregate),
[ ](https://linqify.github.io/classes/ienumerable.html#all),
[ ](https://linqify.github.io/classes/ienumerable.html#any),
[ ](https://linqify.github.io/classes/ienumerable.html#append),
[ ](https://linqify.github.io/classes/ienumerable.html#asenumerable),
[ ](https://linqify.github.io/classes/ienumerable.html#average),
[ ](https://linqify.github.io/classes/ienumerable.html#cast),
[ ](https://linqify.github.io/classes/ienumerable.html#concat),
[ ](https://linqify.github.io/classes/ienumerable.html#contains),
[ ](https://linqify.github.io/classes/ienumerable.html#count),
[ ](https://linqify.github.io/classes/ienumerable.html#custom),
[ ](https://linqify.github.io/classes/ienumerable.html#defaultifempty),
[ ](https://linqify.github.io/classes/ienumerable.html#distinct),
[ ](https://linqify.github.io/classes/ienumerable.html#elementat),
[ ](https://linqify.github.io/classes/ienumerable.html#elementatordefault),
[ ](https://linqify.github.io/classes/ienumerable.html#except),
[ ](https://linqify.github.io/classes/ienumerable.html#first),
[ ](https://linqify.github.io/classes/ienumerable.html#firstordefault),
[ ](https://linqify.github.io/classes/ienumerable.html#foreach),
[ ](https://linqify.github.io/classes/ienumerable.html#groupby),
[ ](https://linqify.github.io/classes/ienumerable.html#groupjoin),
[ ](https://linqify.github.io/classes/ienumerable.html#intersect),
[ ](https://linqify.github.io/classes/ienumerable.html#join),
[ ](https://linqify.github.io/classes/ienumerable.html#last),
[ ](https://linqify.github.io/classes/ienumerable.html#lastordefault),
[ ](https://linqify.github.io/classes/ienumerable.html#max),
[ ](https://linqify.github.io/classes/ienumerable.html#min),
[ ](https://linqify.github.io/classes/ienumerable.html#oftype),
[ ](https://linqify.github.io/classes/ienumerable.html#orderby),
[ ](https://linqify.github.io/classes/ienumerable.html#orderbydescending),
[ ](https://linqify.github.io/classes/ienumerable.html#prepend),
[ ](https://linqify.github.io/classes/ienumerable.html#reverse),
[ ](https://linqify.github.io/classes/ienumerable.html#select),
[ ](https://linqify.github.io/classes/ienumerable.html#selectmany),
[ ](https://linqify.github.io/classes/ienumerable.html#sequenceequal),
[ ](https://linqify.github.io/classes/ienumerable.html#single),
[ ](https://linqify.github.io/classes/ienumerable.html#singleordefault),
[ ](https://linqify.github.io/classes/ienumerable.html#skip),
[ ](https://linqify.github.io/classes/ienumerable.html#skiplast),
[ ](https://linqify.github.io/classes/ienumerable.html#skipwhile),
[ ](https://linqify.github.io/classes/ienumerable.html#sum),
[ ](https://linqify.github.io/classes/ienumerable.html#take),
[ ](https://linqify.github.io/classes/ienumerable.html#takelast),
[ ](https://linqify.github.io/classes/ienumerable.html#takewhile),
[ ](https://linqify.github.io/classes/ienumerable.html#toarray),
[ ](https://linqify.github.io/classes/ienumerable.html#todictionary),
[ ](https://linqify.github.io/classes/ienumerable.html#tohashset),
[ ](https://linqify.github.io/classes/ienumerable.html#tolist),
[ ](https://linqify.github.io/classes/ienumerable.html#tolookup),
[ ](https://linqify.github.io/classes/ienumerable.html#tomap),
[ ](https://linqify.github.io/classes/ienumerable.html#toset),
[ ](https://linqify.github.io/classes/ienumerable.html#union),
[ ](https://linqify.github.io/classes/ienumerable.html#where),
[ ](https://linqify.github.io/classes/ienumerable.html#zip).
[ ](https://linqify.github.io/classes/iorderedenumerable.html#thenby),
[ ](https://linqify.github.io/classes/iorderedenumerable.html#thenbydescending).
[![Build Status](https: