diff --git a/package.json b/package.json index 6306404f3..19b13b982 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "chalk": "^3.0.0", "conf": "^6.1.0", "execa": "^4.0.0", + "fuse.js": "^3.4.6", "inquirer": "^7.0.0", "inquirer-autocomplete-prompt": "^1.0.1", "meow": "^6.0.0", diff --git a/src/commands/commit/prompts.js b/src/commands/commit/prompts.js index 9d35700c3..bae5ecfb9 100644 --- a/src/commands/commit/prompts.js +++ b/src/commands/commit/prompts.js @@ -2,13 +2,14 @@ import inquirer from 'inquirer' import configurationVault from '../../utils/configurationVault' +import filterGitmojis from '../../utils/filterGitmojis' import guard from './guard' const TITLE_MAX_LENGTH_COUNT: number = 48 inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt')) -type Gitmoji = { +export type Gitmoji = { code: string, description: string, emoji: string, @@ -29,15 +30,10 @@ export default (gitmojis: Array): Array => [ type: 'autocomplete', source: (answersSoFor: any, input: string) => { return Promise.resolve( - gitmojis - .filter((gitmoji) => { - const emoji = gitmoji.name.concat(gitmoji.description).toLowerCase() - return !input || emoji.indexOf(input.toLowerCase()) !== -1 - }) - .map((gitmoji) => ({ - name: `${gitmoji.emoji} - ${gitmoji.description}`, - value: gitmoji[configurationVault.getEmojiFormat()] - })) + filterGitmojis(input, gitmojis).map((gitmoji) => ({ + name: `${gitmoji.emoji} - ${gitmoji.description}`, + value: gitmoji[configurationVault.getEmojiFormat()] + })) ) } }, diff --git a/src/commands/search/index.js b/src/commands/search/index.js index 8ce538114..cc39ef0ac 100644 --- a/src/commands/search/index.js +++ b/src/commands/search/index.js @@ -1,16 +1,12 @@ // @flow +import filterGitmojis from '../../utils/filterGitmojis' import getEmojis from '../../utils/getEmojis' import printEmojis from '../../utils/printEmojis' const search = (query: string) => { console.log('query', query) return getEmojis() - .then((gitmojis) => { - return gitmojis.filter((gitmoji) => { - const emoji = gitmoji.name.concat(gitmoji.description).toLowerCase() - return emoji.indexOf(query.toLowerCase()) !== -1 - }) - }) + .then((gitmojis) => filterGitmojis(query, gitmojis)) .then((gitmojisFiltered) => printEmojis(gitmojisFiltered)) .catch((err) => console.error(err)) } diff --git a/src/utils/filterGitmojis.js b/src/utils/filterGitmojis.js new file mode 100644 index 000000000..d7ff168ab --- /dev/null +++ b/src/utils/filterGitmojis.js @@ -0,0 +1,24 @@ +// @flow +import Fuse from 'fuse.js' + +import { type Gitmoji } from '../commands/commit/prompts' + +const options = { + keys: [ + { + name: 'name', + weight: 0.5 + }, + { + name: 'description', + weight: 1 + } + ] +} + +const filterGitmojis = (input: ?string, gitmojis: Array) => { + const fuse = new Fuse(gitmojis, options) + return input ? fuse.search(input) : gitmojis +} + +export default filterGitmojis diff --git a/src/utils/printEmojis.js b/src/utils/printEmojis.js index 19404954c..49910d6af 100644 --- a/src/utils/printEmojis.js +++ b/src/utils/printEmojis.js @@ -4,6 +4,7 @@ import chalk from 'chalk' type Gitmoji = { emoji: string, code: string, + name: string, description: string } diff --git a/test/utils/filterGitmojis.spec.js b/test/utils/filterGitmojis.spec.js new file mode 100644 index 000000000..7fb41975e --- /dev/null +++ b/test/utils/filterGitmojis.spec.js @@ -0,0 +1,16 @@ +import filterGitmojis from '../../src/utils/filterGitmojis' +import * as stubs from './stubs' + +describe('filterGirmojis', () => { + it('should find all gitmojis with empty input', () => { + const filteredGitmojis = filterGitmojis(undefined, stubs.gitmojis) + + expect(filteredGitmojis).toStrictEqual(stubs.gitmojis) + }) + + it('should should find heart gitmoji', () => { + const filteredGitmojis = filterGitmojis('hart', stubs.gitmojis) + + expect(filteredGitmojis[0]).toStrictEqual(stubs.gitmojis[0]) + }) +}) diff --git a/test/utils/stubs.js b/test/utils/stubs.js index 520ff88cc..bcfc438d2 100644 --- a/test/utils/stubs.js +++ b/test/utils/stubs.js @@ -39,6 +39,11 @@ export const gitmojis = [ emoji: '😍', code: ':heart_eyes:', description: 'Heart eyes' + }, + { + emoji: '⚡️', + code: ':zap:', + description: 'Improving performance.' } ] diff --git a/yarn.lock b/yarn.lock index d3fa0f181..9970484e8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2292,6 +2292,11 @@ function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" +fuse.js@^3.4.6: + version "3.4.6" + resolved "https://registry.yarnpkg.com/fuse.js/-/fuse.js-3.4.6.tgz#545c3411fed88bf2e27c457cab6e73e7af697a45" + integrity sha512-H6aJY4UpLFwxj1+5nAvufom5b2BT2v45P1MkPvdGIK8fWjQx/7o6tTT1+ALV0yawQvbmvCF0ufl2et8eJ7v7Cg== + gauge@~2.7.3: version "2.7.4" resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7"