-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parsing header lines where values are containing square bracket l…
…ists (#107)
- Loading branch information
Showing
19 changed files
with
1,989 additions
and
926 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// constructed with the assistance of claude AI | ||
// | ||
// I first prompted it with a regex that splits a comma separated string with | ||
// awareness of quotation from this stackoverflow queston | ||
// https://stackoverflow.com/a/18893443/2129219, and asked it to add support | ||
// for square brackets | ||
// | ||
// the result was this function | ||
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( | ||
customSplit(inside).map(f => { | ||
const [key, val] = f.split('=').map(f => f.trim()) | ||
if (val && val.startsWith('[') && val.endsWith(']')) { | ||
return [ | ||
key, | ||
val | ||
.slice(1, -1) | ||
.split(',') | ||
.map(f => f.trim()), | ||
] | ||
} else if (val && val.startsWith('"') && val.endsWith('"')) { | ||
return [key, val.slice(1, -1)] | ||
} else { | ||
return [key, val?.replaceAll(/^"|"$/g, '')] | ||
} | ||
}), | ||
) | ||
} |
Oops, something went wrong.