@@ -119,17 +119,20 @@ impl UniqueId {
119119
120120impl < T : AsRef < str > > From < T > for UniqueId {
121121 fn from ( s : T ) -> Self {
122+ let mut previous_was_underscore = false ;
122123 Self (
123124 s. as_ref ( )
124125 . chars ( )
125- . filter_map ( |c| {
126- if c. is_ascii_alphanumeric ( ) {
126+ . filter_map ( |c| match ( c, previous_was_underscore) {
127+ ( c, _) if c. is_ascii_alphanumeric ( ) => {
128+ previous_was_underscore = false ;
127129 Some ( c. to_ascii_lowercase ( ) )
128- } else if c == ' ' || c == '_' || c == '-' {
130+ }
131+ ( ' ' | '_' | '-' , false ) => {
132+ previous_was_underscore = true ;
129133 Some ( '_' )
130- } else {
131- None
132134 }
135+ _ => None ,
133136 } )
134137 . collect ( ) ,
135138 )
@@ -143,12 +146,32 @@ impl Display for UniqueId {
143146}
144147
145148#[ cfg( test) ]
146- #[ test]
147- fn test_create_unique_id ( ) {
148- assert_eq ! (
149- UniqueId :: from( "`[i carry your_heart with-me(i carry it in]`" ) . to_string( ) ,
150- "i_carry_your_heart_with_mei_carry_it_in"
151- ) ;
149+ mod test_unique_id {
150+ use super :: UniqueId ;
151+
152+ #[ test]
153+ fn it_handles_special_characters ( ) {
154+ assert_eq ! (
155+ UniqueId :: from( "`[i carry your_heart with-me(i carry it in]`" ) . to_string( ) ,
156+ "i_carry_your_heart_with_mei_carry_it_in"
157+ ) ;
158+ }
159+
160+ #[ test]
161+ fn it_handles_capitalization ( ) {
162+ assert_eq ! (
163+ UniqueId :: from( "This is a Title" ) . to_string( ) ,
164+ "this_is_a_title"
165+ ) ;
166+ }
167+
168+ #[ test]
169+ fn it_doesnt_duplicate_underscores ( ) {
170+ assert_eq ! (
171+ UniqueId :: from( "Something ______ else" ) . to_string( ) ,
172+ "something_else"
173+ ) ;
174+ }
152175}
153176
154177#[ derive( Debug ) ]
0 commit comments