JSPM

@daniel.pickett/linq-js

1.0.9
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q35091F
  • License ISC

Package Exports

  • @daniel.pickett/linq-js
  • @daniel.pickett/linq-js/lib/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@daniel.pickett/linq-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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>