Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuzzy search #267

Merged
merged 4 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 6 additions & 10 deletions src/commands/commit/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,15 +30,10 @@ export default (gitmojis: Array<Gitmoji>): Array<Object> => [
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()]
}))
)
}
},
Expand Down
8 changes: 2 additions & 6 deletions src/commands/search/index.js
Original file line number Diff line number Diff line change
@@ -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))
}
Expand Down
24 changes: 24 additions & 0 deletions src/utils/filterGitmojis.js
Original file line number Diff line number Diff line change
@@ -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<Gitmoji>) => {
const fuse = new Fuse(gitmojis, options)
return input ? fuse.search(input) : gitmojis
}

export default filterGitmojis
1 change: 1 addition & 0 deletions src/utils/printEmojis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import chalk from 'chalk'
type Gitmoji = {
emoji: string,
code: string,
name: string,
description: string
}

Expand Down
16 changes: 16 additions & 0 deletions test/utils/filterGitmojis.spec.js
Original file line number Diff line number Diff line change
@@ -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])
})
})
5 changes: 5 additions & 0 deletions test/utils/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export const gitmojis = [
emoji: '😍',
code: ':heart_eyes:',
description: 'Heart eyes'
},
{
emoji: '⚡️',
code: ':zap:',
description: 'Improving performance.'
}
]

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down