Skip to content

Commit

Permalink
fix: remove filter-value-config format
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 21, 2022
1 parent acbc090 commit a7172b0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 170 deletions.
34 changes: 0 additions & 34 deletions src/parameter/filters/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,8 @@
import { merge } from 'smob';
import { ObjectLiteral } from '../../type';
import { FiltersBuildInput } from './type';
import { FilterInputOperatorValue } from './constants';
import { isFilterValueConfig } from './utils';
import { flattenNestedObject } from '../../utils';

const OperatorWeight = {
[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>(
data?: FiltersBuildInput<T>,
) : Record<string, any> {
Expand All @@ -44,28 +32,6 @@ export function buildQueryFilters<T extends ObjectLiteral = ObjectLiteral>(
return true;
}

if (isFilterValueConfig(input)) {
if (typeof input.value === 'undefined') {
input.value = null;
}

if (Array.isArray(input.value)) {
// todo: check array elements are string
input.value = input.value.join(',');
}

if (Array.isArray(input.operator)) {
// merge operators
input.operator = input.operator
.sort((a, b) => OperatorWeight[a] - OperatorWeight[b])
.join('') as FilterInputOperatorValue;
}

output[key] = `${input.operator}${input.value}`;

return true;
}

return undefined;
},
});
Expand Down
15 changes: 4 additions & 11 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 { FilterComparisonOperator, FilterInputOperatorValue } from './constants';
import { FilterComparisonOperator } from './constants';

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

Expand All @@ -22,25 +22,18 @@ type FilterValueInput = FilterValueInputPrimitive | null | undefined;

export type FilterValueSimple<V extends FilterValueInput = FilterValueInput> = V extends string | number ? (V | V[]) : V;
export type FilterValueWithOperator<V extends FilterValueInput = FilterValueInput> = V extends string | number ?
`!${V}` | `!~${V}` | `~${V}` | `<${V}` | `<=${V}` | `>${V}` | `>=${V}` | null | '!null' :
V extends boolean ? null | '!null' : never;
V | `!${V}` | `!~${V}` | `~${V}` | `<${V}` | `<=${V}` | `>${V}` | `>=${V}` | null | '!null' :
V extends boolean ? V | null | '!null' : never;

export type FilterValue<V extends FilterValueInput = FilterValueInput> = V extends string | number ?
(FilterValueSimple<V> | FilterValueWithOperator<V> | Array<FilterValueWithOperator<V>>) :
V;

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

// -----------------------------------------------------------
// Build
// -----------------------------------------------------------

export type FiltersBuildInputValue<T> = T extends OnlyScalar<T> ?
T | FilterValue<T> | FilterValueConfig<T> :
never;
export type FiltersBuildInputValue<T> = T extends OnlyScalar<T> ? T | FilterValue<T> : never;

