linq-arrays
v3.2.5
Published
Emulate LINQ syntax to work with Arrays in JS
Downloads
187
Maintainers
Readme
linq-arrays
The name LINQ is already used ::sad::
Based on System.Linq
in C#, but in TS for browser and Node.js (server side);
Features
- The same methods (not all) of
Array.prototype
- Typescript out of box
- Make the massive and repetitive operations for you,
like
sort by key
,order by key
,sorter for strings/numbers/date
- A set of tools in Fluent Interface to work with arrays
Install
Simple, just choose your package manage and drop the command
yarn add linq-arrays
npm i linq-arrays
pnpm add linq-arrays
Using
const array = new Linq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// you can create a instance with Linq.From(array)
array.Sum() // 55
/*
with .Where, you can use some symbols to make your filter more simple
- != : compare use !=
- !== : compare use !==
- < : compare use <
- <= : compare use <=
- == : compare use ==
- === : compare use ===
- > : compare use >
- >= : compare use >=
eq: compare use Equals in `src/utils.ts`
is: compare use Object.is (JS method)
*/
array.Where({symbol: "!==", value: 2}).Sum() // 53
// or you can pass you function
array.Where((number, index, allItems) => number !== 2) // the same of Array.prototype.filter
/*
args accept a method, the same of Array.prototype.map.
If empty, just return the array like .ToArray
*/
const result = array.Select((x) => x ** 2) // the same of Array.prototype.map
API
const array = new Linq([1, 2, 3, 4, 5, 6, 7, 8, 9])
Reverse()
: use theArray.prototype.reverse
to reverse arrayAdd(el: Type | Type[])
: Add an item/items to last position of array with push/concatPrepend(el: Type | Type[])
: Add an item/items to first position of array with push/concatConcat(list: Type[])
: Concat an array to your arraySelect(transform?: ArrayCallback<Type>)
: Get array from Linq instance. The argument is a optional function, and works equalsArray.prototype.map
Take(init: number, end?: number)
: Taken
items of array. If end is null, usen:array.length
Head()
: Get first item of arrayTail()
: Return all items, except firstSkip(jumps: number | ArrayCallbackAssertion<Type>)
: Skip then
items or skip while the callback is falseDistinct()
: like Linq.UniqueToArray()
: Get array from instanceFirst(predicate?: ArrayCallbackAssertion<Type>)
: Return the first item of array or applyArray.prototype.find
Last(predicate?: ArrayCallbackAssertion<Type>)
: Return the last item of array or applyArray.prototype.find
in reverse array (usingArray.prototype.reverse
)Sum(key?: keyof Type)
: Sum all items of array. You can specify the property or sum all primitive values in arrayAverage(key?: keyof Type)
: Get Average ofLinq.Sum
GroupBy(key: keyof Type)
: The same of static methodLinq.GroupBy
Except(exceptions: Type[])
: Get all items not in argument arrayIntersect(commons: Type[])
: Get all items in argument arrayOrderBy(key?: keyof Type, sort?: OrderKeys)
: Order array from keyIncludes(object: Type)
: Check if array includes the objectIn(array: Type[])
: Check if instance array has items in argument arrayEmpty()
: nothing to commentToMap<KEY>(key: keyof Type): Map<KEY, Type>
: convert the array to Map, using the value of object[key] to Map keysZip<T>(array: T[], fn: (first: Type, second?: T) => any)
Count(predicate?: ArrayCallbackAssertion<Type>)
: Count items based on predicate or just array.lengthGet(n: number)
: Get item inn
positionClone()
: deepClone instance arrayToObject(key: keyof Type): ArrayAsObj<Type>
All(predicate: ArrayCallbackAssertion<Type>)
: The same ofArray.prototype.every