From 2556813a54cd129df9b8b47327a42d8ff28ba489 Mon Sep 17 00:00:00 2001 From: Lloyd Kupchanko Date: Sat, 24 Feb 2024 21:09:34 -0700 Subject: [PATCH 1/2] Allow getColor to work with an array of color types Added the ability to pass an array of color types to getColor. This allows you to do: let [c1, c2] = getColor(["red", "blue"]) instead of the more repetitive let c1 = getColor("red") let c2 = getColor("blue") --- src/getColor.js | 4 ++++ types/src/getColor.d.ts | 1 + types/test/getColor.ts | 7 +++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/getColor.js b/src/getColor.js index 707bdbe81..f0d40b78b 100644 --- a/src/getColor.js +++ b/src/getColor.js @@ -8,6 +8,10 @@ import parse from "./parse.js"; * @returns {{space, coords, alpha}} */ export default function getColor (color) { + if (Array.isArray(color)) { + return color.map(getColor); + } + if (!color) { throw new TypeError("Empty color reference"); } diff --git a/types/src/getColor.d.ts b/types/src/getColor.d.ts index a7ca667c1..084cc0e13 100644 --- a/types/src/getColor.d.ts +++ b/types/src/getColor.d.ts @@ -1,3 +1,4 @@ import { PlainColorObject, ColorTypes } from "./color.js"; export default function getColor (color: ColorTypes): PlainColorObject; +export default function getColor (color: ColorTypes[]): PlainColorObject[]; diff --git a/types/test/getColor.ts b/types/test/getColor.ts index a38b6fc57..406861dcd 100644 --- a/types/test/getColor.ts +++ b/types/test/getColor.ts @@ -4,5 +4,8 @@ import getColor from "colorjs.io/src/getColor"; // @ts-expect-error getColor(); -getColor("red"); -getColor(new Color("red")); +getColor("red"); // $ExpectType PlainColorObject +getColor(new Color("red")); // $ExpectType PlainColorObject + +getColor(["red", "blue"]); // $ExpectType PlainColorObject[] + From 618a318f51a99d3af622f9fd526391f1e0099096 Mon Sep 17 00:00:00 2001 From: Lloyd Kupchanko Date: Sun, 25 Feb 2024 09:06:49 -0700 Subject: [PATCH 2/2] Update JSDoc comments --- src/getColor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/getColor.js b/src/getColor.js index f0d40b78b..04af389ff 100644 --- a/src/getColor.js +++ b/src/getColor.js @@ -4,8 +4,8 @@ import parse from "./parse.js"; /** * Resolves a color reference (object or string) to a plain color object - * @param {Color | {space, coords, alpha} | string} color - * @returns {{space, coords, alpha}} + * @param {Color | {space, coords, alpha} | string | Array } color + * @returns {{space, coords, alpha} | Array<{space, coords, alpha}}> */ export default function getColor (color) { if (Array.isArray(color)) {