Skip to content

Commit

Permalink
token parser now performs back and forth conversions without throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlwizard committed Nov 27, 2024
1 parent 31b6190 commit 660f787
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});
}

}
41 changes: 41 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 660f787

Please sign in to comment.