export type FiltersBuildInput<T extends Record<string, any>> = {
[K in keyof T]?: Flatten<T[K]> extends Record<string, any> ?
Expand Down
55 changes: 0 additions & 55 deletions src/parameter/filters/utils/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* view the LICENSE file that was distributed with this source code.
*/

import { hasOwnProperty, isSimpleValue } from '../../../utils';
import { FilterComparisonOperator, FilterInputOperatorValue } from '../constants';
import { FilterValueConfig } from '../type';

function matchOperator(key: string, value: string, position: 'start' | 'end' | 'global') : string | undefined {
switch (position) {
Expand Down Expand Up @@ -98,56 +96,3 @@ export function parseFilterValue(input: string) : {
FilterComparisonOperator.EQUAL,
};
}

export function isFilterValueConfig(data: unknown) : data is FilterValueConfig<any> {
if (typeof data !== 'object' || data === null) {
return false;
}

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

if (typeof data.operator === 'string') {
if (operators.indexOf(data.operator) === -1) {
return false;
}
} else if (Array.isArray(data.operator)) {
for (let i = 0; i < data.operator.length; i++) {
if (typeof data.operator[i] !== 'string') {
return false;
}

if (operators.indexOf(data.operator[i]) === -1) {
return false;
}
}
} else {
return false;
}
} else {
return false;
}

if (hasOwnProperty(data, 'value')) {
if (
!isSimpleValue(data.value, {
withNull: true,
withUndefined: true,
})
) {
if (Array.isArray(data.value)) {
for (let i = 0; i < data.value.length; i++) {
if (!isSimpleValue(data.value[i])) {
return false;
}
}
} else {
return false;
}
}
} else {
return false;
}

return true;
}
6 changes: 3 additions & 3 deletions src/parameter/filters/utils/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* view the LICENSE file that was distributed with this source code.
*/

import { FilterValue } from '../type';
import { FilterValue, FilterValueSimple } from '../type';

export function transformFilterValue(input: FilterValue) : FilterValue {
export function transformFilterValue(input: FilterValue) : FilterValueSimple {
if (typeof input === 'string') {
const lower = input.trim().toLowerCase();

Expand Down Expand Up @@ -40,7 +40,7 @@ export function transformFilterValue(input: FilterValue) : FilterValue {
}

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

if (typeof input === 'undefined' || input === null) {
Expand Down
50 changes: 7 additions & 43 deletions test/unit/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import {
FilterInputOperatorValue, Parameter, SortDirection, URLParameter, buildQuery, DEFAULT_ID,
Parameter, SortDirection, URLParameter, buildQuery, DEFAULT_ID,
} from '../../src';
import { buildURLQueryString } from '../../src/utils';

Expand Down Expand Up @@ -97,54 +97,39 @@ describe('src/build.ts', () => {

record = buildQuery<Entity>({
filter: {
id: {
operator: FilterInputOperatorValue.LIKE,
value: 1,
},
id: '~1',
},
});
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '~1' } }));

// with lessThan
record = buildQuery<Entity>({
filter: {
id: {
operator: FilterInputOperatorValue.LESS_THAN,
value: 1,
},
id: '<1',
},
});
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '<1' } }));

// with lessThanEqual
record = buildQuery<Entity>({
filter: {
id: {
operator: FilterInputOperatorValue.LESS_THAN_EQUAL,
value: 1,
},
id: '<=1',
},
});
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '<=1' } }));

// with moreThan
record = buildQuery<Entity>({
filter: {
id: {
operator: FilterInputOperatorValue.MORE_THAN,
value: 1,
},
id: '>1',
},
});
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '>1' } }));

// with moreThanEqual
record = buildQuery<Entity>({
filter: {
id: {
operator: FilterInputOperatorValue.MORE_THAN_EQUAL,
value: 1,
},
id: '>=1',
},
});
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '>=1' } }));
Expand All @@ -160,30 +145,9 @@ describe('src/build.ts', () => {
// with negation & like operator
record = buildQuery<Entity>({
filter: {
id: {
operator: [
FilterInputOperatorValue.NEGATION,
FilterInputOperatorValue.LIKE,
],
value: [1, 2, 3],
},
},
});
expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '!~1,2,3' } }));

// with wrong operator order :)
record = buildQuery<Entity>({
filter: {
id: {
operator: [
FilterInputOperatorValue.LIKE,
FilterInputOperatorValue.NEGATION,
],
value: [1, 2, 3],
},
id: ['!~1', 2, 3],
},
});

expect(record).toEqual(buildURLQueryString({ [URLParameter.FILTERS]: { id: '!~1,2,3' } }));
});

Expand Down
24 changes: 0 additions & 24 deletions test/unit/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import {
isFilterValueConfig,
FiltersParseOptions,
FiltersParseOutput,
parseQueryFilters,
Expand Down Expand Up @@ -385,27 +384,4 @@ describe('src/filter/index.ts', () => {
},
] as FiltersParseOutput);
});

it('should determine filter operator config', () => {
let data = isFilterValueConfig({
value: 1,
operator: '<'
});

expect(data).toBeTruthy();

data = isFilterValueConfig({
value: 1,
operator: {}
})

expect(data).toBeFalsy();

data = isFilterValueConfig({
value: {},
operator: '<'
});

expect(data).toBeFalsy();
})
});

0 comments on commit a7172b0

Please sign in to comment.