Skip to content

Commit

Permalink
Add type anotations, add + use ParsedValue type
Browse files Browse the repository at this point in the history
fixes all lint errors
  • Loading branch information
acusti committed Mar 22, 2024
1 parent d2c1aed commit 919b288
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/parsing/src/as-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ function lengthOf(item: unknown): number {
case 'object':
if (!item) return 0;
if (Array.isArray(item)) {
return item.reduce((acc, item) => acc + lengthOf(item), 0);
return item.reduce(
(acc: number, item) => acc + lengthOf(item),
0,
) as number;
}
return Object.keys(item).reduce(
(acc, key) => acc + key.length + lengthOf((item as GenericObject)[key]),
Expand All @@ -159,17 +162,17 @@ function lengthOf(item: unknown): number {

const OBJECT_KEY_REGEXP = /^"[^"]+":/;

type ParsedValue = string | boolean | number | GenericObject | Array<unknown>;

// Adapted from https://github.com/langchain-ai/langchainjs/blob/215dd52/langchain-core/src/output_parsers/json.ts#L58
// MIT License
export function parseAsJSON(
text: string,
): string | boolean | number | GenericObject | Array<unknown> | null {
export function parseAsJSON(text: string): ParsedValue | null {
// if the input is undefined/null, return null to indicate failure
if (text == null) return null;

// attempt to parse the string as-is
try {
return JSON.parse(text);
return JSON.parse(text) as ParsedValue;
} catch (error) {
// let’s try to fix it
}
Expand All @@ -179,7 +182,7 @@ export function parseAsJSON(
// if this is a two-column markdown table, convert it to JSON key/value pairs
text = text.replace(
/^\| (.+?) \| (.+?)(?: \|)+$/gm,
(_match, key, value) =>
(_match, key: string, value: string) =>
`"${key.replace(/(^"|"$)/g, '')}": "${value.replace(/(^"|"$)/g, '')}",`,
);

Expand Down Expand Up @@ -275,8 +278,7 @@ export function parseAsJSON(
index: startIndex,
text,
});
const maybeKey =
quoteIndex > index ? text.slice(quoteIndex) : '';
const maybeKey = quoteIndex > index ? text.slice(quoteIndex) : '';
if (OBJECT_KEY_REGEXP.test(maybeKey)) {
continue;
}
Expand Down Expand Up @@ -319,7 +321,7 @@ export function parseAsJSON(
// attempt to parse the modified string as JSON
let result = null;
try {
result = JSON.parse(newText);
result = JSON.parse(newText) as ParsedValue;
} catch (error) {
// in case of error, return null (after checking remainder of text)
}
Expand Down

0 comments on commit 919b288

Please sign in to comment.