Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for nested object search #45

Merged
merged 2 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions __test__/filters/equal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ const searchDataArray = {
};

describe('test caseSensitive flag', () => {
test('return false if key is incorrect', () => {
const result = equal(
{
key: 'notExist',
value: 'Ben',
type: SearchType.EQ,
},
true,
searchDataString,
);

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

test('caseSensitive=true(same string, different alphabet case) -> false', () => {
const result = equal(
{
Expand Down
14 changes: 14 additions & 0 deletions __test__/filters/greater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ const searchDataArray = {
};

describe('test caseSensitive flag', () => {
test('return false if key is incorrect', () => {
const result = greater(
{
key: 'notExist',
value: 'Ben',
type: SearchType.GT,
},
true,
searchDataString,
);

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

test('caseSensitive=true(different alphabet case, compare to lowercase) -> false', () => {
const result = greater(
{
Expand Down
14 changes: 14 additions & 0 deletions __test__/filters/like.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ const searchDataArray = {
};

describe('test caseSensitive flag', () => {
test('return false if key is incorrect', () => {
const result = like(
{
key: 'notExist',
value: 'Ben',
type: SearchType.LK,
},
true,
searchDataString,
);

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

test('caseSensitive=true(partial same, different alphabet case) -> false', () => {
const result = like(
{
Expand Down
28 changes: 28 additions & 0 deletions __test__/lib/utils.getObjValue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getObjValue } from '../../src/lib/utils';
import type { DataObjectWithNull } from '../../src/lib/types';

const data: DataObjectWithNull = {
name: 'John',
address: null,
father: {
name: 'Jack',
age: 50,
},
};

test('key is not exist', () => {
expect(getObjValue(data, 'notExist')).toBeUndefined();
expect(getObjValue(data, ['father', 'notExist'])).toBeUndefined();
});

test('key is string', () => {
expect(getObjValue(data, 'name')).toBe('John');
expect(getObjValue(data, 'address')).toBeNull();
expect(getObjValue(data, 'father.name')).toBe('Jack');
});

test('key is string[]', () => {
expect(getObjValue(data, ['name'])).toBe('John');
expect(getObjValue(data, ['address'])).toBeNull();
expect(getObjValue(data, ['father', 'name'])).toBe('Jack');
});
5 changes: 4 additions & 1 deletion src/filters/equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const equal = (
caseSensitive: boolean,
data: DataObject,
): boolean => {
const targetValue = getObjValue(data, key) || '';
const targetValue = getObjValue(data, key);
if (targetValue == null) {
return false;
}

if (typeof targetValue === 'number') {
return targetValue === Number(value);
Expand Down
5 changes: 4 additions & 1 deletion src/filters/greater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const greater = (
caseSensitive: boolean,
data: DataObject,
): boolean => {
const targetValue = getObjValue(data, key) || '';
const targetValue = getObjValue(data, key);
if (targetValue == null) {
return false;
}

// disable like search if targetValue is array
if (Array.isArray(targetValue)) {
Expand Down
5 changes: 4 additions & 1 deletion src/filters/like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const like = (
data: DataObject,
): boolean => {
const { key, value } = searchCondition;
const targetValue = getObjValue(data, key) || '';
const targetValue = getObjValue(data, key);
if (targetValue == null) {
return false;
}

// disable like search if targetValue is array
if (Array.isArray(targetValue)) {
Expand Down
11 changes: 3 additions & 8 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ export interface SearchConditionMultiple {

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

export type DataObjectWithNullValues =
| string
| number
| (string | number)[]
| undefined
| null;
export type DataObjectWithNullValues = DataObjectValues | null;

export interface DataObjectWithNull {
[key: string]: DataObjectWithNullValues;
[key: string]: DataObjectWithNullValues | DataObjectWithNull;
}

export type Predicator = (data: DataObject) => boolean;
6 changes: 4 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ function getObjValue(
data: DataObjectWithNull,
key: string | string[],
): DataObjectValues | undefined {
return path<DataObjectValues>(typeof key === 'string' ? [key] : key, data);
return path<DataObjectValues>(
typeof key === 'string' ? key.split('.') : key,
data,
);
}

export {
Expand All @@ -52,6 +55,5 @@ export {
anyPass,
drop,
take,
path,
getObjValue,
};
3 changes: 1 addition & 2 deletions src/prefilters/targetValueNull.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { curry, getObjValue, path } from '../lib/utils';
import { curry, getObjValue } from '../lib/utils';
import {
DataObjectWithNull,
Predicator,
SearchCondition,
DataObject,
DataObjectWithNullValues,
} from '../lib/types';

/**
Expand Down