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

Added findURLs, isValid, toTitleCase functions and their corresponding test files #13

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

leishab
Copy link
Collaborator

@leishab leishab commented Jul 30, 2024

No description provided.

@gabeabrams
Copy link
Contributor

In /src/index.ts, import your helper functions in the section where all the helpers are imported, then export your helper functions below in the section marked by // Helpers

leishab added 2 commits August 5, 2024 12:34
Updating the code and removing all the red errors for the above three functions
Importing findURLs, toTitleCase, validURL helper functions in index.ts
@leishab leishab changed the title Fixed errors for the functions and their test files Added findURLs, isValid, toTitleCase functions and their corresponding test files Aug 5, 2024
@leishab
Copy link
Collaborator Author

leishab commented Aug 5, 2024

findURLs:
This function iterates through and identifies URLs in a block of text, after which it returns their start and end index. The function contains a while loop to use the regex to locate and then match the URL(s) within the block of text, after which the start and end index of the URL is found and pushed to the found array, which is returned to the user.

The test file for this function involves various test cases which test whether the function returns the correct start and end indices for the correctly identified URLs, edge cases such as when the URL has a trailing or leading punctuation mark and returns empty array for strings without valid URLs or for invalid input formats.

toTitleCase:
This function changes any input string so that it adheres to the rules of Chicago Title Case Style. Before this function can proceed to do this, it first makes sure to throw an error if the input is undefined, null or not a string. It then converts the entire input string to lowercase and splits it into separate words. It maps over this string and capitalizes the first and last word, and any word in the input that is not a part of the lowercaseWords list. It then puts the words back together and returns the new string that has been converted to Chicago Title Case Style.

It also includes the test file to validate the function's behavior. It first tests the function with some tests that would return valid, some edge cases such as single letters, all capitalized strings etc. and tests whether the appropriate error is thrown incase an input is invalid

isValid:
This function checks if a given input string is a valid URL. It does so by using the input URL to create a URL object and using the URL constructor to throw an error if the URL is invalid.

It also includes a test file to verify the function's correctness. The test file consists of many examples of URLs that would be considered valid, as well as invalid.

Changed findURL and isValid functions, all three tests pass now!
Copy link
Contributor

@gabeabrams gabeabrams left a comment

Choose a reason for hiding this comment

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

More comments

src/helpers/validators/findURL.test.ts Outdated Show resolved Hide resolved
@@ -2,19 +2,19 @@
* This function finds URLs within a given string and returns an array of
* their locations.
* @author Leisha Bhandari
* @param block: The block of text to search for URLs
* @param block The block of text to search for URLs
* @returns Arrays where each of them contain the start and end index of the URL
Copy link
Contributor

Choose a reason for hiding this comment

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

JSDoc has to be right above the function. here, the constant urlRegex is between the function and its documentation

src/helpers/validators/findURL.ts Outdated Show resolved Hide resolved
src/helpers/validators/toTitleCase.test.ts Outdated Show resolved Hide resolved
* @returns Input string converted to title case
*/
// List of lowercase words according to Chicago Manual of Style
const lowerCaseWords: string[] = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Put this above so JSDoc isn't separated from function

src/helpers/validators/toTitleCase.ts Outdated Show resolved Hide resolved
src/helpers/validators/validURL.test.ts Outdated Show resolved Hide resolved
};

export default isValid;
Copy link
Contributor

Choose a reason for hiding this comment

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

Need endline

// URLs within the block of text (Also takes care of Unicode characters)
const urlRegex = /(https?:\/\/[^\s\u0000-\u001F.,]+[^\s\u0000-\u001F.,]?(?:\?[^ \s\u0000-\u001F]+)?(?:#[^\s\u0000-\u001F]+)?)/g;

const findURLs = (block: string): { start: number, end: number }[] => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Filename isn't named correctly (must match function name)

Copy link
Contributor

Choose a reason for hiding this comment

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

Q: how hard would it be to add a feature where the URLs themselves are returned? Like this:

{ start: number, end: number, url: string }[]

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'd be good to add a "unit" to the numbers. The unit here is index so, like startIndex and endIndex

src/helpers/validators/findURL.test.ts Show resolved Hide resolved
const parsed = new URL(url);

// Checks that the URL's protocol is valid
if (!['https:', 'http:', 'file:', 'ftp:'].includes(parsed.protocol)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Only https and http

// Capitalize the first, last word, and any word in the input that is not a part of the lowercaseWords list
return (index === 0 || index === words.length - 1 || !lowerCaseWords.includes(word)
? `${word.charAt(0).toUpperCase()}${word.slice(1)}`
: word);
Copy link
Contributor

Choose a reason for hiding this comment

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

One item per line

return (
  (index === 0 || index === words.length - 1 || !lowerCaseWords.includes(word))
    ? `${word.charAt(0).toUpperCase()}${word.slice(1)}`
    : word
);

Copy link
Contributor

@gabeabrams gabeabrams left a comment

Choose a reason for hiding this comment

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

See comments. Super close!

Fixed the errors in all three functions and test cases, all tests pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants