Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"babel"
],
"testPathIgnorePatterns": [
"src/__tests__/utils"
"src/__tests__/test-utils"
]
},
"lint-staged": {
Expand Down
4 changes: 2 additions & 2 deletions src/PowerSelect/__tests__/PowerSelect-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// /* global describe, it, expect */
import React from 'react';
import sinon from 'sinon';
import PageObjectBase from '../../__tests__/utils/create-page-object';
import { frameworks, countries, KEY_CODES } from '../../__tests__/utils/constants';
import PageObjectBase from '../../__tests__/test-utils/create-page-object';
import { frameworks, countries, KEY_CODES } from '../../__tests__/test-utils/constants';
import PowerSelect from '../index';

class PowerSelectPageObject extends PageObjectBase {
Expand Down
4 changes: 2 additions & 2 deletions src/PowerSelectMultiple/__tests__/PowerSelectMultiple-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// /* global describe, it, expect */
import React from 'react';
import sinon from 'sinon';
import PageObjectBase from '../../__tests__/utils/create-page-object';
import { frameworks, countries, KEY_CODES } from '../../__tests__/utils/constants';
import PageObjectBase from '../../__tests__/test-utils/create-page-object';
import { frameworks, countries, KEY_CODES } from '../../__tests__/test-utils/constants';
import PowerSelectMultiple from '../index';

class PowerSelectMultiplePageObject extends PageObjectBase {
Expand Down
4 changes: 2 additions & 2 deletions src/TypeAhead/__tests__/TypeAhead-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// /* global describe, it, expect */
import React from 'react';
import sinon from 'sinon';
import PageObjectBase from '../../__tests__/utils/create-page-object';
import { frameworks, countries, KEY_CODES } from '../../__tests__/utils/constants';
import PageObjectBase from '../../__tests__/test-utils/create-page-object';
import { frameworks, countries, KEY_CODES } from '../../__tests__/test-utils/constants';
import TypeAhead from '../index';

class TypeAheadPageObject extends PageObjectBase {
Expand Down
File renamed without changes.
56 changes: 56 additions & 0 deletions src/__tests__/utils/matcher-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// /* global describe, it, expect */
import { matcher } from './../../utils';

describe('matcher()', () => {
it('should match string', () => {
const option = 'option';
expect(matcher({ option, searchTerm: '' })).toBe(true);
expect(matcher({ option, searchTerm: 'opt' })).toBe(true);
expect(matcher({ option, searchTerm: 'opt ' })).toBe(true);
expect(matcher({ option, searchTerm: 'a' })).toBe(false);
});
it('should match number', () => {
const option = 1234567;
expect(matcher({ option, searchTerm: '' })).toBe(true);
expect(matcher({ option, searchTerm: '12' })).toBe(true);
expect(matcher({ option, searchTerm: '12 ' })).toBe(true);
expect(matcher({ option, searchTerm: '9' })).toBe(false);
});
it('should match not undefined', () => {
const option = undefined;
expect(matcher({ option, searchTerm: '' })).toBe(false);
expect(matcher({ option, searchTerm: 'opt' })).toBe(false);
expect(matcher({ option, searchTerm: '12' })).toBe(false);
});
it('should match not null', () => {
const option = null;
expect(matcher({ option, searchTerm: '' })).toBe(false);
expect(matcher({ option, searchTerm: 'opt' })).toBe(false);
expect(matcher({ option, searchTerm: '12' })).toBe(false);
});

const option = { name: 'Matthew Stevens', age: 6, id: null };
it('should match string in object', () => {
expect(matcher({ option, searchTerm: '', searchIndices: 'name' })).toBe(true);
expect(matcher({ option, searchTerm: 'mAt', searchIndices: 'name' })).toBe(true);
expect(matcher({ option, searchTerm: 'mat', searchIndices: 'name' })).toBe(true);
expect(matcher({ option, searchTerm: 'mat ', searchIndices: 'name' })).toBe(true);
expect(matcher({ option, searchTerm: 'b', searchIndices: 'name' })).toBe(false);
});
it('should match number in object', () => {
expect(matcher({ option, searchTerm: '', searchIndices: 'age' })).toBe(true);
expect(matcher({ option, searchTerm: '6', searchIndices: 'age' })).toBe(true);
expect(matcher({ option, searchTerm: '6 ', searchIndices: 'age' })).toBe(true);
expect(matcher({ option, searchTerm: '1', searchIndices: 'age' })).toBe(false);
});
it('should match not undefined in object', () => {
expect(matcher({ option, searchTerm: '', searchIndices: 'gender' })).toBe(false);
expect(matcher({ option, searchTerm: 'opt', searchIndices: 'gender' })).toBe(false);
expect(matcher({ option, searchTerm: '12', searchIndices: 'gender' })).toBe(false);
});
it('should match not null in object', () => {
expect(matcher({ option, searchTerm: '', searchIndices: 'id' })).toBe(false);
expect(matcher({ option, searchTerm: 'opt', searchIndices: 'id' })).toBe(false);
expect(matcher({ option, searchTerm: '12', searchIndices: 'id' })).toBe(false);
});
});
22 changes: 18 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@ import React, { isValidElement, cloneElement } from 'react';

export const matcher = ({ option, searchTerm = '', searchIndices }) => {
searchTerm = searchTerm.trim().toLowerCase();
if (typeof option === 'string') {
return option.toLowerCase().indexOf(searchTerm) !== -1;
}

if (searchIndices) {
return makeArray(searchIndices).some(index => {
return (option[index] || '').toLowerCase().indexOf(searchTerm) !== -1;
let value = option[index];
return (
!isNone(value) &&
String(value)
.toLowerCase()
.indexOf(searchTerm) !== -1
);
});
}

return (
!isNone(option) &&
String(option)
.toLowerCase()
.indexOf(searchTerm) !== -1
);

return true;
};

export const isNone = value => value === null || value === undefined;

export const makeArray = obj => {
if (obj === null || obj === undefined) {
return [];
Expand Down