-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: add effort directive (#618)
Co-authored-by: Ben McCann <[email protected]>
Showing
9 changed files
with
170 additions
and
6 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 @@ | ||
--- | ||
'imagetools-core': minor | ||
--- | ||
|
||
feat: add effort directive |
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
3 changes: 3 additions & 0 deletions
3
...apshots__/src-transforms-__tests__-format.spec.ts---format---transform---webp-w--effort-1
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...ts-src-transforms-tests-format-spec-ts-format-transform-png-w-effort-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,71 @@ | ||
import { getEffort } from '../effort' | ||
import sharp, { Sharp } from 'sharp' | ||
import { join } from 'path' | ||
import { describe, beforeEach, expect, test, it } from 'vitest' | ||
import { METADATA } from '../../lib/metadata' | ||
|
||
describe('effort', () => { | ||
let img: Sharp | ||
beforeEach(() => { | ||
img = sharp(join(__dirname, '../../__tests__/__fixtures__/pexels-allec-gomes-5195763.png')) | ||
}) | ||
|
||
test('keyword "effort"', () => { | ||
const res = getEffort({ effort: '3' }, img) | ||
|
||
expect(res).toEqual(3) | ||
}) | ||
|
||
test('missing', () => { | ||
const res = getEffort({}, img) | ||
|
||
expect(res).toBeUndefined() | ||
}) | ||
|
||
describe('arguments', () => { | ||
test('invalid', () => { | ||
const res = getEffort({ effort: 'invalid' }, img) | ||
|
||
expect(res).toBeUndefined() | ||
}) | ||
|
||
test('empty', () => { | ||
const res = getEffort({ effort: '' }, img) | ||
|
||
expect(res).toBeUndefined() | ||
}) | ||
|
||
test('integer', () => { | ||
const res = getEffort({ effort: '3' }, img) | ||
|
||
expect(res).toEqual(3) | ||
}) | ||
|
||
it('rounds float to int', () => { | ||
const res = getEffort({ effort: '3.5' }, img) | ||
|
||
expect(res).toEqual(3) | ||
}) | ||
|
||
it('sets to minimum effort with "min"', async () => { | ||
img[METADATA] = { format: 'webp' } | ||
const res = getEffort({ effort: 'min' }, img) | ||
|
||
expect(res).toEqual(0) | ||
}) | ||
|
||
it('sets to maximum effort with "max"', async () => { | ||
img[METADATA] = { format: 'webp' } | ||
const res = getEffort({ effort: 'max' }, img) | ||
|
||
expect(res).toEqual(6) | ||
}) | ||
|
||
it('ignores effort when not applicable', async () => { | ||
img[METADATA] = { format: 'jpeg' } | ||
const res = getEffort({ effort: 'max' }, img) | ||
|
||
expect(res).toBeUndefined() | ||
}) | ||
}) | ||
}) |
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,36 @@ | ||
import { TransformOption } from '../types.js' | ||
import { getMetadata, setMetadata } from '../lib/metadata.js' | ||
|
||
export interface EffortOptions { | ||
effort: string | ||
} | ||
|
||
const FORMAT_TO_EFFORT_RANGE: Record<string, [number, number]> = { | ||
avif: [0, 9], | ||
gif: [1, 10], | ||
heif: [0, 9], | ||
jxl: [3, 9], | ||
png: [1, 10], | ||
webp: [0, 6] | ||
} | ||
|
||
function parseEffort(effort: string, format: string) { | ||
if (effort === 'min') { | ||
return FORMAT_TO_EFFORT_RANGE[format]?.[0] | ||
} else if (effort === 'max') { | ||
return FORMAT_TO_EFFORT_RANGE[format]?.[1] | ||
} | ||
return parseInt(effort) | ||
} | ||
|
||
export const getEffort: TransformOption<EffortOptions, number> = ({ effort: _effort }, image) => { | ||
if (!_effort) return | ||
|
||
const format = (getMetadata(image, 'format') ?? '') as string | ||
const effort = parseEffort(_effort, format) | ||
if (!Number.isInteger(effort)) return | ||
|
||
setMetadata(image, 'effort', effort) | ||
|
||
return effort | ||
} |
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