@@ -19,16 +19,19 @@ interface IRazor {
19
19
}
20
20
21
21
export default class Razor extends Amazon implements IRazor {
22
- private searchCategory : string ;
22
+ private searchCategory : string = "" ;
23
23
24
24
constructor ( amazonUri : string , searchCategory : string ) {
25
25
super ( amazonUri ) ;
26
26
27
- this . searchCategory = searchCategory ;
27
+ this . changeSearchCategory ( searchCategory ) ;
28
28
}
29
29
30
30
public changeSearchCategory ( searchCategory : string ) : void {
31
- this . searchCategory = searchCategory ;
31
+ this . searchCategory = searchCategory . toLowerCase ( )
32
+ . replace ( / \s + / g, "-" )
33
+ . normalize ( "NFD" )
34
+ . replace ( / [ \u0300 - \u036f ] / g, "" ) ;
32
35
}
33
36
34
37
private async getProductsSectionPageHTMLCollection ( page ?: number ) : Promise < HTMLCollection > {
@@ -75,24 +78,9 @@ export default class Razor extends Amazon implements IRazor {
75
78
76
79
public async getProduct ( ) : Promise < ProductData | undefined > {
77
80
try {
78
- const productsCollection : HTMLCollection = await this . getProductsSectionPageHTMLCollection ( ) ;
79
-
80
- for ( const productData of productsCollection ) {
81
- const product : Product = new Product ( this . amazonUri , productData ) ;
82
-
83
- if ( product . content [ 0 ] . toLowerCase ( ) . includes ( this . searchCategory . toLowerCase ( ) ) ) {
84
- return {
85
- id : product . id ,
86
- name : product . getName ( ) ,
87
- price : product . getPrice ( ) ,
88
- stars : product . getStars ( ) ,
89
- uri : product . getUri ( ) ,
90
- __data__ : product . content
91
- }
92
- }
93
- }
81
+ const products : Array < ProductData > = await this . getProducts ( ) ;
94
82
95
- return undefined ;
83
+ return products [ 0 ] ;
96
84
} catch ( err ) {
97
85
return undefined ;
98
86
}
0 commit comments