Skip to content

Commit 6bc8a38

Browse files
authored
Minor refactor to getPrecedence (#2230)
1 parent 063209f commit 6bc8a38

1 file changed

Lines changed: 40 additions & 42 deletions

File tree

src/util.js

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,49 +53,47 @@ function printNumber(rawNumber) {
5353
}
5454

5555
// http://php.net/manual/en/language.operators.precedence.php
56-
const PRECEDENCE = {};
57-
[
58-
["or"],
59-
["xor"],
60-
["and"],
56+
const PRECEDENCE = new Map(
6157
[
62-
"=",
63-
"+=",
64-
"-=",
65-
"*=",
66-
"**=",
67-
"/=",
68-
".=",
69-
"%=",
70-
"&=",
71-
"|=",
72-
"^=",
73-
"<<=",
74-
">>=",
75-
],
76-
["??"],
77-
["||"],
78-
["&&"],
79-
["|"],
80-
["^"],
81-
["&"],
82-
["==", "===", "!=", "!==", "<>", "<=>"],
83-
["<", ">", "<=", ">="],
84-
[">>", "<<"],
85-
["+", "-", "."],
86-
["*", "/", "%"],
87-
["!"],
88-
["instanceof"],
89-
["++", "--", "~"],
90-
["**"],
91-
].forEach((tier, i) => {
92-
tier.forEach((op) => {
93-
PRECEDENCE[op] = i;
94-
});
95-
});
96-
97-
function getPrecedence(op) {
98-
return PRECEDENCE[op];
58+
["or"],
59+
["xor"],
60+
["and"],
61+
[
62+
"=",
63+
"+=",
64+
"-=",
65+
"*=",
66+
"**=",
67+
"/=",
68+
".=",
69+
"%=",
70+
"&=",
71+
"|=",
72+
"^=",
73+
"<<=",
74+
">>=",
75+
],
76+
["??"],
77+
["||"],
78+
["&&"],
79+
["|"],
80+
["^"],
81+
["&"],
82+
["==", "===", "!=", "!==", "<>", "<=>"],
83+
["<", ">", "<=", ">="],
84+
[">>", "<<"],
85+
["+", "-", "."],
86+
["*", "/", "%"],
87+
["!"],
88+
["instanceof"],
89+
["++", "--", "~"],
90+
["**"],
91+
].flatMap((operators, index) =>
92+
operators.map((operator) => [operator, index])
93+
)
94+
);
95+
function getPrecedence(operator) {
96+
return PRECEDENCE.get(operator);
9997
}
10098

10199
const equalityOperators = ["==", "!=", "===", "!==", "<>", "<=>"];

0 commit comments

Comments
 (0)