@kingjs/linq
v1.0.30
Published
Exposes @kingjs/linq.*
Downloads
3
Readme
@kingjs/linq
Exports all @kingjs/linq.*
functionality in one package.
Usage
Join pets to their owners, sorted by last name, then first name, excluding fishes, like this:
var linq = require('@kingjs/linq');
var sequence = require('@kingjs/enumerable.create');
var apply = require('@kingjs/apply');
var people = sequence(
{ firstName: 'Bob', lastName: 'Smith', id: 0 },
{ firstName: 'Alice', lastName: 'Smith', id: 1 },
{ firstName: 'Chris', lastName: 'King', id: 2 },
);
var pets = sequence(
{ name: 'Tiger', type: 'dog', ownerId: 0 },
{ name: 'Spike', type: 'dog', ownerId: 0 },
{ name: 'Fluffy', type: 'cat', ownerId: 1 },
{ name: 'Bubbles', type: 'fish', ownerId: 2 },
);
apply.call(people,
linq.orderBy, [ function(x) { return x.lastName; } ],
linq.thenBy, [ function(x) { return x.firstName; } ],
linq.join, [
pets,
function(x) { return x.id; },
function(x) { return x.ownerId; },
function(owner, pet) { return { owner: owner, pet: pet } }
],
linq.where, [ function(x) { return x.pet.type != 'fish'; } ],
linq.select, [
function(x) {
var owner = x.owner;
var pet = x.pet;
return owner.firstName + " " + owner.lastName +
' owns a ' + pet.type + ' named ' + pet.name + '.';
},
],
linq.toArray, [],
Array.prototype.join, ['\n']
);
result:
Alice Smith owns a cat named Fluffy.
Bob Smith owns a dog named Tiger.
Bob Smith owns a dog named Spike.
API
| Search | Aggregate | Set | Generator | Accrete |
|---|---|---|---|---|
|all
|aggregate
|distinct
|range
|append
|
|any
|average
|except
|repeat
|concat
|
|contains
|count
|intersect
|empty
|prepend
|
|elementAt
OrUndef
|max
|union
|
|first
OrUndef
|min
||
|last
OrUndef
|sum
|
|single
OrUndef
|equal
| Sort | Filter | Join | Transform | Container |
|---|---|---|---|---|
|orderBy
|skip
|groupBy
|select
|toArray
|
|orderByDesc
|skipWhile
|groupJoin
|selectMany
|toDictionary
|
|thenBy
|take
|join
||toLookup
|
|thenByDesc
|takeWhile
|zip
|
||where
|
Install
With npm installed, run
$ npm install @kingjs/link
Remarks
See @kingjs/apply to understand the benefit of the "odd" apply
convention. Basically, it allows for the separation of data from algorithm; Data and algorithm separation cannot be achieved in Javascript using the dot syntax without polluting the data object or prototype with a function.
See Also
Acknowledgments
Like LINQ.
License
MIT