Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] return indices from safeParse #11

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ function safeParse (header) {

const result = {
type: type.toLowerCase(),
parameters: new NullObject()
parameters: new NullObject(),
parametersIndices: new NullObject()
}

// parse parameters
Expand All @@ -131,8 +132,10 @@ function safeParse (header) {
}

let key
let keyIndex
let match
let value
let valueIndex

paramRE.lastIndex = index

Expand All @@ -143,17 +146,22 @@ function safeParse (header) {

index += match[0].length
key = match[1].toLowerCase()
keyIndex = match.index + 2
value = match[2]
valueIndex = match.index + (match[0].length - value.length)

if (value[0] === '"') {
// remove quotes and escapes
value = value
.slice(1, value.length - 1)

++valueIndex

quotedPairRE.test(value) && (value = value.replace(quotedPairRE, '$1'))
}

result.parameters[key] = value
result.parametersIndices[key] = { keyAt: keyIndex, valueAt: valueIndex }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[comment for reference]

}

if (index !== header.length) {
Expand Down