From ad40b02ed2241ced68496f3e9570f97be58e90d7 Mon Sep 17 00:00:00 2001 From: Henri Binsztok <808274+hbbio@users.noreply.github.com> Date: Mon, 5 Feb 2024 11:36:57 +0800 Subject: [PATCH 1/3] cellify: fix Object detection --- src/cellify.test.ts | 8 ++++++++ src/cellify.ts | 7 +++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/cellify.test.ts b/src/cellify.test.ts index 90b550c..60e6929 100644 --- a/src/cellify.test.ts +++ b/src/cellify.test.ts @@ -37,3 +37,11 @@ test("fix point", async () => { expect(u).toEqual(v); } }); + +test("_cellify one", async () => { + const sheet = new Sheet(); + const proxy = new SheetProxy(sheet); + const res = _cellify(proxy, { a: 1 }); + const cell = await res.get(); + await expect(cell.a.get()).resolves.toBe(1); +}); diff --git a/src/cellify.ts b/src/cellify.ts index 375c6ec..649aa0a 100644 --- a/src/cellify.ts +++ b/src/cellify.ts @@ -34,9 +34,7 @@ export const _cellify = (proxy: SheetProxy, v: T): Cellified => { return proxy.new( Array.isArray(v) ? v.map((vv) => _cellify(proxy, vv), "cellify.[]") - : typeof v === "object" && - v !== null && - v?.constructor?.prototype !== Object.prototype // exclude classes + : typeof v === "object" && v !== null && v.constructor?.name === "Object" // exclude classes ? Object.fromEntries( Object.entries(v).map( ([k, vv]) => [k, _cellify(proxy, vv)], @@ -65,12 +63,13 @@ export const _uncellify = async ( if ( typeof value === "object" && value !== null && - v?.constructor?.prototype === Object.prototype + value.constructor?.name === "Object" // exclude classes ) return Object.fromEntries( await Promise.all( Object.entries(value).map(async ([k, vv]) => [k, await _uncellify(vv)]) ) ); + // Classes, null or base types (string, number, ...) return value as Uncellified; }; From 3454f6473e9353f4e9b33b6c678c6a8926cf395d Mon Sep 17 00:00:00 2001 From: Henri Binsztok <808274+hbbio@users.noreply.github.com> Date: Mon, 5 Feb 2024 12:05:36 +0800 Subject: [PATCH 2/3] cellify: revert to v.constructor.prototype --- src/cellify.ts | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/cellify.ts b/src/cellify.ts index 649aa0a..440bc25 100644 --- a/src/cellify.ts +++ b/src/cellify.ts @@ -11,15 +11,16 @@ export type Cellified = T extends object : ValueCell; // Uncellified computes an uncellified type. -export type Uncellified = T extends AnyCell - ? U extends object - ? U extends Array - ? Array> - : { - [P in keyof U]: Uncellified; - } - : U - : T; +export type Uncellified = + T extends AnyCell + ? U extends object + ? U extends Array + ? Array> + : { + [P in keyof U]: Uncellified; + } + : U + : T; /** * cellify converts any value to a Cellified value where each array or record @@ -29,20 +30,22 @@ export type Uncellified = T extends AnyCell * @returns * @todo cell reuses */ -export const _cellify = (proxy: SheetProxy, v: T): Cellified => { +export const _cellify = (proxy: SheetProxy, v: T): Cellified => { if (v instanceof Cell) throw new Error("cell"); return proxy.new( Array.isArray(v) ? v.map((vv) => _cellify(proxy, vv), "cellify.[]") - : typeof v === "object" && v !== null && v.constructor?.name === "Object" // exclude classes + : typeof v === "object" && + v !== null && + v.constructor.prototype === Object.prototype // exclude classes ? Object.fromEntries( Object.entries(v).map( ([k, vv]) => [k, _cellify(proxy, vv)], - "cellify.{}" - ) + "cellify.{}", + ), ) : v, - "cellify" + "cellify", ) as Cellified; }; @@ -51,24 +54,24 @@ export const _cellify = (proxy: SheetProxy, v: T): Cellified => { * @param v any value * @returns value without cells */ -export const _uncellify = async ( - v: T | AnyCell +export const _uncellify = async ( + v: T | AnyCell, ): Promise> => { const value = v instanceof Cell ? await v.get() : v; if (value instanceof Error) throw value; if (Array.isArray(value)) return Promise.all( - value.map(async (_element) => await _uncellify(_element)) + value.map(async (_element) => await _uncellify(_element)), ) as Promise>; if ( typeof value === "object" && value !== null && - value.constructor?.name === "Object" // exclude classes + value.constructor.prototype === Object.prototype // exclude classes ) return Object.fromEntries( await Promise.all( - Object.entries(value).map(async ([k, vv]) => [k, await _uncellify(vv)]) - ) + Object.entries(value).map(async ([k, vv]) => [k, await _uncellify(vv)]), + ), ); // Classes, null or base types (string, number, ...) return value as Uncellified; From d33712cf569f44f12832bce9ea3d1d35d567197e Mon Sep 17 00:00:00 2001 From: Henri Binsztok <808274+hbbio@users.noreply.github.com> Date: Mon, 5 Feb 2024 12:09:37 +0800 Subject: [PATCH 3/3] cellify: fix format --- src/cellify.ts | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/cellify.ts b/src/cellify.ts index 440bc25..68e7d34 100644 --- a/src/cellify.ts +++ b/src/cellify.ts @@ -11,16 +11,15 @@ export type Cellified = T extends object : ValueCell; // Uncellified computes an uncellified type. -export type Uncellified = - T extends AnyCell - ? U extends object - ? U extends Array - ? Array> - : { - [P in keyof U]: Uncellified; - } - : U - : T; +export type Uncellified = T extends AnyCell + ? U extends object + ? U extends Array + ? Array> + : { + [P in keyof U]: Uncellified; + } + : U + : T; /** * cellify converts any value to a Cellified value where each array or record @@ -30,7 +29,7 @@ export type Uncellified = * @returns * @todo cell reuses */ -export const _cellify = (proxy: SheetProxy, v: T): Cellified => { +export const _cellify = (proxy: SheetProxy, v: T): Cellified => { if (v instanceof Cell) throw new Error("cell"); return proxy.new( Array.isArray(v) @@ -41,11 +40,11 @@ export const _cellify = (proxy: SheetProxy, v: T): Cellified => { ? Object.fromEntries( Object.entries(v).map( ([k, vv]) => [k, _cellify(proxy, vv)], - "cellify.{}", - ), + "cellify.{}" + ) ) : v, - "cellify", + "cellify" ) as Cellified; }; @@ -54,14 +53,14 @@ export const _cellify = (proxy: SheetProxy, v: T): Cellified => { * @param v any value * @returns value without cells */ -export const _uncellify = async ( - v: T | AnyCell, +export const _uncellify = async ( + v: T | AnyCell ): Promise> => { const value = v instanceof Cell ? await v.get() : v; if (value instanceof Error) throw value; if (Array.isArray(value)) return Promise.all( - value.map(async (_element) => await _uncellify(_element)), + value.map(async (_element) => await _uncellify(_element)) ) as Promise>; if ( typeof value === "object" && @@ -70,8 +69,8 @@ export const _uncellify = async ( ) return Object.fromEntries( await Promise.all( - Object.entries(value).map(async ([k, vv]) => [k, await _uncellify(vv)]), - ), + Object.entries(value).map(async ([k, vv]) => [k, await _uncellify(vv)]) + ) ); // Classes, null or base types (string, number, ...) return value as Uncellified;