Skip to content

Commit

Permalink
Use null for none instead of NaN, closes #409
Browse files Browse the repository at this point in the history
Also moves the none tests to their own separate section
  • Loading branch information
LeaVerou committed May 23, 2024
1 parent 62066a8 commit cdf6f0d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export function parseArgument (rawArg) {
meta.type = "<number>";
}
else if (value === "none") {
value = NaN;
value = null;
meta.none = true;
}
else if (value === "NaN" || value === "calc(NaN)") {
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function serializeNumber (n, {precision, unit }) {
* @returns {boolean}
*/
export function isNone (n) {
return Number.isNaN(n) || (n instanceof Number && n?.none);
return n === null;
}

/**
Expand Down
72 changes: 37 additions & 35 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,41 @@ const tests = {
description: "These tests parse different color formats and compare the result as JSON",
run: parse,
tests: [
{
name: "none values",
tests: [
{
name: "none hue in lch()",
args: "lch(90 0 none)",
expect: {spaceId: "lch", coords: [90, 0, null], alpha: 1},
},
{
name: "none hue in oklch()",
args: "oklch(1 0 none)",
expect: {spaceId: "oklch", coords: [1, 0, null], alpha: 1},
},
{
name: "none hue in hsl()",
args: "hsl(none, 50%, 50%)",
expect: {spaceId: "hsl", coords: [null, 50, 50], alpha: 1},
},
{
name: "none hue in hwb()",
args: "hwb(none 20% 30%)",
expect: {spaceId: "hwb", coords: [null, 20, 30], alpha: 1},
},
{
name: "none alpha in oklch()",
args: "oklch(1 0 120 / none)",
expect: {spaceId: "oklch", coords: [1, 0, 120], alpha: null},
},
{
name: "none red in color(display-p3)",
args: "color(display-p3 none 1 .5)",
expect: {spaceId: "p3", coords: [null, 1, 0.5], alpha: 1},
},
],
},
{
name: "NaN values",
tests: [
Expand Down Expand Up @@ -126,11 +161,7 @@ const tests = {
args: "lch(100 50 450)",
expect: {spaceId: "lch", coords: [100, 50, 450], alpha: 1},
},
{
name: "none hue",
args: "lch(90 0 none)",
expect: {spaceId: "lch", coords: [90, 0, NaN], alpha: 1},
},

],
},
{
Expand Down Expand Up @@ -209,16 +240,6 @@ const tests = {
args: "oklch(100% 0 30deg)",
expect: {spaceId: "oklch", coords: [1, 0, 30], alpha: 1},
},
{
name: "none hue",
args: "oklch(1 0 none)",
expect: {spaceId: "oklch", coords: [1, 0, NaN], alpha: 1},
},
{
name: "none alpha",
args: "oklch(1 0 120 / none)",
expect: {spaceId: "oklch", coords: [1, 0, 120], alpha: NaN},
},
],
},
{
Expand Down Expand Up @@ -441,11 +462,6 @@ const tests = {
args: "color(display-p3 1 / .5)",
expect: {spaceId: "p3", coords: [1, 0, 0], alpha: 0.5},
},
{
name: "none red",
args: "color(display-p3 none 1 .5)",
expect: {spaceId: "p3", coords: [NaN, 1, 0.5], alpha: 1},
},
],
},
{
Expand Down Expand Up @@ -486,21 +502,12 @@ const tests = {
args: "hsl(0.25turn 0% 0% / .5)",
expect: {spaceId: "hsl", coords: [90, 0, 0], alpha: 0.5},
},
{
name: "hsla(), none hue, spaces and slash",
args: "hsl(none 0% 0% / .5)",
expect: {spaceId: "hsl", coords: [NaN, 0, 0], alpha: 0.5},
},
{
name: "hsla(), oog color(rec2020 0 0 1)",
args: "hsl(230.6 179.7% 37.56% / 1)",
expect: {spaceId: "hsl", coords: [230.6, 179.7, 37.56], alpha: 1},
},
{
name: "hsl(), none hue ",
args: "hsl(none, 50%, 50%)",
expect: {spaceId: "hsl", coords: [NaN, 50, 50], alpha: 1},
},

],
},
{
Expand All @@ -510,11 +517,6 @@ const tests = {
args: "hwb(180 20% 30%)",
expect: {spaceId: "hwb", coords: [180, 20, 30], alpha: 1},
},
{
name: "none hue",
args: "hwb(none 20% 30%)",
expect: {spaceId: "hwb", coords: [NaN, 20, 30], alpha: 1},
},
{
args: "hwb(180 20 30)",
expect: {spaceId: "hwb", coords: [180, 20, 30], alpha: 1},
Expand Down

0 comments on commit cdf6f0d

Please sign in to comment.