diff --git a/.gitignore b/.gitignore index d6cd819d4d8..ee0f7c63a7a 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.prettierignore b/.prettierignore index 5270c577f42..1eaa453cf7a 100644 --- a/.prettierignore +++ b/.prettierignore @@ -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 diff --git a/utils/get-lines.js b/utils/get-lines.js deleted file mode 100644 index 9e883aec0e9..00000000000 --- a/utils/get-lines.js +++ /dev/null @@ -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; diff --git a/utils/get-lines.test.js b/utils/get-lines.test.ts similarity index 95% rename from utils/get-lines.test.js rename to utils/get-lines.test.ts index 2de1f1c0575..e3044167dcd 100644 --- a/utils/get-lines.test.js +++ b/utils/get-lines.test.ts @@ -1,4 +1,4 @@ -const { getLines } = require('./get-lines'); +import { getLines } from './get-lines'; const content = 'one\ntwo\nthree'; diff --git a/utils/get-lines.ts b/utils/get-lines.ts new file mode 100644 index 00000000000..a13112dfee6 --- /dev/null +++ b/utils/get-lines.ts @@ -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'); +} diff --git a/utils/validate.js b/utils/validate.js deleted file mode 100644 index dd888a96885..00000000000 --- a/utils/validate.js +++ /dev/null @@ -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 -}; diff --git a/utils/validate.test.js b/utils/validate.test.ts similarity index 90% rename from utils/validate.test.js rename to utils/validate.test.ts index 24ce35e766a..9ed026e2ef5 100644 --- a/utils/validate.test.js +++ b/utils/validate.test.ts @@ -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; diff --git a/utils/validate.ts b/utils/validate.ts new file mode 100644 index 00000000000..9f5ebf3e6af --- /dev/null +++ b/utils/validate.ts @@ -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; +};