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

chore(gatsby-source-contentful): drop last usages of lodash #28441

Merged
merged 2 commits into from
Dec 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions packages/gatsby-source-contentful/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
} = require(`gatsby/graphql`)
const qs = require(`qs`)
const base64Img = require(`base64-img`)
const _ = require(`lodash`)

const cacheImage = require(`./cache-image`)

Expand Down Expand Up @@ -48,9 +47,8 @@ const {
const CONTENTFUL_IMAGE_MAX_SIZE = 4000

const isImage = image =>
_.includes(
[`image/jpeg`, `image/jpg`, `image/png`, `image/webp`, `image/gif`],
_.get(image, `file.contentType`)
[`image/jpeg`, `image/jpg`, `image/png`, `image/webp`, `image/gif`].includes(
image?.file?.contentType
)

const getBase64Image = imageProps => {
Expand Down Expand Up @@ -178,11 +176,8 @@ const resolveFixed = (image, options) => {
)
})

// Sort sizes for prettiness.
const sortedSizes = _.sortBy(filteredSizes)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The array is initialized ordered. It is only pruned, not shuffled / added. So this sort should be a noop in any case.

Copy link
Contributor

Choose a reason for hiding this comment

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

This warrants a comment that fixedSizes is expected to be sorted in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure


// Create the srcSet.
pvdz marked this conversation as resolved.
Show resolved Hide resolved
const srcSet = sortedSizes
const srcSet = filteredSizes
.map((size, i) => {
let resolution
switch (i) {
Expand Down Expand Up @@ -286,19 +281,17 @@ const resolveFluid = (image, options) => {

// Add the original image (if it isn't already in there) to ensure the largest image possible
// is available for small images.
const pwidth = parseInt(width, 10)
if (
!filteredSizes.includes(parseInt(width)) &&
parseInt(width) < CONTENTFUL_IMAGE_MAX_SIZE &&
Math.round(width / desiredAspectRatio) < CONTENTFUL_IMAGE_MAX_SIZE
!filteredSizes.includes(pwidth) &&
pwidth < CONTENTFUL_IMAGE_MAX_SIZE &&
Math.round(pwidth / desiredAspectRatio) < CONTENTFUL_IMAGE_MAX_SIZE
) {
filteredSizes.push(width)
filteredSizes.push(pwidth)
}

// Sort sizes for prettiness.
const sortedSizes = _.sortBy(filteredSizes)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This sort is similar to above except the condition before it does not exist in the other case.

The condition will only add the current width if it doesn't already exist here. And the loop before will cull any widths that are greater than this width. So either way, the width should be the last element in the list. This means the .sortBy is equally useless here and is safe to drop.


// Create the srcSet.
pvdz marked this conversation as resolved.
Show resolved Hide resolved
const srcSet = sortedSizes
const srcSet = filteredSizes
.map(width => {
const h = Math.round(width / desiredAspectRatio)
return `${createUrl(image.file.url, {
Expand Down