11type Alphabet = {
22 [ key : string ] : string ;
3- }
3+ }
44
5- export class CarSlugger {
5+ export class CarSlugger {
66 /**
77 * Alphabet needs to translate cyrillic letters to latin
88 */
99 private dict : Alphabet = {
10- 'а' :'a' ,
11- 'б' :'b' ,
12- 'в' :'v' ,
13- 'г' :'g' ,
14- 'д' :'d' ,
15- 'е' :'e' ,
16- 'ё' :'yo' ,
17- 'ж' :'zh' ,
18- 'з' :'z' ,
19- 'и' :'i' ,
20- 'й' :'i' ,
21- 'к' :'k' ,
22- 'л' :'l' ,
23- 'м' :'m' ,
24- 'н' :'n' ,
25- 'о' :'o' ,
26- 'п' :'p' ,
27- 'р' :'r' ,
28- 'с' :'s' ,
29- 'т' :'t' ,
30- 'у' :'u' ,
31- 'ф' :'ph' ,
32- 'х' :'h' ,
33- 'ц' :'c' ,
34- 'ч' :'ch' ,
35- 'ш' :'sh' ,
36- 'щ' :'sh' ,
37- 'ъ' :'' ,
38- 'ы' :'y' ,
39- 'ь' :'' ,
40- 'э' :'e' ,
41- 'ю' :'u' ,
42- 'я' :'ya' ,
43- ' ' : ' ' ,
10+ 'а' : 'a' ,
11+ 'б' : 'b' ,
12+ 'в' : 'v' ,
13+ 'г' : 'g' ,
14+ 'д' : 'd' ,
15+ 'е' : 'e' ,
16+ 'ё' : 'yo' ,
17+ 'ж' : 'zh' ,
18+ 'з' : 'z' ,
19+ 'и' : 'i' ,
20+ 'й' : 'i' ,
21+ 'к' : 'k' ,
22+ 'л' : 'l' ,
23+ 'м' : 'm' ,
24+ 'н' : 'n' ,
25+ 'о' : 'o' ,
26+ 'п' : 'p' ,
27+ 'р' : 'r' ,
28+ 'с' : 's' ,
29+ 'т' : 't' ,
30+ 'у' : 'u' ,
31+ 'ф' : 'ph' ,
32+ 'х' : 'h' ,
33+ 'ц' : 'c' ,
34+ 'ч' : 'ch' ,
35+ 'ш' : 'sh' ,
36+ 'щ' : 'sh' ,
37+ 'ъ' : '' ,
38+ 'ы' : 'y' ,
39+ 'ь' : '' ,
40+ 'э' : 'e' ,
41+ 'ю' : 'u' ,
42+ 'я' : 'ya' ,
43+ ' ' : ' ' ,
4444 } ;
4545
4646 /**
@@ -50,7 +50,7 @@ type Alphabet = {
5050 * @returns string
5151 */
5252 private wordsToSlug ( text : string ) {
53- return text . replaceAll ( ' ' , '-' ) ;
53+ return text . replaceAll ( ' ' , '-' ) ;
5454 } ;
5555
5656
@@ -88,30 +88,30 @@ type Alphabet = {
8888 * @param text string
8989 * @returns string
9090 */
91- private parseString ( text : string ) {
92- const words : string [ ] | null = text . match ( this . commonRegExp ) ;
93- let output : string = '' ;
91+ public parseString ( text : string ) {
92+ const words : string [ ] | null = text . match ( this . commonRegExp ) ;
93+ let output : string = '' ;
9494
95- words ?. forEach ( ( word : string , index : number , arr : any ) => {
96- if ( this . cyrillicRegExp . test ( word ) ) {
95+ words ?. forEach ( ( word : string , index : number , arr : any ) => {
96+ if ( this . cyrillicRegExp . test ( word ) ) {
9797
98- for ( let i : number = 0 ; i <= word . length ; i ++ ) {
99- if ( i < word . length ) output += this . dict [ word [ i ] . toLocaleLowerCase ( ) ] ?? '' ;
100- else output += arr . length === index + 1 ? '' : ' ' ;
101- } ;
102- } else {
103- const length = arr . length ;
104- const trueIndex = index + 1 ;
98+ for ( let i : number = 0 ; i <= word . length ; i ++ ) {
99+ if ( i < word . length ) output += this . dict [ word [ i ] . toLocaleLowerCase ( ) ] ?? '' ;
100+ else output += arr . length === index + 1 ? '' : ' ' ;
101+ } ;
102+ } else {
103+ const length = arr . length ;
104+ const trueIndex = index + 1 ;
105105
106- if ( trueIndex !== length && trueIndex > 1 || ( trueIndex === 1 && length > 1 ) ) {
107- output += word . toLowerCase ( ) + ' ' ;
108- } else if ( trueIndex === length ) {
109- output += word . toLowerCase ( ) ;
110- } ;
111- } ;
112- } ) ;
106+ if ( trueIndex !== length && trueIndex > 1 || ( trueIndex === 1 && length > 1 ) ) {
107+ output += word . toLowerCase ( ) + ' ' ;
108+ } else if ( trueIndex === length ) {
109+ output += word . toLowerCase ( ) ;
110+ } ;
111+ } ;
112+ } ) ;
113113
114- return output ;
114+ return output ;
115115 } ;
116116
117117 /**
@@ -121,12 +121,12 @@ type Alphabet = {
121121 * @returns string
122122 */
123123 public translateCyrillic ( text : string ) {
124- if ( this . cyrillicRegExp . test ( text ) ) {
124+ if ( this . cyrillicRegExp . test ( text ) ) {
125125 let output : string = '' ;
126- for ( let i : number = 0 ; i <= text . length ; i ++ ) {
127- if ( this . cyrillicUppercase . test ( text [ i ] ) ) output += this . dict [ text [ i ] . toLowerCase ( ) ] . toUpperCase ( ) ?? '' ;
128- if ( this . cyrillicLowercase . test ( text [ i ] ) ) output += this . dict [ text [ i ] ] ?? '' ;
129- else output += this . dict [ text [ i ] ] ?? '' ;
126+ for ( let i : number = 0 ; i <= text . length ; i ++ ) {
127+ if ( this . cyrillicUppercase . test ( text [ i ] ) ) output += this . dict [ text [ i ] . toLowerCase ( ) ] . toUpperCase ( ) ?? '' ;
128+ if ( this . cyrillicLowercase . test ( text [ i ] ) ) output += this . dict [ text [ i ] ] ?? '' ;
129+ else output += this . dict [ text [ i ] ] ?? '' ;
130130 } ;
131131 return output ;
132132 } else {
@@ -135,13 +135,13 @@ type Alphabet = {
135135 }
136136
137137 public testString ( string : string ) {
138- console . debug ( 'Input string: ' , string ) ;
139- console . debug ( 'Output string: ' , this . parseString ( string ) ) ;
140- console . debug ( 'Output Slug: ' ,
141- this . wordsToSlug (
142- this . parseString ( string )
143- )
144- ) ;
138+ console . debug ( 'Input string: ' , string ) ;
139+ console . debug ( 'Output string: ' , this . parseString ( string ) ) ;
140+ console . debug ( 'Output Slug: ' ,
141+ this . wordsToSlug (
142+ this . parseString ( string )
143+ )
144+ ) ;
145145 } ;
146146
147147 /**
@@ -154,8 +154,8 @@ type Alphabet = {
154154 * @example "LADA (ВАЗ) 2112 4x4" >>> "lada-vaz-2112-4x4"
155155 */
156156 public getSlug ( string : string ) {
157- return this . wordsToSlug (
158- this . parseString ( string )
159- ) ;
157+ return this . wordsToSlug (
158+ this . parseString ( string )
159+ ) ;
160160 } ;
161- }
161+ }
0 commit comments