11var own = { } . hasOwnProperty
22
3- // Transform a string into an array or object of values.
4- export function toJson ( value , options ) {
3+ /**
4+ * @typedef {Object } ToJsonOptions
5+ * @property {boolean } [log=true]
6+ * @property {string } [delimiter=':']
7+ * @property {string[]|string|false } [comment='%']
8+ * @property {boolean|'fix' } [forgiving]
9+ */
10+
11+ /**
12+ * Transform a string into an array or object of values.
13+ *
14+ * @param {string } value
15+ * @param {ToJsonOptions } [options={}]
16+ */
17+ export function toJson ( value , options = { } ) {
18+ var log =
19+ options . log === null || options . log === undefined ? true : options . log
20+ var comment =
21+ options . comment === null || options . comment === undefined
22+ ? '%'
23+ : options . comment
24+ var comments = comment ? ( Array . isArray ( comment ) ? comment : [ comment ] ) : [ ]
25+ var delimiter = options . delimiter || ':'
26+ var forgiving = options . forgiving
527 var propertyOrValues = { }
6- var lines
28+ /** @type { boolean } */
729 var isPropertyValuePair
30+ /** @type {Array.<Array.<string>> } */
831 var pairs
9- var values
10- var comments
1132
12- if ( ! options ) {
13- options = { }
14- }
15-
16- if ( options . log === null || options . log === undefined ) {
17- options . log = true
18- }
19-
20- if ( options . comment === null || options . comment === undefined ) {
21- options . comment = '%'
22- }
33+ var lines = value
34+ . split ( '\n' )
35+ . map ( ( line ) => {
36+ var commentIndex = - 1
37+ /** @type {number } */
38+ var index
2339
24- lines = value . split ( '\n' )
40+ while ( ++ commentIndex < comments . length ) {
41+ index = line . indexOf ( comments [ commentIndex ] )
42+ if ( index !== - 1 ) line = line . slice ( 0 , index )
43+ }
2544
26- comments = options . comment
27- ? Array . isArray ( options . comment )
28- ? options . comment
29- : [ options . comment ]
30- : [ ]
45+ return line . trim ( )
46+ } )
47+ . filter ( Boolean )
3148
32- comments . forEach ( function ( comment ) {
33- lines = lines . map ( stripComments ( comment ) )
34- } )
49+ pairs = lines . map (
50+ // Transform `value` to a property--value tuple.
51+ function ( value ) {
52+ var values = value . split ( delimiter )
53+ var result = [ values . shift ( ) . trim ( ) ]
3554
36- lines = lines . map ( ( d ) => d . trim ( ) ) . filter ( Boolean )
55+ if ( values . length > 0 ) {
56+ result . push ( values . join ( delimiter ) . trim ( ) )
57+ }
3758
38- pairs = lines . map ( toPropertyValuePairs ( options . delimiter || ':' ) )
59+ return result
60+ }
61+ )
3962
4063 pairs . forEach ( function ( line , index ) {
41- var currentLineIsPropertyValuePair
42-
43- currentLineIsPropertyValuePair = line . length === 2
64+ var currentLineIsPropertyValuePair = line . length === 2
4465
4566 if ( index === 0 ) {
4667 isPropertyValuePair = currentLineIsPropertyValuePair
@@ -56,8 +77,8 @@ export function toJson(value, options) {
5677
5778 if ( own . call ( propertyOrValues , line [ 0 ] ) ) {
5879 if (
59- ! options . forgiving ||
60- ( options . forgiving === true &&
80+ ! forgiving ||
81+ ( forgiving === true &&
6182 currentLineIsPropertyValuePair &&
6283 line [ 1 ] !== propertyOrValues [ line [ 0 ] ] )
6384 ) {
@@ -71,11 +92,8 @@ export function toJson(value, options) {
7192 )
7293 }
7394
74- if ( options . log ) {
75- if (
76- options . forgiving === 'fix' &&
77- propertyOrValues [ line [ 0 ] ] !== line [ 1 ]
78- ) {
95+ if ( log ) {
96+ if ( forgiving === 'fix' && propertyOrValues [ line [ 0 ] ] !== line [ 1 ] ) {
7997 console . log (
8098 'Overwriting `' +
8199 propertyOrValues [ line [ 0 ] ] +
@@ -97,59 +115,17 @@ export function toJson(value, options) {
97115
98116 if ( isPropertyValuePair ) {
99117 pairs . sort ( sortOnFirstIndex )
100- values = propertyValuePairsToObject ( pairs )
101- } else {
102- lines . sort ( )
118+ return Object . fromEntries ( pairs )
103119 }
104120
105- return values || lines
106- }
107-
108- // Transform a list of property--value tuples to an object.
109- function propertyValuePairsToObject ( pairs ) {
110- var values = { }
111-
112- pairs . forEach ( function ( pair ) {
113- values [ pair [ 0 ] ] = pair [ 1 ]
114- } )
115-
116- return values
121+ return lines . sort ( )
117122}
118123
119- // Sort on the first (`0`) index.
124+ /**
125+ * Sort on the first (`0`) index.
126+ * @param {Array.<string> } a
127+ * @param {Array.<string> } b
128+ */
120129function sortOnFirstIndex ( a , b ) {
121130 return a [ 0 ] . charCodeAt ( 0 ) - b [ 0 ] . charCodeAt ( 0 )
122131}
123-
124- // Factory to transform lines to property--value tuples.
125- function toPropertyValuePairs ( token ) {
126- return toPropValuePairs
127-
128- // Transform `value` to a property--value tuple.
129- function toPropValuePairs ( value ) {
130- var values = value . split ( token )
131- var result = [ values . shift ( ) . trim ( ) ]
132-
133- if ( values . length > 0 ) {
134- result . push ( values . join ( token ) . trim ( ) )
135- }
136-
137- return result
138- }
139- }
140-
141- // Strip comments factory.
142- function stripComments ( token ) {
143- return strip
144-
145- // Strip comments.
146- function strip ( value ) {
147- var index = value . indexOf ( token )
148-
149- if ( index !== - 1 ) {
150- value = value . slice ( 0 , index )
151- }
152-
153- return value
154- }
155- }
0 commit comments