diff --git a/src/components/picker.js b/src/components/picker.js index eb2f37b..4a1d6fe 100644 --- a/src/components/picker.js +++ b/src/components/picker.js @@ -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} /> diff --git a/src/components/search.js b/src/components/search.js index 4da6f20..c625442 100644 --- a/src/components/search.js +++ b/src/components/search.js @@ -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() { diff --git a/src/utils/emoji-index.js b/src/utils/emoji-index.js index d4a5164..7f48063 100644 --- a/src/utils/emoji-index.js +++ b/src/utils/emoji-index.js @@ -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 diff --git a/src/utils/emoji-indexSpec.js b/src/utils/emoji-indexSpec.js index 41f5eda..011c1f0 100644 --- a/src/utils/emoji-indexSpec.js +++ b/src/utils/emoji-indexSpec.js @@ -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([]) + }) }); });