-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #794 from simonihmig/extract-resolve-helper
Extract resolve helper
- Loading branch information
Showing
6 changed files
with
78 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@responsive-image/core': patch | ||
--- | ||
|
||
Add resolveImage utility |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { getDestinationWidthBySize } from './env'; | ||
import type { ImageData, ImageType } from './types'; | ||
|
||
export interface ResolveImageOptions { | ||
/** | ||
* width in pixel to request the best matching image for | ||
*/ | ||
width?: number; | ||
|
||
/** | ||
* size in vw (0 - 100) to request the best matching image for | ||
*/ | ||
size?: number; | ||
|
||
/** | ||
* preferred image format | ||
*/ | ||
format?: ImageType; | ||
} | ||
|
||
export function resolveImage( | ||
data: ImageData, | ||
{ width, size, format }: ResolveImageOptions = {}, | ||
): string | undefined { | ||
const url = data.imageUrlFor( | ||
width ?? getDestinationWidthBySize(size), | ||
format ?? data.imageTypes[0], | ||
); | ||
|
||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import type { ImageData } from '../src'; | ||
import { resolveImage } from '../src'; | ||
|
||
describe('resolveImage', () => { | ||
const imageData: ImageData = { | ||
imageTypes: ['webp', 'avif'], | ||
imageUrlFor(width, type = 'jpeg') { | ||
return `/${width}.${type}`; | ||
}, | ||
}; | ||
|
||
test('returns image matching device width with no args', () => { | ||
expect(resolveImage(imageData)).toBe('/320.webp'); | ||
}); | ||
|
||
test('supports size', () => { | ||
expect(resolveImage(imageData, { size: 10 })).toBe('/32.webp'); | ||
}); | ||
|
||
test('supports width', () => { | ||
expect(resolveImage(imageData, { width: 1024 })).toBe('/1024.webp'); | ||
}); | ||
|
||
test('supports format', () => { | ||
expect(resolveImage(imageData, { format: 'avif' })).toBe('/320.avif'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,3 @@ | ||
import { | ||
getDestinationWidthBySize, | ||
type ImageData, | ||
type ImageType, | ||
} from '@responsive-image/core'; | ||
import { htmlSafe } from '@ember/template'; | ||
import { resolveImage } from '@responsive-image/core'; | ||
|
||
export default function responsiveImageResolve( | ||
data: ImageData, | ||
{ | ||
size, | ||
format = data.imageTypes[0], | ||
}: { size?: number; format?: ImageType } = {}, | ||
): ReturnType<typeof htmlSafe> | undefined { | ||
const url = data.imageUrlFor(getDestinationWidthBySize(size), format); | ||
|
||
return url ? htmlSafe(url) : undefined; | ||
} | ||
export default resolveImage; |