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

pipeline-linq

v0.1.1

Published

sync/async generators and helpers to form typed linq-like iterable queries

Downloads

2

Readme

pipeline-linq

sync/async generators and helpers to form typed linq-like iterable queries

this is an experimental package and subject to change on minors versions

Install

install with either npm or yarn:

  • npm install pipeline-linq
  • yarn add pipeline-linq

Dependencies

For versions pipeline-linq @ < 1.0, this package, when imported, will polyfill the Symbol.asyncIterator using core-js.

Basic Usage

linq is a helper function used to wrap and chain pipeline methods together. Simply pass through an object that implements [Symbol.iterator], this includes [] or any typed-array.

import {linq} from 'pipeline-linq'

let data = {
  *[Symbol.iterator](){
    yield 1; 
    yield 2; 
    yield 3; 
    yield 4;
  }
}

let result = 
  linq(data)
    .where(x=>x > 2)
    .select(x=>x * 2)
    .first();

console.log(result);

By default the call to linq returns the synchronous query, you can switch to the async equivalents by passing through true at the end of most supporting methods.

Converting to an async query will cause all subsequent method calls to be evaluated using the Symbol.asyncIterator and you must await the resulting promise or use for-await to iterate the query.

import {linq} from 'pipeline-linq'

let data = [1, 2, 3, 4];

async function main() {
  let query = linq(data)
    .where(x => x > 2)
    .select(async x => x * 2, true);

  for await (let value of query) {
    console.log(value);
  }
}
main();

note: when using linq the evaluation of the iterators is lazy, thus you can iterate over query multiple times.

Generators

You can use the sync generators directly by importing them:

import { where, select, first } from 'pipeline-linq';

let data = [1, 2, 3, 4];

let where_ = where(data, x => x > 2);
let select_ = select(where_, x => x * 2);
let result = first(select_);

Advanced Usage

The Linq and LinqAsync interfaces have a method chain that can be used to insert custom iterators into the iterator sequence.

A custom filter method:

let isNumber = function* (source) {
  for (let item of source) {
    if (typeof item === "number") {
      yield item as number;
    }
  }
};

let results = linq([10, "hello", true])
  .chain(isNumber)
  .toArray();

It is also possible to insert complex queries:

let query = function (source: Iterable<number>) {
  return linq(source)
    .where(x => x >= 2)
    .select(x => x * 2)
};

let results = linq([1, 2, 5, "hello", true])
  .chain(isNumber)
  .chain(query)
  .skip(1)
  .toArray();

Pipeline Operator

see proposal pipeline-operator

Subject to change. All generators and helper methods take the source as the first parameter and so will be readily available to use with the pipeline operator. For now, you could use the babel transformer and use:

import { where, select, first } from 'pipeline-linq';

let result = [1, 2, 3, 4] 
  |> _ => where(_, x => x > 2)
  |> _ => select(_, x => x * 2)
  |> first();

Acknowledgements