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

linq-methods-js

v1.0.0

Published

This repository provides a collection of LINQ-style extension methods implemented in JavaScript. These methods allow you to perform common LINQ operations on arrays.

Downloads

1

Readme

LINQ JS Extension Methods

This repository provides a collection of LINQ-style extension methods implemented in JavaScript. These methods allow you to perform common LINQ operations on arrays.

Usage

To use the LINQ extension methods, you can include the provided linq.js file in your JavaScript project. The file contains the implementation of the extension methods as arrow functions.

You can import the extension methods as follows:

// Import the LINQ extension methods
import './linqJs.js';

Extension Methods

The following LINQ extension methods are available:

  • where(condition) - Filters elements in an array based on a condition.
  • select(projection) - Transforms elements in an array using a projection function.
  • orderBy(keySelector) - Sorts elements in an array in ascending order based on a key.
  • orderByDescending(keySelector) - Sorts elements in an array in descending order based on a key.
  • first(condition) - Returns the first element in an array that satisfies a condition.
  • firstOrDefault(condition, defaultValue) - Returns the first element in an array that satisfies a condition, or a default value if no element is found.
  • last(condition) - Returns the last element in an array that satisfies a condition.
  • lastOrDefault(condition, defaultValue) - Returns the last element in an array that satisfies a condition, or a default value if no element is found.
  • single(condition) - Returns the single element in an array that satisfies a condition.
  • singleOrDefault(condition, defaultValue) - Returns the single element in an array that satisfies a condition, or a default value if no element is found or multiple elements are found.
  • count(condition) - Counts the number of elements in an array that satisfy a condition.
  • any(condition) - Checks if any element in an array satisfies a condition.
  • skip(count) - Skips a specified number of elements in an array and returns the remaining elements.
  • take(count) - Returns a specified number of contiguous elements from the start of an array.
  • distinct() - Returns a new array with distinct elements from the original array.
  • aggregate(accumulator, initialValue) - Applies an accumulator function over an array and returns the accumulated result.
  • all(condition) - Checks if all elements in an array satisfy a condition.
  • concat(collection) - Concatenates two arrays.
  • defaultIfEmpty(defaultValue) - Returns an array with a default value if the original array is empty.
  • groupBy(keySelector) - Groups elements in an array based on a key.
  • join(inner, outerKeySelector, innerKeySelector, resultSelector) - Joins two arrays based on a common key and projects the result.
  • max(keySelector) - Returns the maximum value in an array based on a key.
  • min(keySelector) - Returns the minimum value in an array based on a key.
  • ofType(type) - Filters elements in an array based on their type.
  • reverse() - Reverses the order of elements in an array.

Demo

// Original array
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

Example usage of each LINQ extension method

where

const filteredNumbers = numbers.where(num => num % 2 === 0);
console.log(filteredNumbers); // Output: [2, 4, 6, 8, 10]

select

const squaredNumbers = numbers.select(num => num * num);
console.log(squaredNumbers); // Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

orderBy

const sortedNumbers = numbers.orderBy(num => num);
console.log(sortedNumbers); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

orderByDescending

const descendingNumbers = numbers.orderByDescending(num => num);
console.log(descendingNumbers); // Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

first

const firstNumber = numbers.first();
console.log(firstNumber); // Output: 1

last

const lastNumber = numbers.last();
console.log(lastNumber); // Output: 10

count

const count = numbers.count();
console.log(count); // Output: 10

any

const hasEvenNumbers = numbers.any(num => num % 2 === 0);
console.log(hasEvenNumbers); // Output: true

skip

const skippedNumbers = numbers.skip(5);
console.log(skippedNumbers); // Output: [6, 7, 8, 9, 10]

take

const takenNumbers = numbers.take(3);
console.log(takenNumbers); // Output: [1, 2, 3]

distinct

const distinctNumbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4].distinct();
console.log(distinctNumbers); // Output: [1, 2, 3, 4]

aggregate

const sum = numbers.aggregate((accumulator, num) => accumulator + num, 0);
console.log(sum); // Output: 55

all

const allEven = numbers.all(num => num % 2 === 0);
console.log(allEven); // Output: false

concat

const concatenatedArray = numbers.concat([11, 12, 13]);
console.log(concatenatedArray); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

defaultIfEmpty

const emptyArray = [].defaultIfEmpty('No elements found');
console.log(emptyArray); // Output: ['No elements found']

groupBy

const groupedNumbers = numbers.groupBy(num => num % 2 === 0 ? 'Even' : 'Odd');
console.log(groupedNumbers);
// Output: Map {
//   'Even' => [2, 4, 6, 8, 10],
//   'Odd' => [1, 3, 5, 7, 9]
// }

select

const users = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Jane' },
  { id: 3, name: 'Alice' }
];

const userNames = users.select(user => user.name);
console.log(userNames); // Output: ['John', 'Jane', 'Alice']

max

const maxNumber = numbers.max(num => num);
console.log(maxNumber); // Output: 10

min

const minNumber = numbers.min(num => num);
console.log(minNumber); // Output: 1

ofType

const filteredUsers = users.ofType(Object);
console.log(filteredUsers); // Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Alice' }]

reverse

const reversedArray = numbers.reverse();
console.log(reversedArray); // Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Contributing

Contributions are always welcome!

See contributing.md for ways to get started.

Please adhere to this project's code of conduct.

Authors

License

MIT