@@ -4,83 +4,46 @@ var toString = require('nlcst-to-string')
44
55module . 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 WikiPedia:
1717// <https://en.wikipedia.org/wiki/Quotation_mark>.
1818var 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
0 commit comments