From 24a6f43923057311a14f484493596cc77c77b06a Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Fri, 27 Dec 2019 15:19:56 +0100 Subject: [PATCH 1/4] :heavy_plus_sign: Add fuse.js --- package.json | 1 + yarn.lock | 5 +++++ 2 files changed, 6 insertions(+) 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/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" From dc1384e2b272d5def872dada21625282ff33d01b Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Thu, 26 Dec 2019 11:11:58 +0100 Subject: [PATCH 2/4] :sparkles: Add fuzzy gitmoji filter util --- src/commands/commit/prompts.js | 2 +- src/utils/filterGitmojis.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/utils/filterGitmojis.js diff --git a/src/commands/commit/prompts.js b/src/commands/commit/prompts.js index 9d35700c3..8926b90c5 100644 --- a/src/commands/commit/prompts.js +++ b/src/commands/commit/prompts.js @@ -8,7 +8,7 @@ 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, 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 From 7238c79ae04c51780b1bfda23f3143b598657874 Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Thu, 26 Dec 2019 11:12:24 +0100 Subject: [PATCH 3/4] :white_check_mark: Add gitmojis filter tests --- src/utils/printEmojis.js | 1 + test/utils/filterGitmojis.spec.js | 16 ++++++++++++++++ test/utils/stubs.js | 5 +++++ 3 files changed, 22 insertions(+) create mode 100644 test/utils/filterGitmojis.spec.js 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.' } ] From a0a98534e3dc9345e36e987ddd67e124cd6da454 Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Mon, 23 Dec 2019 23:00:41 +0100 Subject: [PATCH 4/4] :sparkles: Make fuzzy gitmoji search --- src/commands/commit/prompts.js | 14 +++++--------- src/commands/search/index.js | 8 ++------ 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/commands/commit/prompts.js b/src/commands/commit/prompts.js index 8926b90c5..bae5ecfb9 100644 --- a/src/commands/commit/prompts.js +++ b/src/commands/commit/prompts.js @@ -2,6 +2,7 @@ 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 @@ -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)) }