Skip to content

Commit ef94e75

Browse files
committed
refactor: extract string handling into a utility function for improved readability
1 parent f2af0a1 commit ef94e75

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

src/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,32 @@ const getLocationFromHash = (): Location | null => {
9494
return null;
9595
};
9696

97+
const extractFirstString = ({ result }: { result: unknown }): string | null => {
98+
if (!Array.isArray(result) || !Array.isArray(result[0])) return null;
99+
const firstValue = result[0][0];
100+
return typeof firstValue === "string" ? firstValue : null;
101+
};
102+
97103
const queryPageTitle = async ({ uid }: { uid: string }): Promise<string | null> => {
98-
const pageResult = await window.roamAlphaAPI.q<[[string]]>(`
104+
const pageResult = await window.roamAlphaAPI.q(`
99105
[:find ?title
100106
:where [?e :block/uid "${uid}"]
101107
[?e :node/title ?title]]
102108
`);
103109

104-
return pageResult?.[0]?.[0] || null;
110+
return extractFirstString({ result: pageResult });
105111
};
106112

107113
const queryBlockString = async ({ uid }: { uid: string }): Promise<string | null> => {
108-
const blockResult = await window.roamAlphaAPI.q<[[string]]>(`
114+
const blockResult = await window.roamAlphaAPI.q(`
109115
[:find ?string
110116
:where [?e :block/uid "${uid}"]
111117
[?e :block/string ?string]]
112118
`);
113119

114-
if (!blockResult?.length) return null;
115-
return blockResult[0][0] || "(empty block)";
120+
const blockString = extractFirstString({ result: blockResult });
121+
if (blockString === null) return null;
122+
return blockString || "(empty block)";
116123
};
117124

118125
const getBreadcrumbItemByUid = async ({ uid }: { uid: string }): Promise<BreadcrumbItem | null> => {

0 commit comments

Comments
 (0)