Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ESLinq

An elegant way of working with iterables

WARNING: this is a beta version. Feel free to play around with it but don't use it in production.

ESLinq aims to be a complete, robust, thoroughly tested port of Linq to Objects to ECMAScript 2015 (ES6).

API Documentation

Build Status Code Climate API Documentation Coverage

A first example

Problem: We have an array of users. A user can have multiple email addresses, and not all users are verified. We need a list of the email addresses of the verified users, and check if all of them end in ".com".

const users = [
	{
		"id": 12,
		"name": "John Smith",
		"emails": ["john.smith@example.com", "jsmith@live.com", "j.s967@gmail.com"],
		"verified": true
	},
	{
		"id": 56,
		"name": "Jennifer Brown",
		"emails": ["jbrown@gmail.com", "jennifer.brown@live.com"],
		"verified": false
	},
	{
		"id": 98,
		"name": "Kate Newton",
		"emails": ["katie6543@yahoo.com", "kate.newton.328@gmail.com"],
		"verified": true
	}
];

First, we need to restrict the list to only contain verified users:

const verifiedUsers = from(users).where(user => user.verified);

Then, we need to get a concatenated list of the email addresses of verified users:

const verifiedUserEmails =
    from(users)
	    .where(user => user.verified)
		.selectMany(user => user.emails);

It is important to note that verifiedUserEmails is a lazily evaluated iterable: filtering and transformation is only done once we start iterating it, e. g. with a for..of loop:

for (let email of verifiedUserEmails) {
	// The first time this loop executes is the first time the original iterable is read from.
	// Filtering (`where`) and transformation (`selectMany`) run element-by-element during iteration.
	console.log(email);
}

Now let's check if all emails end with ".com":

const allEmailsEndWithCom =
    from(users)
	    .where(user => user.verified)
		.selectMany(user => user.emails)
		.all(email => email.endsWith(".com"));

The result is a bool value indicating whether all emails end with the substring ".com". The all operator is an eager one: it iterates through all elements to compute its value.

About

An experiment to port Linq to Objects to ES2015

Resources

License

Packages

No packages published