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

@adamburgess/linq

v3.0.0

Published

A linq library.

Downloads

223

Readme

@adamburgess/linq

A decent linq. With decent types. Less than 2kb gzipped.

npm version gzipped size brotlied size npm type definitions codecov

Docs/Usage

Generated documentation: https://linq.adam.id.au/

import from from '@adamburgess/linq'
const sequence = from(['an', 'iterable', 'here']);
// now use any methods on sequence!
// e.g. mapping:
const uppercases = sequence.map(x => x.toUpperCase());
// note: the sequence hasn't been mapped yet! toUpperCase hasn't been called!
// you must _run_ the sequence (see Outputs below)
Array.from(uppercases); // or uppercases.toArray()
// => ['AN', 'ITERABLE', 'HERE']

// You can extend already existing transforms:
const reversed = uppercases.reverse();
// still! The sequence hasn't been reversed!
// Again you must run it:
Array.from(reversed);
// => ['HERE', 'ITERABLE', 'AN']
// note! When this reversed array was created, it ran:
// 1. the uppercase sequence (yes, again!)
// 2. the reverse method
// _ALL_ operations are deferred until outputting the sequence!

Features

Completely lazy evaluation.

Inputs

  1. Arrays
  2. Iterables
  3. Generators
  4. Infinite Generators*

Transformations

  1. Map
  2. Where (with narrowing!)
  3. Reversing
  4. Group By
  5. Order By
  6. Order By Descending
  7. Order By ..., Then By
  8. Order By ..., Then By Descending
  9. Take
  10. Skip
  11. Take While
  12. Skip While
  13. Append
  14. Prepend
  15. Distinct
  16. Flat (with projection to sequence)
  17. Join (an inner join)
  18. GroupJoin

Outputs

  1. Count
  2. toArray
  3. toMap
  4. toObject
  5. First (+ or Default)
  6. Single (+ or Default)
  7. Last (+ or Default)
  8. All
  9. Any
  10. None
  11. Contains
  12. Sum (with projection to number)
  13. Average (with projection to number)
  14. Max (with projection to number)
  15. Min (with projection to number)
  16. Min By
  17. Max By

Special additions for number sequences:

  1. Sum
  2. Average
  3. Max
  4. Min

Special additions for iterable/array sequences:

  1. Flat

Special additions for string sequences:

  1. JoinString

* Note: Some transformations/most outputs do not work with infinite sequences, such as Group By and Order By.

Other libraries

or: why use this one?

iterare

⚠️ Really doesn't have enough methods to be general purpose. It is missing: Group, Order, Count, First, Last, Distinct. Subjectively, I use all of these.
⚠️ Supports ES iterators, but doesn't support repeatable/lazy ES iterators
✔️ Extremely popular.
✔️ 3,818 bytes minified/1,065 bytes brotlied

linq.js (on npm: linq)

✔️ Has everything.
❌ Except iterable support. It supports iterators but not iterables. Most of the time, you use iterables. Arrays are iterables. You use the iterable protocol to convert them to iterators. Technically, it doesn't even support iterators. Only objects that are both iterators and iterables.
✔️ Very popular.
⚠️ Types could be improved: toObject ~~is~~ was not typed
❌ 35KB minified/6.6KB brotlied

fromfrom

✔️ Has nearly everything you'd like.
✔️ Supports ES iterators, including lazy/repeatable. (Nice!)
⚠️ Not very popular, but hey, this library is awesome.
✔️ 4,216 bytes minified/1,330 bytes brotlied
✅ Great name. import { from } from 'fromfrom'

@adamburgess/linq

✔️ Supports ES iterators, including lazy/repeatable.
✔️ Has everything in fromfrom, everything in iterare, but not everything in linq.js. Thinking about adding an "extended" version.
✔️ Excellent typing, if I do say so myself. Has a couple features that other libraries don't have.
❌ 1 user. Hah.
✔️ 4,457 bytes minified/1,251 bytes brotlied

Others not considered:

@siderite/linqer: 5kb brotlied, ~~and the typings aren't generic.~~ It now has a typescript version. Yet, they've borked the packaging -- I can't import the module without changing their package.json and importing the direct path. For that reason, useless. Has similar features to fromfrom.

