Skip to content

Commit

Permalink
orderBy support of string
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfields committed Feb 11, 2020
1 parent 09a4f9e commit c453274
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
33 changes: 20 additions & 13 deletions src/linq/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface IEnumerable<TEntity> extends Iterable<TEntity>, AsyncIterable<T

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

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

Expand Down Expand Up @@ -186,20 +187,25 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
* A remapper of identifier names, members is seperated with dot.
* @param remapper Function that returns the new name of the identifier
*/
public remap<TResult>(remapper: (name: string) => string) : IEnumerable<TResult>
public remap<TResult>(remapper: (name: string) => string) : Enumerable<TResult>
/**
* A remapper of values that corresponds to a identifier name
* @param remapper Function that returns the new value
*/
public remap<TResult>(remapper: (name: string, value: any) => any) : IEnumerable<TResult>
public remap<TResult>(remapper: (...args) => any) : IEnumerable<TResult> {
public remap<TResult>(remapper: (name: string, value: any) => any) : Enumerable<TResult>
public remap<TResult>(remapper: (...args) => any) : Enumerable<TResult> {
let visitor = remapper.length == 2 ? new RemapVisitor(null, remapper) : new RemapVisitor(remapper, null);

for (let item of this._operations.values()) {
switch (item.type) {
case OperatorType.Where:
(<WhereOperator<TEntity>>item).expression = visitor.visit((<WhereOperator<TEntity>>item).expression);
break;

case OperatorType.OrderBy:
if(remapper.length == 1)
(<OrderByOperator<TEntity>>item).property = remapper((<OrderByOperator<TEntity>>item).property);
break;
}
}

Expand Down Expand Up @@ -244,9 +250,9 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
* returns a new IEnumerable of TResult
* @param selector
*/
public select<TResult>(selector: (it: TEntity) => TResult): IEnumerable<TResult>
public select<TResult extends Partial<TEntity>>(selector: string): IEnumerable<TResult>
public select(selector: string | ((it: TEntity) => Record<string, any>)): IEnumerable<Record<string, any>> {
public select<TResult>(selector: (it: TEntity) => TResult): Enumerable<TResult>
public select<TResult extends Partial<TEntity>>(selector: string): Enumerable<TResult>
public select(selector: string | ((it: TEntity) => Record<string, any>)): Enumerable<Record<string, any>> {
if (typeof this.items == 'object' && typeof this.items[Symbol.asyncIterator] == 'function') {
return new Enumerable(new SelectOperator(selector).evaluateAsync(this));
} else {
Expand All @@ -263,7 +269,7 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
* @param indexing If set to true an Array of outerKey will be passed into iterator of TInner as { keys: Array<any> }. Cons, all elements of outer iterator will be be kept in memory.
* @returns IEnumerable<TResult>
*/
public groupJoin<TInner, TResult>(inner: Iterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): IEnumerable<TResult>
public groupJoin<TInner, TResult>(inner: Iterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): Enumerable<TResult>
/**
* returns a new IEnumerable of TResult (Left Join)
* @param inner
Expand All @@ -273,8 +279,8 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
* @param indexing If set to true an Array of outerKey will be passed into iterator of TInner as { keys: Array<any> }. Cons, all elements of outer iterator will be be kept in memory.
* @returns IEnumerable<TResult>
*/
public groupJoin<TInner, TResult>(inner: AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): IEnumerable<TResult>
public groupJoin<TInner, TResult>(inner: Iterable<TInner> | AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing: boolean = false): IEnumerable<TResult> {
public groupJoin<TInner, TResult>(inner: AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): Enumerable<TResult>
public groupJoin<TInner, TResult>(inner: Iterable<TInner> | AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing: boolean = false): Enumerable<TResult> {
let iterable: Iterable<TInner> | AsyncIterable<TInner> = ((scope) => <any>{
[Symbol.asyncIterator]: (options) => {
return inner[Symbol.asyncIterator](Object.assign({ parent: scope }, options));
Expand All @@ -300,7 +306,7 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
* @param indexing If set to true an Array of outerKey will be passed into iterator of TInner as { keys: Array<any> }. Cons, all elements of outer iterator will be be kept in memory.
* @returns IEnumerable<TResult>
*/
public join<TInner, TResult>(inner: Iterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): IEnumerable<TResult>
public join<TInner, TResult>(inner: Iterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): Enumerable<TResult>
/**
* returns a new IEnumerable of TResult (Inner Join)
* @param inner
Expand All @@ -310,8 +316,8 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>
* @param indexing If set to true an Array of outerKey will be passed into iterator of TInner as { keys: Array<any> }. Cons, all elements of outer iterator will be be kept in memory.
* @returns IEnumerable<TResult>
*/
public join<TInner, TResult>(inner: AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): IEnumerable<TResult>
public join<TInner, TResult>(inner: Iterable<TInner> | AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing: boolean = false): IEnumerable<TResult> {
public join<TInner, TResult>(inner: AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing?: boolean): Enumerable<TResult>
public join<TInner, TResult>(inner: Iterable<TInner> | AsyncIterable<TInner>, outerKey: (outer: TEntity) => void, innerKey: (inner: TInner) => void, selector: (outer: TEntity, inner: IEnumerable<TInner>) => TResult, indexing: boolean = false): Enumerable<TResult> {
let iterable: Iterable<TInner> | AsyncIterable<TInner> = ((scope) => <any>{
[Symbol.asyncIterator]: (options) => {
return inner[Symbol.asyncIterator](Object.assign({ parent: scope }, options));
Expand Down Expand Up @@ -384,7 +390,8 @@ export class Enumerable<TEntity> implements IEnumerable<TEntity>

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

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

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

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

if(typeof property == 'function') {
Expand Down
41 changes: 23 additions & 18 deletions tfso-repository.code-workspace
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"folders": [
{
"path": "."
"path": ".",
"name": "tfso-repository"
}
],
"settings": {
"mocha.env": {
"TS_NODE_COMPILER": "${workspaceRoot}/node_modules/typescript/bin/tsc"
},
"launch": {
"verison": "0.2.0",
"configurations": [
Expand All @@ -17,29 +15,38 @@
"name": "Run program",
"args": ["src/main.ts"],
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",
"cwd": "${workspaceFolder:tfso-repository}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
"internalConsoleOptions": "openOnSessionStart",
"env": {
"TS_NODE_TRANSPILE_ONLY": "1"
}
},
{
"type": "node",
"request": "launch",
"name": "Run current file",
"args": ["${relativeFile}"],
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",
"cwd": "${workspaceFolder:tfso-repository}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
"internalConsoleOptions": "openOnSessionStart",
"env": {
"TS_NODE_TRANSPILE_ONLY": "1"
}
},
{
"name": "Run mocha",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"program": "${workspaceFolder:tfso-repository}/node_modules/mocha/bin/_mocha",
"stopOnEntry": false,
"args": ["--no-timeouts", "--compilers", "ts:ts-node/register", "--compiler", "--require", "ts-node/register", "${workspaceFolder}/src/test/**/*.ts"],
"cwd": "${workspaceRoot}",
"protocol": "inspector"
"cwd": "${workspaceFolder:tfso-repository}",
"protocol": "inspector",
"env": {
"TS_NODE_TRANSPILE_ONLY": "1"
}
}
]
},
Expand All @@ -61,17 +68,15 @@
"obj": true,
"typings": true
},
"typescript.tsdk": "./node_modules/typescript/lib",
"mocha.options": {
"compilers": {
"ts": "ts-node/register"
}
},
"typescript.tsdk": "./node_modules/typescript/lib",
"mocha.files.glob": "src/test/*.ts",
"mocha.requires": [
"ts-node/register"
],
"mocha.parallelTests": 30,
"mocha.env": {
"TS_NODE_TRANSPILE_ONLY": "1"
},
"mocha.parallelTests": 5,
//Mocha: this option allows you to enable/disable lens decorations and set update threshold "
"mocha.sideBarOptions": {
"default": {
Expand Down

0 comments on commit c453274

Please sign in to comment.