@@ -4,83 +4,46 @@ var toString = require('nlcst-to-string')
4
4
5
5
module . exports = isLiteral
6
6
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
+ ]
14
14
15
15
// Pair delimiters.
16
16
// From common sense, and UncycloPedia:
17
17
// <https://en.wikipedia.org/wiki/Quotation_mark>.
18
18
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 )
84
47
}
85
48
86
49
// Check if the node in `parent` at `position` is enclosed by matching
@@ -102,30 +65,23 @@ function isLiteral(parent, index) {
102
65
throw new Error ( 'Index must be a number' )
103
66
}
104
67
105
- if (
68
+ return Boolean (
106
69
( ! containsWord ( parent , - 1 , index ) &&
107
70
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
+ )
116
75
}
117
76
118
77
// Check if the node in `parent` at `position` is enclosed by matching
119
78
// 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 )
123
81
124
82
if ( previous ) {
125
- next = siblingDelimiter ( parent , position , 1 , delimiters [ toString ( previous ) ] )
83
+ return siblingDelimiter ( parent , position , 1 , pairs [ toString ( previous ) ] )
126
84
}
127
-
128
- return next
129
85
}
130
86
131
87
// Find the previous or next delimiter before or after `position` in `parent`.
@@ -142,7 +98,7 @@ function siblingDelimiter(parent, position, step, delimiters) {
142
98
}
143
99
144
100
if ( sibling . type !== 'WhiteSpaceNode' ) {
145
- return toString ( sibling ) in delimiters && sibling
101
+ return delimiters . indexOf ( toString ( sibling ) ) > - 1 && sibling
146
102
}
147
103
148
104
index += step
0 commit comments