From f7f190ceb523504956065959436fa57f4d60f4f0 Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Thu, 28 Dec 2023 17:48:45 +0000 Subject: [PATCH 1/5] Define default options as separate object --- lib/options.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/options.ts b/lib/options.ts index 25926b5..1cb1498 100644 --- a/lib/options.ts +++ b/lib/options.ts @@ -1,8 +1,10 @@ import { z } from "https://deno.land/x/zod@v3.22.4/mod.ts"; +const defaults = { radius: 0, size: 64 }; + const validator = z.object({ - radius: z.number().int().min(0).default(0), - size: z.number().int().min(1).default(64), + radius: z.number().int().min(0).default(defaults.radius), + size: z.number().int().min(1).default(defaults.size), }); type Options = z.input; @@ -14,5 +16,5 @@ function validate(opts: Options): Options { return validator.parse(opts); } -export { validate }; export type { Options }; +export { defaults, validate }; From bbdd0abaa72f8f16a482090db49ac9b92db239fb Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Thu, 28 Dec 2023 17:50:09 +0000 Subject: [PATCH 2/5] Create separate types for pre- and post-validated options --- lib/options.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/options.ts b/lib/options.ts index 1cb1498..9754722 100644 --- a/lib/options.ts +++ b/lib/options.ts @@ -7,14 +7,15 @@ const validator = z.object({ size: z.number().int().min(1).default(defaults.size), }); -type Options = z.input; +type InputOptions = z.input; +type OutputOptions = z.output; /** * Validates and normalizes the given options. Raises if validation fails. */ -function validate(opts: Options): Options { +function validate(opts: InputOptions): OutputOptions { return validator.parse(opts); } -export type { Options }; export { defaults, validate }; +export type { InputOptions, OutputOptions }; From 1ec9206fea454610606bcb8b37a007526fdc7904 Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Thu, 28 Dec 2023 17:51:27 +0000 Subject: [PATCH 3/5] Fix bug whereby radius scales with size Closes #6. --- lib/png.ts | 7 +++++-- lib/svg.ts | 13 +++++++------ tests/lib/svg_test.ts | 8 ++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/png.ts b/lib/png.ts index 320838d..013a586 100644 --- a/lib/png.ts +++ b/lib/png.ts @@ -1,12 +1,15 @@ import { resvg } from "../deps.ts"; import * as svg from "./svg.ts"; -import type { Options } from "./options.ts"; +import type { InputOptions } from "./options.ts"; /** * Generates a reproducible PNG image from a given seed. * Renders the image as an SVG, and then converts it to a PNG using resvg. */ -async function generate(seed: string, opts?: Options): Promise { +async function generate( + seed: string, + opts?: InputOptions, +): Promise { const svgString = await svg.generate(seed, opts); return resvg.render(svgString); } diff --git a/lib/svg.ts b/lib/svg.ts index f8db26d..56d1cd8 100644 --- a/lib/svg.ts +++ b/lib/svg.ts @@ -1,19 +1,20 @@ import * as keys from "./keys.ts"; import miniavs from "./collections/miniavs/index.ts"; -import * as options from "./options.ts"; -import type { Options } from "./options.ts"; +import { defaults, validate } from "./options.ts"; +import type { InputOptions } from "./options.ts"; /** * Generates a reproducible SVG image for the given seed. */ -async function generate(seed: string, opts?: Options): Promise { +async function generate(seed: string, opts?: InputOptions): Promise { const key = await keys.generate(seed); - opts = options.validate(opts || {}); + const { radius, size } = validate(opts || {}); + const scaledRadius = (defaults.size / size) * radius; return ` - + - + diff --git a/tests/lib/svg_test.ts b/tests/lib/svg_test.ts index f5dfdeb..9ac107d 100644 --- a/tests/lib/svg_test.ts +++ b/tests/lib/svg_test.ts @@ -53,6 +53,14 @@ Deno.test("svg", async (t) => { }, ); + await t.step("it treats the radius as an absolute value", async () => { + // 64 is the default size, 256 is the given size, 10 is the desired radius. + const radius = (64 / 256) * 10; + const regex = new RegExp(`]+ rx="${radius}" ry="${radius}"`, "is"); + const svg = (await generate("test", { size: 256, radius: 10 })).trim(); + assert(regex.test(svg)); + }); + // keys_test.ts covers the seed validation, this is just a sanity check. await t.step("it raises if the seed is invalid", () => { assertRejects(() => generate(1)); From 0f9438aa7f0c46f19154c6e6c714e3d6e40a6793 Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Thu, 28 Dec 2023 17:51:48 +0000 Subject: [PATCH 4/5] Fix formatting --- CONTRIBUTING.md | 17 ++++++++++++----- README.md | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c21aa4f..1d1298a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,13 +1,20 @@ # Contributing -Thank you for thinking about contributing to this project. This document will help you to get started. + +Thank you for thinking about contributing to this project. This document will +help you to get started. ## Adding a feature -If you'd like to add a feature to Avatar (or modify its existing behaviour), please start by [creating an issue][create_issue] so we can discuss it. -Don't go straight to a pull request in case it's something I don't wish to accept; I don't want to waste your time. + +If you'd like to add a feature to Avatar (or modify its existing behaviour), +please start by [creating an issue][create_issue] so we can discuss it. Don't go +straight to a pull request in case it's something I don't wish to accept; I +don't want to waste your time. ## Fixing a bug -If you've found a bug, please start by [creating an issue][create_issue]. If you'd like to [make a pull request][create_pr] for the fix once I've confirmed it's a bug in Avatar, that would be great. + +If you've found a bug, please start by [creating an issue][create_issue]. If +you'd like to [make a pull request][create_pr] for the fix once I've confirmed +it's a bug in Avatar, that would be great. [create_issue]: https://github.com/monooso/avatar/issues/new [create_pr]: https://github.com/monooso/avatar/compare - diff --git a/README.md b/README.md index 3c8792e..ffee4a7 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ avatar = await generatePng("cleetus", { radius: 20, size: 256 }); ``` ## License + Avatar is open source software, released under [the MIT license](./LICENSE.txt). ## Credits From 58cf9c475602ebe9217a098182946f55d465af91 Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Thu, 28 Dec 2023 17:54:02 +0000 Subject: [PATCH 5/5] Remove version number from README example --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index ffee4a7..044ada3 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,7 @@ avatars based on usernames or ids. ## Usage ```typescript -import { - generatePng, - generateSvg, -} from "https://deno.land/x/avatar@v1.3.0/mod.ts"; +import { generatePng, generateSvg } from "https://deno.land/x/avatar/mod.ts"; // Generate an SVG avatar with the default options. let avatar = await generateSvg("jimbob");