1- 'use strict' ;
1+ 'use strict'
22
3- var trim = require ( 'trim' ) ;
3+ var trim = require ( 'trim' )
44
5- module . exports = toJSON ;
5+ module . exports = toJSON
66
7- var own = { } . hasOwnProperty ;
7+ var own = { } . hasOwnProperty
88
99/* Transform a string into an array or object of values. */
1010function toJSON ( value , options ) {
11- var propertyOrValues = { } ;
12- var lines ;
13- var isPropertyValuePair ;
14- var pairs ;
15- var values ;
11+ var propertyOrValues = { }
12+ var lines
13+ var isPropertyValuePair
14+ var pairs
15+ var values
1616
1717 if ( ! options ) {
18- options = { } ;
18+ options = { }
1919 }
2020
2121 if ( options . log === null || options . log === undefined ) {
22- options . log = true ;
22+ options . log = true
2323 }
2424
2525 if ( options . comment === null || options . comment === undefined ) {
26- options . comment = '%' ;
26+ options . comment = '%'
2727 }
2828
29- lines = value . split ( '\n' ) ;
29+ lines = value . split ( '\n' )
3030
3131 if ( options . comment ) {
32- lines = lines . map ( stripComments ( options . comment ) ) ;
32+ lines = lines . map ( stripComments ( options . comment ) )
3333 }
3434
35- lines = lines . map ( trim ) . filter ( Boolean ) ;
35+ lines = lines . map ( trim ) . filter ( Boolean )
3636
37- pairs = lines . map ( toPropertyValuePairs ( options . delimiter || ':' ) ) ;
37+ pairs = lines . map ( toPropertyValuePairs ( options . delimiter || ':' ) )
3838
39- pairs . forEach ( function ( line , index ) {
40- var currentLineIsPropertyValuePair ;
39+ pairs . forEach ( function ( line , index ) {
40+ var currentLineIsPropertyValuePair
4141
42- currentLineIsPropertyValuePair = line . length === 2 ;
42+ currentLineIsPropertyValuePair = line . length === 2
4343
4444 if ( index === 0 ) {
45- isPropertyValuePair = currentLineIsPropertyValuePair ;
45+ isPropertyValuePair = currentLineIsPropertyValuePair
4646 } else if ( currentLineIsPropertyValuePair !== isPropertyValuePair ) {
4747 throw new Error (
48- 'Error at `' + line + '`: ' +
49- 'Both property-value pairs and array values found. ' +
50- 'Make sure either exists.'
51- ) ;
48+ 'Error at `' +
49+ line +
50+ '`: ' +
51+ 'Both property-value pairs and array values found. ' +
52+ 'Make sure either exists.'
53+ )
5254 }
5355
5456 if ( own . call ( propertyOrValues , line [ 0 ] ) ) {
5557 if (
5658 ! options . forgiving ||
57- (
58- options . forgiving === true &&
59+ ( options . forgiving === true &&
5960 currentLineIsPropertyValuePair &&
60- line [ 1 ] !== propertyOrValues [ line [ 0 ] ]
61- )
61+ line [ 1 ] !== propertyOrValues [ line [ 0 ] ] )
6262 ) {
6363 throw new Error (
64- 'Error at `' + line + '`: ' +
65- 'Duplicate data found. ' +
66- 'Make sure, in objects, no duplicate properties exist;' +
67- 'in arrays, no duplicate values.'
68- ) ;
64+ 'Error at `' +
65+ line +
66+ '`: ' +
67+ 'Duplicate data found. ' +
68+ 'Make sure, in objects, no duplicate properties exist;' +
69+ 'in arrays, no duplicate values.'
70+ )
6971 }
7072
7173 if ( options . log ) {
@@ -74,75 +76,79 @@ function toJSON(value, options) {
7476 propertyOrValues [ line [ 0 ] ] !== line [ 1 ]
7577 ) {
7678 console . log (
77- 'Overwriting `' + propertyOrValues [ line [ 0 ] ] + '` ' +
78- 'to `' + line [ 1 ] + '` for `' + line [ 0 ] + '`'
79- ) ;
79+ 'Overwriting `' +
80+ propertyOrValues [ line [ 0 ] ] +
81+ '` ' +
82+ 'to `' +
83+ line [ 1 ] +
84+ '` for `' +
85+ line [ 0 ] +
86+ '`'
87+ )
8088 } else {
81- console . log (
82- 'Ignoring duplicate key for `' + line [ 0 ] + '`'
83- ) ;
89+ console . log ( 'Ignoring duplicate key for `' + line [ 0 ] + '`' )
8490 }
8591 }
8692 }
8793
88- propertyOrValues [ line [ 0 ] ] = line [ 1 ] ;
89- } ) ;
94+ propertyOrValues [ line [ 0 ] ] = line [ 1 ]
95+ } )
9096
9197 if ( isPropertyValuePair ) {
92- pairs . sort ( sortOnFirstIndex ) ;
93- values = propertyValuePairsToObject ( pairs ) ;
98+ pairs . sort ( sortOnFirstIndex )
99+ values = propertyValuePairsToObject ( pairs )
94100 } else {
95- lines . sort ( ) ;
101+ lines . sort ( )
96102 }
97103
98- return values || lines ;
104+ return values || lines
99105}
100106
101107/* Transform a list of property--value tuples to an object. */
102108function propertyValuePairsToObject ( pairs ) {
103- var values = { } ;
109+ var values = { }
104110
105- pairs . forEach ( function ( pair ) {
106- values [ pair [ 0 ] ] = pair [ 1 ] ;
107- } ) ;
111+ pairs . forEach ( function ( pair ) {
112+ values [ pair [ 0 ] ] = pair [ 1 ]
113+ } )
108114
109- return values ;
115+ return values
110116}
111117
112118/* Sort on the first (`0`) index. */
113119function sortOnFirstIndex ( a , b ) {
114- return a [ 0 ] . charCodeAt ( 0 ) - b [ 0 ] . charCodeAt ( 0 ) ;
120+ return a [ 0 ] . charCodeAt ( 0 ) - b [ 0 ] . charCodeAt ( 0 )
115121}
116122
117123/* Factory to transform lines to property--value tuples. */
118124function toPropertyValuePairs ( token ) {
119- return toPropValuePairs ;
125+ return toPropValuePairs
120126
121127 /* Transform `value` to a property--value tuple. */
122128 function toPropValuePairs ( value ) {
123- var values = value . split ( token ) ;
124- var result = [ trim ( values . shift ( ) ) ] ;
129+ var values = value . split ( token )
130+ var result = [ trim ( values . shift ( ) ) ]
125131
126132 if ( values . length !== 0 ) {
127- result . push ( trim ( values . join ( token ) ) ) ;
133+ result . push ( trim ( values . join ( token ) ) )
128134 }
129135
130- return result ;
136+ return result
131137 }
132138}
133139
134140/* Strip comments factory. */
135141function stripComments ( token ) {
136- return strip ;
142+ return strip
137143
138144 /* Strip comments. */
139145 function strip ( value ) {
140- var index = value . indexOf ( token ) ;
146+ var index = value . indexOf ( token )
141147
142148 if ( index !== - 1 ) {
143- value = value . substr ( 0 , index ) ;
149+ value = value . substr ( 0 , index )
144150 }
145151
146- return value ;
152+ return value
147153 }
148154}
0 commit comments