-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Builder.io * Fix accidentally checked in file * Update src/transformers/builder.ts Co-authored-by: Matt Kane <m@mk.gg> * PR feedback and aim to get tests to pass * Small tweaks * Minor fix * Fix test * Tests passing * Fixes * Minor CSS suggestion --------- Co-authored-by: Matt Kane <m@mk.gg>
- Loading branch information
Showing
9 changed files
with
111 additions
and
2 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
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
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,58 @@ | ||
import { assertEquals } from "https://deno.land/std@0.172.0/testing/asserts.ts"; | ||
|
||
import { transform } from "./builder.ts"; | ||
|
||
const img = | ||
"https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F462d29d57dda42cb9e26441501db535f"; | ||
|
||
Deno.test("builder.io", async (t) => { | ||
await t.step("should format a URL", () => { | ||
const result = transform({ | ||
url: img, | ||
width: 200, | ||
height: 100, | ||
}); | ||
assertEquals( | ||
result?.toString(), | ||
"https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F462d29d57dda42cb9e26441501db535f?fit=cover&width=200&height=100", | ||
); | ||
}); | ||
await t.step("should not set height if not provided", () => { | ||
const result = transform({ url: img, width: 200 }); | ||
assertEquals( | ||
result?.toString(), | ||
"https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F462d29d57dda42cb9e26441501db535f?fit=cover&width=200", | ||
); | ||
}); | ||
await t.step("should delete height if not set", () => { | ||
const url = new URL(img); | ||
url.searchParams.set("height", "100"); | ||
const result = transform({ url, width: 200 }); | ||
assertEquals( | ||
result?.toString(), | ||
"https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F462d29d57dda42cb9e26441501db535f?fit=cover&width=200", | ||
); | ||
}); | ||
|
||
await t.step("should round non-integer params", () => { | ||
const result = transform({ | ||
url: img, | ||
width: 200.6, | ||
height: 100.2, | ||
}); | ||
assertEquals( | ||
result?.toString(), | ||
"https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F462d29d57dda42cb9e26441501db535f?fit=cover&width=201&height=100", | ||
); | ||
}); | ||
|
||
await t.step("should not set fit=cover if another value exists", () => { | ||
const url = new URL(img); | ||
url.searchParams.set("fit", "inside"); | ||
const result = transform({ url, width: 200 }); | ||
assertEquals( | ||
result?.toString(), | ||
"https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F462d29d57dda42cb9e26441501db535f?fit=inside&width=200", | ||
); | ||
}); | ||
}); |
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,37 @@ | ||
import { UrlParser, UrlTransformer } from "../types.ts"; | ||
import { | ||
getNumericParam, | ||
setParamIfDefined, | ||
setParamIfUndefined, | ||
} from "../utils.ts"; | ||
|
||
export const parse: UrlParser<{ fit?: string; quality?: number }> = (url) => { | ||
const parsedUrl = new URL(url); | ||
|
||
const width = getNumericParam(parsedUrl, "width"); | ||
const height = getNumericParam(parsedUrl, "height"); | ||
const quality = getNumericParam(parsedUrl, "quality"); | ||
const format = parsedUrl.searchParams.get("format") || undefined; | ||
const fit = parsedUrl.searchParams.get("fit") || undefined; | ||
parsedUrl.search = ""; | ||
|
||
return { | ||
width, | ||
height, | ||
format, | ||
base: parsedUrl.toString(), | ||
params: { quality, fit }, | ||
cdn: "builder.io", | ||
}; | ||
}; | ||
|
||
export const transform: UrlTransformer = ( | ||
{ url: originalUrl, width, height, format }, | ||
) => { | ||
const url = new URL(originalUrl); | ||
setParamIfUndefined(url, "fit", "cover"); | ||
setParamIfDefined(url, "width", width, true, true); | ||
setParamIfDefined(url, "height", height, true, true); | ||
setParamIfDefined(url, "format", format); | ||
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