kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
fix: revert to original regex for now
rodzic
0cc76b0f3d
commit
132084eba5
|
@ -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
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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]')
|
||||
})
|
||||
|
|
Ładowanie…
Reference in New Issue