Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a8aa0d

Browse files
authored
fix: Use the lineWidth option for line breaking in flow collections (#522)
This should address issues #288 and #507.
1 parent b7696fc commit 9a8aa0d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/stringify/stringifyCollection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function stringifyFlowCollection(
148148
} else {
149149
if (!reqNewline) {
150150
const len = lines.reduce((sum, line) => sum + line.length + 2, 2)
151-
reqNewline = len > Collection.maxFlowStringSingleLineLength
151+
reqNewline = ctx.options.lineWidth > 0 && len > ctx.options.lineWidth
152152
}
153153
if (reqNewline) {
154154
str = start

tests/doc/stringify.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ describe('scalar styles', () => {
616616
y,
617617
n
618618
]\n`
619-
expect(String(doc)).toBe(str)
619+
expect(doc.toString({ lineWidth: 1 })).toBe(str)
620620
expect(YAML.parse(str)).toEqual([
621621
true,
622622
false,
@@ -868,6 +868,36 @@ describe('indentSeq: false', () => {
868868
})
869869
})
870870

871+
describe('lineWidth', () => {
872+
const doc = YAML.parseDocument(
873+
"[ 'Sed', 'ut', 'perspiciatis', 'unde', 'omnis', 'iste', 'natus', 'error', 'sit', 'voluptatem', 'accusantium', 'doloremque', 'laudantium,', 'totam' ]"
874+
)
875+
876+
test('limit to 80 with overlong flow collection', () => {
877+
expect(doc.toString({lineWidth: 80})).toBe(`[
878+
'Sed',
879+
'ut',
880+
'perspiciatis',
881+
'unde',
882+
'omnis',
883+
'iste',
884+
'natus',
885+
'error',
886+
'sit',
887+
'voluptatem',
888+
'accusantium',
889+
'doloremque',
890+
'laudantium,',
891+
'totam'
892+
]
893+
`)
894+
})
895+
896+
test('limit > flow collection length', () => {
897+
expect(doc.toString({lineWidth: 162})).toBe("[ 'Sed', 'ut', 'perspiciatis', 'unde', 'omnis', 'iste', 'natus', 'error', 'sit', 'voluptatem', 'accusantium', 'doloremque', 'laudantium,', 'totam' ]\n")
898+
})
899+
})
900+
871901
describe('collectionStyle', () => {
872902
test('collectionStyle: undefined', () => {
873903
const doc = new YAML.Document<YAML.YAMLMap, false>({ foo: ['bar'] })

0 commit comments

Comments
 (0)