Skip to content

Commit

Permalink
Merge pull request #794 from simonihmig/extract-resolve-helper
Browse files Browse the repository at this point in the history
Extract resolve helper
  • Loading branch information
simonihmig authored Nov 23, 2024
2 parents 2e7799e + 7cef8c6 commit a0edf8d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-dragons-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@responsive-image/core': patch
---

Add resolveImage utility
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ module("Helper: responsive-image-resolve", function (hooks) {
assert.dom("h1").hasText(new RegExp("/images/aurora-2048w(-\\w+)?.jpg"));
});

test("supports sidth", async function (assert) {
await render(
<template>
<h1>{{responsiveImageResolve testImage width=2048}}</h1>
</template>,
);

assert.dom("h1").hasText(new RegExp("/images/aurora-2048w(-\\w+)?.jpg"));
});

test("supports size", async function (assert) {
await render(
<template>
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './env.ts';
export * from './config.ts';
export * from './debug.ts';
export * from './lqip.ts';
export * from './resolve.ts';

export type * from './types.ts';
31 changes: 31 additions & 0 deletions packages/core/src/resolve.ts
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;
}
29 changes: 29 additions & 0 deletions packages/core/tests/resolve.test.ts
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');
});
});
19 changes: 2 additions & 17 deletions packages/ember/src/helpers/responsive-image-resolve.ts
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;

0 comments on commit a0edf8d

Please sign in to comment.