@daniel.pickett/linq-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

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>

Package Sidebar

Install

npm i @daniel.pickett/linq-js

Weekly Downloads

1

Version

1.0.9

License

ISC

Unpacked Size

347 kB

Total Files

57

Last publish

Collaborators

  • dan3988