Skip to content

Commit

Permalink
feat: use fuzzy search for scopes and types
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich authored Jan 18, 2020
2 parents 298e69c + eeb8eda commit e6d615f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 45 deletions.
6 changes: 3 additions & 3 deletions lib/questions/scope.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const fuzzy = require('fuzzy');

/**
* Searches for the scopes containing the given substring.
*
* @param {string} substring Substring to search with.
* @param {string[]} scopes Scopes list.
*/
const findScope = function (substring, scopes) {
return substring === '' || substring === undefined ?
Promise.resolve(scopes) :
Promise.resolve(scopes.filter((scope) => scope.toLowerCase().includes(substring.toLowerCase().trim())));
return Promise.resolve(fuzzy.filter(substring || '', scopes).map(({original: scope}) => scope));
};

exports.createQuestion = (state) => {
Expand Down
10 changes: 3 additions & 7 deletions lib/questions/type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const pad = require('pad-right');
const fuzzy = require('fuzzy');

const typeToListItem = ({types, disableEmoji}, type) => {
const {description, emoji, value} = types[type];
Expand All @@ -17,14 +18,9 @@ const typeToListItem = ({types, disableEmoji}, type) => {
* @param {string[]} config The whole config.
*/
const findType = function (substring, config) {
const input = (substring || '').toLowerCase();
let foundTypes = config.list;
const types = config.list;

if (input !== '') {
foundTypes = config.list.filter((type) => type.toLowerCase().includes(input));
}

return Promise.resolve(foundTypes.map((type) => typeToListItem(config, type)))
return Promise.resolve(fuzzy.filter(substring || '', types).map(({original: type}) => typeToListItem(config, type)));
};

exports.createQuestion = (state) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"commitizen": "2.10.1",
"eslint": "4.19.1",
"eslint-config-mailonline": "9.0.0",
"fuzzy": "^0.1.3",
"husky": "4.0.10",
"inquirer": "6.5.2",
"inquirer-list-search-prompt": "1.0.2",
Expand Down
43 changes: 8 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
p-retry "^4.0.0"
url-join "^4.0.0"

"@semantic-release/npm@5.3.5", "@semantic-release/npm@^5.0.5":
"@semantic-release/npm@^5.0.5":
version "5.3.5"
resolved "https://registry.yarnpkg.com/@semantic-release/npm/-/npm-5.3.5.tgz#4a83952056d32e1401e1078c3910a159afc0cba8"
integrity sha512-AOREQ6rUT8OAiqXvWCp0kMNjcdnLLq1JdP0voZL4l5zf6Tgs/65YA7ctP+9shthW01Ps85Nu0pILW3p9GkaYuw==
Expand Down Expand Up @@ -1690,7 +1690,7 @@ debug@^2.6.8, debug@^2.6.9:
dependencies:
ms "2.0.0"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -2775,6 +2775,11 @@ functional-red-black-tree@^1.0.1:
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=

fuzzy@^0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/fuzzy/-/fuzzy-0.1.3.tgz#4c76ec2ff0ac1a36a9dccf9a00df8623078d4ed8"
integrity sha1-THbsL/CsGjap3M+aAN+GIweNTtg=

gauge@~2.7.3:
version "2.7.4"
resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7"
Expand Down Expand Up @@ -3274,7 +3279,7 @@ import-modules@^1.1.0:
resolved "https://registry.yarnpkg.com/import-modules/-/import-modules-1.1.0.tgz#748db79c5cc42bb9701efab424f894e72600e9dc"
integrity sha1-dI23nFzEK7lwHvq0JPiU5yYA6dw=

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -4190,11 +4195,6 @@ lodash._baseget@^3.0.0:
resolved "https://registry.yarnpkg.com/lodash._baseget/-/lodash._baseget-3.7.2.tgz#1b6ae1d5facf3c25532350a13c1197cb8bb674f4"
integrity sha1-G2rh1frPPCVTI1ChPBGXy4u2dPQ=

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -4203,33 +4203,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -4310,11 +4288,6 @@ lodash.memoize@~3.0.3:
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-3.0.4.tgz#2dcbd2c287cbc0a55cc42328bd0c736150d53e3f"
integrity sha1-LcvSwofLwKVcxCMovQxzYVDVPj8=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

1 comment on commit e6d615f

@streamich
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build version: 4.1.0-master.557 🤞 master on Travis 🎉

Please sign in to comment.