ect-ng-linq

ECT NgLinq draws inspiration from .NET LINQ and brings this inspiration to TypeScript.

$0

11 downloads
exoduscloudtech

ECT NgLinq

This library was generated with Angular CLI version 18.0.0.

Description

Language-Integrated Query (LINQ) was created for C# by Microsoft. It's a way of retrieving and sorting data in arrays, using strongly typed collections of objects, and using familiar operators and functions used in languages like SQL. More details can be found here: (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/)

ECT NgLinq draws inspiration from LINQ and brings this inspiration to TypeScript. One of the great ideas in LINQ was the ability to chain methods, and this is also built as standard in NgLinq too.

Usage

First add the EctNgLinqService service to your Modules' or AppComponent's Providers list.

Then, if an IUser interface is defined as this:

export interface IUser {
  id: number;
  firstName: string;
  lastName: string;
  email: string;
}

An array can be created like this:

const usersArray: IUser[] = [];

or

const usersArray: Array<IUser> = new Array<IUser>();

With NqLinq, first we import the EctNgLinqService service like this:

import { EctNgLinqService } from 'ect-ng-linq';

Secondly we inject into a component's constructor the EctNgLinqService service e.g.:

constructor(public ngLinqService: EctNgLinqService)

Lastly, we create an NqLinq array like this:

const usersArray = this.ngLinqService.createNgLinq<IUser>();

To search for a users with a lastName value of Jones, then sort by firstName, then take the first, just like in LINQ, using NgLinq we can chain a query up like this:

const firstUserWithLastNameOfJones = usersList.where((rec: IUser) => rec.lastName.endsWith('Jones'))
          .orderBy((rec: IUser) => rec.firstName)
          .takeFirst();

Functions

INgLinqArray exposes the following functions:

  • add: Add a single item to the current list
  • addRange: Add an array of items to the current list
  • all: Do ALL elements in the current list match the search condition
  • any: Do ANY elements in the current list match the search condition
  • average: Get the average of all elements by a field condition
  • clear: Remove all elements from the array
  • count: Count the number of elements in the current list
  • distinct: Distinct matching at record or field level
  • empty: Is the current list null or contain no items
  • first: Fetch the FIRST element in the list that matches with the search condition
  • groupBy: Group all elements by a key field
  • last: Fetch the LAST element in the list that matches with the search condition
  • min: Get the minimum of all elements by a field condition
  • max: Get the maximum of all elements by a field condition
  • orderBy: Order the list in ascending order
  • orderByDescending: Order the list in descending order
  • remove: Remove a single item from the current list
  • removeRange: Remove an array of items from the current list
  • skipAndTake: Skip a number of elements in the list, then take a specified number of elements from then on
  • sum: Total all elements by a field condition
  • takeFirst: Fetch first element in the current list
  • takeLast: Fetch last element in the current list
  • takeTop: Fetch the first X number of elements in the current list
  • where: Same as a SQL where clause to filter the list

Dependencies

None

Cost

If you find some benefit from using this package, please consider the time it took to put this together, and why not buy me a coffee? Goodness only knows that most of us would not function without coffee. All donations very much welcomed: (https://www.buymeacoffee.com/exoduscloudtech)

Further help

To get more help on the this ECT NgLinq, please visit (https://angular-grid.net/free/ect-ng-linq)

Monetize your
open-source work

Supercharge your OSS projects by selling npm packages. Get started in just 5 minutes.