✨
Linqable.ts Open documentation! ;3
Install
-
yarn add linqable.ts
or npm i linqable.ts
CDN
<script src="https://cdn.jsdelivr.net/npm/linqable.ts@1.7.18/lib/web/linq.min.js"></script>
Changelog
See Changelog
🌱
Usage Browser:
<head>
<script src="https://cdn.jsdelivr.net/npm/linqable.ts@1.7.18/lib/web/linq.min.js"></script>
</head>
<!-- ... -->
<script>
[2, 4].Sum() // -> 6
</script>
Node:
import "linqable.ts";
console.log([3,5].Sum());
Use Advanced & Base Linqable:
import { AdvancedLinqable, BaseLinqable } from "linqable.ts";
console.log(new BaseLinqable([3,5]).Sum());
console.log(new AdvancedLinqable([3,5]).Acquire());
☄️
Build yarn build
- You are great!
💫
🔥
Dependences for build - TypeScript 3.0 or above (in global)
- AVA 1.0.0-beta.8 or above (in global)
🍒
Test yarn test
- ava write test-report to screen
API:
Advanced API
Advanced API
Transpose
Transposes the rows of a sequence into columns.
let array = [
[
"Nola", "Myse"
],
[
"Ruq"
],
[
"Dufna",
"Nygglatho",
"Kumesh"
]
];
/* ... */
array.Transpose();
// result ->
[
[
'Nola',
'Ruq',
'Dufna',
],
[
'Myse',
'Nygglatho',
],
[
'Kumesh',
],
]
Evaluate
Returns a sequence containing the values resulting from invoking (in order) each function in the source sequence of functions.
let array = [() => "Chtholly", () => "Ithea", () => 1 + 1, () => !true]
/* ... */
array.Evaluate(); // => ["Chtholly", "Ithea", 2, false]
Acquire
Ensures that a source sequence of objects are all acquired successfully. If the acquisition of any one fails then those successfully acquired till that point are delete
let array = [{name: "Chtholly Nola", age: 17}, { name: "Ithea Myse", age: 18 }]
/* ... */
array.Acquire(); // => success // => [{name: "Chtholly Nola", age: 17}, { name: "Ithea Myse", age: 18 }]
array.Acquire(); // => fail // => [] // => throw
Consume
Completely consumes the given sequence. This method uses immediate execution, and doesn't store any data during execution
let array = [{name: "Chtholly Nola", age: 17}, { name: "Ithea Myse", age: 18 }]
/* ... */
array.Consume();
Batch
Batches the source sequence into sized buckets.
let array = [{name: "Chtholly Nola"},
{name: "Nephren Ruq"},
{name: "Almaria Dufna"},
{name: "Ithea Myse"}]
/* ... */
array.Batch(2); // => [[{name: "Chtholly Nola"}, {name: "Nephren Ruq"}],[{name: "Almaria Dufna"}, {name: "Ithea Myse"}]]
// Returns an array with 2 arrays 😏
MaxBy
Returns the maxima (maximal elements) of the given sequence, based on the given projection.
let array = [{name: "Chtholly Nola", age: 17}, { name: "Ithea Myse", age: 18 }]
/* ... */
array.MaxBy(x => x.age) // => { name: "Ithea Myse", age: 18 }
MinBy
Returns the minima (minimal elements) of the given sequence, based on the given projection.
let array = [{name: "Chtholly Nola", age: 17}, { name: "Ithea Myse", age: 18 }]
/* ... */
array.MinBy(x => x.age) // => {name: "Chtholly Nola", age: 17}
Exclude
Excludes elements from a sequence starting at a given index
let array = ["CO2", "Ir2O", "C2O3", "NH3", "C2H6", "H2C03"]
/* ... */
array.Exclude(1, 2) // -> ["CO2", "NH3", "C2H6", "H2C03"]
Flatten
Flattens a sequence containing arbitrarily-nested sequences.
let array = ["CO2", ["C2O3", ["NH3", 127.4], 241, "H2C03"]
/* ... */
array.Flatten() // -> ["CO2", "C2O3", "NH3", 127.4, 241, "H2C03"]
Pairwise
Returns a sequence resulting from applying a function to each element in the source sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element
let array = ["atom", "core", "neutron"];
/* ... */
array.Pairwise((x, y) => `${x} contains ${y}`) // -> ["atom contains core", "core contains neutron"]
Pipe
Executes the given action on each element in the source sequence and yields it
let array = [{name: 'neutron', lifetime: 880}, {name: "proton", lifetime: Infinity}]
/* ... */
array.Pipe(x => x.lifetime++);
array.Where(x => x.name == "neutron").lifetime // -> 881
Lag
Produces a projection of a sequence by evaluating pairs of elements separated by a negative offset.
let array = [0, 1, 2, 3, 4];
/* ... */
array.Lag(/*step*/2, /*defaultValue*/0, (a, b) => { return { A: a, B: b}; })
//returned -> [{"A":0,"B":0},{"A":1,"B":0},{"A":2,"B":0},{"A":3,"B":1},{"A":4,"B":2}]
Standard API
Standard API
[First]OrDefault
Returns the first element of a sequence. (Predicate Support)
let array = [{formula: "CeO2", MolarMass: 172.115 }, {formula: "O", MolarMass: 15.999 }];
/* ... */
array.First() // => {formula: "CeO2", MolarMass: 172.115 }
let defaultValue = {formula: "H", MollarMass: 14.1 }
[].FirstOrDefault(null, defaultValue) // => {formula: "H", MollarMass: 14.1 }
[Last]OrDefault
Returns the last element of a sequence. (Predicate Support)
let array = [{formula: "CeO2", MolarMass: 172.115 }, {formula: "O", MolarMass: 15.999 }];
/* ... */
array.Last() // => {formula: "O", MolarMass: 15.999 }
let defaultValue = {formula: "H", MollarMass: 14.1 }
[].LastOrDefault(null, defaultValue) // => {formula: "H", MollarMass: 14.1 }
Select
Projects each element of a sequence into a new form.
let array = [{name: "Chtholly Nola", age: 17}, { name: "Nephren Ruq", age: 17}]
/* ... */
array.Select(x => x.name.split(' ').First()) // => [{name: "Chtholly"}, {"Nephren"}]
Where
Filters a sequence of values based on a predicate.
let array = [{name: "Chtholly Nola", age: 17},
{name: "Nephren Ruq", age: 17},
{name: "Almaria Dufna", age: 19},
{name: "Ithea Myse", age: 18}]
/* ... */
// where adult only 🙈
array.Where(x => x.age >= 18) // => [ {name: "Almaria Dufna", age: 19}, {name: "Ithea Myse", age: 18}]
Any
Determines whether any element of a sequence exists or satisfies a condition.
let array = [{name: "Chtholly Nola", IsDead: true},
{name: "Nephren Ruq", IsDead: false},
{name: "Almaria Dufna", IsDead: true},
{name: "Ithea Myse", IsDead: true}]
/* ... */
array.Any(x => x.IsDead) // => true
array.Where(x => !x.IsDead).Any(x => x.IsDead) // => false
All
Determines whether all elements of a sequence satisfy a condition.
let array = [{name: "Chtholly Nola", IsDead: true},
{name: "Nephren Ruq", IsDead: false},
{name: "Almaria Dufna", IsDead: true},
{name: "Ithea Myse", IsDead: true}]
/* ... */
array.All(x => x.IsDead) // => false
array.Where(x => x.IsDead).All(x => x.IsDead) // => true
Sum
Computes the sum of the sequence of Decimal values that are obtained by invoking a transform function on each element of the input sequence.
let array1 = [1, 2, 3];
let array2 = [{num: 15}, {num: 10}];
/* ... */
array1.Sum() // => 6
array2.Sum(x => x.num) // => 25
IsEmpty
Gets a value indicating whether this array contains no elemets.
let array1 = [];
let array2 = ["Cobalt","Mithril"];
/* ... */
array1.IsEmpty() // => true
array2.IsEmpty() // => false
Min
Invokes a transform function on each element of a sequence and returns the minimum number value.
let array = [{name: "Chtholly Nola", age: 17}, { name: "Ithea Myse", age: 18 }]
/* ... */
array.Min(x => x.age) // => 17
Max
Invokes a transform function on each element of a sequence and returns the maximum number value.
let array = [{name: "Chtholly Nola", age: 17}, { name: "Ithea Myse", age: 18 }]
/* ... */
array.Max(x => x.age) // => 18
Take
Returns a specified number of contiguous elements from the start of a sequence.
let array = ["Cobalt","Mithril","Adamantium"];
/* ... */
array.Take(2) // => ["Cobalt","Mithril"]
OrderBy
Sorts the elements of a sequence in a particular direction (ascending, descending) according to a key.
let array = [4, 2, 7, 3, 0, 6];
/* ... */
array.OrderBy(); // => [0, 2, 3, 4, 6, 7];
Supports primitives, including Date.
To compare other objects,
you need to implement interface IComparer (TypeScript)
or implement function [Compare(y) : number
]
As well support Descending.
Reverse
Inverts the order of the elements in a sequence.
let array = [{name: "Chtholly Nola"},
{name: "Nephren Ruq"},
{name: "Almaria Dufna"},
{name: "Ithea Myse"}]
/* ... */
array.Reverse() // => [{name: "Ithea Myse"},{name: "Almaria Dufna"},{name: "Nephren Ruq"},{name: "Chtholly Nola"}]
Distinct
Returns distinct elements from a sequence by using the default equality comparer to compare values.
let array1 = ["Alkaloid", "Protein", "Chlorophyll", "Alkaloid"];
/* ... */
array1.Distinct() // => ["Alkaloid", "Protein", "Chlorophyll"]
Union
Produces the set union of two sequences.
let array1 = ["Alkaloid", "Protein", "Chlorophyll", "Alkaloid"];
let array2 = ["Uranium", "Iridium", "Iridium", "Plutonium"];
/* ... */
array1.Union(array2) // => ["Alkaloid", "Protein", "Chlorophyll", "Uranium", "Iridium", "Plutonium"]
Zip
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
let woman = [ "Chtholly", "Nephren" ];
let man = [ "Willem", "Willem" ];
woman.Zip(man, (w, m) => `${w} love ${m}`) // => ["Chtholly love Willem", "Nephren love Willem"]
Single[OrDefault]
Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
let array = [{synthesis: "Nuclear"}, {synthesis: "Thermonuclear"}]
array.Single() // => Throw Error
/* ... */
array.SingleOrDefault({synthesis: "none"}) // => return default value // => {synthesis: "none"}
/* ... */
array = [{synthesis: "Nuclear"}];
/* ... */
array.Single() // => {synthesis: "Nuclear"}
Road Map
RoadMap
Standard:
- First
- FirstOrDefault
- Last
- LastOrDefault
- Select
- SelectMany
- Where
- Any
- All
- Sum
- Take
- TakeWhile
- Min & Max
- MinBy & MaxBy
- IsDefault
- OrderBy
- Range
- Reverse
- Single
- SingleOrDefault
- SkipWhile
- ThenBy
- ThenByDescending
- ToArray
- Union
- Zip
- Aggregate
- Count
- Average
- Append
- Contains
- DefaultIfEmpty
- Distinct
- Except
- GroupBy
- GroupJoin
- Join
Advanced:
- Acquire
- AggregateRight
- Assert
- AssertCount
- AtLeast
- AtMost
-
Backsert - Batch
-
Cartesian - Choose (Deferred typescript 3)
- Concat
- Consume
- CountBetween & CountBy & CountDown & CompareCount
- EndsWith
- EquiZip
- DistinctBy
- Exactly
- ExceptBy
- Exclude
- Evaluate
-
FallbackIfEmpty - FillBackward & FillForward
- Flatten
-
Fold - FullGroupJoin
- FullJoin
- Generate & GenerateByIndex
- GroupAdjacent
- Index
- Insert
- Interleave
- Lag
- Lead
- LeftJoin
- Move
- OrderedMerge
- Pad & PadStart
- Pairwise
- PartialSort & PartialSortBy
- Partition
- Permutations
- Pipe
- Prepend
-
PreScan - Random & RandomDouble & RandomSubset
-
Rank & RankBy - Repeat
- RightJoin
- RunLengthEncode
- Scan & ScanRight
- Segment
-
Sequence - Shuffle
- SkipLast & SkipUntil
- Slice
- SortedMerge
- Split
- StartsWith
- Subsets
- TagFirstLast
-
Transpose - TakeEvery & TakeLast & TakeUntil
-
ZipLongest & ZipShortest