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

Small simplifications (redo) #3250

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

Conversation

Zearin
Copy link
Contributor

@Zearin Zearin commented Apr 13, 2024

NOTE: This is a redo of #3243, after I did something stupid in Git and messed up the branch history. (😮!)


These are an assortment of small tweaks.

My goal was to make these bits of code easier to understand at a glance. There’s plenty of subjectivity in that, to be sure.

Some of these make use of Javascript features that were not available a few years ago. (Both syntax and APIs of built-in objects.) Hopefully, I applied them in a way that makes it easier to understand each block of code. Since Eleventy v3 is still in alpha, I thought this would be a good time to introduce them, since they are supported pretty much everywhere and a major version increment doesn’t have to worry about breaking compatibility.

Some tweaks to (hopefully) make some loops a little faster to grok
A little use of the newer optional chaning operator to make some repetetive conditionals slightly faster to read.
Some targeted use of `.some()` and `.includes()` to convey the intention in compact fashion.
Some rewrites involving `Set`s, often involved in making it easier to grok what’s populating the set at a glance.
The standard functional tools for manipulating Arrays (usually `.map()` and `.filter()`) make it easy to write some code that reads a little bit like a pipeline.

Some small rewrites can make these even easier to read. If the arrow function is short enough, using the single-line syntax can make it less  noisy by avoiding the need for braces `{`|`}` or `return`.

Using the static methods _directly_ for the callback is even better, since it’s not even necessary to use an anonymous arrow function or name any arguments.
As I have recently learned, `Object.values()` already returns an Array. :D
(Rather than decide between using `target` or `path` in the loop body, I just smooshed them together.)
@Zearin
Copy link
Contributor Author

Zearin commented Apr 14, 2024

@uncenter, @zachleat : Would you be willing to review this?

@@ -599,7 +598,7 @@ class TemplateData {
}

debug("getLocalDataPaths(%o): %o", templatePath, paths);
return unique(paths).reverse();
return [...new Set(paths)].reverse();
Copy link

Choose a reason for hiding this comment

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

i'm not sure this javascript incantation is necessarily easier to understand than a named function

Copy link
Contributor

@uncenter uncenter Apr 16, 2024

Choose a reason for hiding this comment

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

Also - looks like this is exactly what the unique function is/was doing already anyway! Might as well use the function for readability me thinks. https://github.com/11ty/eleventy/blob/main/src/Util/Unique.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True. I’m all over the place :( I will fix this one.

@Zearin Zearin requested review from uncenter and chee April 16, 2024 16:41
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.

None yet

3 participants