npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jslinq

v1.0.22

Published

Another LINQ provider for Javascript

Downloads

1,867

Readme

jslinq

Another LINQ provider for Javascript

Install NPM

npm install jslinq --save

Install Bower

bower install zenprogramming.jslinq --save

Usage

Given the following source array:

var data = [
	{ id: 1, name: "one", category: 'fruits', countries: ["Italy", "Austria"] },
	{ id: 2, name: "two", category: 'vegetables', countries: ["Italy", "Germany"] },
	{ id: 3, name: "three", category: 'vegetables', countries: ["Germany"] },
	{ id: 4, name: "four", category: 'fruits', countries: ["Japan"] },
	{ id: 5, name: "five", category: 'fruits', countries: ["Japan", "Italy"] }
];

Get jslinq queryable object

var queryObj = jslinq(data);

Get count of elements

var result = queryObj
	.count();

/*
result => 5
*/

Get all elements with toList

var result = queryObj
	.toList();
	
/*
result => [
	{ id: 1, name: "one", ... },
	{ id: 2, name: "two", ... },
	{ id: 3, name: "three", ... },
	{ id: 4, name: "four", ... },
	{ id: 5, name: "five", ... }
];
*/

Get single element (or null) on list with singleOrDefault

var result = queryObj
	.singleOrDefault(function(el){
		return el.name == "one";
	});

/*
result => { id: 1, name: "one", ... };
*/

Projection on one or more properties with select

var result = queryObj
	.select(function(el){
		return el.id;
	})
	.toList();

/*
result => [1, 2, 3, 4, 5];
*/

Filter elements with where

var result = queryObj
	.where(function(el){
		return el.name == 'two';
	})
	.toList();

/*
result => [{ id: 2, name: "two", ... }];
*/

Make a groupBy and count on each group

var result = queryObj
	.groupBy(function(el){
		return el.category;
	})
	.toList();

/*
result => [
	{ key: 'vegetables', count: 2, elements: [...] }, 
	{ key: 'fruits', count: 3, elements: [...] }, 
];
*/

Merge two arrays with join

var otherData = [
	{ id: 7, name: "seven", category: 'vegetables' }, 
	{ id: 8, name: "eight", category: 'fruit' }
];

var result = queryObj
	.join(otherData)
	.toList();
/*
result => [
	{ id: 1, name: "one", ... },
	{ id: 2, name: "two", ... },
	{ id: 3, name: "three", ... },
	{ id: 4, name: "four", ... },
	{ id: 5, name: "five", ... }, 
	{ id: 7, name: "seven", ... }, 
	{ id: 8, name: "eight", ... }
];
*/

Get distinct elements without repetitions

var extraData = ["A", "B", "C", "B", "A", "D"];

var result = jslinq(extraData)
	.distinct()
	.toList();
/*
result => ["A", "B", "C", "D"];
*/

Sort ascending using orderBy

var result = queryObj
	.orderBy(function(el){
		return el.name;
	})
	.toList();
/*
result => [
	{ id: 5, name: "five", ... },
	{ id: 4, name: "four", ... },
	{ id: 1, name: "one", ... },
	{ id: 3, name: "three", ... },
	{ id: 2, name: "two", ... }
];
*/

Sort descending using orderByDescending

var result = queryObj
	.orderByDescending(function(el){
		return el.name;
	})
	.toList();
/*
result => [
	{ id: 2, name: "two", ... },
	{ id: 3, name: "three", ... },
	{ id: 1, name: "one", ... },
	{ id: 4, name: "four", ... },
	{ id: 5, name: "five", ... }
];
*/

Select multiple elements with selectMany

var result = queryObj
	.selectMany(function(el){
		return el.countries;
	})
	.toList();
/*
result => [
	"Italy", "Austria", "Italy", "Germany", 
	"Germany", "Japan", "Japan", "Italy"] }
];

Get the first matching element with firstOrDefault

var result = queryObj
	.firstOrDefault(function(el){
		return el.category == "vegetables";
	});
/*
result => { id: 2, name: "two", ... };
*/

Get the last matching element with lastOrDefault

var result = queryObj
	.lastOrDefault(function(el){
		return el.category == "vegetables";
	});
/*
result => { id: 3, name: "three", ... };
*/

Check if at least one elements matchs expression with any

var result = queryObj
	.any(function(el){
		return el.name == "two";
	});
/*
result => true;
*/

Check if all elements match expression with all

var result = queryObj
	.all(function(el){
		return el.countries.length > 0;
	});
/*
result => true;
*/

Skip the number of specified elements with skip

var result = queryObj
	.skip(3)
	.toList();
/*
result => [
	{ id: 4, name: "four", ... },
	{ id: 5, name: "five", ... }
];
*/

Take the number of specified elements with take

var result = queryObj
	.take(2)
	.toList();
/*
result => [
	{ id: 1, name: "one", ... },
	{ id: 2, name: "two", ... }
];
*/

Get the maximum element using specific expression with max

var result = queryObj
	.max(function(el){
		return el.id;
	});
/*
result => 5;
*/

Get the minimum element using specific expression with min

var result = queryObj
	.min(function(el){
		return el.id;
	});
/*
result => 1;
*/

Get elements contained on two array with intersect

var otherData = [
	{ id: 2, name: "two", category: 'vegetables' }, 
	{ id: 8, name: "eight", category: 'fruit' }
];

var result = queryObj
	.intersect(otherData, function(el){
		return el.id;
	})
	.toList();
/*
result => [
	{ id: 2, name: "two", ... }
];
*/

Remove one element using remove


var elementToRemove = queryObj
	.singleOrDefault(function(el){
		return el.id == 2;
	});

var result = queryObj
	.remove(elementToRemove)
	.toList();
/*
result => [
	{ id: 1, name: "one", ... },
	{ id: 3, name: "three", ... },
	{ id: 4, name: "four", ... },
	{ id: 5, name: "five", ... }
];
*/

Remove, from source array, specified elements with subtract


var elementsToSubtract = [
	{ id: 2, name: "two", ... },
	{ id: 4, name: "four", ... },
	{ id: 7, name: "seven", ... }
];
			
var result = queryObj
	.subtract(elementsToSubtract, function(el){
		return el.id;
	})
	.toList();
/*
result => [
	{ id: 1, name: "one", ... },
	{ id: 3, name: "three", ... },
	{ id: 5, name: "five", ... }
];
*/

Sum numeric values with sum

			
var result = queryObj
	.sum(function(el){
		return el.id;
	});
/*
result => 15
];
*/

Calculate average on numeric values with average


var sampleData = [
	{ value: 3 },
	{ value: 2 },
	{ value: 5 },
	{ value: 2 },
];

var result = jslinq(sampleData)
	.average(function(x) { 
		return x.value; 
	});
			
/*
result => 3
];
*/

You can also chain multiple methods


var result = queryObj
	.where(function(el) { return el.category == 'fruits' })
	.select(function(el) { return el.id; })
	.toList();
/*
result => [1, 4, 5];
*/

...and use jslinq nested inside functions


var result = queryObj
	.where(function(el) { 
	
		//Check if element has at least one country equals to "Italy"
		var hasItaly = jslinq(el.countries)
			.any(function(c){
				returc c == "Italy";
			});
		return hasItaly; 
	})
	.toList();
/*
result => [
	{ id: 1, name: "one", ... , countries: ["Italy", "Austria"] },
	{ id: 2, name: "two", ... , countries: ["Italy", "Germany"] },
	{ id: 5, name: "five", ... , countries: ["Japan", "Italy"] }
];
*/