Skip to content

Commit

Permalink
order by string
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfields committed Feb 6, 2020
1 parent c6e0bd8 commit d56cae7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/linq/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface IEnumerable<TEntity> extends Iterable<TEntity>, AsyncIterable<T
where(predicate: (it: TEntity, ...param: any[]) => boolean, ...parameters: any[]): this

orderBy(property: (it: TEntity) => void): this
orderBy(property: keyof TEntity): this

//range(start: number, count: number): this

Expand Down Expand Up @@ -381,7 +382,9 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
return this;
}

public orderBy(property: (it: TEntity) => void): this {
public orderBy(property: (it: TEntity) => void): this
public orderBy(property: keyof TEntity): this
public orderBy(property: keyof TEntity | ((it: TEntity) => void)): this {
this._operations.add(new OrderByOperator<TEntity>(property));

return this;
Expand Down
45 changes: 18 additions & 27 deletions src/linq/operators/orderbyoperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,43 @@
import { ExpressionVisitor, IExpression, ExpressionType, IMemberExpression, IIdentifierExpression } from './../expressions/expressionvisitor';

export class OrderByOperator<TEntity> extends Operator<TEntity> {
private _expression: IExpression;
private property: string | number | symbol

constructor(public property: (it: TEntity) => void) {
constructor(property: keyof TEntity | ((it: TEntity) => void)) {
super(OperatorType.OrderBy);

this._expression = new ExpressionVisitor().visitLambda(property);
}

public * evaluate(items: Iterable<TEntity>): IterableIterator<TEntity> {
if (this._expression.type != ExpressionType.Member)
throw new TypeError('Order by is expecting a member property as sorting property');
if(typeof property == 'function') {
let expression = new ExpressionVisitor().visitLambda(property);

var memberProperty: IExpression = (<IMemberExpression>this._expression).property,
property: IIdentifierExpression;
if (expression.type != ExpressionType.Member)
throw new TypeError('Order by is expecting a member property as sorting property');

if (memberProperty.type != ExpressionType.Identifier)
throw new TypeError('Order by is expecting a member property as sorting property');
if ((<IMemberExpression>expression).property.type != ExpressionType.Identifier)
throw new TypeError('Order by is expecting a member property as sorting property');

property = <IIdentifierExpression>memberProperty;
this.property = (<IIdentifierExpression>(<IMemberExpression>expression).property).name
}
else {
this.property = property
}
}

public * evaluate(items: Iterable<TEntity>): IterableIterator<TEntity> {
let ar = Array.from(items);
ar.sort((a, b) => {
return a[property.name] == b[property.name] ? 0 : a[property.name] < b[property.name] ? -1 : 1;
return a[this.property] == b[this.property] ? 0 : a[this.property] < b[this.property] ? -1 : 1;
})

yield* ar;
yield* ar;
}

public async * evaluateAsync(items: AsyncIterable<TEntity>): AsyncIterableIterator<TEntity> {
if (this._expression.type != ExpressionType.Member)
throw new TypeError('Order by is expecting a member property as sorting property');

var memberProperty: IExpression = (<IMemberExpression>this._expression).property,
property: IIdentifierExpression;

if (memberProperty.type != ExpressionType.Identifier)
throw new TypeError('Order by is expecting a member property as sorting property');

property = <IIdentifierExpression>memberProperty;

let ar: Array<TEntity> = [];
for await(let item of items)
ar.push(item);

ar.sort((a, b) => {
return a[property.name] == b[property.name] ? 0 : a[property.name] < b[property.name] ? -1 : 1;
return a[this.property] == b[this.property] ? 0 : a[this.property] < b[this.property] ? -1 : 1;
})

yield* ar;
Expand Down
14 changes: 13 additions & 1 deletion src/test/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,24 @@ describe("When using Enumerable", () => {
assert.deepEqual(ar.map(item => item.location), ["BREVIK", "BREVIK", "HEISTAD", "LANGESUND", "LARVIK", "PORSGRUNN", "PORSGRUNN", "SKIEN"]);
})

it("should be able to get first element", () => {
it("should be able to get first element after order by a property", () => {
var el = new Enumerable(cars).orderBy(it => it.location).first();

assert.equal(el.id, 5);
})

it("should order by a string", () => {
var ar = new Enumerable(cars).orderBy('location').toArray();

assert.deepEqual(ar.map(item => item.location), ["BREVIK", "BREVIK", "HEISTAD", "LANGESUND", "LARVIK", "PORSGRUNN", "PORSGRUNN", "SKIEN"]);
})

it("should be able to get first element after order by a string", () => {
var el = new Enumerable(cars).orderBy('location').first();

assert.equal(el.id, 5);
})

it("should slice a portion", () => {
var ar = new Enumerable(cars).slice(3, 6).toArray();

Expand Down

0 comments on commit d56cae7

Please sign in to comment.