-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add support for commands * refactor: Rename useCommands hook * test: Add useCommands hook tests * fix: Fix issue when undefined commands are passed to useCommands hook * test: Add more tests for useCommands hook * feat: Add precision parameter to useCommands hook * test: Add more tests for useCommands hook and Vocal component * docs(README): Add section for commands prop in README * refactor: Fix commands prop type to fit expected shape * refactor: Switch from `toLocaleLowerCase` to `toLowerCase` to reference command keys, #60
- Loading branch information
Showing
9 changed files
with
349 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { renderHook } from '@testing-library/react-hooks' | ||
|
||
import useCommands from '../useCommands' | ||
|
||
describe('useCommands', () => { | ||
it('returns triggerCommand function', () => { | ||
const triggerCommand = renderHook(() => useCommands()) | ||
expect(triggerCommand).toBeDefined() | ||
}) | ||
|
||
it('triggers callback mapped to the exact input', () => { | ||
const commands = { | ||
foo: () => 'bar', | ||
} | ||
const { | ||
result: { current: triggerCommand }, | ||
} = renderHook(() => useCommands(commands)) | ||
expect(triggerCommand('foo')).toBe('bar') | ||
}) | ||
|
||
it('passes input as callback argument', () => { | ||
const commands = { | ||
foo: (input) => input, | ||
} | ||
const { | ||
result: { current: triggerCommand }, | ||
} = renderHook(() => useCommands(commands)) | ||
expect(triggerCommand('foo')).toBe('foo') | ||
}) | ||
|
||
describe('Approximate inputs', () => { | ||
const value = 'foo' | ||
it.each([ | ||
['Change la bordure en vert', 'Change la bordure en verre', value], | ||
['Change la bordure en vert', 'Change la bordure en verres', value], | ||
['Change la bordure en vert', 'Change la bordure en vers', value], | ||
['Change la bordure en vert', 'Change la bordure en vairs', value], | ||
['Change la bordure en vert', 'Changez la bordure en verre', value], | ||
['Change la bordure en vert', 'Changez la bodure en verre', null], | ||
['Change la bordure en vert', 'Change la bordure en rouge', null], | ||
['Change la bordure en vert', 'Change la bordure en verre de rouge', null], | ||
['Change la bordure en vert', 'Change la bordure en violet', null], | ||
['Change la bordure en vert', 'Modifie la bordure en violet', null], | ||
])('triggers callback mapped to approximate inputs', (command, input, expected) => { | ||
const commands = { | ||
[command]: () => value, | ||
} | ||
const { | ||
result: { current: triggerCommand }, | ||
} = renderHook(() => useCommands(commands)) | ||
expect(triggerCommand(input)).toBe(expected) | ||
}) | ||
}) | ||
|
||
it('returns null as no command is mapped to the input', () => { | ||
const commands = { | ||
foo: () => 'bar', | ||
} | ||
const { | ||
result: { current: triggerCommand }, | ||
} = renderHook(() => useCommands(commands)) | ||
expect(triggerCommand('gag')).toBeNull() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Fuse from 'fuse.js' | ||
|
||
const useCommands = (commands, precision = 0.4) => { | ||
commands = !!commands | ||
? Object.entries(commands)?.reduce((acc, [key, value]) => ({ [key.toLowerCase()]: value }), {}) | ||
: {} | ||
|
||
const triggerCommand = (input) => { | ||
const fuse = new Fuse(Object.keys(commands), { includeScore: true, ignoreLocation: true }) | ||
const result = fuse.search(input).filter((r) => r.score < precision) | ||
if (!!result?.length) { | ||
const key = result[0].item.toLowerCase() | ||
return commands[key]?.(input) | ||
} | ||
return null | ||
} | ||
|
||
return triggerCommand | ||
} | ||
|
||
export default useCommands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters