Skip to content

Commit

Permalink
merge code
Browse files Browse the repository at this point in the history
  • Loading branch information
ashik-75 committed Oct 14, 2023
2 parents 8ae6b61 + ebdbb12 commit 611fa05
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ui/src/pages/Generator/QRcode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { downloadQRCode } from "./utils/helper";
import style from "./QRcode.module.scss";
import DropdownDownloadButton from "components/General/DropdownDownloadButton";
import Warning from "components/General/Warning";
import { detectData } from "pages/Tools/Sorting/utils/helper";
import ColorPickerWithInput from "components/General/ColorPickerWithInput";
import { ResponsiveInputWithLabel } from "components/General/FormComponents";
import { handleImageUpload } from "utils/helper-functions/files";
import QRCodeErrorBoundary from "./components/QRCodeErrorBoundary";
import { classNames } from "utils/helper-functions/string";
import { DataDetection } from "utils/helper-classes/ DataDetection";

const { TextArea } = Input;

const detection = new DataDetection(["number", "string", "url"]);
const QRcode: React.FC = () => {
const [value, setValue] = useState("");
const [dataType, setDataType] = useState("");
Expand All @@ -25,7 +26,8 @@ const QRcode: React.FC = () => {
const [iconSize, setIconSize] = useState(size / 4);

useEffect(() => {
setDataType(detectData(value));
detection.setData(value);
setDataType(detection.detect());
}, [value]);

return (
Expand Down
60 changes: 60 additions & 0 deletions ui/src/utils/helper-classes/ DataDetection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export type DataType =
| "number"
| "string"
| "array"
| "boolean"
| "null"
| "object"
| "url"
| "No data"
| "Can't detect data";

export class DataDetection {
data: string | null = null;
typesToDetect: DataType[];

constructor(typesToDetect: DataType[]) {
this.typesToDetect = typesToDetect;
}

setData(data: string): void {
this.data = data;
}

private isUrl(): boolean {
const urlRegex =
/^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
return urlRegex.test(this.data as string);
}

detect(): DataType {
if (!this.data) {
return "No data";
}

if (this.typesToDetect.includes("url") && this.isUrl()) {
return "url";
}

try {
const parsedData = JSON.parse(this.data);
const actualType: DataType = Array.isArray(parsedData)
? "array"
: parsedData === null
? "null"
: (typeof parsedData as DataType);

if (this.typesToDetect.includes(actualType)) {
return actualType;
}
} catch (error) {
if (this.typesToDetect.includes("string")) {
return "string";
}
}

if (this.data === "[]" || this.data === "{}") return "string";

return "Can't detect data";
}
}

0 comments on commit 611fa05

Please sign in to comment.