-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collection): Add support for string items (#2195)
Add support for string items in the collection system. This includes string items. `slugify` was added to support strings to be used as id and attributes in HTML. This can be used to support autocomplete where the items are most likely to be string. [category:Components] Co-authored-by: @alanbsmith <a.bax.smith@gmail.com>
- Loading branch information
1 parent
76b571e
commit b81bd7c
Showing
7 changed files
with
62 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Function that takes a string and returns a "slug" which can be used in HTML | ||
*/ | ||
export function slugify(input: string): string { | ||
return input | ||
.trim() | ||
.replace(/[^\w\s-]/g, '') | ||
.replace(/[\s_-]+/g, '-') | ||
.replace(/^-+|-+$/g, '') | ||
.toLowerCase(); | ||
} |
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,39 @@ | ||
import {slugify} from '../lib/utils/slugify'; | ||
|
||
describe('slugify', () => { | ||
it('should remove leading spaces', () => { | ||
expect(slugify(' a')).toEqual('a'); | ||
}); | ||
|
||
it('should remove trailing spaces', () => { | ||
expect(slugify('a ')).toEqual('a'); | ||
}); | ||
|
||
it('should lowercase all letters', () => { | ||
expect(slugify('Ab')).toEqual('ab'); | ||
}); | ||
|
||
it('should add dashes for all spaces', () => { | ||
expect(slugify('a b')).toEqual('a-b'); | ||
}); | ||
|
||
it('should remove all non-alphanumeric characters', () => { | ||
expect(slugify('a.b')).toEqual('ab'); | ||
}); | ||
|
||
it('should collapse multiple spaces into a single dash', () => { | ||
expect(slugify('a b')).toEqual('a-b'); | ||
}); | ||
|
||
it('should remove leading dashes', () => { | ||
expect(slugify('-ab')).toEqual('ab'); | ||
}); | ||
|
||
it('should remove trailing dashes', () => { | ||
expect(slugify('ab-')).toEqual('ab'); | ||
}); | ||
|
||
it('should collapse spaces and dashes to a single dash', () => { | ||
expect(slugify('a-- --b')).toEqual('a-b'); | ||
}); | ||
}); |
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