Skip to content

Commit 9c33154

Browse files
committed
chore: Align isObject/isRecord usage
1 parent c9927e0 commit 9c33154

File tree

20 files changed

+56
-59
lines changed

20 files changed

+56
-59
lines changed

builder/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { isObj } from "@commontools/utils";
2-
import { Mutable } from "@commontools/utils/types";
1+
import { isObject, Mutable } from "@commontools/utils/types";
32

43
export const ID: unique symbol = Symbol("ID, unique to the context");
54
export const ID_FIELD: unique symbol = Symbol(
@@ -166,7 +165,8 @@ export type Alias = {
166165
};
167166

168167
export function isAlias(value: any): value is Alias {
169-
return isObj(value) && isObj(value.$alias) &&
168+
return isObject(value) && "$alias" in value && isObject(value.$alias) &&
169+
"path" in value.$alias &&
170170
Array.isArray(value.$alias.path);
171171
}
172172

charm/src/iterate.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
recipeManager,
66
runtime,
77
} from "@commontools/runner";
8-
import { isObj } from "@commontools/utils";
8+
import { isObject } from "@commontools/utils/types";
99
import {
1010
createJsonSchema,
1111
JSONSchema,
@@ -215,7 +215,7 @@ export function scrub(data: any): any {
215215
// If there are properties, remove $UI and $NAME and any streams
216216
const scrubbed = Object.fromEntries(
217217
Object.entries(data.schema.properties).filter(([key, value]) =>
218-
!key.startsWith("$") && (!isObj(value) || !value.asStream)
218+
!key.startsWith("$") && (!isObject(value) || !value.asStream)
219219
),
220220
);
221221
console.log("scrubbed modified schema", scrubbed, data.schema);
@@ -227,7 +227,7 @@ export function scrub(data: any): any {
227227
);
228228
} else {
229229
const value = data.asSchema().get();
230-
if (isObj(value)) {
230+
if (isObject(value)) {
231231
// Generate a new schema for all properties except $UI and $NAME and streams
232232
const scrubbed = {
233233
type: "object",
@@ -248,7 +248,7 @@ export function scrub(data: any): any {
248248
}
249249
} else if (Array.isArray(data)) {
250250
return data.map((value) => scrub(value));
251-
} else if (isObj(data)) {
251+
} else if (isObject(data)) {
252252
return Object.fromEntries(
253253
Object.entries(data).map(([key, value]) => [key, scrub(value)]),
254254
);
@@ -264,7 +264,7 @@ function turnCellsIntoAliases(data: any): any {
264264
return { $alias: data.getAsCellLink() };
265265
} else if (Array.isArray(data)) {
266266
return data.map((value) => turnCellsIntoAliases(value));
267-
} else if (isObj(data)) {
267+
} else if (isObject(data)) {
268268
return Object.fromEntries(
269269
Object.entries(data).map((
270270
[key, value],

charm/src/manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
} from "@commontools/runner";
2828
import { storage } from "@commontools/runner";
2929
import { type Session } from "@commontools/identity";
30-
import { isObj } from "@commontools/utils";
30+
import { isObject } from "@commontools/utils/types";
3131

3232
export function charmId(charm: Charm): string | undefined {
3333
const id = getEntityId(charm);
@@ -343,7 +343,7 @@ export class CharmManager {
343343
// If there is no result schema, create one from top level properties that omits UI, NAME
344344
if (!resultSchema) {
345345
const resultValue = charm.get();
346-
if (isObj(resultValue)) {
346+
if (isObject(resultValue)) {
347347
resultSchema = {
348348
type: "object",
349349
properties: Object.fromEntries(

html/deno.json renamed to html/deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@commontools/html",
33
"exports": "./src/index.ts",
44
"tasks": {
5+
// JSDOM dependencies require env.
56
"test": "deno test --allow-env"
67
},
78
"imports": {

html/src/path.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as logger from "./logger.ts";
2-
3-
export const isObject = (value: unknown): value is object => {
4-
return typeof value === "object" && value !== null;
5-
};
2+
import { isObject } from "@commontools/utils/types";
63

74
/** A keypath is an array of property keys */
85
export type KeyPath = Array<PropertyKey>;

html/src/render.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useCancelGroup,
99
} from "@commontools/runner";
1010
import { JSONSchema } from "@commontools/builder";
11-
import { isObj } from "@commontools/utils";
11+
import { isObject } from "@commontools/utils/types";
1212
import * as logger from "./logger.ts";
1313

1414
const vdomSchema: JSONSchema = {
@@ -271,7 +271,7 @@ const sanitizeScripts = (node: VNode): VNode | null => {
271271
if (node.name === "script") {
272272
return null;
273273
}
274-
if (!isCell(node.props) && !isObj(node.props)) {
274+
if (!isCell(node.props) && !isObject(node.props)) {
275275
node = { ...node, props: {} };
276276
}
277277
if (!isCell(node.children) && !Array.isArray(node.children)) {

html/test/assert.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isRecord } from "@commontools/utils/types";
2+
13
export class AssertionError<A, E> extends Error {
24
actual: A | undefined;
35
expected: E | undefined;
@@ -43,7 +45,7 @@ export const matchObject = (
4345
expected: unknown,
4446
message = "",
4547
) => {
46-
if (!isObject(actual) || !isObject(expected)) {
48+
if (!isRecord(actual) || !isRecord(expected)) {
4749
throw new AssertionError({
4850
message: message || "Both arguments must be objects",
4951
actual,
@@ -61,7 +63,7 @@ export const matchObject = (
6163
});
6264
}
6365

64-
if (isObject(expected[key]) && isObject(actual[key])) {
66+
if (isRecord(expected[key]) && isRecord(actual[key])) {
6567
// Recursively check nested objects
6668
matchObject(actual[key], expected[key], message);
6769
} else if (actual[key] !== expected[key]) {
@@ -84,10 +86,6 @@ export const matchObject = (
8486
}
8587
};
8688

87-
function isObject(value: unknown): value is Record<string, unknown> {
88-
return typeof value === "object" && value !== null;
89-
}
90-
9189
export const throws = (run: () => void, message = "") => {
9290
try {
9391
run();

iframe-sandbox/src/ipc.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type JSONSchema } from "@commontools/builder";
2+
import { isObject } from "@commontools/utils/types";
23

34
// Types used by the `common-iframe-sandbox` IPC.
45

@@ -107,8 +108,6 @@ export function isGuestError(e: object): e is GuestError {
107108
"stacktrace" in e && typeof e.stacktrace === "string";
108109
}
109110

110-
const isObject = (source: unknown): source is Record<string, unknown> =>
111-
typeof source === "object" && source != null;
112111
export const isTaskPerform = (source: unknown): source is TaskPerform =>
113112
isObject(source) &&
114113
typeof source?.intent === "string" &&

jumble/src/iframe-ctx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
removeAction,
1515
} from "@commontools/runner";
1616
import { DEFAULT_IFRAME_MODELS, LLMClient } from "@commontools/llm";
17-
import { isObj } from "@commontools/utils";
17+
import { isObject } from "@commontools/utils/types";
1818
import {
1919
completeJob,
2020
failJob,
@@ -196,7 +196,7 @@ export const setupIframe = () =>
196196
: undefined;
197197
const type = context.key(key).schema?.type ??
198198
currentValueType ?? typeof value;
199-
if (type === "object" && isObj(value) && !Array.isArray(value)) {
199+
if (type === "object" && isObject(value)) {
200200
context.key(key).update(value);
201201
} else if (
202202
(type === "array" && Array.isArray(value)) ||

jumble/src/views/CharmDetailView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "@commontools/charm";
88
import { useCharmReferences } from "@/hooks/use-charm-references.ts";
99
import { isCell, isStream } from "@commontools/runner";
10-
import { isObj } from "@commontools/utils";
10+
import { isObject } from "@commontools/utils/types";
1111
import {
1212
CheckboxToggle,
1313
CommonCheckbox,
@@ -1339,7 +1339,7 @@ function translateCellsAndStreamsToPlainJSON(
13391339
result = data.map((value) =>
13401340
translateCellsAndStreamsToPlainJSON(value, partial, complete)
13411341
);
1342-
} else if (isObj(data)) {
1342+
} else if (isObject(data)) {
13431343
result = Object.fromEntries(
13441344
Object.entries(data).map(([key, value]) => [
13451345
key,

0 commit comments

Comments
 (0)