antares/src/common/libs/sqlEscaper.ts

15 lines
534 B
TypeScript
Raw Normal View History

/* eslint-disable no-useless-escape */
// eslint-disable-next-line no-control-regex
2020-11-13 12:39:40 +01:00
const pattern = /[\0\x08\x09\x1a\n\r"'\\\%]/gm;
const regex = new RegExp(pattern);
2022-05-10 12:57:25 +02:00
function sqlEscaper (string: string) {
return string.replace(regex, char => {
const m = ['\\0', '\\x08', '\\x09', '\\x1a', '\\n', '\\r', '\'', '\"', '\\', '\\\\', '%'];
const r = ['\\\\0', '\\\\b', '\\\\t', '\\\\z', '\\\\n', '\\\\r', '\\\'', '\\\"', '\\\\', '\\\\\\\\', '\%'];
return r[m.indexOf(char)] || char;
});
}
export { sqlEscaper };