Repository: eslint/eslint
File: docs/.eleventy.js (JavaScript)

Code before:
    }

    //------------------------------------------------------------------------------
    // Data
    //------------------------------------------------------------------------------

    // Load site-specific data
    const siteName = process.env.ESLINT_SITE_NAME || "en";

    /**
     * Determines whether the given version is a prerelease.
     * @returns {boolean} `true` if it is a prerelease, `false` otherwise.
     */
    async function isPreRelease() {
        const eslintVersions = await require("./src/_data/eslintVersions")();

        return eslintVersions.items.some(item => item.branch === "next");
    }

    eleventyConfig.addGlobalData("site_name", siteName);
    eleventyConfig.addGlobalData("GIT_BRANCH", process.env.BRANCH);
    eleventyConfig.addGlobalData("HEAD", process.env.BRANCH === "main");
    eleventyConfig.addGlobalData("NOINDEX", process.env.BRANCH !== "latest");
    eleventyConfig.addGlobalData("PATH_PREFIX", pathPrefix);
    eleventyConfig.addGlobalData("is_pre_release", isPreRelease);
    eleventyConfig.addGlobalData("is_number_version", process.env.BRANCH && /^v\d+\.x$/u.test(process.env.BRANCH));
    eleventyConfig.addDataExtension("yml", contents => yaml.load(contents));

    //------------------------------------------------------------------------------
    // Filters
    //------------------------------------------------------------------------------

    eleventyConfig.addFilter("limitTo", (arr, limit) => arr.slice(0, limit));

    eleventyConfig.addFilter("jsonify", variable => JSON.stringify(variable));

    eleventyConfig.addFilter("slugify", str => {
        if (!str) {
            return "";
        }

        return slug(str);
    });

    eleventyConfig.addFilter("URIencode", str => {
        if (!str) {
            return "";
        }
        return encodeURI(str);
    });


Review comment (refactor):
Since the same condition is already used on line 47, can we extract it to a variable `isNumberVersion` to avoid duplicating logic?

Code after:
        pathPrefix = `/docs/${process.env.BRANCH}/`; // `/docs/v8.x/`, `/docs/v9.x/`, `/docs/v10.x/` ...
    }

    //------------------------------------------------------------------------------
    // Data
    //------------------------------------------------------------------------------

    // Load site-specific data
    const siteName = process.env.ESLINT_SITE_NAME || "en";

    eleventyConfig.addGlobalData("site_name", siteName);
    eleventyConfig.addGlobalData("GIT_BRANCH", process.env.BRANCH);
    eleventyConfig.addGlobalData("HEAD", process.env.BRANCH === "main");
    eleventyConfig.addGlobalData("NOINDEX", process.env.BRANCH !== "latest");
    eleventyConfig.addGlobalData("PATH_PREFIX", pathPrefix);
    eleventyConfig.addGlobalData("is_number_version", isNumberVersion);
    eleventyConfig.addDataExtension("yml", contents => yaml.load(contents));

    //------------------------------------------------------------------------------
    // Filters
    //------------------------------------------------------------------------------

    eleventyConfig.addFilter("limitTo", (arr, limit) => arr.slice(0, limit));

    eleventyConfig.addFilter("jsonify", variable => JSON.stringify(variable));

    eleventyConfig.addFilter("slugify", str => {
        if (!str) {
            return "";
        }

        return slug(str);
    });

    eleventyConfig.addFilter("URIencode", str => {
        if (!str) {
            return "";
        }
        return encodeURI(str);
    });

    /* order collection by the order specified in the front matter */
    eleventyConfig.addFilter("sortByPageOrder", values => values.slice().sort((a, b) => a.data.order - b.data.order));

    eleventyConfig.addFilter("readableDate", dateObj => {

        // turn it into a JS Date string
        const date = new Date(dateObj);

        // pass it to luxon for formatting
        return DateTime.fromJSDate(date).toFormat("dd MMM, yyyy");