Open documentation! ;3
yarn add linqable.ts
ornpm i linqable.ts
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/web/linq.min.js"></script>
See Changelog
Browser:
<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/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());
yarn build
- You are great! π«
- TypeScript 3.0 or above (in global)
- AVA 1.0.0-beta.8 or above (in global)
yarn test
- ava write test-report to screen
Advanced API
Advanced API
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',
],
]
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]
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
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();
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 π
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 }
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}
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"]
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"]
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"]
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
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
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 }
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 }
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"}]
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}]
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
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
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
Gets a value indicating whether this array contains no elemets.
let array1 = [];
let array2 = ["Cobalt","Mithril"];
/* ... */
array1.IsEmpty() // => true
array2.IsEmpty() // => false
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
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
Returns a specified number of contiguous elements from the start of a sequence.
let array = ["Cobalt","Mithril","Adamantium"];
/* ... */
array.Take(2) // => ["Cobalt","Mithril"]
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.
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"}]
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"]
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"]
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"]
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
- 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
- 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