Skip to content

Commit

Permalink
Merge pull request #54 from nigrosimone/patch-1
Browse files Browse the repository at this point in the history
Use loop for acceptParams
  • Loading branch information
dimdenGD authored Nov 16, 2024
2 parents 080f007 + b43befa commit 08696d3
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,33 @@ function canBeOptimized(pattern) {
}

function acceptParams(str) {
const parts = str.split(/ *; */);
const ret = { value: parts[0], quality: 1, params: {} }

for (let i = 1, len = parts.length; i < len; ++i) {
const pms = parts[i].split(/ *= */);
const [pms_0, pms_1] = pms;
if ('q' === pms_0) {
ret.quality = parseFloat(pms_1);
} else {
ret.params[pms_0] = pms_1;
}
const length = str.length;
const colonIndex = str.indexOf(';');
const index = colonIndex === -1 ? length : colonIndex;
const ret = { value: str.slice(0, index).trim(), quality: 1, params: {} };

while (index < length) {
const splitIndex = str.indexOf('=', index);
if (splitIndex === -1) break;

const colonIndex = str.indexOf(';', index);
const endIndex = colonIndex === -1 ? length : colonIndex;

if (splitIndex > endIndex) {
index = str.lastIndexOf(';', splitIndex - 1) + 1;
continue;
}

const key = str.slice(index, splitIndex).trim();
const value = str.slice(splitIndex + 1, endIndex).trim();

if (key === 'q') {
ret.quality = parseFloat(value);
} else {
ret.params[key] = value;
}

index = endIndex + 1;
}

return ret;
Expand Down

0 comments on commit 08696d3

Please sign in to comment.