Skip to content

Commit 4c25739

Browse files
committed
feat: Alternatively accept number of spaces for the indent parameter
1 parent 80964f7 commit 4c25739

File tree

8 files changed

+41
-32
lines changed

8 files changed

+41
-32
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ By default, `jsonlint` will either report a syntax error with details or pretty-
8484
-E, --extensions [ext] file extensions to process for directory walk
8585
(default: ["json","JSON"])
8686
-i, --in-place overwrite the input files
87-
-t, --indent [char] characters to use for indentation
88-
(default: " ")
87+
-t, --indent [num|char] number of spaces or specific characters
88+
to use for indentation (default: 2)
8989
-c, --compact compact error display
9090
-M, --mode [mode] set other parsing flags according to a format
9191
type (default: "json")
@@ -190,7 +190,7 @@ You can parse a JSON string to an array of tokens and print it back to a string
190190
const { tokenize } = require('@prantlf/jsonlint')
191191
const tokens = tokenize('string with JSON data', { rawTokens: true })
192192
const { print } = require('@prantlf/jsonlint/lib/printer')
193-
const output = print(tokens, { indent: ' ' })
193+
const output = print(tokens, { indent: 2 })
194194
```
195195

196196
The [`tokenize`](#tokenizing) method accepts options in the second optional parameter. See the [`tokenize`](#tokenizing) method above for more information.
@@ -199,7 +199,7 @@ The [`print`](#pretty-printing) method accepts an object `options` as the second
199199

200200
| Option | Description |
201201
| --------------------------- | ------------------------------------------------------- |
202-
| `indent` | whitespace characters to be used as an indentation unit |
202+
| `indent` | count of spaces or the specific characters to be used as an indentation unit |
203203
| `pruneComments` | will omit all tokens with comments |
204204
| `stripObjectKeys` | will not print quotes around object keys which are JavaScript identifier names |
205205

@@ -213,13 +213,13 @@ print(tokens, {})
213213
// (Just introduce line breaks.)
214214
print(tokens, { indent: '' })
215215
// Print to multiple lines with object and array indentation. (Just like
216-
//`JSON.stringify(json, undefined, ' ')` would do it, but retaining comments.)
217-
print(tokens, { indent: ' ' })
216+
//`JSON.stringify(json, undefined, 2)` would do it, but retaining comments.)
217+
print(tokens, { indent: 2 })
218218
// Print to multiple lines with object and array indentation, omit comments.
219219
// (Just like `JSON.stringify(json, undefined, ' ')` would do it.)
220220
print(tokens, { indent: ' ', pruneComments: true })
221221
// Print to multiple lines with indentation enabled and JSON5 object keys.
222-
print(tokens, { indent: ' ', stripObjectKeys: true })
222+
print(tokens, { indent: '\t', stripObjectKeys: true })
223223
```
224224

