mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 18:18:27 -05:00
refactor(tools/scripts): convert validate and get-lines to TS (#48622)
* 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>
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -174,6 +174,10 @@ utils/block-nameify.test.js
|
||||
utils/slugs.js
|
||||
utils/slugs.test.js
|
||||
utils/index.js
|
||||
utils/get-lines.js
|
||||
utils/get-lines.test.js
|
||||
utils/validate.js
|
||||
utils/validate.test.js
|
||||
|
||||
### vim ###
|
||||
# Swap
|
||||
|
||||
@@ -16,6 +16,10 @@ utils/block-nameify.test.js
|
||||
utils/slugs.js
|
||||
utils/slugs.test.js
|
||||
utils/index.js
|
||||
utils/get-lines.js
|
||||
utils/get-lines.test.js
|
||||
utils/validate.js
|
||||
utils/validate.test.js
|
||||
**/package-lock.json
|
||||
web/.next
|
||||
curriculum-server/data/curriculum.json
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
const { isEmpty } = require('lodash');
|
||||
|
||||
function getLines(contents, range) {
|
||||
if (isEmpty(range)) {
|
||||
return '';
|
||||
}
|
||||
const lines = contents.split('\n');
|
||||
const editableLines =
|
||||
isEmpty(lines) || range[1] <= range[0]
|
||||
? []
|
||||
: lines.slice(range[0], range[1] - 1);
|
||||
return editableLines.join('\n');
|
||||
}
|
||||
|
||||
exports.getLines = getLines;
|
||||
@@ -1,4 +1,4 @@
|
||||
const { getLines } = require('./get-lines');
|
||||
import { getLines } from './get-lines';
|
||||
|
||||
const content = 'one\ntwo\nthree';
|
||||
|
||||
10
utils/get-lines.ts
Normal file
10
utils/get-lines.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export function getLines(contents: string, range?: number[]) {
|
||||
if (!range) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const lines = contents.split('\n');
|
||||
const editableLines =
|
||||
range[1] <= range[0] ? [] : lines.slice(range[0], range[1] - 1);
|
||||
return editableLines.join('\n');
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
const invalidCharError = {
|
||||
valid: false,
|
||||
error: 'contains invalid characters'
|
||||
};
|
||||
const validationSuccess = { valid: true, error: null };
|
||||
const usernameTooShort = { valid: false, error: 'is too short' };
|
||||
const usernameIsHttpStatusCode = {
|
||||
valid: false,
|
||||
error: 'is a reserved error code'
|
||||
};
|
||||
|
||||
const isNumeric = num => !isNaN(num);
|
||||
const validCharsRE = /^[a-zA-Z0-9\-_+]*$/;
|
||||
const isHttpStatusCode = str =>
|
||||
isNumeric(str) && parseInt(str, 10) >= 100 && parseInt(str, 10) <= 599;
|
||||
|
||||
const isValidUsername = str => {
|
||||
if (!validCharsRE.test(str)) return invalidCharError;
|
||||
if (str.length < 3) return usernameTooShort;
|
||||
if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
|
||||
return validationSuccess;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
isNumeric,
|
||||
isHttpStatusCode,
|
||||
isValidUsername,
|
||||
validationSuccess,
|
||||
usernameTooShort,
|
||||
usernameIsHttpStatusCode,
|
||||
invalidCharError
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
const {
|
||||
import {
|
||||
isValidUsername,
|
||||
usernameTooShort,
|
||||
validationSuccess,
|
||||
usernameIsHttpStatusCode,
|
||||
invalidCharError
|
||||
} = require('./validate');
|
||||
} from './validate';
|
||||
|
||||
function inRange(num, range) {
|
||||
function inRange(num: number, range: number[]) {
|
||||
return num >= range[0] && num <= range[1];
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ describe('isValidUsername', () => {
|
||||
const finalCode = 127;
|
||||
|
||||
for (let code = 0; code <= finalCode; code++) {
|
||||
let char = String.fromCharCode(code);
|
||||
let expected = invalidCharError;
|
||||
const char = String.fromCharCode(code);
|
||||
let expected: { valid: boolean; error: null | string } = invalidCharError;
|
||||
if (allowedCharactersList.includes(char)) expected = validationSuccess;
|
||||
if (inRange(code, numbers)) expected = validationSuccess;
|
||||
if (inRange(code, upperCase)) expected = validationSuccess;
|
||||
23
utils/validate.ts
Normal file
23
utils/validate.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user