Skip to content

Commit 8e4fee8

Browse files
committed
Fix collapse not working when in: variable
1 parent f8bb00c commit 8e4fee8

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

src/stack/jsonify/jsonify.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ export default function jsonifyString(inputString) {
2323

2424
const inputOnly = extractInputFromObject(jsonLikeString);
2525
const inputOnlyWithoutWeirdObject = handleWeirdObject(inputOnly);
26-
const modifiedString = replaceJSONPlaceholders(inputOnlyWithoutWeirdObject);
26+
let modifiedString = replaceJSONPlaceholders(inputOnlyWithoutWeirdObject);
2727

2828
// Step 3: Convert to JSON
2929
let json;
3030
try {
31+
// if modified string doesn't start with " or {, add { to the beginning
32+
if (modifiedString[0] !== '"' && modifiedString[0] !== '{' && modifiedString[0] !== "'") {
33+
modifiedString = `"${modifiedString}"`;
34+
}
3135
json = resilientJSONParse(modifiedString);
3236
} catch (error) {
3337
return null;
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
stack(
22
'brief',
33
{
4-
'in': ['this is an example', 'this is an example']
4+
'in': ['this \'\'\'is an example', 'this is an example', '{"test": "json to confuse the parser"}'],
55
}
66
);
77

88
// TODO: allow the usage of in a string, but for now incentiviwe people to use in: ['this is an example', 'this is an example']
9-
import processInputArray from '../../stacks/processInputArray';
109

11-
await processInputArray('this is an example', 'this is an example');
10+
stack(
11+
'brief',
12+
{
13+
'in': ['this \'\'\'is an example', 'this is an example', '{"test": "json to confuse the parser"}'],
14+
}
15+
);
1216

1317
// TODO: allow the usage of in a string, but for now incentiviwe people to use in: ['this is an example', 'this is an example']
1418

15-
/**
16-
* Brief: brief
17-
*/
18-
export default async function processInputArray(in: string[]): Promise<null> {
19-
in.forEach((item) => {
20-
console.log(item);
21-
});
22-
return null;
23-
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
const question = 'what is the meaning of life?';
3+
4+
stack('answer my question using OpenAI', {in: question, out: '42'});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
const question = 'what is the meaning of life?';
3+
4+
stack('answer my question using OpenAI', {in: question, out: '42'});
5+
6+
import answerQuestionUsingOpenAI from '../../stacks/answerQuestionUsingOpenAI';
7+
8+
9+
const question = 'what is the meaning of life?';
10+
11+
await answerQuestionUsingOpenAI('question');
12+
13+
14+
import axios from 'axios';
15+
16+
/**
17+
* Brief: answer my question using OpenAI
18+
*/
19+
export default async function answerQuestionUsingOpenAI(input: string): Promise<string> {
20+
const response = await axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', {
21+
prompt: input,
22+
max_tokens: 60
23+
}, {
24+
headers: {
25+
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
26+
}
27+
});
28+
29+
return response.data.choices[0].text.trim();
30+
}

test-workspace/fixtures/inputEmptyString/output.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ stack('this is an example', {
55

66
import generateBooleanResponse from '../../stacks/generateBooleanResponse';
77

8-
await generateBooleanResponse('');
8+
await generateBooleanResponse("''");
99

1010

1111
/**

0 commit comments

Comments
 (0)