Skip to content

Commit 19328c3

Browse files
committed
Refactor to improve bundle size
1 parent 9542d50 commit 19328c3

File tree

2 files changed

+46
-88
lines changed

2 files changed

+46
-88
lines changed

index.js

Lines changed: 44 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,46 @@ var toString = require('nlcst-to-string')
44

55
module.exports = isLiteral
66

7-
var single = {
8-
'-': true, // Hyphen-minus
9-
'–': true, // En dash
10-
'—': true, // Em dash
11-
':': true, // Colon
12-
';': true // Semi-colon
13-
}
7+
var single = [
8+
'-', // Hyphen-minus
9+
'–', // En dash
10+
'—', // Em dash
11+
':', // Colon
12+
';' // Semi-colon
13+
]
1414

1515
// Pair delimiters.
1616
// From common sense, and UncycloPedia:
1717
// <https://en.wikipedia.org/wiki/Quotation_mark>.
1818
var pairs = {
19-
',': {
20-
',': true
21-
},
22-
'-': {
23-
'-': true
24-
},
25-
'–': {
26-
'–': true
27-
},
28-
'—': {
29-
'—': true
30-
},
31-
'"': {
32-
'"': true
33-
},
34-
"'": {
35-
"'": true
36-
},
37-
'‘': {
38-
'’': true
39-
},
40-
'‚': {
41-
'’': true
42-
},
43-
'’': {
44-
'’': true,
45-
'‚': true
46-
},
47-
'“': {
48-
'”': true
49-
},
50-
'”': {
51-
'”': true
52-
},
53-
'„': {
54-
'”': true,
55-
'“': true
56-
},
57-
'«': {
58-
'»': true
59-
},
60-
'»': {
61-
'«': true
62-
},
63-
'‹': {
64-
'›': true
65-
},
66-
'›': {
67-
'‹': true
68-
},
69-
'(': {
70-
')': true
71-
},
72-
'[': {
73-
']': true
74-
},
75-
'{': {
76-
'}': true
77-
},
78-
'⟨': {
79-
'⟩': true
80-
},
81-
'「': {
82-
'」': true
83-
}
19+
',': [','],
20+
'-': ['-'],
21+
'–': ['–'],
22+
'—': ['—'],
23+
'"': ['"'],
24+
"'": ["'"],
25+
'‘': ['’'],
26+
'‚': ['’'],
27+
'’': ['’', '‚'],
28+
'“': ['”'],
29+
'”': ['”'],
30+
'„': ['”', '“'],
31+
'«': ['»'],
32+
'»': ['«'],
33+
'‹': ['›'],
34+
'›': ['‹'],
35+
'(': [')'],
36+
'[': [']'],
37+
'{': ['}'],
38+
'⟨': ['⟩'],
39+
'「': ['」']
40+
}
41+
42+
var open = []
43+
var key
44+
45+
for (key in pairs) {
46+
open.push(key)
8447
}
8548

8649
// Check if the node in `parent` at `position` is enclosed by matching
@@ -102,30 +65,23 @@ function isLiteral(parent, index) {
10265
throw new Error('Index must be a number')
10366
}
10467

105-
if (
68+
return Boolean(
10669
(!containsWord(parent, -1, index) &&
10770
siblingDelimiter(parent, index, 1, single)) ||
108-
(!containsWord(parent, index, parent.children.length) &&
109-
siblingDelimiter(parent, index, -1, single)) ||
110-
isWrapped(parent, index, pairs)
111-
) {
112-
return true
113-
}
114-
115-
return false
71+
(!containsWord(parent, index, parent.children.length) &&
72+
siblingDelimiter(parent, index, -1, single)) ||
73+
isWrapped(parent, index)
74+
)
11675
}
11776

11877
// Check if the node in `parent` at `position` is enclosed by matching
11978
// delimiters.
120-
function isWrapped(parent, position, delimiters) {
121-
var previous = siblingDelimiter(parent, position, -1, delimiters)
122-
var next
79+
function isWrapped(parent, position) {
80+
var previous = siblingDelimiter(parent, position, -1, open)
12381

12482
if (previous) {
125-
next = siblingDelimiter(parent, position, 1, delimiters[toString(previous)])
83+
return siblingDelimiter(parent, position, 1, pairs[toString(previous)])
12684
}
127-
128-
return next
12985
}
13086

13187
// Find the previous or next delimiter before or after `position` in `parent`.
@@ -142,7 +98,7 @@ function siblingDelimiter(parent, position, step, delimiters) {
14298
}
14399

144100
if (sibling.type !== 'WhiteSpaceNode') {
145-
return toString(sibling) in delimiters && sibling
101+
return delimiters.indexOf(toString(sibling)) > -1 && sibling
146102
}
147103

148104
index += step

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
"prettier": true,
6868
"esnext": false,
6969
"rules": {
70+
"guard-for-in": "off",
71+
"unicorn/prefer-includes": "off",
7072
"unicorn/prefer-number-properties": "off",
7173
"unicorn/prefer-type-error": "off"
7274
},

0 commit comments

Comments
 (0)