-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsonc.safe.d.ts
451 lines (451 loc) · 19.6 KB
/
jsonc.safe.d.ts
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import { IConfig, IParseOptions, IReadOptions, IStringifyOptions, IWriteOptions, Replacer, Reviver } from './interfaces';
/**
* Class that provides safe versions of `jsonc` methods. Safe methods provide a
* way to easily handle errors without throwing; so that you don't need to use
* try/catch blocks.
*
* Each method (except a few such as `.isJSON`), will return an array with the
* first item being the `Error` instance caught. If successful, second item
* will be the result.
* @name jsonc.safe
* @class
*
* @example
* const { safe } = require('jsonc');
* // or
* import { safe as jsonc } from 'jsonc';
*
* const [err, result] = jsonc.parse('[invalid JSON}');
* if (err) {
* console.log(`Failed to parse JSON: ${err.message}`);
* } else {
* console.log(result);
* }
*/
declare class jsoncSafe {
/**
* Configures `jsonc` object.
*
* <blockquote>This method is added for convenience. Works the same as `jsonc.config()`.</blockquote>
*
* @name jsonc.safe.config
* @function
*
* @param {IConfig} cfg - Configurations.
* @param {NodeJS.WriteStream} [stream] - Stream to write logs to. This is
* used with `.log()` and `.logp()` methods.
* @param {NodeJS.WriteStream} [streamErr] - Stream to write error logs to.
* This is used with `.log()` and `.logp()` methods.
*
* @example
* import { safe as jsonc } from 'jsonc';
* // Output logs to stdout but logs containing errors to a file.
* jsonc.config({
* stream: process.stdout,
* streamErr: fs.createWriteStream('path/to/log.txt')
* });
* jsonc.log({ info: 'this is logged to console' });
* jsonc.log(new Error('this is logged to file'));
*/
static config(cfg: IConfig): void;
/**
* Stringifies and logs the given arguments to console. This will
* automatically handle circular references; so it won't throw.
*
* If an `Error` instance is passed, it will log the `.stack` property on
* the instance, without stringifying the object.
*
* <blockquote>This method is added for convenience. Works the same as `jsonc.log()`.</blockquote>
* @name jsonc.safe.log
* @function
*
* @param {...any[]} [args] - Arguments to be logged.
* @returns {void}
*/
static log(...args: any[]): void;
/**
* Pretty version of `log()` method. Stringifies and logs the given
* arguments to console, with indents. This will automatically handle
* circular references; so it won't throw.
*
* If an `Error` instance is passed, it will log the `.stack` property on
* the instance, without stringifying the object.
*
* <blockquote>This method is added for convenience. Works the same as `jsonc.logp()`.</blockquote>
* @name jsonc.safe.logp
* @function
*
* @param {...any[]} [args] - Arguments to be logged.
* @returns {void}
*/
static logp(...args: any[]): void;
/**
* Safe version of `jsonc.parse()`. Parses the given string into a
* JavaScript object.
* @name jsonc.safe.parse
* @function
*
* @param {string} str - JSON string to be parsed.
* @param {IParseOptions|Reviver} [options] - Either a parse options
* object or a reviver function.
* @param {Reviver} [options.reviver] - A function that can filter and
* transform the results. It receives each of the keys and values, and
* its return value is used instead of the original value. If it
* returns what it received, then the structure is not modified. If it
* returns `undefined` then the member is deleted.
* @param {boolean} [options.stripComments=true] - Whether to strip
* comments from the JSON string. Note that it will return the first
* parameter as an error if this is set to `false` and the string
* includes comments.
*
* @returns {Array} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful, second
* item will be the result: `[Error, any]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const [err, result] = jsonc.parse('--invalid JSON--');
* if (err) {
* console.log('Failed to parse JSON: ' + err.message);
* } else {
* console.log(result);
* }
*/
static parse(str: string, options?: IParseOptions | Reviver): [Error, undefined] | [null, any];
/**
* Safe version of `jsonc.stringify()`. Stringifies the given
* JavaScript object. The input object can have circular references
* which will return the string `"[Circular]"` for each circular
* reference, by default. You can use a replacer function to replace or
* remove circular references instead.
* @name jsonc.safe.stringify
* @function
*
* @param {*} value - JavaScript value to be stringified.
* @param {IStringifyOptions|Replacer} [options] - Stringify options or
* a replacer.
* @param {Replacer} [options.replacer] - Determines how object values
* are stringified for objects. It can be an array of strings or
* numbers; or a function with the following signature: `(key: string,
* value: any) => any`.
* @param {string|number} [options.space] - Specifies the indentation
* of nested structures. If it is omitted, the text will be packed
* without extra whitespace. If it is a number, it will specify the
* number of spaces to indent at each level. If it is a string (such as
* `"\t"` or `" "`), it contains the characters used to indent at
* each level.
* @param {boolean} [options.handleCircular=true] - Whether to handle
* circular references (if any) by replacing their values with the
* string `"[Circular]"`. You can also use a replacer function to
* replace or remove circular references instead.
* @param {string|number} [space] - This takes effect if second
* argument is the `replacer` or a falsy value. Included for supporting
* the signature of native `JSON.stringify()` method.
*
* @returns {Array} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful, second
* item will be the result: `[Error, string]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const obj = { key: 'value' };
* let [err, str] = jsonc.stringify(obj);
* if (!err) console.log(str); // '{"key":"value"}'
*
* // pretty output with indents
* let [err, pretty] = jsonc.stringify(obj, null, 2);
* // equivalent to:
* [err, pretty] = jsonc.stringify(obj, { reviver: null, space: 2 });
* if (!err) console.log(pretty);
* // {
* // "key": "value"
* // }
*/
static stringify(value: any, option?: IStringifyOptions): [Error, undefined] | [null, string];
static stringify(value: any, replacer: Replacer, space?: string | number): [Error, undefined] | [null, string];
/**
* Specifies whether the given string has well-formed JSON structure.
*
* Note that, not all JSON-parsable strings are considered well-formed JSON
* structures. JSON is built on two structures; a collection of name/value
* pairs (object) or an ordered list of values (array).
*
* For example, `JSON.parse('true')` will parse successfully but
* `jsonc.isJSON('true')` will return `false` since it has no object or
* array structure.
*
* <blockquote>This method is added for convenience. Works the same as
* `jsonc.isJSON()`.</blockquote>
* @name jsonc.safe.isJSON
* @function
*
* @param {string} str - String to be validated.
* @param {boolean} [allowComments=false] - Whether comments should be
* considered valid.
*
* @returns {boolean}
*
* @example
* import { safe as jsonc } from 'jsonc';
* jsonc.isJSON('{"x":1}'); // true
* jsonc.isJSON('true'); // false
* jsonc.isJSON('[1, false, null]'); // true
* jsonc.isJSON('string'); // false
* jsonc.isJSON('null'); // false
*/
static isJSON(str: string, allowComments?: boolean): boolean;
/**
* Strips comments from the given JSON string.
* @name jsonc.safe.stripComments
* @function
*
* @param {string} str - JSON string.
* @param {boolean} [whitespace=false] - Whether to replace comments
* with whitespace instead of stripping them entirely.
*
* @returns {Array} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful, second
* item will be the result: `[Error, string]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const [err, str] = jsonc.stripComments('// comments\n{"key":"value"}');
* if (!err) console.log(str); // '\n{"key":"value"}'
*/
static stripComments(str: string, whitespace?: boolean): [Error, undefined] | [null, string];
/**
* Safe version of `jsonc.uglify()`. Uglifies the given JSON string.
* @name jsonc.safe.uglify
* @function
*
* @param {string} str - JSON string to be uglified.
*
* @returns {Array} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful, second
* item will be the result: `[Error, string]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const pretty = `
* {
* // comments...
* "key": "value"
* }
* `;
* const [err, ugly] = jsonc.uglify(pretty);
* if (!err) console.log(ugly); // '{"key":"value"}'
*/
static uglify(str: string): [Error, undefined] | [null, string];
/**
* Safe version of `jsonc.beautify()`. Beautifies the given JSON
* string. Note that this will remove comments, if any.
* @name jsonc.safe.beautify
* @function
*
* @param {string} str - JSON string to be beautified.
* @param {string|number} [space=2] Specifies the indentation of nested
* structures. If it is omitted, the text will be packed without extra
* whitespace. If it is a number, it will specify the number of spaces
* to indent at each level. If it is a string (such as "\t" or
* " "), it contains the characters used to indent at each level.
*
* @returns {Array} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful, second
* item will be the result: `[Error, string]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const ugly = '{"key":"value"}';
* const [err, pretty] = jsonc.beautify(ugly);
* if (!err) console.log(pretty);
* // {
* // "key": "value"
* // }
*/
static beautify(str: string, space?: string | number): [Error, undefined] | [null, string];
/**
* Safe version of `jsonc.normalize()`. Normalizes the given value by
* stringifying and parsing it back to a Javascript object.
* @name jsonc.safe.normalize
* @function
*
* @param {any} value
* @param {Replacer} [replacer] - Determines how object values are
* normalized for objects. It can be a function or an array of strings.
*
* @returns {Array} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful, second
* item will be the result: `[Error, any]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const c = new SomeClass();
* console.log(c.constructor.name); // "SomeClass"
* const [err, normalized] = jsonc.normalize(c);
* if (err) {
* console.log('Failed to normalize: ' + err.message);
* } else {
* console.log(normalized.constructor.name); // "Object"
* }
*/
static normalize(value: any, replacer?: Replacer): [Error, undefined] | [null, any];
/**
* Safe version of `jsonc.read()`. Asynchronously reads a JSON file,
* strips comments and UTF-8 BOM and parses the JSON content.
* @name jsonc.safe.read
* @function
*
* @param {string} filePath - Path to JSON file.
* @param {Function|IReadOptions} [options] - Read options.
* @param {Function} [options.reviver] - A function that can filter and
* transform the results. It receives each of the keys and values, and
* its return value is used instead of the original value. If it
* returns what it received, then the structure is not modified. If it
* returns undefined then the member is deleted.
* @param {boolean} [options.stripComments=true] - Whether to strip
* comments from the JSON string. Note that it will fail if this is
* set to `false` and the string includes comments.
*
* @returns {Promise<Array>} - Safe methods return an array with
* the first item being the `Error` instance caught. If successful,
* second item will be the result: `Promise<[Error, any]>`
*
* @example <caption>Using async/await (recommended)</caption>
* import { safe as jsonc } from 'jsonc';
* (async () => {
* const [err, obj] = await jsonc.read('path/to/file.json');
* if (err) {
* console.log('Failed to read JSON file');
* } catch (err) {
* console.log(typeof obj); // "object"
* }
* })();
*
* @example <caption>Using promises</caption>
* import { safe as jsonc } from 'jsonc';
* jsonc.read('path/to/file.json')
* .then([err, obj] => {
* if (err) {
* console.log('Failed to read JSON file');
* } else {
* console.log(typeof obj); // "object"
* }
* })
* // .catch(err => {}); // this is never invoked when safe version is used.
*/
static read(filePath: string, options?: IReadOptions): Promise<[Error, undefined] | [null, any]>;
/**
* Safe version of `jsonc.readSync()`. Synchronously reads a JSON file,
* strips UTF-8 BOM and parses the JSON content.
* @name jsonc.safe.readSync
* @function
*
* @param {string} filePath - Path to JSON file.
* @param {Function|IReadOptions} [options] - Read options.
* @param {Function} [options.reviver] - A function that can filter and
* transform the results. It receives each of the keys and values, and
* its return value is used instead of the original value. If it
* returns what it received, then the structure is not modified. If it
* returns undefined then the member is deleted.
* @param {boolean} [options.stripComments=true] - Whether to strip
* comments from the JSON string. Note that it will fail if this is
* set to `false` and the string includes comments.
*
* @returns {Array} - Safe methods return an array with
* the first item being the `Error` instance caught. If successful,
* second item will be the result: `[Error, any]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const [err, obj] = jsonc.readSync('path/to/file.json');
* if (!err) console.log(typeof obj); // "object"
*/
static readSync(filePath: string, options?: IReadOptions): [Error, undefined] | [null, any];
/**
* Safe version of `jsonc.write()`. Asynchronously writes a JSON file
* from the given JavaScript object.
* @name jsonc.safe.write
* @function
*
* @param {string} filePath - Path to JSON file to be written.
* @param {any} data - Data to be stringified into JSON.
* @param {IWriteOptions} [options] - Write options.
* @param {Replacer} [options.replacer] - Determines how object values
* are stringified for objects. It can be a function or an array of
* strings.
* @param {string|number} [options.space] - Specifies the indentation
* of nested structures. If it is omitted, the text will be packed
* without extra whitespace. If it is a number, it will specify the
* number of spaces to indent at each level. If it is a string (such as
* "\t" or " "), it contains the characters used to indent at each
* level.
* @param {number} [options.mode=438] - FileSystem permission mode to
* be used when writing the file. Default is `438` (`0666` in octal).
* @param {boolean} [options.autoPath=true] - Specifies whether to
* create path directories if they don't exist. This will throw if set
* to `false` and path does not exist.
*
* @returns {Promise<Array>} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful,
* second item will be the result: `Promise<[Error, boolean]>`
*
* @example <caption>Using async/await (recommended)</caption>
* import { safe as jsonc } from 'jsonc';
* (async () => {
* const [err, success] = await jsonc.write('path/to/file.json', data);
* if (err) {
* console.log('Failed to read JSON file');
* } else {
* console.log('Successfully wrote JSON file');
* }
* })();
*
* @example <caption>Using promises</caption>
* import { safe as jsonc } from 'jsonc';
* jsonc.write('path/to/file.json', data)
* .then([err, obj] => {
* if (err) {
* console.log('Failed to read JSON file');
* } else {
* console.log('Successfully wrote JSON file');
* }
* })
* // .catch(err => {}); // this is never invoked when safe version is used.
*/
static write(filePath: string, data: any, options?: IWriteOptions): Promise<[Error, undefined] | [null, boolean]>;
/**
* Safe version of `jsonc.writeSync()`. Synchronously writes a JSON
* file from the given JavaScript object.
* @name jsonc.safe.writeSync
* @function
*
* @param {string} filePath - Path to JSON file to be written.
* @param {any} data - Data to be stringified into JSON.
* @param {IWriteOptions} [options] - Write options.
* @param {Replacer} [options.replacer] - Determines how object values
* are stringified for objects. It can be a function or an array of
* strings.
* @param {string|number} [options.space] - Specifies the indentation
* of nested structures. If it is omitted, the text will be packed
* without extra whitespace. If it is a number, it will specify the
* number of spaces to indent at each level. If it is a string (such as
* "\t" or " "), it contains the characters used to indent at each
* level.
* @param {number} [options.mode=438] - FileSystem permission mode to
* be used when writing the file. Default is `438` (`0666` in octal).
* @param {boolean} [options.autoPath=true] - Specifies whether to
* create path directories if they don't exist. This will throw if set
* to `false` and path does not exist.
*
* @returns {Array} - Safe methods return an array with the
* first item being the `Error` instance caught. If successful, second
* item will be the result: `[Error, boolean]`
*
* @example
* import { safe as jsonc } from 'jsonc';
* const [err, obj] = jsonc.writeSync('path/to/file.json');
* if (!err) console.log(typeof obj); // "object"
*/
static writeSync(filePath: string, data: any, options?: IWriteOptions): [Error, undefined] | [null, boolean];
}
export { jsoncSafe };