From 01cc3da42d17dbcbaae3d9163df80d75ed6f346e Mon Sep 17 00:00:00 2001 From: Ben Carter Date: Sun, 10 Sep 2023 03:03:14 -0400 Subject: [PATCH] Added original helpers from eluna-ts --- common/src/__tests__/db.helpers.spec.ts | 31 +++++++++++++++++++++++ common/src/__tests__/tsconfig.json | 9 +++++++ common/src/db.helpers.d.ts | 7 +++++ common/src/db.helpers.ts | 13 ++++++++++ common/src/declarations/db.helpers.d.ts | 7 +++++ common/src/declarations/hook.helpers.d.ts | 6 +++++ common/src/declarations/index.d.ts | 2 ++ common/src/hook.helpers.d.ts | 6 +++++ common/src/hook.helpers.ts | 12 +++++++++ common/src/index.d.ts | 2 ++ common/src/index.ts | 2 ++ 11 files changed, 97 insertions(+) create mode 100644 common/src/__tests__/db.helpers.spec.ts create mode 100644 common/src/__tests__/tsconfig.json create mode 100644 common/src/db.helpers.d.ts create mode 100644 common/src/db.helpers.ts create mode 100644 common/src/declarations/db.helpers.d.ts create mode 100644 common/src/declarations/hook.helpers.d.ts create mode 100644 common/src/declarations/index.d.ts create mode 100644 common/src/hook.helpers.d.ts create mode 100644 common/src/hook.helpers.ts create mode 100644 common/src/index.d.ts create mode 100644 common/src/index.ts diff --git a/common/src/__tests__/db.helpers.spec.ts b/common/src/__tests__/db.helpers.spec.ts new file mode 100644 index 0000000..15f3993 --- /dev/null +++ b/common/src/__tests__/db.helpers.spec.ts @@ -0,0 +1,31 @@ +import { stringSanitizer } from "../db.helpers"; + +declare var global: any; +global.string = {}; +global.string.gsub = function ( + str: string, + pattern: string, + fn: any, +): [string, number] { + const regExp = RegExp(pattern, "g"); + return [ + str.replace(regExp, fn), + 0, + ]; +}; + +describe("Test db.helpers.ts", () => { + it("Test string to sanitize", () => { + const toSanitize = "'\"\\ \\%\tt\be\0s',\"\x1at\n\r\"'"; + const res = stringSanitizer(toSanitize); + + expect(res).not.toEqual(toSanitize); + }); + + it("Test normal string", () => { + const toSanitize = "tonotnormalize"; + const res = stringSanitizer(toSanitize); + + expect(res).toEqual(toSanitize); + }); +}); diff --git a/common/src/__tests__/tsconfig.json b/common/src/__tests__/tsconfig.json new file mode 100644 index 0000000..b5093c8 --- /dev/null +++ b/common/src/__tests__/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "lib": [ + "DOM","ESNext" + ], + }, + "exclude": [] +} diff --git a/common/src/db.helpers.d.ts b/common/src/db.helpers.d.ts new file mode 100644 index 0000000..7fe6f82 --- /dev/null +++ b/common/src/db.helpers.d.ts @@ -0,0 +1,7 @@ +/** + * Sanitize a string for SQL + * + * @param str + * @returns Sanitized string + */ +export declare function stringSanitizer(str: string): string; diff --git a/common/src/db.helpers.ts b/common/src/db.helpers.ts new file mode 100644 index 0000000..4fcd9da --- /dev/null +++ b/common/src/db.helpers.ts @@ -0,0 +1,13 @@ +/** + * Sanitize a string for SQL + * + * @param str + * @returns Sanitized string + */ +export function stringSanitizer(str: string): string { + return string.gsub( + str, + "[';\\, ]", + "", + )[0]; +} diff --git a/common/src/declarations/db.helpers.d.ts b/common/src/declarations/db.helpers.d.ts new file mode 100644 index 0000000..7fe6f82 --- /dev/null +++ b/common/src/declarations/db.helpers.d.ts @@ -0,0 +1,7 @@ +/** + * Sanitize a string for SQL + * + * @param str + * @returns Sanitized string + */ +export declare function stringSanitizer(str: string): string; diff --git a/common/src/declarations/hook.helpers.d.ts b/common/src/declarations/hook.helpers.d.ts new file mode 100644 index 0000000..dc8ab04 --- /dev/null +++ b/common/src/declarations/hook.helpers.d.ts @@ -0,0 +1,6 @@ +export type IRegisterHook = (hook: T, +/** the number of times the function will be called, 0 means "always call this function" */ +shots?: number) => any; +export type IRegisterHookWithId = (id: number, hook: T, +/** the number of times the function will be called, 0 means "always call this function" */ +shots?: number) => any; diff --git a/common/src/declarations/index.d.ts b/common/src/declarations/index.d.ts new file mode 100644 index 0000000..538c890 --- /dev/null +++ b/common/src/declarations/index.d.ts @@ -0,0 +1,2 @@ +export * from "./hook.helpers"; +export * from "./db.helpers"; diff --git a/common/src/hook.helpers.d.ts b/common/src/hook.helpers.d.ts new file mode 100644 index 0000000..bf09f95 --- /dev/null +++ b/common/src/hook.helpers.d.ts @@ -0,0 +1,6 @@ +export declare type IRegisterHook = (hook: T, +/** the number of times the function will be called, 0 means "always call this function" */ +shots?: number) => any; +export declare type IRegisterHookWithId = (id: number, hook: T, +/** the number of times the function will be called, 0 means "always call this function" */ +shots?: number) => any; diff --git a/common/src/hook.helpers.ts b/common/src/hook.helpers.ts new file mode 100644 index 0000000..c535a82 --- /dev/null +++ b/common/src/hook.helpers.ts @@ -0,0 +1,12 @@ +export type IRegisterHook = ( + hook: T, + /** the number of times the function will be called, 0 means "always call this function" */ + shots?: number +) => any; + +export type IRegisterHookWithId = ( + id: number, + hook: T, + /** the number of times the function will be called, 0 means "always call this function" */ + shots?: number, +) => any; diff --git a/common/src/index.d.ts b/common/src/index.d.ts new file mode 100644 index 0000000..538c890 --- /dev/null +++ b/common/src/index.d.ts @@ -0,0 +1,2 @@ +export * from "./hook.helpers"; +export * from "./db.helpers"; diff --git a/common/src/index.ts b/common/src/index.ts new file mode 100644 index 0000000..c74c8ce --- /dev/null +++ b/common/src/index.ts @@ -0,0 +1,2 @@ +export * from "./hook.helpers" +export * from "./db.helpers"