Handle include/exclude in search results

release
Etienne Lemay 2017-04-18 11:00:20 -04:00
parent 6a3b05d85e
commit ebd3c6f6f0
4 changed files with 37 additions and 7 deletions

View File

@ -292,7 +292,7 @@ export default class Picker extends React.Component {
} }
render() { 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, { skin } = this.state,
width = (perLine * (emojiSize + 12)) + 12 + 2 width = (perLine * (emojiSize + 12)) + 12 + 2
@ -313,6 +313,8 @@ export default class Picker extends React.Component {
onSearch={this.handleSearch.bind(this)} onSearch={this.handleSearch.bind(this)}
i18n={this.i18n} i18n={this.i18n}
emojisToShowFilter={emojisToShowFilter} emojisToShowFilter={emojisToShowFilter}
include={include}
exclude={exclude}
autoFocus={autoFocus} autoFocus={autoFocus}
/> />

View File

@ -6,7 +6,12 @@ export default class Search extends React.Component {
var { input } = this.refs, var { input } = this.refs,
value = input.value 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() { clear() {

View File

@ -1,5 +1,6 @@
import data from '../../data' const extend = require('util')._extend
import data from '../../data'
import { getSanitizedData, intersect } from '.' import { getSanitizedData, intersect } from '.'
var index = {} var index = {}
@ -20,8 +21,11 @@ for (let emoji in data.emojis) {
emojisList[id] = getSanitizedData(id) emojisList[id] = getSanitizedData(id)
} }
function search(value, emojisToShowFilter = () => true, maxResults = 75) { function search(value, { emojisToShowFilter, maxResults, include, exclude } = {}) {
var results = null maxResults || (maxResults = 75)
var results = null,
pool = data.emojis
if (value.length) { if (value.length) {
var values = value.toLowerCase().split(/[\s|,|\-|_]+/), var values = value.toLowerCase().split(/[\s|,|\-|_]+/),
@ -31,8 +35,22 @@ function search(value, emojisToShowFilter = () => true, maxResults = 75) {
values = [values[0], values[1]] 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) => { allResults = values.map((value) => {
var aPool = data.emojis, var aPool = pool,
aIndex = index, aIndex = index,
length = 0 length = 0

View File

@ -16,8 +16,13 @@ describe('#emojiIndex', () => {
it('should filter only emojis we care about, exclude pineapple', () => { it('should filter only emojis we care about, exclude pineapple', () => {
let emojisToShowFilter = (unified) => unified !== '1F34D'; 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'); .not.toContain('pineapple');
}); });
it('can include/exclude categories', () => {
expect(emojiIndex.search('flag', { include: ['people'] }))
.toEqual([])
})
}); });
}); });