Skip to content

Commit c8e9765

Browse files
committed
fixed get products with accents on name bug && refactored getProduct method
1 parent 2417075 commit c8e9765

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

src/Razor.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@ interface IRazor {
1919
}
2020

2121
export default class Razor extends Amazon implements IRazor {
22-
private searchCategory: string;
22+
private searchCategory: string = "";
2323

2424
constructor(amazonUri: string, searchCategory: string) {
2525
super(amazonUri);
2626

27-
this.searchCategory = searchCategory;
27+
this.changeSearchCategory(searchCategory);
2828
}
2929

3030
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, "");
3235
}
3336

3437
private async getProductsSectionPageHTMLCollection(page?: number): Promise<HTMLCollection> {
@@ -75,24 +78,9 @@ export default class Razor extends Amazon implements IRazor {
7578

7679
public async getProduct(): Promise<ProductData | undefined> {
7780
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();
9482

95-
return undefined;
83+
return products[0];
9684
} catch (err) {
9785
return undefined;
9886
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Razor, { ProductData as Product } from "./Razor";
22

3-
const razor: Razor = new Razor("https://www.amazon.com.br", "A Prisão do Rei");
3+
const razor: Razor = new Razor("https://www.amazon.com.br", "A prisão do rei");
44

55
(async (): Promise<void> => {
66
//const products: Array<Product> = await razor.getProducts(2);

0 commit comments

Comments
 (0)