From a1d33ab786aeda2b9008a6e46f330d1a6ac27dc8 Mon Sep 17 00:00:00 2001 From: fcrozatier Date: Fri, 17 Jan 2025 09:48:22 +0100 Subject: [PATCH 1/2] improve guards --- src/wrapper.ts | 4 ++-- tests/behaviors/guards.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/behaviors/guards.test.ts diff --git a/src/wrapper.ts b/src/wrapper.ts index 1f002d4..ad59eb4 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -44,7 +44,7 @@ export namespace Signal { #brand() {} static { - isState = (s) => #brand in s; + isState = (s) => typeof s === "object" && #brand in s; } constructor(initialValue: T, options: Signal.Options = {}) { @@ -84,7 +84,7 @@ export namespace Signal { #brand() {} // eslint-disable-next-line @typescript-eslint/no-explicit-any static { - isComputed = (c: any) => #brand in c; + isComputed = (c: any) => typeof c === "object" && #brand in c; } // Create a Signal which evaluates to the value returned by the callback. diff --git a/tests/behaviors/guards.test.ts b/tests/behaviors/guards.test.ts new file mode 100644 index 0000000..fd466fe --- /dev/null +++ b/tests/behaviors/guards.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; +import { Signal } from "../../src/wrapper.js"; + +describe("Guards", () => { + it("should work with Signals", () => { + const state = new Signal.State(1); + const computed = new Signal.Computed(() => state.get() * 2); + expect(Signal.isState(state)).toBe(true); + expect(Signal.isComputed(state)).toBe(false); + + expect(Signal.isState(computed)).toBe(false); + expect(Signal.isComputed(computed)).toBe(true); + }); + + it("shouldn't error with values", () => { + expect(Signal.isState(1)).toBe(false); + expect(Signal.isComputed(2)).toBe(false); + + expect(Signal.isState({})).toBe(false); + expect(Signal.isComputed({})).toBe(false); + }); +}); From 2d6b9d831cf884374951c37c51e8149dc33ffe60 Mon Sep 17 00:00:00 2001 From: fcrozatier Date: Fri, 17 Jan 2025 16:26:05 +0100 Subject: [PATCH 2/2] run prettier --- src/wrapper.ts | 4 ++-- tests/behaviors/guards.test.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wrapper.ts b/src/wrapper.ts index ad59eb4..e15af74 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -44,7 +44,7 @@ export namespace Signal { #brand() {} static { - isState = (s) => typeof s === "object" && #brand in s; + isState = (s) => typeof s === 'object' && #brand in s; } constructor(initialValue: T, options: Signal.Options = {}) { @@ -84,7 +84,7 @@ export namespace Signal { #brand() {} // eslint-disable-next-line @typescript-eslint/no-explicit-any static { - isComputed = (c: any) => typeof c === "object" && #brand in c; + isComputed = (c: any) => typeof c === 'object' && #brand in c; } // Create a Signal which evaluates to the value returned by the callback. diff --git a/tests/behaviors/guards.test.ts b/tests/behaviors/guards.test.ts index fd466fe..d66a143 100644 --- a/tests/behaviors/guards.test.ts +++ b/tests/behaviors/guards.test.ts @@ -1,8 +1,8 @@ -import { describe, expect, it } from "vitest"; -import { Signal } from "../../src/wrapper.js"; +import {describe, expect, it} from 'vitest'; +import {Signal} from '../../src/wrapper.js'; -describe("Guards", () => { - it("should work with Signals", () => { +describe('Guards', () => { + it('should work with Signals', () => { const state = new Signal.State(1); const computed = new Signal.Computed(() => state.get() * 2); expect(Signal.isState(state)).toBe(true);