-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
343 lines (299 loc) · 7.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'use strict';
/**
* Default options
* @const
* @type {Object}
*/
var DEFAULTS = {
keyValDelim: ':',
propsDelim: ',',
useSpaceBeforeKeyValDelim: false,
useSpaceAfterKeyValDelim: false,
useSpaceBeforePropsDelim: false,
useSpaceAfterPropsDelim: false,
useAlwaysQuotesForStrings: false
};
/**
* @const
* @type {RegExp}
*/
var rDoubleQuotesFilter = /^"(.*)"$/;
/**
* Has own property?
* @private
* @param {String} key
* @param {Object} ctx
* @returns {Boolean}
*/
function isOwnProperty(key, ctx) {
return Object.hasOwnProperty.call(ctx, key);
}
/**
* Extends default options
* @private
* @param {Object} opts
* @returns {Object}
*/
function extendDefaults(opts) {
opts = opts || {};
var res = {};
var key;
for (key in DEFAULTS) {
if (isOwnProperty(key, DEFAULTS)) {
res[key] = isOwnProperty(key, opts) ? opts[key] : DEFAULTS[key];
}
}
return res;
}
/**
* Removes extra spaces
* @private
* @param {String} str
* @param {Object} opts
* @param {String} opts.propsDelim
* @param {String} opts.keyValDelim
* @returns {String}
*/
function clearString(str, opts) {
var propsDelim = opts.propsDelim;
var keyValDelim = opts.keyValDelim;
var rPropsFilter = new RegExp('\\s*' + propsDelim + '\\s*', 'g');
var rKeyValFilter = new RegExp('\\s*' + keyValDelim + '\\s*', 'g');
return str.replace(rKeyValFilter, keyValDelim).replace(rPropsFilter, propsDelim);
}
/**
* Splits a string
* @private
* @param {String} str
* @param {String} delimiter
* @param {Boolean} useOnce Finish after the first match
* @returns {Array}
*/
function splitString(str, delimiter, useOnce) {
var i = 0;
var len = str.length;
var res = [];
var isBetweenQuotes = false;
var char;
// Find delimiters and cut the string
while (i !== len) {
char = str.charAt(i);
i++;
// Do not split if the delimiter is between quotes
if (char === '\"') {
isBetweenQuotes = !isBetweenQuotes;
} else if (char === delimiter && !isBetweenQuotes) {
res.push(str.substring(0, i - 1));
str = str.substring(i);
if (useOnce) {
break;
}
len = str.length;
i = 0;
}
}
// Add the last part
res.push(str);
return res;
}
/**
* Checks if a stringified value is the boolean type
* @private
* @param {String} value
* @returns {Boolean}
*/
function isBoolean(value) {
return value === 'true' || value === 'false';
}
/**
* Checks if a stringified value is the number type
* @private
* @param {String} value
* @returns {Boolean}
*/
function isNumber(value) {
return !isNaN(value);
}
/**
* Checks if a stringified value is undefined
* @private
* @param {String} value
* @returns {Boolean}
*/
function isUndefined(value) {
return value === 'undefined';
}
/**
* Checks if a stringified value is null
* @private
* @param {String} value
* @returns {Boolean}
*/
function isNull(value) {
return value === 'null';
}
/**
* Checks if a stringified value is unsafe
* @private
* @param {String} value
* @param {Object} opts
* @param {String} opts.propsDelim
* @returns {Boolean}
*/
function isUnsafe(value, opts) {
return value.search(rDoubleQuotesFilter) !== 0 && value.indexOf(opts.propsDelim) !== -1;
}
/**
* Parses a stringified number
* @private
* @param {String} value
* @returns {Number}
*/
function parseNumber(value) {
// Float
if (value.indexOf('.') !== -1) {
return parseFloat(value);
}
// If the input string begins with "0x" or "0X", radix is 16
if (value.length > 2 && (value.indexOf('0x') === 0 || value.indexOf('0X') === 0)) {
return parseInt(value, 16);
}
return parseInt(value, 10);
}
/**
* Parses a stringified value to the expected type
* @private
* @param {String} value
* @returns {Number|Boolean|String|null|undefined}
*/
function parseValue(value) {
if (isNull(value)) {
return null;
}
if (isUndefined(value)) {
return undefined;
}
if (isBoolean(value)) {
return value === 'true';
}
if (isNumber(value)) {
return parseNumber(value);
}
// Remove double quotes
return value.replace(rDoubleQuotesFilter, '$1');
}
/**
* Stringifies a value
* @private
* @param {*} value
* @param {Object} opts
* @returns {String}
*/
function stringifyValue(value, opts) {
if (typeof value === 'string' || value instanceof String) {
if (
opts.useAlwaysQuotesForStrings ||
isUnsafe(value, opts) ||
isNull(value) ||
isUndefined(value) ||
isBoolean(value) ||
isNumber(value)
) {
return '\"' + value + '\"';
}
}
// Convert to the string type, always
return '' + value;
}
/**
* Stringifies a property
* @private
* @param {String} key
* @param {*} value
* @param {Object} opts
* @param {String} opts.keyValDelim
* @param {Boolean} opts.useSpaceBeforeKeyValDelim
* @param {Boolean} opts.useSpaceAfterKeyValDelim
* @param {Boolean} opts.useSpaceBeforePropsDelim
* @param {Boolean} opts.useSpaceAfterPropsDelim
* @returns {String}
*/
function stringifyProp(key, value, opts) {
return [
key,
opts.useSpaceBeforeKeyValDelim ? ' ' : '',
opts.keyValDelim,
opts.useSpaceAfterKeyValDelim ? ' ' : '',
stringifyValue(value, opts)
].join('');
}
/**
* Join an array of processed properties
* @private
* @param {Array} props
* @param {Object} opts
* @param {String} opts.propsDelim
* @param {Boolean} opts.useSpaceBeforePropsDelim
* @param {Boolean} opts.useSpaceAfterPropsDelim
* @returns {String}
*/
function joinProps(props, opts) {
var delimiter = [
opts.useSpaceBeforePropsDelim ? ' ' : '',
opts.propsDelim,
opts.useSpaceAfterPropsDelim ? ' ' : ''
].join('');
return props.join(delimiter);
}
/**
* Parses stringified options
* @public
* @param {String} str Stringified options for parsing
* @param {Object} opts
* @param {String} [opts.propsDelim=',']
* @param {String} [opts.keyValDelim=':']
* @returns {Object}
*/
module.exports.parse = function(str, opts) {
opts = extendDefaults(opts);
var result = {};
var i;
var len;
var prop;
var props;
// Get an array of stringified properties
props = splitString(clearString(str, opts), opts.propsDelim);
// Convert stringified properties to the object with parsed values
for (i = 0, len = props.length; i < len; i++) {
prop = splitString(props[i], opts.keyValDelim, true);
if (prop.length === 2) {
result[prop[0]] = parseValue(prop[1]);
}
}
return result;
};
/**
* Stringifies an object with options
* @public
* @param {Object} obj
* @param {Object} opts
* @param {String} [opts.propsDelim=',']
* @param {String} [opts.keyValDelim=':']
* @param {Boolean} [opts.useSpaceBeforeKeyValDelim=false]
* @param {Boolean} [opts.useSpaceAfterKeyValDelim=false]
* @param {Boolean} [opts.useSpaceBeforePropsDelim=false]
* @param {Boolean} [opts.useSpaceAfterPropsDelim=false]
* @param {Boolean} [opts.useAlwaysQuotesForStrings=false]
* @returns {String}
*/
module.exports.stringify = function(obj, opts) {
opts = extendDefaults(opts);
var res = [];
var key;
for (key in obj) {
if (isOwnProperty(key, obj)) {
res.push(stringifyProp(key, obj[key], opts));
}
}
return joinProps(res, opts);
};