Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert util DbNameValidator to TS #11

Merged
merged 1 commit into from
May 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ const STERILE_COLUMN_AND_QUERY_REGEX = /[^A-Za-z0-9_$]/g;
* Table and Column names.
*/
class DbNameValidator {
static legalize = (name, prefix, regex, checkReserved, i) => {
static legalize = (
name: string,
prefix: string,
regex: RegExp,
checkReserved: boolean,
i: number
): string => {
// Replace all dashes and spaces with underscores
let legalName = name.trim().replace(/[- ]/g, '_');

Expand All @@ -95,16 +101,14 @@ class DbNameValidator {
}

// If name starts with a number, append prefix to the front
// Intentionally using isNaN rather than Number.isNaN
// eslint-disable-next-line no-restricted-globals
if (!isNaN(legalName.charAt(0))) {
if (!Number.isNaN(Number(legalName.charAt(0)))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be safe since it's only 1 character. Important to note though that it wouldn't be safe if you were doing an entire string. Number(false) would be coerced to 0 and then pass the number check.

But since it's 1 char it's fine like this (and is pretty much what global isNaN does)

legalName = prefix + legalName;
}

return legalName;
};

static legalizeTableName = name =>
static legalizeTableName = (name: string): string =>
DbNameValidator.legalize(
name,
TABLE_PREFIX,
Expand All @@ -113,8 +117,8 @@ class DbNameValidator {
0
);

static legalizeColumnNames = headers => {
const legalHeaders = [];
static legalizeColumnNames = (headers: string[]): string[] => {
const legalHeaders: string[] = [];
headers.forEach((header, i) => {
let legalHeader = DbNameValidator.legalizeColumnName(header, i);

Expand All @@ -128,7 +132,7 @@ class DbNameValidator {
return legalHeaders;
};

static legalizeColumnName = (header, i = 0) =>
static legalizeColumnName = (header: string, i = 0): string =>
// Replace all dashes and spaces with underscores
DbNameValidator.legalize(
header,
Expand All @@ -138,10 +142,10 @@ class DbNameValidator {
i
);

static isValidTableName = name =>
static isValidTableName = (name: string): boolean =>
DbNameValidator.legalizeTableName(name) === name;

static isValidColumnName = name =>
static isValidColumnName = (name: string): boolean =>
DbNameValidator.legalizeColumnName(name) === name;
}

Expand Down