Handle include/exclude in search results
parent
6a3b05d85e
commit
ebd3c6f6f0
|
@ -292,7 +292,7 @@ export default class Picker extends React.Component {
|
|||
}
|
||||
|
||||
render() {
|
||||
var { perLine, emojiSize, set, sheetSize, style, title, emoji, color, native, backgroundImageFn, emojisToShowFilter, autoFocus } = this.props,
|
||||
var { perLine, emojiSize, set, sheetSize, style, title, emoji, color, native, backgroundImageFn, emojisToShowFilter, include, exclude, autoFocus } = this.props,
|
||||
{ skin } = this.state,
|
||||
width = (perLine * (emojiSize + 12)) + 12 + 2
|
||||
|
||||
|
@ -313,6 +313,8 @@ export default class Picker extends React.Component {
|
|||
onSearch={this.handleSearch.bind(this)}
|
||||
i18n={this.i18n}
|
||||
emojisToShowFilter={emojisToShowFilter}
|
||||
include={include}
|
||||
exclude={exclude}
|
||||
autoFocus={autoFocus}
|
||||
/>
|
||||
|
||||
|
|
|
@ -6,7 +6,12 @@ export default class Search extends React.Component {
|
|||
var { input } = this.refs,
|
||||
value = input.value
|
||||
|
||||
this.props.onSearch(emojiIndex.search(value, this.props.emojisToShowFilter, this.props.maxResults))
|
||||
this.props.onSearch(emojiIndex.search(value, {
|
||||
emojisToShowFilter: this.props.emojisToShowFilter,
|
||||
maxResults: this.props.maxResults,
|
||||
include: this.props.include,
|
||||
exclude: this.props.exclude,
|
||||
}))
|
||||
}
|
||||
|
||||
clear() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import data from '../../data'
|
||||
const extend = require('util')._extend
|
||||
|
||||
import data from '../../data'
|
||||
import { getSanitizedData, intersect } from '.'
|
||||
|
||||
var index = {}
|
||||
|
@ -20,8 +21,11 @@ for (let emoji in data.emojis) {
|
|||
emojisList[id] = getSanitizedData(id)
|
||||
}
|
||||
|
||||
function search(value, emojisToShowFilter = () => true, maxResults = 75) {
|
||||
var results = null
|
||||
function search(value, { emojisToShowFilter, maxResults, include, exclude } = {}) {
|
||||
maxResults || (maxResults = 75)
|
||||
|
||||
var results = null,
|
||||
pool = data.emojis
|
||||
|
||||
if (value.length) {
|
||||
var values = value.toLowerCase().split(/[\s|,|\-|_]+/),
|
||||
|
@ -31,8 +35,22 @@ function search(value, emojisToShowFilter = () => true, maxResults = 75) {
|
|||
values = [values[0], values[1]]
|
||||
}
|
||||
|
||||
if ((include && include.length) || (exclude && exclude.length)) {
|
||||
pool = {}
|
||||
|
||||
for (let category of data.categories) {
|
||||
let isIncluded = include == undefined ? true : include.indexOf(category.name.toLowerCase()) > -1
|
||||
let isExcluded = exclude == undefined ? false : exclude.indexOf(category.name.toLowerCase()) > -1
|
||||
if (!isIncluded || isExcluded) { continue }
|
||||
|
||||
for (let emojiId of category.emojis) {
|
||||
pool[emojiId] = data.emojis[emojiId]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
allResults = values.map((value) => {
|
||||
var aPool = data.emojis,
|
||||
var aPool = pool,
|
||||
aIndex = index,
|
||||
length = 0
|
||||
|
||||
|
|
|
@ -16,8 +16,13 @@ describe('#emojiIndex', () => {
|
|||
|
||||
it('should filter only emojis we care about, exclude pineapple', () => {
|
||||
let emojisToShowFilter = (unified) => unified !== '1F34D';
|
||||
expect(emojiIndex.search('apple', emojisToShowFilter).map((obj) => obj.id))
|
||||
expect(emojiIndex.search('apple', { emojisToShowFilter }).map((obj) => obj.id))
|
||||
.not.toContain('pineapple');
|
||||
});
|
||||
|
||||
it('can include/exclude categories', () => {
|
||||
expect(emojiIndex.search('flag', { include: ['people'] }))
|
||||
.toEqual([])
|
||||
})
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue