Skip to content

Commit

Permalink
cellify: isObject
Browse files Browse the repository at this point in the history
  • Loading branch information
hbbio committed Apr 4, 2024
1 parent b3ada91 commit 8eac607
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/cellify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export type Uncellified<T> = T extends AnyCell<infer U>
: U
: T;

// @todo is type only if true
// exclude classes
export const isObject = <K extends string | number | symbol>(
v: unknown
): v is Record<K, unknown> =>
typeof v === "object" && v !== null && v.constructor?.name === "Object";

/**
* cellify converts any value to a Cellified value where each array or record
* becomes a Cell in canonical form.
Expand All @@ -34,9 +41,7 @@ export const _cellify = <T>(proxy: SheetProxy, v: T): Cellified<T> => {
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
: isObject(v)
? Object.fromEntries(
Object.entries(v).map(
([k, vv]) => [k, _cellify(proxy, vv)],
Expand All @@ -62,11 +67,7 @@ export const _uncellify = async <T>(
return Promise.all(
value.map(async (_element) => await _uncellify(_element))
) as Promise<Uncellified<T>>;
if (
typeof value === "object" &&
value !== null &&
value.constructor.prototype === Object.prototype // exclude classes
)
if (isObject(value))
return Object.fromEntries(
await Promise.all(
Object.entries(value).map(async ([k, vv]) => [k, await _uncellify(vv)])
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export {
reduce,
sort
} from "./array";
export {
_cellify,
_uncellify,
isObject,
type Cellified,
type Uncellified
} from "./cellify";
export { Debugger } from "./debug";
export { jsonStringify } from "./json";
export { nextSubscriber } from "./next";
Expand Down

0 comments on commit 8eac607

Please sign in to comment.