Skip to content

Commit

Permalink
fix: Resolved lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Axeloooo committed Mar 31, 2024
1 parent 3009f14 commit 6a90d0d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
6 changes: 2 additions & 4 deletions backend/scanner-service/src/controller/scanner.controller.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,12 +12,10 @@ class ScannerController {
res: Response,
next: NextFunction,
): Promise<Response<any, Record<string, any>> | 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);
Expand Down
111 changes: 65 additions & 46 deletions backend/scanner-service/src/service/scanner.service.ts
Original file line number Diff line number Diff line change
@@ -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 = "";

Expand All @@ -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 = {
Expand All @@ -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<arr.length;i++){
if(total < 100){
res.push(arr[i]);
total += +arr[i].percentage;
}
const res: Tag[] = [];
for (let i = 0; i < arr.length; i++) {
if (total < 100) {
res.push(arr[i]);
total += +arr[i].percentage;
}
}
if(total != 100){
console.log("Composition did not add to 100%");
return [];
if (total != 100) {
console.log("Composition did not add to 100%");
return [];
}
return res;
}
};

public getSustainability = (arr: Flags): Info => { //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<arr.tags.length;i++){
if(sustainable.includes(arr.tags[i].material.toLowerCase())){
score += +arr.tags[i].percentage;
notes = notes.concat(" " + arr.tags[i].material + " is sustainable ");
}
else{
notes = notes.concat(" " + arr.tags[i].material + " is not sustainable");
}
for (let i = 0; i < arr.tags.length; i++) {
if (sustainable.includes(arr.tags[i].material.toLowerCase())) {
score += +arr.tags[i].percentage;
notes = notes.concat(" " + arr.tags[i].material + " is sustainable ");
} else {
notes = notes.concat(
" " + arr.tags[i].material + " is not sustainable",
);
}
}
if(arr.country != ""){
if(countries.includes(arr.country.toLowerCase())){
if (arr.country != "") {
if (countries.includes(arr.country.toLowerCase())) {
score += 50;
notes = notes.concat(". Produced in " + arr.country + " which is has strong environmental and labor regulations")
}
else{
notes = notes.concat(". Produced in " + arr.country + " which does not have strong environmental and labor regulations")
notes = notes.concat(
". Produced in " +
arr.country +
" which is has strong environmental and labor regulations",
);
} else {
notes = notes.concat(
". Produced in " +
arr.country +
" which does not have strong environmental and labor regulations",
);
}
}
if(arr.lineDry){
if (arr.lineDry) {
score += 20;
notes = notes.concat(". Line drying is recommended, which consumes less energy");
notes = notes.concat(
". Line drying is recommended, which consumes less energy",
);
}
if(arr.coldWater){
if (arr.coldWater) {
score += 20;
notes = notes.concat(". Cold water is recommended, which consumes less energy");
notes = notes.concat(
". Cold water is recommended, which consumes less energy",
);
}
if(arr.dryClean){
if (arr.dryClean) {
score -= 20;
notes = notes.concat(". Dry cleaning is recommended, which consumes more energy");
notes = notes.concat(
". Dry cleaning is recommended, which consumes more energy",
);
}
const info: Info = {score: score,
notes: notes,};
const info: Info = { score: score, notes: notes };
return info;
}
};

public getTextFromImage = async (
scannerRequest: ScannerRequest,
): Promise<string> => {
Expand Down
4 changes: 2 additions & 2 deletions backend/scanner-service/test/scanner.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
const { imageUrl } = scannerRequest;
if (!imageUrl) {
Expand Down

0 comments on commit 6a90d0d

Please sign in to comment.