Skip to content

Commit 028568e

Browse files
matthprostlisalupi
authored andcommitted
refactor: import fuzzy search from scaleway lib (#5653)
1 parent 09a680f commit 028568e

File tree

6 files changed

+16
-126
lines changed

6 files changed

+16
-126
lines changed

.changeset/tired-deer-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ultraviolet/ui": minor
3+
---
4+
5+
Refactor search input to use fuzzy search from scaleway lib

packages/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"@nivo/line": "0.89.1",
9494
"@nivo/pie": "0.89.1",
9595
"@nivo/scales": "0.89.0",
96+
"@scaleway/fuzzy-search": "1.0.0",
9697
"@scaleway/random-name": "5.1.2",
9798
"@scaleway/use-media": "3.0.4",
9899
"@ultraviolet/icons": "workspace:*",

packages/ui/src/components/SelectInput/components/SearchBarDropdown.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use client'
22

3+
import { isFuzzyMatch, normalizeString } from '@scaleway/fuzzy-search'
34
import { SearchIcon } from '@ultraviolet/icons'
45
import type { Dispatch, SetStateAction } from 'react'
56
import { useEffect, useRef } from 'react'
6-
import { isFuzzyMatch, normalizeString } from '../../../utils/searchAlgorithm'
77
import { TextInput } from '../../TextInput'
88
import { useSelectInput } from '../SelectInputProvider'
99
import type { DataType, OptionType } from '../types'
Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
22
import { getUUID } from '../ids'
3-
import {
4-
isFuzzyMatch,
5-
levenshteinDistance,
6-
normalizeString,
7-
} from '../searchAlgorithm'
83

94
describe('ids', () => {
105
describe('getUUID', () => {
@@ -25,55 +20,3 @@ describe('ids', () => {
2520
})
2621
})
2722
})
28-
29-
describe('fuzzySearch', () => {
30-
describe('normalizeString', () => {
31-
test('returns correct string', () => {
32-
expect(normalizeString('île-de-France')).toBe('ile de france')
33-
})
34-
})
35-
36-
describe('levenshteinDistance', () => {
37-
test('returns correct lenvenshtein distance', () => {
38-
expect(levenshteinDistance('test', 'test')).toBe(0)
39-
40-
expect(levenshteinDistance('tests', 'test')).toBe(1)
41-
expect(levenshteinDistance('test', 'tests')).toBe(1)
42-
43-
expect(levenshteinDistance('tset', 'test')).toBe(2)
44-
45-
expect(levenshteinDistance('hello', 'test')).toBe(4)
46-
47-
expect(levenshteinDistance('', 'test')).toBe(4)
48-
expect(levenshteinDistance('test', '0')).toBe(4)
49-
})
50-
})
51-
describe('fuzzySearch', () => {
52-
test('with default distance (1)', () => {
53-
expect(isFuzzyMatch('test', 'test')).toBeTruthy()
54-
expect(isFuzzyMatch('tests', 'test')).toBeFalsy()
55-
expect(isFuzzyMatch('test', 'tests')).toBeTruthy()
56-
expect(isFuzzyMatch('tset', 'test')).toBeFalsy()
57-
expect(isFuzzyMatch('hello', 'test')).toBeFalsy()
58-
expect(isFuzzyMatch('', 'test')).toBeTruthy()
59-
})
60-
61-
test('with distance = 0 (exact match)', () => {
62-
expect(isFuzzyMatch('test', 'test', 0)).toBeTruthy()
63-
expect(isFuzzyMatch('tests', 'test', 0)).toBeFalsy()
64-
expect(isFuzzyMatch('test', 'tests', 0)).toBeTruthy()
65-
expect(isFuzzyMatch('tset', 'test', 0)).toBeFalsy()
66-
expect(isFuzzyMatch('hello', 'test', 0)).toBeFalsy()
67-
expect(isFuzzyMatch('', 'test')).toBeTruthy()
68-
})
69-
70-
test('with distance = 2 (swap tolerant)', () => {
71-
expect(isFuzzyMatch('test', 'test', 2)).toBeTruthy()
72-
expect(isFuzzyMatch('tests', 'test', 2)).toBeFalsy()
73-
expect(isFuzzyMatch('test', 'tests', 2)).toBeTruthy()
74-
expect(isFuzzyMatch('tset', 'test', 2)).toBeTruthy()
75-
expect(isFuzzyMatch('hello', 'test', 2)).toBeFalsy()
76-
expect(isFuzzyMatch('', 'test')).toBeTruthy()
77-
})
78-
})
79-
})

packages/ui/src/utils/searchAlgorithm.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)