Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Dec 8, 2024
1 parent 567fd1d commit 4d5f04e
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 28 deletions.
43 changes: 36 additions & 7 deletions src/parseMetaString.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
// constructed with the assistance of claude AI
function customSplit(str: string) {
const result = []
let current = ''
let inQuotes = false
let inBrackets = false

for (const char of str) {
if (char === '"') {
inQuotes = !inQuotes
current += char
} else if (char === '[') {
inBrackets = true
current += char
} else if (char === ']') {
inBrackets = false
current += char
} else if (char === ',' && !inQuotes && !inBrackets) {
result.push(current.trim())
current = ''
} else {
current += char
}
}

if (current) {
result.push(current.trim())
}

return result
}

export function parseMetaString(metaString: string) {
const inside = metaString.replace(/^<|>$/g, '')
return Object.fromEntries(
[
// split string on comma except when in square bracket
// https://stackoverflow.com/questions/74238461/
...metaString.replace(/^<|>$/g, '').matchAll(/(?:\[[^\][]*\]|[^,])+/g),
].map(f => {
const [key, val] = f[0].split('=').map(f => f.trim())
customSplit(inside).map(f => {
const [key, val] = f.split('=').map(f => f.trim())
if (val && val.startsWith('[') && val.endsWith(']')) {
return [
key,
Expand All @@ -17,7 +46,7 @@ export function parseMetaString(metaString: string) {
} else if (val && val.startsWith('"') && val.endsWith('"')) {
return [key, val.slice(1, -1)]
} else {
return [key, val]
return [key, val?.replaceAll(/^"|"$/g, '')]
}
}),
)
Expand Down
Loading

0 comments on commit 4d5f04e

Please sign in to comment.