Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* ____ _ _ _____
* /___ \ |_(_)___ / ___
* // / / __| | |_ \ / _ \
* / \_/ /| |_| |___) | __/
* \___,_\ \__|_|____/ \___|
*/
import { Request, Response } from "express";
export function normalizeText(text: string): string {
let tmp = "";
const len = text.length;
for (let i = 0; i < len; ++i) {
switch (text[i]) {
case "۰":
tmp += "0";
break;
case "۱":
tmp += "1";
break;
case "۲":
tmp += "2";
break;
case "۳":
tmp += "3";
break;
case "۴":
tmp += "4";
break;
case "۵":
tmp += "5";
break;
case "۶":
tmp += "6";
break;
case "۷":
tmp += "7";
break;
case "۸":
tmp += "8";
break;
case "۹":
tmp += "9";
break;
case "ي":
case "ی":
case "":
case "":
case "ﯿ":
case "":
case "ى":
case "":
case "":
case "ﯿ":
case "ئ":
case "":
case "":
case "":
case "ي":
case "":
case "":
case "ﯿ":
case "":
case "":
tmp += "ی";
break;
case "ب":
case "":
case "":
case "":
tmp += "ب";
break;
case "پ":
case "":
case "":
case "":
tmp += "پ";
break;
case "ت":
case "":
case "":
case "":
tmp += "ت";
break;
case "ث":
case "":
case "":
case "":
tmp += "ث";
break;
case "ج":
case "":
case "":
case "":
tmp += "ج";
break;
case "چ":
case "":
case "":
case "":
tmp += "چ";
break;
case "ح":
case "":
case "":
case "":
tmp += "ح";
break;
case "خ":
case "":
case "":
case "":
tmp += "خ";
break;
case "س":
case "":
case "":
case "":
tmp += "س";
break;
case "ش":
case "":
case "":
case "":
tmp += "ش";
break;
case "ص":
case "":
case "":
case "":
tmp += "ص";
break;
case "ض":
case "ﺿ":
case "":
case "":
tmp += "ض";
break;
case "ط":
case "":
case "":
case "":
tmp += "ط";
break;
case "ظ":
case "":
case "":
case "":
tmp += "ظ";
break;
case "ع":
case "":
case "":
case "":
tmp += "ع";
break;
case "غ":
case "":
case "":
case "":
tmp += "غ";
break;
case "ف":
case "":
case "":
case "":
tmp += "ف";
break;
case "ق":
case "":
case "":
case "":
tmp += "ق";
break;
case "ک":
case "":
case "":
case "":
case "":
case "":
case "ك":
case "":
case "":
case "":
case "":
tmp += "ک";
break;
case "گ":
case "":
case "":
case "":
tmp += "گ";
break;
case "ل":
case "":
case "":
case "":
tmp += "ل";
break;
case "م":
case "":
case "":
case "":
tmp += "م";
break;
case "ن":
case "":
case "":
case "":
tmp += "ن";
break;
case "":
case "":
case "":
tmp += "لا";
break;
case "أ":
case "إ":
case "ا":
case "":
tmp += "ا";
break;
case "":
case "":
case "و":
case "ؤ":
tmp += "و";
break;
case "":
case "د":
tmp += "د";
break;
case "ذ":
case "":
tmp += "ذ";
break;
case "ر":
case "":
tmp += "ر";
break;
case "ز":
case "":
tmp += "ز";
break;
case "":
case "ژ":
tmp += "ژ";
break;
case "ة":
case "":
case "ه":
case "ه":
case "":
case "":
case "":
case "":
case "":
case "":
case "":
tmp += "ه";
break;
case "":
tmp += "الله";
break;
default:
tmp += text[i];
}
}
return tmp;
}
// Express middleware.
export function normalizeBody(req: Request, res: Response): void {
for (const key in req.body) {
if (typeof req.body[key] === "string") {
req.body[key] = normalizeText(req.body[key]).trim();
} else if (typeof req.body[key] === "number") {
req.body[key] = String(req.body[key]);
}
}
req.next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment