From 132084eba5fe79b7a871d07bc9e8f5696e57cea5 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Thu, 15 Jun 2023 10:05:52 -0400 Subject: [PATCH] fix: revert to original regex for now --- src/utils.ts | 8 ++++---- test/utils.test.ts | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index d5954a7..d8aec87 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 } /** diff --git a/test/utils.test.ts b/test/utils.test.ts index 65413d6..e6021e5 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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]') })