Skip to content

Commit

Permalink
feat: support boolean search (#110)
Browse files Browse the repository at this point in the history
* feat: support boolean search

* fix: boolean search problem in caseSensitive mode

* chore: add changeset

* revert: return false when type is different in caseSensitive mode
  • Loading branch information
thundermiracle authored Aug 14, 2023
1 parent 0d01fbb commit 719e5ad
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-hornets-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"filter-data": minor
---

feat: support boolean search
66 changes: 65 additions & 1 deletion __test__/filters/equal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const searchDataArray = {
fullName: ['Ben', 'Jackson'],
};

const searchDataBoolean = {
married: true,
};

const searchDataArrayBoolean = {
married: [true, true],
};

describe('test caseSensitive flag', () => {
test('return false if key is incorrect', () => {
const result = equal(
Expand Down Expand Up @@ -146,7 +154,7 @@ describe('test searchCondition', () => {
expect(result).toBe(true);
});

test('targetvalue is array, different alphabet case -> true', () => {
test('target value is array, different alphabet case -> true', () => {
const result = equal(
{
key: 'fullName',
Expand Down Expand Up @@ -229,4 +237,60 @@ describe('test searchCondition', () => {

expect(result).toBe(true);
});

test('target value is boolean string and search value is boolean -> true', () => {
const result = equal(
{
key: 'married',
value: true,
type: SearchType.EQ,
},
false,
searchDataBoolean,
);

expect(result).toBe(true);
});

test('target value is array of boolean string and search value is boolean -> true', () => {
const result = equal(
{
key: 'married',
value: true,
type: SearchType.EQ,
},
false,
searchDataArrayBoolean,
);

expect(result).toBe(true);
});

test('target value is boolean string and search value is boolean -> false', () => {
const result = equal(
{
key: 'married',
value: false,
type: SearchType.EQ,
},
false,
searchDataBoolean,
);

expect(result).toBe(false);
});

test('target value is array of boolean string and search value is boolean -> false', () => {
const result = equal(
{
key: 'married',
value: false,
type: SearchType.EQ,
},
false,
searchDataArrayBoolean,
);

expect(result).toBe(false);
});
});
64 changes: 64 additions & 0 deletions __test__/filters/notEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const searchDataArray = {
fullName: ['Ben', 'Jackson'],
};

const searchDataBoolean = {
married: true,
};

const searchDataArrayBoolean = {
married: [true, true],
};

describe('test caseSensitive flag', () => {
test('caseSensitive=true(same string, different alphabet case) -> true', () => {
const result = notEqual(
Expand Down Expand Up @@ -187,4 +195,60 @@ describe('test searchCondition', () => {

expect(result).toBe(false);
});

test('target value is boolean string and search value is boolean -> true', () => {
const result = notEqual(
{
key: 'married',
value: true,
type: SearchType.EQ,
},
false,
searchDataBoolean,
);

expect(result).toBe(false);
});

test('target value is array of boolean string and search value is boolean -> true', () => {
const result = notEqual(
{
key: 'married',
value: true,
type: SearchType.EQ,
},
false,
searchDataArrayBoolean,
);

expect(result).toBe(false);
});

test('target value is boolean string and search value is boolean -> false', () => {
const result = notEqual(
{
key: 'married',
value: false,
type: SearchType.EQ,
},
false,
searchDataBoolean,
);

expect(result).toBe(true);
});

test('target value is array of boolean string and search value is boolean -> false', () => {
const result = notEqual(
{
key: 'married',
value: false,
type: SearchType.EQ,
},
false,
searchDataArrayBoolean,
);

expect(result).toBe(true);
});
});
6 changes: 3 additions & 3 deletions src/filters/equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ const equal: FilterFunction = ({ key, value }, caseSensitive, data) => {

if (Array.isArray(targetValue)) {
if (caseSensitive) {
return targetValue.includes(value);
return targetValue.includes(value.toString());
}

return targetValue
.map((x: string | number) => x.toString().toUpperCase())
.map((x) => x.toString().toUpperCase())
.includes(value.toString().toUpperCase());
}

if (caseSensitive) {
return targetValue === value.toString();
}

return targetValue.toUpperCase() === value.toString().toUpperCase();
return targetValue.toString().toUpperCase() === value.toString().toUpperCase();
};

export default equal;
2 changes: 1 addition & 1 deletion src/filters/greater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const greater: FilterFunction = ({ key, value }, caseSensitive, data) => {
return targetValue > value;
}

return targetValue.toUpperCase() > value.toString().toUpperCase();
return targetValue.toString().toUpperCase() > value.toString().toUpperCase();
};

export default greater;
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface FilterDataOption {

export interface SearchCondition {
key: string | string[];
value: string | number;
value: string | number | boolean;
type: SearchType;
}

Expand All @@ -19,7 +19,7 @@ export interface SearchConditionMultiple {
type: SearchType;
}

export type DataObjectValues = string | number | (string | number)[];
export type DataObjectValues = string | number | boolean | (string | number | boolean)[];
export interface DataObject {
[key: string]: DataObjectValues | DataObject;
}
Expand Down

0 comments on commit 719e5ad

Please sign in to comment.