Skip to content

Commit

Permalink
Update linq.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nd1012 committed Feb 2, 2022
1 parent 03d17dd commit fa6da80
Showing 1 changed file with 74 additions and 76 deletions.
150 changes: 74 additions & 76 deletions src/linq.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class LinqArray extends Array{
* @return {LinqArray} Extended LINQ array
*/
Union(arr,comp=null,inPlace=false){
if(this._EnsureFinite().#IsDynamic){
if(this._EnsureFinite().#IsDynamic)
if(inPlace){
const generator=this.#Generator;
this.#Generator=function*(){
Expand Down Expand Up @@ -521,21 +521,19 @@ class LinqArray extends Array{
}
});
}
}else{
const res=inPlace?this.EnsureGenerated():this.EnsureGenerated().slice(),
values=new LinqArray(res.slice());
if(!inPlace) res.#Parent=this;
res.#IsGenerated=false;
res.#Generator=function*(){
let item;
for(item of arr){
if(values.Contains(item,comp)) continue;
yield item;
values.push(item);
}
}();
return res;
}
const res=inPlace?this.EnsureGenerated():this.EnsureGenerated().slice(),
values=new LinqArray(res.slice());
if(!inPlace) res.#Parent=this;
res.#IsGenerated=false;
res.#Generator=function*(){
let item;
for(item of arr){
if(values.Contains(item,comp)) continue;
yield item;
values.push(item);
}
}();
return res;
}

/**
Expand All @@ -549,7 +547,7 @@ class LinqArray extends Array{
*/
UnionBy(arr,action,comp=null,inPlace=false){
action=LinqArray.Helper.EnsureValueGetter(action);
if(this._EnsureFinite().#IsDynamic){
if(this._EnsureFinite().#IsDynamic)
if(inPlace){
const generator=this.#Generator;
this.#Generator=function*(){
Expand Down Expand Up @@ -592,25 +590,23 @@ class LinqArray extends Array{
}
});
}
}else{
const res=inPlace?this.EnsureGenerated():this.EnsureGenerated().slice(),
values=new LinqArray(res.slice());
if(!inPlace) res.#Parent=this;
res.#IsGenerated=false;
res.#Generator=function*(){
let item,
value,
index=0;
for(item of arr){
value=action(item,index);
index++;
if(values.Contains(value,comp)) continue;
yield item;
values.push(value);
}
}();
return res;
}
const res=inPlace?this.EnsureGenerated():this.EnsureGenerated().slice(),
values=new LinqArray(res.slice());
if(!inPlace) res.#Parent=this;
res.#IsGenerated=false;
res.#Generator=function*(){
let item,
value,
index=0;
for(item of arr){
value=action(item,index);
index++;
if(values.Contains(value,comp)) continue;
yield item;
values.push(value);
}
}();
return res;
}

/**
Expand Down Expand Up @@ -673,11 +669,11 @@ class LinqArray extends Array{
}
index++;
}
}else{
for(item of self._GetIterator()){
for(i of selector(item,index)) yield i;
index++;
}
return;
}
for(item of self._GetIterator()){
for(i of selector(item,index)) yield i;
index++;
}
});
}
Expand Down Expand Up @@ -705,9 +701,9 @@ class LinqArray extends Array{
ii++;
}
}
}else{
for(i of await selector(item,index)) res.push(i);
return;
}
for(i of await selector(item,index)) res.push(i);
});
return res;
}
Expand Down Expand Up @@ -875,16 +871,16 @@ class LinqArray extends Array{
if(comp?b.Any((b)=>comp(v,action(b))):b.Any((b)=>v==action(b))) yield item;
index++;
}
}else{
const lenB=arr.length,
a=lenA!=null&&lenB!=null&&lenA<lenB?self:arr,
b=a==self?arr:self,
isLinq=LinqArray.Helper.IsLinqArray(b);
for(item of a){
v=action(item,index);
if(comp?(isLinq?b.Any((b)=>comp(v,action(b))):b.some((b)=>comp(v,action(b)))):(isLinq?b.Any((b)=>v==action(b)):b.some((b)=>v==action(b)))) yield item;
index++;
}
return;
}
const lenB=arr.length,
a=lenA!=null&&lenB!=null&&lenA<lenB?self:arr,
b=a==self?arr:self,
isLinq=LinqArray.Helper.IsLinqArray(b);
for(item of a){
v=action(item,index);
if(comp?(isLinq?b.Any((b)=>comp(v,action(b))):b.some((b)=>comp(v,action(b)))):(isLinq?b.Any((b)=>v==action(b)):b.some((b)=>v==action(b)))) yield item;
index++;
}
});
}
Expand Down Expand Up @@ -1902,7 +1898,7 @@ class LinqArray extends Array{
item,
index=0;
for(item of this._GetIterator()){
v=action(item);
v=action(item,index);
if(v==Number.MAX_VALUE) return item;
if(max!=null&&v<=max){
index++;
Expand Down Expand Up @@ -1978,7 +1974,7 @@ class LinqArray extends Array{
item,
index=0;
for(item of this._GetIterator()){
v=action(item);
v=action(item,index);
if(v==Number.MIN_VALUE) return item;
if(min!=null&&v>=min){
index++;
Expand Down Expand Up @@ -2611,30 +2607,32 @@ class LinqArray extends Array{
*[Symbol.iterator](){
if(this.#IsDynamic){
yield* this.#Generator();
}else if(this.#Extended){
return;
}
if(this.#Extended){
yield* this.#Extended[Symbol.iterator]();
}else{
const generator=this.#Generator,
superGenerator=this.#Store?super[Symbol.iterator]():null;
if(!generator){
if(!this.#Store) throw new TypeError('Iterated already - buffer is disabled');
yield* superGenerator;
}else if(generator){
let count=0;
if(superGenerator) for(let item=superGenerator.next();!item.done;item=superGenerator.next(),count++) yield item.value;
for(let item=generator.next();!item.done;item=generator.next(),count++){
if(this.#Store){
this.push(item.value);
}else{
this.#Iterated=true;
}
yield item.value;
}
this.#Generator=null;
this.#IsGenerated=true;
this.#EstimatedCount=this.#Store||this.#IsDynamic?null:count;
return;
}
const generator=this.#Generator,
superGenerator=this.#Store?super[Symbol.iterator]():null;
if(!generator){
if(!this.#Store) throw new TypeError('Iterated already - buffer is disabled');
yield* superGenerator;
return;
}
let count=0;
if(superGenerator) for(let item=superGenerator.next();!item.done;item=superGenerator.next(),count++) yield item.value;
for(let item=generator.next();!item.done;item=generator.next(),count++){
if(this.#Store){
this.push(item.value);
}else{
this.#Iterated=true;
}
yield item.value;
}
this.#Generator=null;
this.#IsGenerated=true;
this.#EstimatedCount=this.#Store||this.#IsDynamic?null:count;
}

/**
Expand Down

0 comments on commit fa6da80

Please sign in to comment.