@sparklink-pro/apant
Version:
Apollo & Antd tools
32 lines • 1.19 kB
JavaScript
import { get, isObject, isArray, deburr, toLower } from 'lodash-es';
/**
* Check if `value` contains `search` after setting both value to lower case, having their accents removed and removing all spaces.
*/
export const executeSearch = (search, value) => {
const searchValue = deburr(toLower(search.replace(/\s/g, '')));
const searching = deburr(toLower(value.replace(/\s/g, '')));
return searching.indexOf(searchValue) !== -1;
};
/**
* Extract and concatenate the properties value from the given object.
* Handle arrays and objects.
* For example:
* Given the object:
```js
{ id: 1, comments: [{ text: 'hello' }, { text: 'world' }] }
```
* The extracted value of the property `comments.text` will be `hello world`
*/
export const extractValue = (searchItem, property) => {
const path = property.split('.');
const prop = path.shift();
const propValue = prop ? get(searchItem, prop, '') : '';
if (isArray(propValue)) {
return propValue.map((o) => extractValue(o, path.join('.'))).join(' ');
}
if (isObject(propValue)) {
return extractValue(propValue, path.join('.'));
}
return propValue;
};
//# sourceMappingURL=search.js.map