Skip to content

Commit

Permalink
cellify: fix failOnCell
Browse files Browse the repository at this point in the history
  • Loading branch information
hbbio committed Apr 26, 2024
1 parent f763d18 commit 4482b25
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/cellify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ test("follow", async () => {
await expect(f.get()).resolves.toBeInstanceOf(Error);
expect(sheet.stats).toEqual({ size: 13, count: 14 }); // unchanged
});

test("cellify failOnCell", async () => {
const sheet = new Sheet();
const proxy = new SheetProxy(sheet);
const v = { a: [1, 2, 3], b: { c: { foo: proxy.new(1, "1"), bar: 1 } } };
expect(() => _cellify(proxy, v, "cv", true)).toThrowError("value is cell");
});
10 changes: 7 additions & 3 deletions src/cellify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const isObject = <K extends string | number | symbol>(
): v is Record<K, unknown> =>
typeof v === "object" && v !== null && v.constructor?.name === "Object";

const errIsCell = new Error("value is cell");
/**
* cellify converts any value to a Cellified value where each array or record
* becomes a Cell in canonical form.
Expand All @@ -44,15 +45,18 @@ export const _cellify = <T>(
failOnCell = false
): Cellified<T> => {
if (v instanceof Cell) {
if (failOnCell) throw new Error("cell");
if (failOnCell) throw errIsCell;
return v as Cellified<T>;
}
return proxy.new(
Array.isArray(v)
? v.map((vv) => _cellify(proxy, vv), "cellify.[]")
? v.map((vv) => _cellify(proxy, vv, name, failOnCell), "cellify.[]")
: isObject(v)
? Object.fromEntries(
Object.entries(v).map(([k, vv]) => [k, _cellify(proxy, vv)], "ç{}")
Object.entries(v).map(
([k, vv]) => [k, _cellify(proxy, vv, name, failOnCell)],
"ç{}"
)
)
: v,
name
Expand Down

0 comments on commit 4482b25

Please sign in to comment.