mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 18:18:27 -05:00
* fix(tools/scripts) convert validate and get-lines to typescript files * fix(tools/scripts) refactor validate and get-lines to typescript * remove any type and eslint ignore * Update utils/validate.ts Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * address comments * update .ignore files to ignore newly generated utils files * Update utils/get-lines.ts Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * remove unneeded ErrorInterface * add local type annotation to 'expected' variable Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
24 lines
765 B
TypeScript
24 lines
765 B
TypeScript
export const invalidCharError = {
|
|
valid: false,
|
|
error: 'contains invalid characters'
|
|
};
|
|
export const validationSuccess = { valid: true, error: null };
|
|
export const usernameTooShort = { valid: false, error: 'is too short' };
|
|
export const usernameIsHttpStatusCode = {
|
|
valid: false,
|
|
error: 'is a reserved error code'
|
|
};
|
|
|
|
const validCharsRE = /^[a-zA-Z0-9\-_+]*$/;
|
|
export const isHttpStatusCode = (str: string) => {
|
|
const output = parseInt(str, 10);
|
|
return !isNaN(output) && output >= 100 && output <= 599;
|
|
};
|
|
|
|
export const isValidUsername = (str: string) => {
|
|
if (!validCharsRE.test(str)) return invalidCharError;
|
|
if (str.length < 3) return usernameTooShort;
|
|
if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
|
|
return validationSuccess;
|
|
};
|