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

@daniel.pickett/linq-js

v1.0.9

Published

A javascript library which adds [C# LINQ](https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable?view=net-6.0) inspired helper methods for iterators. Supported in Node.js and web browsers.

Downloads

87

Readme

linq.js

A javascript library which adds C# LINQ inspired helper methods for iterators. Supported in Node.js and web browsers.

npm version

import Linq from "linq";

interface User {
  id: string;
  firstName: string;
  lastName: string;
  city: string;
  age: number;
}

const users: User[] = loadUsers();
const query = Linq(users)
  .where(v => v.age >= 20 && v.age <= 50)
  .orderBy('lastName')
  .then('firstName')
  .groupBy('city');

for (let group of query) {
  let i = 0;
  for (let { firstName, lastName } of group) {
    console.log("%s %s lives in %s!", firstName, lastName, group.key)
    i++;
  }

  console.log("%s people between 20 and 50 live in %s", i, group.key);
}

Features

  • Support for async iterables and promises
declare const asyncIterable: AsyncIterable<string>;
var startsWithA = await Linq(asyncIterable).where(v => v.startsWith("A")).toArray();
  • Performance enhanced implementations for Arrays, TypedArrays, Maps and Sets
declare const numbers: number[] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
numbers[Symbol.iterator] = () => throw new Error("This should not be called!");
// gets the length property instead of iterating
var count = Linq(numbers).select(v => -v).count();
// gets the last element in the array without iterating all values
var last = Linq(numbers).last();
  • Simplified syntax using property keys
//select a single property value:
var names = Linq(users).select(v => v.firstName).toArray();
//or
var names = Linq(users).select("firstName").toArray();

//select an object with multiple properties:
var names = Linq(users).select(({ firstName, lastName }) => ({ firstName, lastName })).toArray();
//or
var names = Linq(users).select(["firstName", "lastName"]).toArray();

//filter based on a property
var names = Linq(users).where(v => v.isActive).toArray();
//or
var names = Linq(users).where("isActive").toArray();
  • Support for JavaScript collections
var array: User[] = Linq(users).toArray();
var set: Set<User> = Linq(users).toSet();
var map: Map<string, User> = Linq(users).toMap(({ firstName, lastName }) => firstName + " " + lastName);
var obj: Record<string, User> = Linq(users).toObject("id");
  • Static functions for creating sequences
Linq.range(1, 10).toArray();
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
Linq.range(0, 4, 5).toArray();
// [ 0, 5, 10, 15 ]
Linq.repeat("test", 3).toArray();
// [ 'test', 'test', 'test' ]

Importing

LinqJS is built in both ESM and UMD formats.

Node.JS (ESM)

import Linq from "@daniel.pickett/linq-js";

Node.JS (UMD)

const Linq = require("node_modules/@daniel.pickett/linq-js/lib/index.cjs");

Browser (ESM)

<script type="module">
	import Linq from "./node_modules/@daniel.pickett/linq-js/lib/index.mjs";

	let sum = Linq([1,2,3,4,5]).sum();
	console.log({ sum });
</script>

Browser (UMD)

<script type="text/javascript" src="/node_modules/requirejs/require.js"></script>
<script type="text/javascript" src="/node_modules/@daniel.pickett/linq-js/lib/index.cjs"></script>
<script type="text/javascript">
	const Linq = require("linq-js");
	const sum = Linq([1,2,3,4,5]).sum();
	console.log({ sum });
</script>