Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jul 7, 2021
1 parent 006920e commit 0748a56
Show file tree
Hide file tree
Showing 23 changed files with 5,931 additions and 2,870 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"@typescript-eslint"
],
"extends": [
"plugin:@typescript-eslint/eslint-recommended"
"plugin:@typescript-eslint/eslint-recommended",
"prettier"
],
"env": {
"es6": true,
Expand Down
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage
.vscode
node_modules
report
lib
test
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
28 changes: 20 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"jest": "^27.0.6",
"prettier": "2.3.2",
"rimraf": "^3.0.2",
"standard-version": "^9.3.0",
"ts-jest": "^27.0.3",
Expand Down
30 changes: 15 additions & 15 deletions src/builder/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
import { ODataMethod } from '../constants';

export interface JsonBatchRequestBundle {
requests: JsonBatchRequest[];
requests: JsonBatchRequest[];
}

export type JsonBatchMethod = ODataMethod
export type JsonBatchMethod = ODataMethod;

export type JsonBatchHeaders = Record<string, string>
export type JsonBatchHeaders = Record<string, string>;

export interface JsonBatchRequest<T = any> {
id: string;
method: JsonBatchMethod;
url: string;
atomicityGroup?: string;
dependsOn?: string[];
headers?: JsonBatchHeaders;
body?: T;
id: string;
method: JsonBatchMethod;
url: string;
atomicityGroup?: string;
dependsOn?: string[];
headers?: JsonBatchHeaders;
body?: T;
}

export interface JsonBatchResponseBundle {
responses: JsonBatchResponse[]
responses: JsonBatchResponse[];
}

export interface JsonBatchResponse<T = any> {
id: string;
status: number;
body?: any;
headers?: JsonBatchHeaders;
id: string;
status: number;
body?: any;
headers?: JsonBatchHeaders;
}
80 changes: 42 additions & 38 deletions src/builder/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ export enum ExprOperator {
type FieldExpr = {
op: ExprOperator;
value: string;
}
};

type FieldExprMappings = {
[key: string]: FieldExpr[]
}
[key: string]: FieldExpr[];
};

/**
* @private
* @internal
*/
class ODataFieldExpr {

constructor(filter: ODataFilter, fieldName: string, mapping: FieldExprMappings) {
constructor(
filter: ODataFilter,
fieldName: string,
mapping: FieldExprMappings
) {
this._exprMappings = mapping;
this._fieldName = fieldName;
this._filter = filter;
Expand All @@ -40,20 +43,20 @@ class ODataFieldExpr {

private _fieldName: string;

private _exprMappings: FieldExprMappings
private _exprMappings: FieldExprMappings;

private _getFieldExprs() {
return this._exprMappings[this._fieldName];
}

private _addExpr(op: ExprOperator, value: any) {

if (value === null) {
this._getFieldExprs().push({ op, value: 'null' });
}

switch (typeof value) {
case 'number': case 'boolean':
case 'number':
case 'boolean':
this._getFieldExprs().push({ op, value: `${value}` });
break;
case 'string':
Expand All @@ -65,17 +68,27 @@ class ODataFieldExpr {
break;
case 'object':
if (value instanceof Edm.PrimitiveTypeValue) {
this._getFieldExprs().push({ op, value: convertPrimitiveValueToString(value) });
this._getFieldExprs().push({
op,
value: convertPrimitiveValueToString(value)
});
} else {
throw new Error(`Not support object ${value?.constructor?.name || typeof value} in odata filter eq/ne/gt/ge/ne/nt ...`);
throw new Error(
`Not support object ${
value?.constructor?.name || typeof value
} in odata filter eq/ne/gt/ge/ne/nt ...`
);
}
break;
case 'undefined':
throw new Error(`You must set value in odata filter eq/ne/gt/ge/ne/nt ...`);
throw new Error(
`You must set value in odata filter eq/ne/gt/ge/ne/nt ...`
);
default:
throw new Error(`Not support typeof ${typeof value}: ${value} in odata filter eq/ne/gt/ge/ne/nt ...`);
throw new Error(
`Not support typeof ${typeof value}: ${value} in odata filter eq/ne/gt/ge/ne/nt ...`
);
}

}

/**
Expand Down Expand Up @@ -147,7 +160,9 @@ class ODataFieldExpr {
*
* @param values
*/
in(values: Array<number | string | Edm.PrimitiveTypeValue> = []): ODataFilter {
in(
values: Array<number | string | Edm.PrimitiveTypeValue> = []
): ODataFilter {
if (values.length > 0) {
values.forEach((value) => {
this.eq(value);
Expand Down Expand Up @@ -176,15 +191,12 @@ class ODataFieldExpr {
}
return this._filter;
}

}


/**
* OData filter builder
*/
export class ODataFilter<T = any> {

static New<E>(obj?: Partial<E>): ODataFilter {
return new ODataFilter<E>(obj as E);
}
Expand Down Expand Up @@ -216,7 +228,6 @@ export class ODataFilter<T = any> {
return new ODataFieldExpr(this, name as string, this.getExprMapping());
}


toString(): string {
return this.build();
}
Expand All @@ -234,37 +245,30 @@ export class ODataFilter<T = any> {
exprs.map(({ op, value }) => `${field} ${op} ${value}`),
' or '
)})`;

}
return '';


}

build(): string {
let _rt = '';
_rt = join(
// join all fields exprs string
Object.entries(this.getExprMapping()).map(
([fieldName, exprs]) => {
switch (exprs.length) {
// if one field expr mapping array is empty
case 0:
return '';
// only have one expr
case 1:
const { op, value } = exprs[0];
return `${fieldName} ${op} ${value}`;
default:
// multi exprs
return this._buildFieldExprString(fieldName);
}

Object.entries(this.getExprMapping()).map(([fieldName, exprs]) => {
switch (exprs.length) {
// if one field expr mapping array is empty
case 0:
return '';
// only have one expr
case 1:
const { op, value } = exprs[0];
return `${fieldName} ${op} ${value}`;
default:
// multi exprs
return this._buildFieldExprString(fieldName);
}
),
}),
' and '
);
return _rt;
}

}
24 changes: 16 additions & 8 deletions src/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const literalValues = {
Boolean: (value: Boolean) => Edm.Boolean.createValue(value),
Byte: (value: number) => Edm.Byte.createValue(value),
Date: (value: Date | string) => Edm.Date.createValue(value),
DateTimeOffset: (value: Date | String) => Edm.DateTimeOffset.createValue(value),
DateTimeOffset: (value: Date | String) =>
Edm.DateTimeOffset.createValue(value),
Decimal: (value: number) => Edm.Decimal.createValue(value),
Double: (value: number) => Edm.Double.createValue(value),
Duration: (value: string) => Edm.Duration.createValue(value),
Expand All @@ -38,18 +39,25 @@ export const literalValues = {
TimeOfDay: (value: any) => Edm.TimeOfDay.createValue(value),
Geography: (value: any) => Edm.Geography.createValue(value),
GeographyPoint: (value: any) => Edm.GeographyPoint.createValue(value),
GeographyLineString: (value: any) => Edm.GeographyLineString.createValue(value),
GeographyLineString: (value: any) =>
Edm.GeographyLineString.createValue(value),
GeographyPolygon: (value: any) => Edm.GeographyPolygon.createValue(value),
GeographyMultiPoint: (value: any) => Edm.GeographyMultiPoint.createValue(value),
GeographyMultiLineString: (value: any) => Edm.GeographyMultiLineString.createValue(value),
GeographyMultiPolygon: (value: any) => Edm.GeographyMultiPolygon.createValue(value),
GeographyCollection: (value: any) => Edm.GeographyCollection.createValue(value),
GeographyMultiPoint: (value: any) =>
Edm.GeographyMultiPoint.createValue(value),
GeographyMultiLineString: (value: any) =>
Edm.GeographyMultiLineString.createValue(value),
GeographyMultiPolygon: (value: any) =>
Edm.GeographyMultiPolygon.createValue(value),
GeographyCollection: (value: any) =>
Edm.GeographyCollection.createValue(value),
Geometry: (value: any) => Edm.Geometry.createValue(value),
GeometryPoint: (value: any) => Edm.GeometryPoint.createValue(value),
GeometryLineString: (value: any) => Edm.GeometryLineString.createValue(value),
GeometryPolygon: (value: any) => Edm.GeometryPolygon.createValue(value),
GeometryMultiPoint: (value: any) => Edm.GeometryMultiPoint.createValue(value),
GeometryMultiLineString: (value: any) => Edm.GeometryMultiLineString.createValue(value),
GeometryMultiPolygon: (value: any) => Edm.GeometryMultiPolygon.createValue(value),
GeometryMultiLineString: (value: any) =>
Edm.GeometryMultiLineString.createValue(value),
GeometryMultiPolygon: (value: any) =>
Edm.GeometryMultiPolygon.createValue(value),
GeometryCollection: (value: any) => Edm.GeometryCollection.createValue(value)
};
Loading

0 comments on commit 0748a56

Please sign in to comment.