Table comparison to other libraries

✔️ - has it
⚠️ - doesn't have it, but has a one liner work around
❌ - have to reimplement yourself, and reimplementing would be annoying if done multiple times

| | this one | fromfrom | iterare | linq.js | |------------------------------------:|:--------:|:---------------------------------:|:---------------------------------:|:---------------------------------:| | Size in bytes (minified) | 4,457 | 4,216 | 3,818 | 35,451 (+800% ❌) | | Size in bytes (brotlied) | 1,251 | 1,330 | 1,065 | 6,516 (+500% ❌) | | Arrays | ✔️ | ✔️ | ✔️ | ✔️ | | Iterables | ✔️ | ✔️ | ✔️ | ❌ | | Generators | ✔️ | ✔️ | ✔️ | ✔️ | | Infinite Iterables | ✔️ | ✔️ | ✔️ | ❌ | | Lazy Iterables | ✔️ | ✔️ | ❌ | ❌ | | Map | ✔️ | ✔️ | ✔️ | ✔️ | | Where | ✔️ | ✔️ | ✔️ | ✔️ | | Reverse | ✔️ | ✔️ | ❌ | ✔️ | | Group By | ✔️ | ✔️ | ❌ | ✔️ | | Order By | ✔️ | ✔️ | ❌ | ✔️ | | Then By | ✔️ | ✔️ | ❌ | ✔️ | | Take | ✔️ | ✔️ | ✔️ | ✔️ | | Skip | ✔️ | ✔️ | ⚠️[5] | ✔️ | | Take While | ✔️ | ✔️ | ❌ | ✔️ | | Skip While | ✔️ | ✔️ | ❌ | ✔️ | | Append | ✔️ | ✔️ | ✔️ | ❌ | | Prepend | ✔️ | ✔️ | ❌ | ❌ | | Distinct | ✔️ | ✔️ | ❌ | ✔️ | | Flat | ✔️ | ⚠️[1] | ✔️ | ✔️ | | Join | ✔️ | ❌ | ❌ | ✔️ | | Group Join | ✔️ | ❌ | ❌ | ✔️ | | Count | ✔️ | ⚠️[2] | ⚠️[2] | ✔️ | | to Array | ✔️ | ✔️ | ✔️ | ✔️ | | To Map | ✔️ | ✔️ | ✔️ | ❌ | | to Object | ✔️ | ✔️ | ❌ | ✔️ | | to Set | ✔️ | ✔️ | ✔️ | ⚠️[8] | | First | ✔️ | ✔️ | ✔️ | ✔️ | | Single | ✔️ | ❌ | ❌ | ✔️ | | Last | ✔️ | ✔️ | ❌ | ✔️ | | All | ✔️ | ✔️ | ✔️ | ✔️ | | Any | ✔️ | ✔️ | ✔️ | ✔️ | | None | ✔️ | ⚠️[3] | ⚠️[3] | ⚠️[3] | | Contains | ✔️ | ✔️ | ⚠️[6] | ✔️ | | Sum | ✔️ | ✔️ | ❌ | ✔️ | | Average | ✔️ | ❌ | ❌ | ✔️ | | Max | ✔️ | ❌ | ❌ | ✔️ | | Min | ✔️ | ❌ | ❌ | ✔️ | | Min By | ✔️ | ❌ | ❌ | ✔️ | | Max By | ✔️ | ❌ | ❌ | ✔️ | | Sum/Avg/Max/Min fail on non-numbers | ✔️ | ❌[4] | ⁿ/ₐ | ❌ | | Flatten fails on non-iterables | ✔️ | ⁿ/ₐ | ⚠️[7] | ⚠️[7] | | | | | | |

notes:
1. Use flatmap with identity.
2. Use forEach with a count.
3. Use !any
4. There is some typing to prevent Sum on non-numbers, but it actually has no effect.
5. Use slice
6. Use find, check for !== undefined
7. If used on non-iterables, it returns the element unchanged. This follows how JS's .flat() works. My opinion: Why are you flattening an array of things that aren't arrays? Don't.
8. It's untyped!

Performance

It's probably slow.
It uses iterators for everything.
If you want performance, maybe use iterare. Their readme puts performance front and center.