File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
src/documentation/composables Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ export function useSlugifyString ( string = false , divider = '-' ) {
2+ if ( divider !== '_' && divider !== '-' ) {
3+ throw new Error ( '[slugifyStringHelper]: Divider must be either "_" or "-"' ) ;
4+ }
5+
6+ const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/-,:;' ;
7+ const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------' ;
8+ const p = new RegExp ( a . split ( '' ) . join ( '|' ) , 'g' ) ;
9+
10+ let response = string . toString ( ) . toLowerCase ( )
11+ // Replace spaces with divider //
12+ . replace ( / \s + / g, divider )
13+ . replace ( p , ( c ) => b . charAt ( a . indexOf ( c ) ) )
14+ . replace ( / & / g, `${ divider } and${ divider } ` )
15+ . replace ( / [ ^ \w - ] + / g, '' ) ;
16+
17+ if ( divider === '_' ) {
18+ response = response . replace ( / - + / g, '_' )
19+ . replace ( / _ _ + / g, '_' )
20+ . replace ( / ^ _ / , '' )
21+ . replace ( / ^ - + / , '' )
22+ . replace ( / - + $ / , '' ) ;
23+ }
24+
25+ if ( divider === '-' ) {
26+ response = response . replace ( / _ + / g, '-' )
27+ . replace ( / - - + / g, '-' )
28+ . replace ( / ^ - / , '' )
29+ . replace ( / ^ _ + / , '' )
30+ . replace ( / _ + $ / , '' ) ;
31+ }
32+
33+ return response ;
34+ }
You can’t perform that action at this time.
0 commit comments