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

x-queryjs

v0.1.1

Published

`X-Queryjs` is small utility `JavaScript` library that is fully type safety, zero-dependencies and mimics the functionality and chaining capabilities of C# LINQ, allowing you to work with collections in a similar and more effective way.

Downloads

3

Readme

X-Queryjs

X-Queryjs is small utility JavaScript library that is fully type safety, zero-dependencies and mimics the functionality and chaining capabilities of C# LINQ, allowing you to work with collections in a similar and more effective way.

Installation

npm

npm install x-queryjs

pnpm

pnpm add x-queryjs

Usage

Here is a step-by-step guide on how to use the Queryable class.

Creating a Queryable Collection

You can create a new Queryable collection by passing an array of items to the constructor.

import { Queryable } from 'x-queryjs';

const numbers = new Queryable([1, 2, 3, 4, 5]);
const people = new Queryable([
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 25 },
  { name: 'Charlie', age: 35 },
]);

Methods

select

Projects each element of a collection into a new form.

const names = people.select((person) => person.name);
console.log(names.toArray()); // Output: ['Alice', 'Bob', 'Charlie']

where

Filters a collection based on a predicate.

const adults = people.where((person) => person.age >= 30);
console.log(adults.toArray()); // Output: [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 35 }]

orderBy

Sorts the elements of a collection in ascending order according to a key.

const sortedByName = people.orderBy((person) => person.name);
console.log(sortedByName.toArray()); // Output: [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 35 }]

orderByDescending

Sorts the elements of a collection in descending order according to a key.

const sortedByAgeDesc = people.orderByDescending((person) => person.age);
console.log(sortedByAgeDesc.toArray()); // Output: [{ name: 'Charlie', age: 35 }, { name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]

groupBy

Groups the elements of a collection according to a specified key selector function.

const groupedByAge = people.groupBy((person) => person.age);
console.log(groupedByAge);
// Output:
// {
// 25: [{ name: 'Bob', age: 25 }],
// 30: [{ name: 'Alice', age: 30 }],
// 35: [{ name: 'Charlie', age: 35 }]
// }

distinct

Returns distinct elements from a collection according to a specified key selector function.

const distinctAges = people.distinct((person) => person.age);
console.log(distinctAges.toArray()); // Output: [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 35 }]

take

Returns a specified number of contiguous elements from the start of a collection.

const firstTwo = people.take(2);
console.log(firstTwo.toArray()); // Output: [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]

skip

Bypasses a specified number of elements in a collection and then returns the remaining elements.

const skippedTwo = people.skip(2);
console.log(skippedTwo.toArray()); // Output: [{ name: 'Charlie', age: 35 }]

toArray

Converts the collection to an array.

const array = people.toArray();
console.log(array); // Output: [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 35 }]

sum

Computes the sum of the sequence of numeric values that are obtained by invoking a transform function on each element of the input collection.

const totalAge = people.sum((person) => person.age);
console.log(totalAge); // Output: 90

average

Computes the average of the sequence of numeric values that are obtained by invoking a transform function on each element of the input collection.

const averageAge = people.average((person) => person.age);
console.log(averageAge); // Output: 30

any

Determines whether any element of a collection satisfies a condition.

const hasAdults = people.any((person) => person.age >= 18);
console.log(hasAdults); // Output: true

all

Determines whether all elements of a collection satisfy a condition.

const allAdults = people.all((person) => person.age >= 18);
console.log(allAdults); // Output: true

first

Returns the first element of a sequence that satisfies a specified condition or the first element if no condition is specified.

const firstAdult = people.first((person) => person.age >= 30);
console.log(firstAdult); // Output: { name: 'Alice', age: 30 }

firstOrDefault

Returns the first element of a sequence that satisfies a specified condition or the first element if no condition is specified, or a default value if no such element is found.

const firstYoungAdult = people.firstOrDefault((person) => person.age >= 20, {
  name: 'Default',
  age: 0,
});
console.log(firstYoungAdult); // Output: { name: 'Alice', age: 30 }

last

Returns the last element of a sequence that satisfies a specified condition or the last element if no condition is specified.

const lastAdult = people.last((person) => person.age >= 30);
console.log(lastAdult); // Output: { name: 'Charlie', age: 35 }

lastOrDefault

Returns the last element of a sequence that satisfies a specified condition or the last element if no condition is specified, or a default value if no such element is found.

const lastYoungAdult = people.lastOrDefault((person) => person.age >= 20, {
  name: 'Default',
  age: 0,
});
console.log(lastYoungAdult); // Output: { name: 'Charlie', age: 35 }

Contributors

All contributors are welcome. Please create a pull request or issue.

TODO

  • [ ] Implement other queryable class for other data type like JSON and so on.
  • [ ] Write thorough unit tests for every methods.
  • [ ] Improve performance without using any other dependencies
  • [ ] Write clear and self explanatory documentation

License

This project is licensed under the MIT License.