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

Truncate search query in not found message #17

Merged
merged 2 commits into from
Feb 19, 2023
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
4 changes: 3 additions & 1 deletion src/components/FilteredApps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { selectAppsSearchQuery, selectAppsSearchResult } from '../slices/appsSea
import SearchContext from '../contexts/SearchContext'
// Constants
import { APP_ITEM_HEIGHT_ICON_DISPLAYED, BACKGROUND_COLOR } from '../constants'
// Utils
import { truncateString } from '../utils/string'
// Models
import { RenderedIn } from '../models/rendered-in'
import { AppDetails } from '../models/app-details'
Expand All @@ -30,7 +32,7 @@ const FilteredApps = () => {
if (apps.length === 0 || invalidCharacters) {
return (
<View style={[styles.wrapper, styles.noAppsWrapper]}>
<Text style={styles.noAppsWrapperText}>No applications found for "{searchQuery}"</Text>
<Text style={styles.noAppsWrapperText}>No application found for "{truncateString(searchQuery, 20)}"</Text>
</View>
)
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { truncateString } from './string'

describe('String utils tests', () => {
it(`should truncate string if it's length surpasses the max length`, () => {
expect(truncateString('Hello World!', 5)).toBe('Hello...')
})

it(`should not truncate string if it's length doesn't surpasses the max length`, () => {
expect(truncateString('Hi', 5)).toBe('Hi')
})
})
3 changes: 3 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const truncateString = (value: string | undefined, maxLength: number): string | undefined => {
return value && value.length > maxLength ? `${value.substring(0, maxLength)}...` : value
}