Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
+ `Repeat` may be called with an element factory callback
+ `Partition` extension overworked
+ `PartitionInfo` for the `Partition`extension
* Some code refactoring
  • Loading branch information
nd1012 committed Jan 16, 2022
1 parent 3382c70 commit 9118ad8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
25 changes: 22 additions & 3 deletions src/linq.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@
*/
get Store(){return this.#Store;}

/**
* Get if created instances won't store generated items, too
*
* @return {boolean} Pass disabled storing to new instances?
*/
get Pass(){return this.#Pass;}

/**
* Get the extended object
*
Expand Down Expand Up @@ -1579,6 +1586,17 @@
*/
_GetIterator(){return this._Iterable[Symbol.iterator]();}

/**
* Set the group key
*
* @param {any} key Key
* @return {LinqArray} This
*/
_SetGroupKey(key){
this.#GroupKey=key;
return this;
}

/**
* Ensure having an action or a default result
*
Expand Down Expand Up @@ -1686,16 +1704,17 @@
/**
* Repeat an element
*
* @param {any} e Element
* @param {any|Function<int,any>} e Element or action that returns an element (gets the index as parameter)
* @param {int} count Count
* @param {boolean} store (optional) Store generated items (default: `true`)?
* @param {boolean} pass (optional) Pass this behavior to created instances?
* @return {LinqArray} LINQ array
*/
static Repeat(e,count,store=true,pass=false){
const res=new this(null,store,pass);
const res=new this(null,store,pass),
isFnc=LinqArray.Helper.IsFunction(e);
res.#IsGenerated=false;
res.#Generator=function*(){for(let i=0;i<count;i++) yield e;}();
res.#Generator=function*(){for(let i=0;i<count;i++) yield isFnc?e(i):e;}();
res.#EstimatedCount=count;
return res;
}
Expand Down
52 changes: 34 additions & 18 deletions src/linqext.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
* @param {Function<any,any,boolean>} comp (optional) Key comparing action
* @return {LinqArrayExt} Resulting LINQ array
*/
InnerJoin(arr,action,arrAction,result=null,comp=null){
action=LinqArray.Helper.EnsureValueGetter(action);
arrAction=LinqArray.Helper.EnsureValueGetter(arrAction);
if(!result) result=(a,b)=>({...a,...b});
return this.Join(arr,action,arrAction,result,comp);
}
InnerJoin(arr,action,arrAction,result=null,comp=null){return this.Join(arr,action,arrAction,result??((a,b)=>({...a,...b})),comp);}

/**
* Left join this array with another array by their common keys
Expand All @@ -34,10 +29,8 @@
* @return {LinqArrayExt} Resulting LINQ array
*/
LeftJoin(arr,action,arrAction,result,comp=null){
action=LinqArray.Helper.EnsureValueGetter(action);
arrAction=LinqArray.Helper.EnsureValueGetter(arrAction);
arr=LinqArray.Helper.EnsureLinqArray(arr,true);
return arr.GroupJoin(this,action,arrAction,(a,b)=>{a,b},comp)
return LinqArray.Helper.EnsureLinqArray(arr,true)
.GroupJoin(this,action,arrAction,(a,b)=>{a,b},comp)
.SelectMany((item)=>item.b,(a,b)=>result(a.a,b));
}

Expand All @@ -52,10 +45,8 @@
* @return {LinqArrayExt} Resulting LINQ array
*/
RightJoin(arr,action,arrAction,result,comp=null){
action=LinqArray.Helper.EnsureValueGetter(action);
arrAction=LinqArray.Helper.EnsureValueGetter(arrAction);
arr=LinqArray.Helper.EnsureLinqArray(arr,true);
return arr.GroupJoin(this,arrAction,action,(a,b)=>{a,b},comp)
return LinqArray.Helper.EnsureLinqArray(arr,true)
.GroupJoin(this,arrAction,action,(a,b)=>{a,b},comp)
.SelectMany((item)=>item.b,(a,b)=>result(b,a.a));
}

Expand Down Expand Up @@ -89,12 +80,27 @@
/**
* Create a partition table
*
* **NOTE**: The contents of this instance needs to be an array of LINQ arrays (having all items generated), as it will be returned from `GroupBy`!
* **NOTE**: There'll be a partition without `GroupKey` that contains all items that didn't match into a partition.
*
* @param {string} rowKey Row key property name
* @return {LinqArrayExt} Partition table
* @param {...object} partitions Partition informations (as returned from `LinqArrayExt.PartitionInfo`)
* @return {LinqArrayExt} LINQ array of LINQ array partitions
*/
Partition(rowKey){return this.SelectMany((item)=>item.Zip(LinqArray.Range(1,item.length),(j,i)=>({...j,[rowKey]:i})));}
Partition(...partitions){
const linqArrayExt=this.constructor,
res=LinqArrayExt.Repeat(i=>i==partitions.length?new linqArrayExt():new linqArrayExt()._SetGroupKey(partitions[i].key),partitions.length+1).EnsureGenerated();
let item,
i,
match;
for(item of this){
for(i=0,match=false;!match&&i<partitions.length;i++){
if((partitions[i].max&&res[i].length>=partitions[i].max)||!partitions[i].action(item)) continue;
res[i].push(item);
match=true;
}
if(!match) res.Last().push(item);
}
return res;
}

/**
* Create a pivot table or summaries
Expand Down Expand Up @@ -432,6 +438,16 @@
* @return {object} Pivot column configuration
*/
static PivotCalcColumn(key,data){return {key,group:null,data};}

/**
* Create a partition information
*
* @param {string} key Partition key
* @param {Function<any,boolean>} action Filter action
* @param {int} max (optional) Maximum number of items to store
* @return {object} Partition information
*/
static PartitionInfo(key,action,max=null){return{key,action,max};}
}

// Override the global `From` function to generate a LINQ array extensions object from now on
Expand Down

0 comments on commit 9118ad8

Please sign in to comment.