Skip to content

Commit f149f2b

Browse files
committed
perf: do not parse all map string if a key match
1 parent d74b0a5 commit f149f2b

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

src/constant.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const METHOD = 'map-get((';
22

3-
export const ERROR_PREFIX = 'postcss – map-get';
3+
export const ERROR_PREFIX = 'postcss – map-get';

src/parse-parenthesis-content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default function parseParenthesisContent(stringToParse, startingPosition
3333
* String should be already validated by postcss but to avoid unclear stacktrace I'll manage the error anyway.
3434
*/
3535
if (stack.length !== 0) {
36-
throw new Error(`${ERROR_PREFIX} parenthesis not closed`);
36+
throw new Error(`${ERROR_PREFIX} parenthesis not closed`);
3737
}
3838

3939
return {content, position};

src/process-value.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,33 @@ function getKeyFromMapString(mapString, keyParameter) {
1313
// remove open and close parenthesis from the map string
1414
mapString = mapString.slice(1, -1);
1515

16-
const map = {};
17-
1816
let isParsingKey = true;
17+
let hasFinishedParsingValue = false;
1918

2019
let key = '';
2120
let value = '';
2221

2322
for (let position = 0; position < mapString.length; position++) {
2423
const currentCharacter = mapString[position];
2524

26-
if (isParsingKey) { // process the key (add all characters until find a :)
25+
// process the key (add all characters until find a `:`)
26+
if (isParsingKey) {
2727
if (currentCharacter === ':') {
2828
isParsingKey = false;
2929
} else {
3030
key += currentCharacter;
3131
}
32-
} else if (currentCharacter === '(') {
32+
33+
continue;
34+
}
35+
36+
if (currentCharacter === '(') {
3337
// if value contains a `(` that means that is map so parse the string until the `(` is closed
3438
const output = parseParenthesisContent(mapString, position);
3539
value += output.content;
3640
position = output.position;
3741

38-
map[key] = value.trim();
39-
40-
// value declaration is complete return to check key and reset both variables
41-
isParsingKey = true;
42-
key = '';
43-
value = '';
42+
hasFinishedParsingValue = true;
4443
} else {
4544
// simple map with property / value pairs
4645
const isLastCharacter = position === mapString.length - 1;
@@ -49,22 +48,26 @@ function getKeyFromMapString(mapString, keyParameter) {
4948
value += currentCharacter;
5049
}
5150

52-
map[key] = value.trim();
53-
54-
isParsingKey = true;
55-
key = '';
56-
value = '';
51+
hasFinishedParsingValue = true;
5752
} else {
5853
value += currentCharacter;
5954
}
6055
}
61-
}
6256

63-
if (!map[keyValue]) {
64-
throw new Error(`${ERROR_PREFIX} – unable to find “${keyValue}“ key inside map “(${mapString})“`);
57+
if (hasFinishedParsingValue) {
58+
if (key === keyValue) {
59+
return value.trim();
60+
}
61+
62+
// value declaration is complete return to check key and reset both variables
63+
isParsingKey = true;
64+
hasFinishedParsingValue = false;
65+
key = '';
66+
value = '';
67+
}
6568
}
6669

67-
return map[keyValue];
70+
throw new Error(`${ERROR_PREFIX} unable to find “${keyValue}“ key inside map “(${mapString})“`);
6871
}
6972

7073
/**
@@ -88,6 +91,10 @@ export default function (value) {
8891

8992
// resolve the desidered requested key
9093
let keyString = '';
94+
95+
// indicates if we found the come which separate map and requested key:
96+
// map-get((...) !default, bar)
97+
// ↑
9198
let hasFoundComa = false;
9299

93100
for (; position < resolvedValue.length; position++) {

0 commit comments

Comments
 (0)