fix: revert to original regex for now

old-agentic-v1^2
Philipp Burckhardt 2023-06-15 10:05:52 -04:00
rodzic 0cc76b0f3d
commit 132084eba5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: A2C3BCA4F31D1DDD
2 zmienionych plików z 9 dodań i 9 usunięć

Wyświetl plik

@ -3,23 +3,23 @@ import { customAlphabet, urlAlphabet } from 'nanoid'
import * as types from './types'
/**
* Extracts the first JSON object string from a given string.
* Extracts a JSON object string from a given string.
*
* @param text - string from which to extract the JSON object
* @returns extracted JSON object string, or `undefined` if no JSON object is found
*/
export function extractJSONObjectFromString(text: string): string | undefined {
return text.match(/\{([^}]|\n)*\}/gm)?.[0]
return text.match(/\{(.|\n)*\}/gm)?.[0] // FIXME: This breaks if there are multiple JSON objects in the string
}
/**
* Extracts the first JSON array string from a given string.
* Extracts a JSON array string from a given string.
*
* @param text - string from which to extract the JSON array
* @returns extracted JSON array string, or `undefined` if no JSON array is found
*/
export function extractJSONArrayFromString(text: string): string | undefined {
return text.match(/\[([^\]]|\n)*\]/gm)?.[0]
return text.match(/\[(.|\n)*\]/gm)?.[0] // FIXME: This breaks if there are multiple JSON arrays in the string
}
/**

10
test/utils.test.ts vendored
Wyświetl plik

@ -26,14 +26,14 @@ test('isValidTaskIdentifier - invalid', async (t) => {
t.false(isValidTaskIdentifier('-foo'))
})
test('extractJSONObjectFromString should extract first JSON object from string', (t) => {
const jsonString = '{"name":"John"} Some other text {"name":"Doe"}'
test('extractJSONObjectFromString should extract JSON object from string', (t) => {
const jsonString = 'Some text {"name":"John Doe"} more text'
const result = extractJSONObjectFromString(jsonString)
t.is(result, '{"name":"John"}')
t.is(result, '{"name":"John Doe"}')
})
test('extractJSONArrayFromString should extract first JSON array from string', (t) => {
const jsonString = '[1,2,3] Some other text [4,5,6]'
test('extractJSONArrayFromString should extract JSON array from string', (t) => {
const jsonString = 'Some text [1,2,3] more text'
const result = extractJSONArrayFromString(jsonString)
t.is(result, '[1,2,3]')
})