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

feat: add helper for matching function strings #144

Merged
merged 17 commits into from
Jun 3, 2024

Conversation

ojeytonwilliams
Copy link
Contributor

@ojeytonwilliams ojeytonwilliams commented Feb 13, 2024

Checklist:

TODO

  • Docs
  • Return RegExp instead of string (it's trivial to combine them with other regexes)
  • Test against fcc curriculum

The docs should explain this pretty well, but the gist is: this function provides a regex to match function expressions/declarations and arrow functions.

@ojeytonwilliams ojeytonwilliams marked this pull request as ready for review February 14, 2024 15:13
@ojeytonwilliams ojeytonwilliams requested a review from a team as a code owner February 14, 2024 15:13
@naomi-lgbt
Copy link
Member

naomi-lgbt commented Feb 14, 2024

Can we add an option to not look for the closing bracket?

That is, I would want to be able to test:

const naomi = (love) => {
  return love ** 2
}

With something like

const regex = new RexExp(`${__helpers.functionRegex("naomi", ["love"], {closed: false})}\s*return\s*love\s*\*\*\s*2)`);
assert.match(code, regex);

@ojeytonwilliams
Copy link
Contributor Author

Sure! Just to check, do you want something that matches everything up to and including the opening bracket (if there is one)?

e.g.

let codeOne = `const naomi = (love) => {
  return love ** 2
}`
let codeTwo = `function naomi(love) {
  return love ** 3
}`
let trickyArrowCode = `const naomi = love => love**2`

let startRE = functionRegex("naomi", ["love"], { closed: false });

startRE.test(codeOne); // true
startRE.test(codeTwo): // true

let fullRE = new RegExp(startRE.source + /\s*return\s*love\s*\*\*\s*2}/.source)

fullRE.test(codeOne); // true
fullRE.test(codeTwo); // false

let weirdRE = new RegExp(startRE.source + /\s*return\s*love/.source)

weirdRE.test(codeOne); // true
weirdRE.test(codeTwo); // true

let arrowRE = new RegExp(startRE.source + /\s*love\*\*2/.source)
arrowRE.test(trickyArrowCode); // true

@ojeytonwilliams
Copy link
Contributor Author

@naomi-lgbt I'm revisiting this, are those specs more or less what you wanted?

@naomi-lgbt
Copy link
Member

Yes that looks great! Thanks!

@ojeytonwilliams
Copy link
Contributor Author

Hey @naomi-lgbt I think it's worked as intended now. There's still the ugly case where it captures too much (described in the docs), but I can't think of a way around that just using regexes. At least not in the time I allocated myself for this!

It should work well enough for our uses and tide us over until we can incorporate the ast-based tooling Shaun created.

Copy link
Member

@naomi-lgbt naomi-lgbt left a comment

Choose a reason for hiding this comment

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

This should be fine as is

@naomi-lgbt naomi-lgbt merged commit 0cfc800 into freeCodeCamp:main Jun 3, 2024
4 checks passed
@ojeytonwilliams ojeytonwilliams deleted the feat/regex-generators branch June 4, 2024 05:49
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