Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github pages update #9

Merged
merged 6 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
15 changes: 7 additions & 8 deletions LInQer.Enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace Linqer {
toObject(keySelector: ISelector, valueSelector: ISelector): { [key: string]: any };
toHashSet(): never;
toSet(): Set<any>;
toList(): never;
union(iterable: IterableType, equalityComparer: IEqualityComparer): Enumerable;
zip(iterable: IterableType, zipper: (item1: any, item2: any, index: number) => any): any;

Expand Down Expand Up @@ -89,9 +88,9 @@ namespace Linqer {
const f: ((x: any) => boolean) = typeof type === 'string'
? x => typeof x === type
: x => x instanceof type;
return this.where(item => {
return this.select(item => {
if (!f(item)) throw new Error(item + ' not of type ' + type);
return true;
return item;
});
}

Expand Down Expand Up @@ -422,10 +421,6 @@ namespace Linqer {
return result;
}

Enumerable.prototype.toList = function (): never {
throw new Error('use toArray instead of toList');
}

/// Produces the set union of two sequences.
Enumerable.prototype.union = function (iterable: IterableType, equalityComparer: IEqualityComparer = EqualityComparer.default): Enumerable {
_ensureIterable(iterable);
Expand All @@ -435,7 +430,11 @@ namespace Linqer {
/// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
Enumerable.prototype.zip = function (iterable: IterableType, zipper: (item1: any, item2: any, index: number) => any): any {
_ensureIterable(iterable);
_ensureFunction(zipper);
if (!zipper) {
zipper = (i1,i2)=>[i1,i2];
} else {
_ensureFunction(zipper);
}
const self: Enumerable = this;
const gen = function* () {
let index = 0;
Expand Down
36 changes: 26 additions & 10 deletions LInQer.OrderedEnumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,8 @@ namespace Linqer {
const sort: (item1: any, item2: any) => void = this._useQuickSort && this._restrictions.length
? (a, c) => _quickSort(a, 0, a.length - 1, c, startIndex, endIndex)
: (a, c) => a.sort(c);
sort(arr, (i1: any, i2: any) => {
for (const selector of self._keySelectors) {
const v1 = selector.keySelector(i1);
const v2 = selector.keySelector(i2);
if (v1 > v2) return selector.ascending ? 1 : -1;
if (v1 < v2) return selector.ascending ? -1 : 1;
}
return 0;
});
const sortFunc = this.generateSortFunc(self._keySelectors);
sort(arr, sortFunc);
for (let index = startIndex; index < endIndex; index++) {
yield arr[index];
}
Expand All @@ -94,10 +87,33 @@ namespace Linqer {
};
}

private generateSortFunc(selectors: { keySelector: ISelector, ascending: boolean }[]): (i1: any, i2: any)=> number {
const comparers = selectors.map(s=>{
const f = s.keySelector;
const comparer = (i1:any,i2:any)=> {
const k1 = f(i1);
const k2 = f(i2);
if (k1>k2) return 1;
if (k1<k2) return -1;
return 0;
};
return s.ascending
? comparer
: (i1:any,i2:any)=> -comparer(i1,i2);
});
return (i1: any, i2: any) => {
for (const comparer of comparers) {
const v = comparer(i1,i2);
if (v) return v;
}
return 0;
};
}

private getStartAndEndIndexes(restrictions: { type: RestrictionType, nr: number }[], arrLength: number) {
let startIndex = 0;
let endIndex = arrLength;
for (var restriction of restrictions) {
for (const restriction of restrictions) {
switch (restriction.type) {
case RestrictionType.take:
endIndex = Math.min(endIndex, startIndex + restriction.nr);
Expand Down
7 changes: 7 additions & 0 deletions LInQer.Slim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ namespace Linqer {
return Array.from(this);
}

/// similar to toArray, but returns a seekable Enumerable (itself if already seekable) that can do count and elementAt without iterating
toList(): Enumerable {
_ensureInternalTryGetAt(this);
if (this._canSeek) return this;
return Enumerable.from(Array.from(this));
}

/// Filters a sequence of values based on a predicate.
where(condition: IFilter): Enumerable {
_ensureFunction(condition);
Expand Down
Loading