diff --git a/backend/scanner-service/src/controller/scanner.controller.ts b/backend/scanner-service/src/controller/scanner.controller.ts index 88c8aa6..5607cf8 100644 --- a/backend/scanner-service/src/controller/scanner.controller.ts +++ b/backend/scanner-service/src/controller/scanner.controller.ts @@ -1,4 +1,4 @@ -import { ScannerRequest, Tag, Info, Flags} from "../types.js"; +import { ScannerRequest, Tag, Info, Flags } from "../types.js"; import ScannerProvider from "../abstract/scanner.abstract.js"; import { Request, Response, NextFunction } from "express"; @@ -12,12 +12,10 @@ class ScannerController { res: Response, next: NextFunction, ): Promise> | void> => { - let notes = ""; try { const text: string = await this.service.getTextFromImage(req.body); - // const text: string = "20% Cashmere 50% Recycled Cotton 30% Cork made in Canada dry clean only"; const materials: Flags = this.service.getMaterials(text); - const score: Info = this.service.getSustainability(materials); + const score: Info = this.service.getSustainability(materials); return res.status(201).json(score); } catch (e) { next(e); diff --git a/backend/scanner-service/src/service/scanner.service.ts b/backend/scanner-service/src/service/scanner.service.ts index ff1be6e..adf7eed 100644 --- a/backend/scanner-service/src/service/scanner.service.ts +++ b/backend/scanner-service/src/service/scanner.service.ts @@ -1,21 +1,27 @@ import ScannerProvider from "../abstract/scanner.abstract.js"; -import { ScannerRequest, Tag, sustainable, Info, Flags, countries} from "../types.js"; +import { + ScannerRequest, + Tag, + sustainable, + Info, + Flags, + countries, +} from "../types.js"; class ScannerService implements ScannerProvider { constructor(private provider: ScannerProvider) { this.provider = provider; } - //want to add functions that detect made in sustainable countries and care instructions + public getMaterials = (text: string): Flags => { const materialRegex = /(100|\d{1,2})% *(\b\w+\b)/g; - const countryRegex = /(Made\s+in|Product\s+of) *(\b\w+\b)/ig; + const countryRegex = /(Made\s+in|Product\s+of) *(\b\w+\b)/gi; const materialMatches = text.matchAll(materialRegex); const countryMatches = text.matchAll(countryRegex); let dryClean = false; let coldWater = false; let lineDry = false; - - + const scannedTags: Tag[] = []; let country = ""; @@ -35,17 +41,17 @@ class ScannerService implements ScannerProvider { console.log("Country: ", country); } } - - if (text.match(/dry\s+clean/ig)){//checking if dry cleaning is recommended + + if (text.match(/dry\s+clean/gi)) { console.log("dry cleaning"); dryClean = true; } - if (text.match(/cold/ig)){//checking if cold is recommended + if (text.match(/cold/gi)) { console.log("cold water"); coldWater = true; } - if (text.match(/(line|hang)\s+dry/ig)){//checking if line drying is recommended + if (text.match(/(line|hang)\s+dry/gi)) { lineDry = true; } const infoFound: Flags = { @@ -58,63 +64,76 @@ class ScannerService implements ScannerProvider { return infoFound; }; - public checkPercent = (arr: Tag[]): Tag[] => { //for checking if percentages add to 100 and ignoring any excess materials + public checkPercent = (arr: Tag[]): Tag[] => { let total = 0; - let res: Tag[] = []; - for(let i=0;i { //gives a score for the sustainability of the article of clothing + public getSustainability = (arr: Flags): Info => { let score = 0; let notes = ""; - if(arr.tags.length == 0){ + if (arr.tags.length == 0) { notes = "Composition did not add to 100%"; } - for(let i=0;i => { diff --git a/backend/scanner-service/test/scanner.service.test.ts b/backend/scanner-service/test/scanner.service.test.ts index 99e4424..dd9adc8 100644 --- a/backend/scanner-service/test/scanner.service.test.ts +++ b/backend/scanner-service/test/scanner.service.test.ts @@ -50,10 +50,10 @@ class MockScannerProvider implements ScannerProvider { } checkPercent = (arr: Tag[]): Tag[] => { throw new Error("Method not implemented."); - } + }; getSustainability = (arr: Flags): Info => { throw new Error("Method not implemented."); - } + }; getTextFromImage(scannerRequest: ScannerRequest): Promise { const { imageUrl } = scannerRequest; if (!imageUrl) {