-
Notifications
You must be signed in to change notification settings - Fork 6
Add flip option support
#73
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| "@imgproxy/imgproxy-js-core": minor | ||
| --- | ||
|
|
||
| Add [flip](https://docs.imgproxy.net/latest/usage/processing#flip) option support |
This file contains hidden or 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,20 @@ | ||
| import type { FlipOptions, FlipOptionsPartial } from "../types/flip"; | ||
| import { guardIsUndef, normalizeBoolean } from "../utils"; | ||
|
|
||
| const getOpt = (options: FlipOptionsPartial): FlipOptions | undefined => | ||
| options.flip || options.fl; | ||
|
|
||
| const test = (options: FlipOptionsPartial): boolean => Boolean(getOpt(options)); | ||
|
|
||
| const build = (options: FlipOptionsPartial): string => { | ||
| const flipOptions = getOpt(options); | ||
|
|
||
| guardIsUndef(flipOptions, "flip"); | ||
|
|
||
| const horizontal = flipOptions.horizontal ?? false; | ||
| const vertical = flipOptions.vertical ?? false; | ||
|
|
||
| return `fl:${normalizeBoolean(horizontal)}:${normalizeBoolean(vertical)}`; | ||
| }; | ||
|
|
||
| export { test, build }; |
This file contains hidden or 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 hidden or 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,43 @@ | ||
| /** | ||
| * Boolean-like values for flip options | ||
| * | ||
| * @note Only `1`, `"t"`, or `true` are recognized as truthy values. | ||
| * Any other value (including `"true"` or `"1"` as strings) will be treated as false. | ||
| */ | ||
| type FlipBooleanValue = boolean | 1 | 0 | "t" | "f"; | ||
|
|
||
| /** | ||
| * *Flip option* | ||
| * | ||
| * When set, imgproxy will flip the image along the specified axes. | ||
| * | ||
| * @default false:false | ||
| * | ||
| * @see {@link https://docs.imgproxy.net/latest/usage/processing#flip | Flip imgproxy docs} | ||
| */ | ||
| type FlipOptions = { | ||
| /** | ||
| * When set to `1`, `"t"`, or boolean `true`, imgproxy will flip the image horizontally. | ||
| * @default false | ||
| */ | ||
| horizontal?: FlipBooleanValue; | ||
| /** | ||
| * When set to `1`, `t`, or boolean `true`, imgproxy will flip the image vertically. | ||
| * @default false | ||
| */ | ||
| vertical?: FlipBooleanValue; | ||
| }; | ||
|
|
||
| /** | ||
| * *Flip option* | ||
| * | ||
| * To describe the flip option, you can use the keyword `flip` or `fl`. | ||
| * | ||
| * @see https://docs.imgproxy.net/latest/usage/processing#flip | ||
| */ | ||
| interface FlipOptionsPartial { | ||
| flip?: FlipOptions; | ||
| fl?: FlipOptions; | ||
| } | ||
|
|
||
| export { FlipOptions, FlipOptionsPartial }; | ||
This file contains hidden or 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 hidden or 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,88 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { test, build } from "../../src/options/flip"; | ||
|
|
||
| describe("flip", () => { | ||
| describe("test", () => { | ||
| it("should return true if flip option is defined", () => { | ||
| expect(test({ flip: { horizontal: true } })).toEqual(true); | ||
| }); | ||
|
|
||
| it("should return true if fl option is defined", () => { | ||
| expect(test({ fl: { vertical: true } })).toEqual(true); | ||
| }); | ||
|
|
||
| it("should return false if flip option is undefined", () => { | ||
| expect(test({})).toEqual(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("build", () => { | ||
| it("should throw an error if flip is undefined", () => { | ||
| expect(() => build({})).toThrow("flip option is undefined"); | ||
| }); | ||
|
|
||
| it("should return fl:f:f if both horizontal and vertical are false", () => { | ||
| expect(build({ flip: { horizontal: false, vertical: false } })).toEqual( | ||
| "fl:f:f" | ||
| ); | ||
| }); | ||
|
|
||
| it("should return fl:f:f if flip object is empty (defaults)", () => { | ||
| expect(build({ flip: {} })).toEqual("fl:f:f"); | ||
| }); | ||
|
|
||
| it("should return fl:t:f if horizontal is true", () => { | ||
| expect(build({ flip: { horizontal: true } })).toEqual("fl:t:f"); | ||
| }); | ||
|
|
||
| it("should return fl:f:t if vertical is true", () => { | ||
| expect(build({ flip: { vertical: true } })).toEqual("fl:f:t"); | ||
| }); | ||
|
|
||
| it("should return fl:t:t if both are true", () => { | ||
| expect(build({ flip: { horizontal: true, vertical: true } })).toEqual( | ||
| "fl:t:t" | ||
| ); | ||
| }); | ||
|
|
||
| it("should support horizontal as 1", () => { | ||
| expect(build({ flip: { horizontal: 1 } })).toEqual("fl:t:f"); | ||
| }); | ||
|
|
||
| it("should support horizontal as 't'", () => { | ||
| expect(build({ flip: { horizontal: "t" } })).toEqual("fl:t:f"); | ||
| }); | ||
|
|
||
| it("should support vertical as 1", () => { | ||
| expect(build({ flip: { vertical: 1 } })).toEqual("fl:f:t"); | ||
| }); | ||
|
|
||
| it("should support vertical as 't'", () => { | ||
| expect(build({ flip: { vertical: "t" } })).toEqual("fl:f:t"); | ||
| }); | ||
|
|
||
| it("should support both with various truthy values", () => { | ||
| expect(build({ flip: { horizontal: 1, vertical: "t" } })).toEqual( | ||
| "fl:t:t" | ||
| ); | ||
| }); | ||
|
|
||
| it("should support 0 as false for horizontal", () => { | ||
| expect(build({ flip: { horizontal: 0, vertical: true } })).toEqual( | ||
| "fl:f:t" | ||
| ); | ||
| }); | ||
11bit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it("should work with fl shorthand", () => { | ||
| expect(build({ fl: { horizontal: true, vertical: false } })).toEqual( | ||
| "fl:t:f" | ||
| ); | ||
| }); | ||
|
|
||
| it("should work with both horizontal and vertical using different value types", () => { | ||
| expect(build({ flip: { horizontal: true, vertical: 1 } })).toEqual( | ||
| "fl:t:t" | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.