-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
990b359
commit 9f061ab
Showing
3 changed files
with
105 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function filterStationsForList(search, stations) { | ||
const normalizedSearch = search | ||
.normalize('NFD') | ||
.replace(/[\u0300-\u036f]/g, "") | ||
.replace(/[ -]/g, "[ -]") | ||
.toLowerCase(); | ||
const searchRegex = new RegExp(normalizedSearch, 'i'); | ||
const startRegex = new RegExp(`^${normalizedSearch}`); | ||
return stations | ||
.sort((a, b) => a.localeCompare(b)) | ||
.filter((name) => { | ||
const normalizedName = name.normalize('NFD').replace(/[\u0300-\u036f]/g, ""); | ||
return normalizedName.match(searchRegex); | ||
}) | ||
.sort((a, b) => { | ||
const aLower = a.toLowerCase(); | ||
const bLower = b.toLowerCase(); | ||
if (aLower.match(startRegex) && !bLower.match(startRegex)) { | ||
return -1; | ||
} | ||
if (!aLower.match(startRegex) && bLower.match(startRegex)) { | ||
return 1; | ||
} | ||
return 0; | ||
}) | ||
} | ||
|
||
|
||
export { | ||
filterStationsForList, | ||
} |
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,71 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
import { filterStationsForList } from '../src/utils.front.js'; | ||
|
||
describe('utils.front', () => { | ||
describe('#filterStationsForList', () => { | ||
test('should return stations with current search case insensitive', () => { | ||
const stations = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; | ||
|
||
const filteredStations = filterStationsForList('a', stations); | ||
|
||
expect(filteredStations).to.deep.equal(['A']); | ||
}) | ||
|
||
test('should return stations beginning with current search before', () => { | ||
const stations = ['AB', 'B', 'CB', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; | ||
|
||
const filteredStations = filterStationsForList('b', stations); | ||
|
||
expect(filteredStations).to.deep.equal(['B', 'AB', 'CB']); | ||
}); | ||
|
||
test('should return stations beginning with current search before case insensitive', () => { | ||
const stations = ['Abbesses', 'Alésia', 'Saint-Lazare', 'Saint-Michel']; | ||
|
||
const filteredStations = filterStationsForList('S', stations); | ||
|
||
expect(filteredStations).to.deep.equal([ | ||
'Saint-Lazare', | ||
'Saint-Michel', | ||
'Abbesses', | ||
'Alésia' | ||
]); | ||
}); | ||
|
||
test('should return stations beginning with current search with space instead of -', () => { | ||
const stations = ['Basilique de Saint-Denis', 'Saint-Lazare', 'Saint-Michel']; | ||
|
||
const filteredStations = filterStationsForList('Saint ', stations); | ||
|
||
expect(filteredStations).to.deep.equal([ | ||
'Saint-Lazare', | ||
'Saint-Michel', | ||
'Basilique de Saint-Denis' | ||
]); | ||
}); | ||
|
||
test('should search without accents', () => { | ||
const stations = ['E', 'É']; | ||
|
||
const filteredStations = filterStationsForList('E', stations); | ||
|
||
expect(filteredStations).to.deep.equal(['E', 'É']); | ||
}); | ||
|
||
test('should normalize search wit accents', () => { | ||
const stations = ['E', 'É']; | ||
|
||
const filteredStations = filterStationsForList('é', stations); | ||
|
||
expect(filteredStations).to.deep.equal(['E', 'É']); | ||
}); | ||
|
||
test('should return station without - in search', () => { | ||
const stations = ['Saint-Lazare']; | ||
|
||
const filteredStations = filterStationsForList('Saint Lazare', stations); | ||
|
||
expect(filteredStations).to.deep.equal(['Saint-Lazare']); | ||
}); | ||
}) | ||
}) |