From ce7d81a7d920d0024adbe6d5a6d5ab586fa36b1a Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:40:01 +0100 Subject: [PATCH] fix(utils): keep falsy non-nullish values in asArray --- .changeset/utils-as-array-falsy.md | 5 +++++ packages/utils/src/index.ts | 2 +- packages/utils/test/index.test.ts | 32 +++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .changeset/utils-as-array-falsy.md diff --git a/.changeset/utils-as-array-falsy.md b/.changeset/utils-as-array-falsy.md new file mode 100644 index 000000000..964474a57 --- /dev/null +++ b/.changeset/utils-as-array-falsy.md @@ -0,0 +1,5 @@ +--- +"@solid-primitives/utils": patch +--- + +Fix `asArray` dropping falsy values that are not nullish: `asArray(0)`, `asArray("")` and `asArray(false)` returned `[]` instead of `[0]`, `[""]` and `[false]`. diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index d571a9ce8..899d80ae5 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -107,7 +107,7 @@ export const access = >(v: T): MaybeAccessorValue(value: T): (T extends any[] ? T[number] : NonNullable)[] => - Array.isArray(value) ? (value as any) : value ? [value as any] : []; + Array.isArray(value) ? (value as any) : value != null ? [value as any] : []; /** * Access an array of MaybeAccessors diff --git a/packages/utils/test/index.test.ts b/packages/utils/test/index.test.ts index 1b6c68f21..d1de46abc 100644 --- a/packages/utils/test/index.test.ts +++ b/packages/utils/test/index.test.ts @@ -1,5 +1,11 @@ import { describe, test, expect, assert, vi } from "vitest"; -import { handleDiffArray, arrayEquals, createHydratableSignal, withAccess } from "../src/index.js"; +import { + handleDiffArray, + arrayEquals, + asArray, + createHydratableSignal, + withAccess, +} from "../src/index.js"; describe("handleDiffArray", () => { test("handleAdded called for new array", () => { @@ -124,3 +130,27 @@ describe("withAccess", () => { expect(fn).not.toHaveBeenCalled(); }); }); + +describe("asArray", () => { + test("wraps single values", () => { + expect(asArray("a")).toEqual(["a"]); + const obj = {}; + expect(asArray(obj)).toEqual([obj]); + }); + + test("keeps arrays as they are", () => { + const arr = [1, 2]; + expect(asArray(arr)).toBe(arr); + }); + + test("keeps falsy values that are not nullish", () => { + expect(asArray(0)).toEqual([0]); + expect(asArray("")).toEqual([""]); + expect(asArray(false)).toEqual([false]); + }); + + test("returns an empty array for undefined and null", () => { + expect(asArray(undefined)).toEqual([]); + expect(asArray(null)).toEqual([]); + }); +});