diff --git a/lib/utils.ts b/lib/utils.ts index 37ec7718..d192c8c4 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -424,8 +424,8 @@ function token( srcChannelValues = (color as ColorTuple) .filter((a) => eq(typeof a, "number")); - - if (eq(typeof color, "object")) + // @ts-ignore: + if (eq(typeof color, "object") && !color?.length) // @ts-ignore: srcChannelValues = srcChannels.map((a) => color[a]); @@ -456,13 +456,13 @@ function token( const res = targetMode ? parseToken(result, targetMode) : result; srcChannels = targetMode ? gmchn(targetMode) : srcChannels; - if (and(and(eq(srcMode, "rgb"), normalizeRgb), not(targetMode))) { + if (and(and(eq(srcMode, "rgb"), normalizeRgb), not(targetMode))) // @ts-ignore: - if (srcChannels.some((c) => gt(Math.abs(result[c]), 1))) { + if (srcChannels.some((c) => gt(Math.abs(result[c]), 1))) // @ts-ignore: for (const k of srcChannels) result[k] /= 255; - } - } + + if (eq(col, "obj")) { omitMode diff --git a/tests/runner.ts b/tests/runner.ts index 059e314d..b93b44cb 100644 --- a/tests/runner.ts +++ b/tests/runner.ts @@ -5,14 +5,15 @@ import { expect } from "jsr:@std/expect"; export type Spec = { description?: string; - matcher?: string; callback: unknown; params: unknown[]; }; export default function (specs: Spec[]) { - for (const spec of specs) + for (const spec of specs) { + console.log(`${spec?.description} \n`, spec?.callback(...spec?.params), `\n`) Deno.test(spec?.description || "Running test...", () => { expect(spec?.callback(...spec?.params)).toBeTruthy() }); + } } diff --git a/tests/utils.test.ts b/tests/utils.test.ts new file mode 100644 index 00000000..ca554753 --- /dev/null +++ b/tests/utils.test.ts @@ -0,0 +1,41 @@ +import { token } from "../lib/utils.ts"; +import runner, { type Spec } from "./runner.ts"; + + + + +const str = '#ffc300', arr = ['rgb', 0.4, 0.3, 0.1], obj = { r: 0.2, g: 0.4, b: 0.5, mode: 'rgb' } +const specs: Spec[] = [{ + description: "converts an object to a number", + callback: token, + params: [obj, { kind: 'num', }] +}, +{ + description: "converts a hex string to an array of channel values", + callback: token, + params: [str, { kind: 'arr', targetMode: 'lab' }] +}, +{ + description: "converts an array to a hex string", + callback: token, + params: [arr, { kind: 'str' }] +}, +{ + description: "converts an array to an object", + callback: token, + params: [arr, { kind: 'obj' }] +}, +{ + description: "converts an object to an array", + callback: token, + params: [obj, { kind: 'arr' }] +}, +{ + description: "converts an array to a number", + params: [arr, { kind: 'num' }], + callback: token +} +]; + + +runner(specs) \ No newline at end of file