An elegant way of working with ES6 iterables
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


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": ["", "", ""],
		"verified": true
		"id": 56,
		"name": "Jennifer Brown",
		"emails": ["", ""],
		"verified": false
		"id": 98,
		"name": "Kate Newton",
		"emails": ["", ""],
		"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 =
	    .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.

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

const allEmailsEndWithCom =
	    .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.