Skip to content

Commit 03506f0

Browse files
🎨 simplify token predicates (#19)
1 parent b8820bc commit 03506f0

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

‎src/token-predicate.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ function negate(f) {
1616
return negate0.bind(f)
1717
}
1818

19+
/**
20+
* Checks if the given token is a PunctuatorToken with the given value
21+
* @param {Token} token - The token to check.
22+
* @param {string} value - The value to check.
23+
* @returns {boolean} `true` if the token is a PunctuatorToken with the given value.
24+
*/
25+
function isPunctuatorTokenWithValue(token, value) {
26+
return token.type === "Punctuator" && token.value === value
27+
}
28+
1929
/**
2030
* Checks if the given token is an arrow token or not.
2131
* @param {Token} token - The token to check.
2232
* @returns {boolean} `true` if the token is an arrow token.
2333
*/
2434
export function isArrowToken(token) {
25-
return token.value === "=>" && token.type === "Punctuator"
35+
return isPunctuatorTokenWithValue(token, "=>")
2636
}
2737

2838
/**
@@ -31,7 +41,7 @@ export function isArrowToken(token) {
3141
* @returns {boolean} `true` if the token is a comma token.
3242
*/
3343
export function isCommaToken(token) {
34-
return token.value === "," && token.type === "Punctuator"
44+
return isPunctuatorTokenWithValue(token, ",")
3545
}
3646

3747
/**
@@ -40,7 +50,7 @@ export function isCommaToken(token) {
4050
* @returns {boolean} `true` if the token is a semicolon token.
4151
*/
4252
export function isSemicolonToken(token) {
43-
return token.value === ";" && token.type === "Punctuator"
53+
return isPunctuatorTokenWithValue(token, ";")
4454
}
4555

4656
/**
@@ -49,7 +59,7 @@ export function isSemicolonToken(token) {
4959
* @returns {boolean} `true` if the token is a colon token.
5060
*/
5161
export function isColonToken(token) {
52-
return token.value === ":" && token.type === "Punctuator"
62+
return isPunctuatorTokenWithValue(token, ":")
5363
}
5464

5565
/**
@@ -58,7 +68,7 @@ export function isColonToken(token) {
5868
* @returns {boolean} `true` if the token is an opening parenthesis token.
5969
*/
6070
export function isOpeningParenToken(token) {
61-
return token.value === "(" && token.type === "Punctuator"
71+
return isPunctuatorTokenWithValue(token, "(")
6272
}
6373

6474
/**
@@ -67,7 +77,7 @@ export function isOpeningParenToken(token) {
6777
* @returns {boolean} `true` if the token is a closing parenthesis token.
6878
*/
6979
export function isClosingParenToken(token) {
70-
return token.value === ")" && token.type === "Punctuator"
80+
return isPunctuatorTokenWithValue(token, ")")
7181
}
7282

7383
/**
@@ -76,7 +86,7 @@ export function isClosingParenToken(token) {
7686
* @returns {boolean} `true` if the token is an opening square bracket token.
7787
*/
7888
export function isOpeningBracketToken(token) {
79-
return token.value === "[" && token.type === "Punctuator"
89+
return isPunctuatorTokenWithValue(token, "[")
8090
}
8191

8292
/**
@@ -85,7 +95,7 @@ export function isOpeningBracketToken(token) {
8595
* @returns {boolean} `true` if the token is a closing square bracket token.
8696
*/
8797
export function isClosingBracketToken(token) {
88-
return token.value === "]" && token.type === "Punctuator"
98+
return isPunctuatorTokenWithValue(token, "]")
8999
}
90100

91101
/**
@@ -94,7 +104,7 @@ export function isClosingBracketToken(token) {
94104
* @returns {boolean} `true` if the token is an opening brace token.
95105
*/
96106
export function isOpeningBraceToken(token) {
97-
return token.value === "{" && token.type === "Punctuator"
107+
return isPunctuatorTokenWithValue(token, "{")
98108
}
99109

100110
/**
@@ -103,7 +113,7 @@ export function isOpeningBraceToken(token) {
103113
* @returns {boolean} `true` if the token is a closing brace token.
104114
*/
105115
export function isClosingBraceToken(token) {
106-
return token.value === "}" && token.type === "Punctuator"
116+
return isPunctuatorTokenWithValue(token, "}")
107117
}
108118

109119
/**
@@ -112,11 +122,7 @@ export function isClosingBraceToken(token) {
112122
* @returns {boolean} `true` if the token is a comment token.
113123
*/
114124
export function isCommentToken(token) {
115-
return (
116-
token.type === "Line" ||
117-
token.type === "Block" ||
118-
token.type === "Shebang"
119-
)
125+
return ["Block", "Line", "Shebang"].includes(token.type)
120126
}
121127

122128
export const isNotArrowToken = negate(isArrowToken)

0 commit comments

Comments
 (0)