Skip to content

Commit

Permalink
refactor HashtagScriptManager to enhance JSON handling and update tes…
Browse files Browse the repository at this point in the history
…ts for image editing and input operations
  • Loading branch information
BlackRam-oss committed Nov 23, 2024
1 parent 9a10978 commit 0af1909
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
38 changes: 37 additions & 1 deletion src/managers/HashtagScriptManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,31 @@ export default class HashtagScriptManager {
if (index % 2 === 0) {
objJson += `"${item}": `
} else {
objJson += `${item}`
switch (item) {
case "null":
case "undefined":
case "true":
case "false":
objJson += `${item}`
break;
default:
if (HashtagScriptManager.containExtraDoubleQuotes(item)) {
item = HashtagScriptManager.removeExtraDoubleQuotes(item);
objJson += `"${item}"`
}
else if (item.startsWith("{") && item.endsWith("}")) {
objJson += `${item}`
}
else if (item.startsWith("\"") && item.endsWith("\"")) {
objJson += `${item}`
}
else if (!isNaN(parseFloat(item))) {
objJson += `${item}`
}
else {
objJson += `"${item}"`
}
}
if (index < list.length - 1) {
objJson += ", ";
}
Expand Down Expand Up @@ -406,5 +430,17 @@ export default class HashtagScriptManager {
}
return value;
}
private static containExtraDoubleQuotes(value: string): boolean {
if (value.startsWith("\"") && value.endsWith("\"")) {
return true;
}
if (value.startsWith("\'") && value.endsWith("\'")) {
return true;
}
if (value.startsWith("\`") && value.endsWith("\`")) {
return true;
}
return false;
}

}
14 changes: 7 additions & 7 deletions tests/pixivn-features.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ test('edit image', async () => {
{
type: "operationtoconvert",
values: [
"edit image bg position { \"x\": 20, \"y\": 30, \"test\": \"test \\} ' test\", \"test2\": \"'\" } visible true cursor \"pointer\" alpha 0.5",
"edit image bg position { \"x\": -20.5, \"y\": 30, \"test\": \"test \\} ' test\", \"test2\": \"'\" } visible true cursor \"pointer\" alpha 0.5",
],
},
],
Expand All @@ -336,7 +336,7 @@ test('edit image', async () => {
alias: "bg",
props: {
position: {
x: 20,
x: -20.5,
y: 30,
test: "test } ' test",
test2: "'",
Expand All @@ -361,7 +361,7 @@ test('edit image', async () => {
}
let res = convertInkText(`
=== start
#edit image bg position \\\{ "x": 20, "y": 30, "test": "test \\\\\\\} ' test", "test2": "'" \\\} visible true cursor "pointer" alpha 0.5
#edit image bg position \\\{ "x": -20.5, "y": 30, "test": "test \\\\\\\} ' test", "test2": "'" \\\} visible true cursor "pointer" alpha 0.5
hello
-> DONE
`);
Expand Down Expand Up @@ -955,7 +955,7 @@ test('input', async () => {
{
type: "operationtoconvert",
values: [
"request input number default 0",
"request input type number default 0",
],
},
],
Expand All @@ -977,7 +977,7 @@ test('input', async () => {
{
type: "operationtoconvert",
values: [
"request input 'array of string'",
"request input type 'array of string'",
],
},
],
Expand Down Expand Up @@ -1043,9 +1043,9 @@ test('input', async () => {
let res = convertInkText(`
=== start
# request input
# request input number default 0
# request input type number default 0
# pause
# request input 'array of string'
# request input type 'array of string'
Hello
-> DONE
`);
Expand Down

0 comments on commit 0af1909

Please sign in to comment.