-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add Cloudimage support * add to subdomains
- Loading branch information
Showing
7 changed files
with
103 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { ParsedUrl } from "../types.ts"; | ||
import { parse, transform, CloudimageParams } from "./cloudimage.ts"; | ||
|
||
Deno.test("Cloudimage parser", async (t) => { | ||
await t.step("parses a URL", () => { | ||
const parsed = parse("https://doc.cloudimg.io/sample.li/flat1.jpg?w=450&h=200&q=90"); | ||
const expected: ParsedUrl<CloudimageParams> = { | ||
base: "https://doc.cloudimg.io/sample.li/flat1.jpg", | ||
cdn: "cloudimage", | ||
width: 450, | ||
height: 200, | ||
params: { | ||
quality: 90 | ||
}, | ||
}; | ||
assertEquals(parsed, expected); | ||
}); | ||
|
||
await t.step("parses a URL without transforms", () => { | ||
const parsed = parse("https://doc.cloudimg.io/sample.li/flat1.jpg"); | ||
const expected: ParsedUrl<CloudimageParams> = { | ||
base: "https://doc.cloudimg.io/sample.li/flat1.jpg", | ||
cdn: "cloudimage", | ||
width: undefined, | ||
height: undefined, | ||
params: { | ||
quality: undefined | ||
}, | ||
}; | ||
assertEquals(parsed, expected); | ||
}); | ||
}); | ||
|
||
Deno.test("Cloudimage transformer", async (t) => { | ||
await t.step("should format a URL", () => { | ||
const result = transform({ | ||
url: "https://doc.cloudimg.io/sample.li/flat1.jpg?q=90", | ||
width: 450, | ||
height: 200 | ||
}); | ||
assertEquals( | ||
result?.toString(), | ||
"https://doc.cloudimg.io/sample.li/flat1.jpg?q=90&w=450&h=200", | ||
); | ||
}); | ||
await t.step("should not set height if not provided", () => { | ||
const result = transform({ url: "https://doc.cloudimg.io/sample.li/flat1.jpg?q=90", width: 450 }); | ||
assertEquals( | ||
result?.toString(), | ||
"https://doc.cloudimg.io/sample.li/flat1.jpg?q=90&w=450", | ||
); | ||
}); | ||
}); |
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,38 @@ | ||
import { UrlParser, UrlTransformer } from "../types.ts"; | ||
import { | ||
getNumericParam, | ||
setParamIfDefined, | ||
toUrl, | ||
} from "../utils.ts"; | ||
|
||
export interface CloudimageParams { | ||
quality?: number; | ||
} | ||
|
||
export const parse: UrlParser<CloudimageParams> = (url) => { | ||
const parsedUrl = toUrl(url); | ||
|
||
const width = getNumericParam(parsedUrl, "w") || undefined; | ||
const height = getNumericParam(parsedUrl, "h") || undefined; | ||
const quality = getNumericParam(parsedUrl, "q") || undefined; | ||
parsedUrl.search = ""; | ||
return { | ||
width, | ||
height, | ||
base: parsedUrl.toString(), | ||
params: { | ||
quality | ||
}, | ||
cdn: "cloudimage", | ||
}; | ||
}; | ||
|
||
export const transform: UrlTransformer = ( | ||
{ url: originalUrl, width, height }, | ||
) => { | ||
const url = toUrl(originalUrl); | ||
setParamIfDefined(url, "w", width, true, true); | ||
setParamIfDefined(url, "h", height, true, true); | ||
setParamIfDefined(url, "q", getNumericParam(url, "q"), true); | ||
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