225225
### Tokenizing
@@ -305,8 +305,9 @@ ${reason}`)
305305

306306
Copyright (C) 2012-2019 Zachary Carter, Ferdinand Prantl
307307

308-
Licensed under the MIT license.
308+
Licensed under the [MIT License].
309309

310+
[MIT License]: http://en.wikipedia.org/wiki/MIT_License
310311
[pure JavaScript version]: http://prantlf.github.com/jsonlint/
311312
[jsonlint.com]: http://jsonlint.com
312313
[JSON]: https://tools.ietf.org/html/rfc8259

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var options = require('commander')
2020
.option('-s, --sort-keys', 'sort object keys (not when prettifying)')
2121
.option('-E, --extensions [ext]', 'file extensions to process for directory walk', collectExtensions, ['json', 'JSON'])
2222
.option('-i, --in-place', 'overwrite the input files')
23-
.option('-t, --indent [char]', 'characters to use for indentation', ' ')
23+
.option('-t, --indent [num|char]', 'number of spaces or specific characters to use for indentation', 2)
2424
.option('-c, --compact', 'compact error display')
2525
.option('-M, --mode [mode]', 'set other parsing flags according to a format type', 'json')
2626
.option('-C, --comments', 'recognize and ignore JavaScript-style comments')

lib/formatter.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,70 +20,72 @@
2020
return new Array(count + 1).join(s)
2121
}
2222

23-
function format (json, indentChars) {
23+
function format (json, indent) {
2424
var i = 0
25-
var il = 0
26-
var tab = indentChars !== undefined ? indentChars : ' '
27-
var newJson = ''
25+
var length = 0
26+
var indentString = indent !== undefined
27+
? typeof indent === 'number'
28+
? ' '.repeat(indent) : indent : ' '
29+
var outputString = ''
2830
var indentLevel = 0
29-
var inString = false
30-
var currentChar = null
31+
var inString
32+
var currentChar
3133

32-
for (i = 0, il = json.length; i < il; i += 1) {
34+
for (i = 0, length = json.length; i < length; i += 1) {
3335
currentChar = json.charAt(i)
3436
switch (currentChar) {
3537
case '{':
3638
case '[':
3739
if (!inString) {
38-
newJson += currentChar + '\n' + repeat(tab, indentLevel + 1)
40+
outputString += currentChar + '\n' + repeat(indentString, indentLevel + 1)
3941
indentLevel += 1
4042
} else {
41-
newJson += currentChar
43+
outputString += currentChar
4244
}
4345
break
4446
case '}':
4547
case ']':
4648
if (!inString) {
4749
indentLevel -= 1
48-
newJson += '\n' + repeat(tab, indentLevel) + currentChar
50+
outputString += '\n' + repeat(indentString, indentLevel) + currentChar
4951
} else {
50-
newJson += currentChar
52+
outputString += currentChar
5153
}
5254
break
5355
case ',':
5456
if (!inString) {
55-
newJson += ',\n' + repeat(tab, indentLevel)
57+
outputString += ',\n' + repeat(indentString, indentLevel)
5658
} else {
57-
newJson += currentChar
59+
outputString += currentChar
5860
}
5961
break
6062
case ':':
6163
if (!inString) {
62-
newJson += ': '
64+
outputString += ': '
6365
} else {
64-
newJson += currentChar
66+
outputString += currentChar
6567
}
6668
break
6769
case ' ':
6870
case '\n':
6971
case '\t':
7072
if (inString) {
71-
newJson += currentChar
73+
outputString += currentChar
7274
}
7375
break
7476
case '"':
7577
if (i > 0 && json.charAt(i - 1) !== '\\') {
7678
inString = !inString
7779
}
78-
newJson += currentChar
80+
outputString += currentChar
7981
break
8082
default:
81-
newJson += currentChar
83+
outputString += currentChar
8284
break
8385
}
8486
}
8587

86-
return newJson
88+
return outputString
8789
}
8890

8991
exports.format = format

lib/jsonlint.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ declare module '@prantlf/jsonlint/lib/validator' {
8787

8888
declare module '@prantlf/jsonlint/lib/printer' {
8989
interface PrintOptions {
90-
indent?: string
90+
indent?: number | string
9191
pruneComments?: boolean
9292
stripObjectKeys?: boolean
9393
}
@@ -100,7 +100,7 @@ declare module '@prantlf/jsonlint/lib/printer' {
100100
* import { tokenize } from '@prantlf/jsonlint'
101101
* import { print } from '@prantlf/jsonlint/lib/printer'
102102
* const tokens = tokenize('string with JSON data', { rawTokens: true })
103-
* const outputString = print(tokens, { indent: ' ' })
103+
* const outputString = print(tokens, { indent: 2 })
104104
* ```
105105
*
106106
* @param tokens - an array of JSON tokens

lib/printer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
}
4141

4242
var indentString = options.indent
43+
if (typeof indentString === 'number') {
44+
indentString = ' '.repeat(indentString)
45+
}
4346
// Setting the indent to an empty string enables pretty-printing too.
4447
// It will just insert line breaks without any indentation.
4548
var prettyPrint = indentString !== undefined

test/print.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ addTest('introduce line breaks', function () {
5353
})
5454

5555
addTest('apply indent', function () {
56-
var output = print(tokens, { indent: ' ' })
56+
var output = print(tokens, { indent: 2 })
5757
assert.equal(output, '/* start */\n{\n "a": 1, // c\n "0b": [\n 2,\n 3\n ]\n}')
5858
})
5959

test/typings.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ addTest('print', () => {
6161
const result = print(tokens)
6262
assert.equal(result, '{}')
6363
print(tokens, {})
64+
print(tokens, { indent: 2 })
6465
print(tokens, { indent: ' ' })
66+
print(tokens, { pruneComments: true })
67+
print(tokens, { stripObjectKeys: true })
6568
assert.ok(true)
6669
})
6770

web/jsonlint.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ <h2>Result</h2>
280280
var tokens = jsonlint.tokenize(source, parserOptions)
281281
// TODO: Support sorting tor the tokenized input too.
282282
return jsonlintPrinter.print(tokens, {
283-
indent: ' ',
283+
indent: 2,
284284
pruneComments: document.getElementById('prune-comments').checked,
285285
stripObjectKeys: document.getElementById('strip-object-keys').checked
286286
})

0 commit comments

Comments
 (0)