1
- 'use strict' ;
1
+ 'use strict'
2
2
3
- var toString = require ( 'nlcst-to-string' ) ;
3
+ var toString = require ( 'nlcst-to-string' )
4
4
5
- module . exports = isLiteral ;
5
+ module . exports = isLiteral
6
6
7
7
var single = {
8
8
'-' : true , // Hyphen-minus
9
9
'–' : true , // En dash
10
10
'—' : true , // Em dash
11
11
':' : true , // Colon
12
12
';' : true // Semi-colon
13
- } ;
13
+ }
14
14
15
15
/* Pair delimiters. From common sense, and wikipedia:
16
16
* Mostly from https://en.wikipedia.org/wiki/Quotation_mark. */
@@ -30,8 +30,8 @@ var pairs = {
30
30
'"' : {
31
31
'"' : true
32
32
} ,
33
- '\'' : {
34
- '\'' : true
33
+ "'" : {
34
+ "'" : true
35
35
} ,
36
36
'‘' : {
37
37
'’' : true
@@ -80,122 +80,123 @@ var pairs = {
80
80
'「' : {
81
81
'」' : true
82
82
}
83
- } ;
83
+ }
84
84
85
85
/* Check if the node in `parent` at `position` is enclosed
86
86
* by matching delimiters. */
87
87
function isLiteral ( parent , index ) {
88
88
if ( ! ( parent && parent . children ) ) {
89
- throw new Error ( 'Parent must be a node' ) ;
89
+ throw new Error ( 'Parent must be a node' )
90
90
}
91
91
92
92
if ( isNaN ( index ) ) {
93
- throw new Error ( 'Index must be a number' ) ;
93
+ throw new Error ( 'Index must be a number' )
94
94
}
95
95
96
96
if (
97
97
( ! hasWordsBefore ( parent , index ) && nextDelimiter ( parent , index , single ) ) ||
98
- ( ! hasWordsAfter ( parent , index ) && previousDelimiter ( parent , index , single ) ) ||
98
+ ( ! hasWordsAfter ( parent , index ) &&
99
+ previousDelimiter ( parent , index , single ) ) ||
99
100
isWrapped ( parent , index , pairs )
100
101
) {
101
- return true ;
102
+ return true
102
103
}
103
104
104
- return false ;
105
+ return false
105
106
}
106
107
107
108
/* Check if the node in `parent` at `position` is enclosed
108
109
* by matching delimiters. */
109
110
function isWrapped ( parent , position , delimiters ) {
110
- var prev = previousDelimiter ( parent , position , delimiters ) ;
111
- var next ;
111
+ var prev = previousDelimiter ( parent , position , delimiters )
112
+ var next
112
113
113
114
if ( prev ) {
114
- next = nextDelimiter ( parent , position , delimiters [ toString ( prev ) ] ) ;
115
+ next = nextDelimiter ( parent , position , delimiters [ toString ( prev ) ] )
115
116
}
116
117
117
- return Boolean ( next ) ;
118
+ return Boolean ( next )
118
119
}
119
120
120
121
/* Find the previous delimiter before `position` in
121
122
* `parent`. Returns the delimiter node when found. */
122
123
function previousDelimiter ( parent , position , delimiters ) {
123
- var siblings = parent . children ;
124
- var index = position ;
125
- var result ;
124
+ var siblings = parent . children
125
+ var index = position
126
+ var result
126
127
127
128
while ( index -- ) {
128
- result = delimiterCheck ( siblings [ index ] , delimiters ) ;
129
+ result = delimiterCheck ( siblings [ index ] , delimiters )
129
130
130
131
if ( result === null ) {
131
- continue ;
132
+ continue
132
133
}
133
134
134
- return result ;
135
+ return result
135
136
}
136
137
137
- return null ;
138
+ return null
138
139
}
139
140
140
141
/* Find the next delimiter after `position` in
141
142
* `parent`. Returns the delimiter node when found. */
142
143
function nextDelimiter ( parent , position , delimiters ) {
143
- var siblings = parent . children ;
144
- var index = position ;
145
- var length = siblings . length ;
146
- var result ;
144
+ var siblings = parent . children
145
+ var index = position
146
+ var length = siblings . length
147
+ var result
147
148
148
149
while ( ++ index < length ) {
149
- result = delimiterCheck ( siblings [ index ] , delimiters ) ;
150
+ result = delimiterCheck ( siblings [ index ] , delimiters )
150
151
151
152
if ( result === null ) {
152
- continue ;
153
+ continue
153
154
}
154
155
155
- return result ;
156
+ return result
156
157
}
157
158
158
- return null ;
159
+ return null
159
160
}
160
161
161
162
/* Check if `node` is in `delimiters`. */
162
163
function delimiterCheck ( node , delimiters ) {
163
- var type = node . type ;
164
+ var type = node . type
164
165
165
166
if ( type === 'WordNode' || type === 'SourceNode' ) {
166
- return false ;
167
+ return false
167
168
}
168
169
169
170
if ( type === 'WhiteSpaceNode' ) {
170
- return null ;
171
+ return null
171
172
}
172
173
173
- return toString ( node ) in delimiters ? node : false ;
174
+ return toString ( node ) in delimiters ? node : false
174
175
}
175
176
176
177
/* Check if there are word nodes before `position`
177
178
* in `parent`. */
178
179
function hasWordsBefore ( parent , position ) {
179
- return containsWord ( parent , 0 , position ) ;
180
+ return containsWord ( parent , 0 , position )
180
181
}
181
182
182
183
/* Check if there are word nodes before `position`
183
184
* in `parent`. */
184
185
function hasWordsAfter ( parent , position ) {
185
- return containsWord ( parent , position + 1 , parent . children . length ) ;
186
+ return containsWord ( parent , position + 1 , parent . children . length )
186
187
}
187
188
188
189
/* Check if parent contains word-nodes between
189
190
* `start` and `end`. */
190
191
function containsWord ( parent , start , end ) {
191
- var siblings = parent . children ;
192
- var index = start - 1 ;
192
+ var siblings = parent . children
193
+ var index = start - 1
193
194
194
195
while ( ++ index < end ) {
195
196
if ( siblings [ index ] . type === 'WordNode' ) {
196
- return true ;
197
+ return true
197
198
}
198
199
}
199
200
200
- return false ;
201
+ return false
201
202
}
0 commit comments