1.0.2 • Published 2 years ago

@geffencode/linqscript v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Project Title

A basic implementation of LINQ methods for Typescript.

Installation

$ npm install @geffencode/linqscript

Usage/Examples

import { AsLinq, LINQArray} from "@geffencode/linqscript";

interface Person {
    id: number;
    name: string;
    age: number;
    city: string;
    hobbies: string[]
}

const persons: Person[] = myService.getPersons();

// By declaring a new LINQArray
const linqArray = new LINQArray<Person>(...persons);
const names = linqArray.Select(p => p.name); // ['John Doe', 'Jane Doe', 'Jane Smith', ...]

// Copying an existing array as LINQArray
const ids = AsLinq(persons).Select(p => p.id); // [1, 2, 3, ...]

// Getting the hobbies of people older than 30 for a city
const hobbies = linqArray.Where(p => p.age > 30 && p.city == 'Mogadishu')
                                  .SelectMany(p => p.hobbies)
                                  .Distinct(); // Remove duplicates

// Id, name and age of all people that play competitive chess boxing in Thimphu
// ordered by age descending, returning a regular array.
const chessboxers = linqArray.Where(p => p.city === 'Thimphu')
                            .Select(({id, name, age}) => ({id, name, age}))
                            .OrderByDescending(p => p.age)
                            .ToArray();

// Get all persons grouped by city
const byCity = linqArray.GroupBy(p => p.city);

API Reference

MethodArgumentReturnDescription
Additem: TNoneAdds an item to the end of the array.
AddRangearr: T[]NoneAdds multiple items to the end of the array.
GroupBypredicate: (item: T) => KRecord<K, T[]>Groups the elements in the array by the specified key selector.
Selectpredicate: (item: T) => KLINQArrayProjects each element of the array to a new form using the specified selector function.
Wherepredicate: (item: T) => booleanLINQArrayFilters the elements of the array based on a predicate function.
Firstpredicate?: (item: T) => booleanT or undefinedReturns the first element of the array that satisfies a specified condition, or undefined if no such element is found.
FirstOrDefaultpredicate?: (item: T) => booleanT or undefinedReturns the first element of the array that satisfies a specified condition, or undefined if no such element is found.
SelectManypredicate: (item: T) => K[]LINQArrayProjects each element of the array to a sequence and flattens the resulting sequences into one LINQArray.
DistinctNoneLINQArrayReturns a new LINQArray containing only the distinct elements of the original array.
DistinctBypredicate: (item: T) => KLINQArrayReturns a new LINQArray containing only the distinct elements of the original array, determined by the specified key selector function.
OrderBypredicate: (item: T) => KLINQArraySorts the elements of the array in ascending order according to the specified key selector function.
OrderByDescendingpredicate: (item: T) => KLINQArraySorts the elements of the array in descending order based on the specified predicate.
Anypredicate?: (item: T) => booleanbooleanDetermines whether any element of the array satisfies the specified predicate.
Takei: numberLINQArrayReturns a new array that contains the specified number of elements from the start of the array.
Skipi: numberLINQArrayBypasses a specified number of elements in the array and returns the remaining elements.
ToArrayNoneT[]Copies the elements of the LINQArray to a new Array.
_logNoneLINQArrayLogs the current array to the console and returns it.
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago