Skip to content

Commit

Permalink
fix: changed structure of filters-parse-output-element
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 21, 2022
1 parent 882a5dc commit 0f3ec4e
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 162 deletions.
4 changes: 1 addition & 3 deletions docs/guide/filters-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ type FiltersParseOptions<

```typescript
type FiltersParseOutputElement = {
operator?: {
[K in FilterOperatorLabel]?: boolean
},
operator: `${FilterComparisonOperator}`,
value: FilterValueSimple,
key: string,
path?: string
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/parse-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ console.log(output);
// {key: 'age', operator: FieldOperator.INCLUDE}
// ],
// filters: [
// {key: 'name', value: 'pe', operator: FilterOperator.LIKE}
// {key: 'name', value: 'pe', operator: FilterComparisonOperator.LIKE}
// ]
//}
```
Expand Down
18 changes: 9 additions & 9 deletions src/parameter/filters/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
import { merge } from 'smob';
import { ObjectLiteral } from '../../type';
import { FiltersBuildInput } from './type';
import { FilterOperator } from './constants';
import { FilterInputOperatorValue } from './constants';
import { isFilterValueConfig } from './utils';
import { flattenNestedObject } from '../../utils';

const OperatorWeight = {
[FilterOperator.NEGATION]: 0,
[FilterOperator.LIKE]: 50,
[FilterOperator.LESS_THAN_EQUAL]: 150,
[FilterOperator.LESS_THAN]: 450,
[FilterOperator.MORE_THAN_EQUAL]: 1350,
[FilterOperator.MORE_THAN]: 4050,
[FilterOperator.IN]: 13105,
[FilterInputOperatorValue.NEGATION]: 0,
[FilterInputOperatorValue.LIKE]: 50,
[FilterInputOperatorValue.LESS_THAN_EQUAL]: 150,
[FilterInputOperatorValue.LESS_THAN]: 450,
[FilterInputOperatorValue.MORE_THAN_EQUAL]: 1350,
[FilterInputOperatorValue.MORE_THAN]: 4050,
[FilterInputOperatorValue.IN]: 13105,
};

export function buildQueryFilters<T extends ObjectLiteral = ObjectLiteral>(
Expand Down Expand Up @@ -58,7 +58,7 @@ export function buildQueryFilters<T extends ObjectLiteral = ObjectLiteral>(
// merge operators
input.operator = input.operator
.sort((a, b) => OperatorWeight[a] - OperatorWeight[b])
.join('') as FilterOperator;
.join('') as FilterInputOperatorValue;
}

output[key] = `${input.operator}${input.value}`;
Expand Down
21 changes: 12 additions & 9 deletions src/parameter/filters/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
* view the LICENSE file that was distributed with this source code.
*/

export enum FilterOperatorLabel {
NEGATION = 'negation',
LIKE = 'like',
LESS_THAN_EQUAL = 'lessThanEqual',
LESS_THAN = 'lessThan',
MORE_THAN_EQUAL = 'moreThanEqual',
MORE_THAN = 'moreThan',
IN = 'in',
export enum FilterComparisonOperator {
EQUAL = '$eq',
NOT_EQUAL = '$ne',
LIKE = '$l',
NOT_LIKE = '$nl',
LESS_THAN_EQUAL = '$lte',
LESS_THAN = '$lt',
GREATER_THAN_EQUAL = '$gte',
GREATER_THAN = '$gt',
IN = '$in',
NOT_IN = '$nin',
}

export enum FilterOperator {
export enum FilterInputOperatorValue {
NEGATION = '!',
LIKE = '~',
LESS_THAN_EQUAL = '<=',
Expand Down
27 changes: 13 additions & 14 deletions src/parameter/filters/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,31 @@ import {
hasOwnProperty, isFieldNonRelational, isFieldPathAllowedByRelations,
} from '../../utils';
import { isPathCoveredByParseAllowed } from '../utils';
import { FilterComparisonOperator } from './constants';
import { FiltersParseOptions, FiltersParseOutput, FiltersParseOutputElement } from './type';
import { determineFilterOperatorLabelsByValue, transformFilterValue } from './utils';
import { parseFilterValue, transformFilterValue } from './utils';

// --------------------------------------------------

function transformFiltersParseOutputElement(element: FiltersParseOutputElement) : FiltersParseOutputElement {
if (
hasOwnProperty(element, 'path') &&
(
typeof element.path === 'undefined' ||
element.path === null
)
(typeof element.path === 'undefined' || element.path === null)
) {
delete element.path;
}

if (typeof element.value === 'string') {
const { value, operators } = determineFilterOperatorLabelsByValue(element.value);
if (operators.length > 0) {
element.value = value;
element.operator = {};
if (element.operator) {
return element;
}

for (let i = 0; i < operators.length; i++) {
element.operator[operators[i]] = true;
}
}
if (typeof element.value === 'string') {
element = {
...element,
...parseFilterValue(element.value),
};
} else {
element.operator = FilterComparisonOperator.EQUAL;
}

element.value = transformFilterValue(element.value);
Expand Down
8 changes: 3 additions & 5 deletions src/parameter/filters/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { RelationsParseOutput } from '../relations';
import {
ParseAllowedKeys,
} from '../type';
import { FilterOperator, FilterOperatorLabel } from './constants';
import { FilterComparisonOperator, FilterInputOperatorValue } from './constants';

// -----------------------------------------------------------

Expand All @@ -30,7 +30,7 @@ export type FilterValue<V extends FilterValueInput = FilterValueInput> = V exten
V;

export type FilterValueConfig<V extends FilterValueInput = FilterValueInput> = {
operator: `${FilterOperator}` | (`${FilterOperator}`)[];
operator: `${FilterInputOperatorValue}` | (`${FilterInputOperatorValue}`)[];
value: FilterValueSimple<V>
};

Expand Down Expand Up @@ -77,9 +77,7 @@ export type FiltersParseOptions<
};

export type FiltersParseOutputElement = {
operator?: {
[K in FilterOperatorLabel]?: boolean
},
operator?: `${FilterComparisonOperator}`,
value: FilterValueSimple,
key: string,
path?: string
Expand Down
130 changes: 81 additions & 49 deletions src/parameter/filters/utils/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,97 @@
* view the LICENSE file that was distributed with this source code.
*/

import {
FilterValueConfig,
} from '../type';
import { hasOwnProperty, isSimpleValue } from '../../../utils';
import { FilterOperator, FilterOperatorLabel } from '../constants';

const config : {
sign: FilterOperator | `${FilterOperator}`,
label: FilterOperatorLabel | `${FilterOperatorLabel}`
}[] = [];

const operatorKeys = Object.keys(FilterOperator) as (keyof typeof FilterOperator)[];
for (let i = 0; i < operatorKeys.length; i++) {
config.push({
sign: FilterOperator[operatorKeys[i]],
label: FilterOperatorLabel[operatorKeys[i]],
});
import { FilterComparisonOperator, FilterInputOperatorValue } from '../constants';
import { FilterValueConfig } from '../type';

function matchOperator(key: string, value: string, position: 'start' | 'end' | 'global') : string | undefined {
switch (position) {
case 'start': {
if (value.substring(0, key.length) === key) {
return value.substring(key.length);
}
break;
}
case 'end': {
if (value.substring(0 - key.length) === key) {
return value.substring(0, value.length - key.length - 1);
}
break;
}
}

return undefined;
}

export function determineFilterOperatorLabelsByValue(input: string) : {
operators: (`${FilterOperatorLabel}`)[],
export function parseFilterValue(input: string) : {
operator: `${FilterComparisonOperator}`,
value: string | string[]
} {
let value : string[] | string = input;
let negation = false;

const operators : (`${FilterOperatorLabel}`)[] = [];
let value = matchOperator(FilterInputOperatorValue.NEGATION, input, 'start');
if (typeof value !== 'undefined') {
negation = true;
input = value;
}

for (let i = 0; i < config.length; i++) {
if (typeof value !== 'string') {
// eslint-disable-next-line no-continue
continue;
}
value = matchOperator(FilterInputOperatorValue.LIKE, input, 'start');
if (typeof value !== 'undefined') {
return {
value,
operator: negation ?
FilterComparisonOperator.NOT_LIKE :
FilterComparisonOperator.LIKE,
};
}

switch (config[i].sign) {
case FilterOperator.IN:
if (
value.includes(config[i].sign)
) {
operators.push(config[i].label);
value = value.split(config[i].sign);
}
break;
default:
if (
value.slice(0, config[i].sign.length) === config[i].sign
) {
operators.push(config[i].label);
value = value.slice(config[i].sign.length);
if (value.toLowerCase() === 'null') {
value = null;
}
}
break;
}
value = matchOperator(FilterInputOperatorValue.LESS_THAN_EQUAL, input, 'start');
if (typeof value !== 'undefined') {
return {
value,
operator: FilterComparisonOperator.LESS_THAN_EQUAL,
};
}

value = matchOperator(FilterInputOperatorValue.LESS_THAN, input, 'start');
if (typeof value !== 'undefined') {
return {
value,
operator: FilterComparisonOperator.LESS_THAN,
};
}

value = matchOperator(FilterInputOperatorValue.MORE_THAN_EQUAL, input, 'start');
if (typeof value !== 'undefined') {
return {
value,
operator: FilterComparisonOperator.GREATER_THAN_EQUAL,
};
}

value = matchOperator(FilterInputOperatorValue.MORE_THAN, input, 'start');
if (typeof value !== 'undefined') {
return {
value,
operator: FilterComparisonOperator.GREATER_THAN,
};
}

if (input.includes(FilterInputOperatorValue.IN)) {
return {
value: input.split(FilterInputOperatorValue.IN),
operator: negation ?
FilterComparisonOperator.NOT_IN :
FilterComparisonOperator.IN,
};
}

return {
operators,
value,
value: input,
operator: negation ?
FilterComparisonOperator.NOT_EQUAL :
FilterComparisonOperator.EQUAL,
};
}

Expand All @@ -73,7 +105,7 @@ export function isFilterValueConfig(data: unknown) : data is FilterValueConfig<a
}

if (hasOwnProperty(data, 'operator')) {
const operators : string[] = Object.values(FilterOperator);
const operators : string[] = Object.values(FilterInputOperatorValue);

if (typeof data.operator === 'string') {
if (operators.indexOf(data.operator) === -1) {
Expand Down
31 changes: 18 additions & 13 deletions src/parameter/filters/utils/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@
import { FilterValue } from '../type';

export function transformFilterValue(input: FilterValue) : FilterValue {
if (Array.isArray(input)) {
for (let i = 0; i < input.length; i++) {
input[i] = transformFilterValue(input[i]) as string | number;
}

return (input as unknown[])
.filter((n) => n === 0 || !!n) as FilterValue;
}

if (typeof input === 'undefined' || input === null) {
return null;
}

if (typeof input === 'string') {
const lower = input.trim().toLowerCase();

Expand All @@ -40,6 +27,24 @@ export function transformFilterValue(input: FilterValue) : FilterValue {
if (!Number.isNaN(num)) {
return num;
}

const parts = input.split(',');
if (parts.length > 1) {
return transformFilterValue(parts);
}
}

if (Array.isArray(input)) {
for (let i = 0; i < input.length; i++) {
input[i] = transformFilterValue(input[i]) as string | number;
}

return (input as unknown[])
.filter((n) => n === 0 || !!n) as FilterValue;
}

if (typeof input === 'undefined' || input === null) {
return null;
}

return input;
Expand Down
Loading

0 comments on commit 0f3ec4e

Please sign in to comment.