Skip to content

Commit 29732f3

Browse files
authoredNov 1, 2022
feat: Add flowCollectionPadding toString option (#420)
1 parent 8e7e57f commit 29732f3

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed
 

‎docs/03_options.md

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ Used by: `stringify()` and `doc.toString()`
157157
| doubleQuotedAsJSON | `boolean` | `false` | Restrict double-quoted strings to use JSON-compatible syntax. |
158158
| doubleQuotedMinMultiLineLength | `number` | `40` | Minimum length for double-quoted strings to use multiple lines to represent the value. |
159159
| falseStr | `string` | `'false'` | String representation for `false` values. |
160+
| flowCollectionPadding | `boolean` | `true` | When true, a single space of padding will be added inside the delimiters of non-empty single-line flow collections. |
160161
| indent | `number` | `2` | The number of spaces to use when indenting code. Should be a strictly positive integer. |
161162
| indentSeq | `boolean` | `true` | Whether block sequences should be indented. |
162163
| lineWidth | `number` | `80` | Maximum line width (set to `0` to disable folding). This is a soft limit, as only double-quoted semantics allow for inserting a line break in the middle of a word. |

‎src/options.ts

+8
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ export type ToStringOptions = {
300300
*/
301301
falseStr?: string
302302

303+
/**
304+
* When true, a single space of padding will be added inside the delimiters
305+
* of non-empty single-line flow collections.
306+
*
307+
* Default: `true`
308+
*/
309+
flowCollectionPadding?: boolean
310+
303311
/**
304312
* The number of spaces to use when indenting code.
305313
*

‎src/stringify/stringify.ts

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type StringifyContext = {
2727
indentAtStart?: number
2828
inFlow: boolean | null
2929
inStringifyKey?: boolean
30+
flowCollectionPadding: string
3031
options: Readonly<
3132
Required<Omit<ToStringOptions, 'collectionStyle' | 'indent'>>
3233
>
@@ -47,6 +48,7 @@ export function createStringifyContext(
4748
doubleQuotedAsJSON: false,
4849
doubleQuotedMinMultiLineLength: 40,
4950
falseStr: 'false',
51+
flowCollectionPadding: true,
5052
indentSeq: true,
5153
lineWidth: 80,
5254
minContentWidth: 20,
@@ -75,6 +77,7 @@ export function createStringifyContext(
7577
return {
7678
anchors: new Set(),
7779
doc,
80+
flowCollectionPadding: opt.flowCollectionPadding ? ' ' : '',
7881
indent: '',
7982
indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
8083
inFlow,

‎src/stringify/stringifyCollection.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function stringifyFlowCollection(
9494
const {
9595
indent,
9696
indentStep,
97+
flowCollectionPadding,
9798
options: { commentString }
9899
} = ctx
99100
itemIndent += indentStep
@@ -155,7 +156,7 @@ function stringifyFlowCollection(
155156
str += line ? `\n${indentStep}${indent}${line}` : '\n'
156157
str += `\n${indent}${end}`
157158
} else {
158-
str = `${start} ${lines.join(' ')} ${end}`
159+
str = `${start}${flowCollectionPadding}${lines.join(' ')}${flowCollectionPadding}${end}`
159160
}
160161
}
161162

‎tests/doc/stringify.js

+14
Original file line numberDiff line numberDiff line change
@@ -1180,3 +1180,17 @@ describe('YAML.stringify on ast Document', () => {
11801180
expect(YAML.stringify(doc)).toBe('null\n')
11811181
})
11821182
})
1183+
1184+
describe('flow collection padding', () => {
1185+
const doc = new YAML.Document();
1186+
doc.contents = new YAML.YAMLSeq();
1187+
doc.contents.items = [1, 2];
1188+
doc.contents.flow = true;
1189+
1190+
test('default', () => {
1191+
expect(doc.toString()).toBe('[ 1, 2 ]\n')
1192+
});
1193+
test('default', () => {
1194+
expect(doc.toString({flowCollectionPadding: false})).toBe('[1, 2]\n')
1195+
});
1196+
})

0 commit comments

Comments
 (0)
Please sign in to comment.