Skip to content

Commit

Permalink
Add new option foldChars (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli authored Nov 20, 2024
1 parent 29986df commit bd344fc
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 62 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const defaultOptions = {
latin1: true, // default encoding for .properties files
lineWidth: 80, // use null to disable
newline: '\n', // Windows uses \r\n
pathSep: '.' // if non-default, use the same in parse()
pathSep: '.', // if non-default, use the same in parse()
foldChars: '\f\t .' // preferred characters for line folding
}
```

Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ interface StringifyOptions {
lineWidth?: 80 | number, // use null to disable
newline?: '\n' | string, // Windows uses \r\n
pathSep?: '.' | string // if non-default, use the same in parse()
foldChars?: '\f\t .' | string
}

/**
Expand Down
37 changes: 26 additions & 11 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const escapeValue = value => escape(value).replace(/^ /, '\\ ')
const prefixComment = (str, prefix) =>
str.replace(/^\s*([#!][ \t\f]*)?/g, prefix)

function fold({ indent, latin1, lineWidth, newline }, key, value) {
function fold({ indent, latin1, lineWidth, newline, foldChars }, key, value) {
const printKey = escapeNonPrintable(key, latin1)
const printValue = escapeNonPrintable(value, latin1)
let line = printKey + printValue
Expand Down Expand Up @@ -52,18 +52,20 @@ function fold({ indent, latin1, lineWidth, newline }, key, value) {
end = i + 1
break
case ' ':
case '=':
case ':':
if (foldChars.includes(line[i])) split = i + 1
break
case 'f':
if (foldChars.includes('\f')) split = i + 1
break
case 't':
split = i + 1
if (foldChars.includes('\t')) split = i + 1
break
}
break
case '\f':
case '\t':
case ' ':
case '.':
split = i + 1
break
default:
if (foldChars.includes(ch)) split = i + 1
}
}
if (end) {
Expand Down Expand Up @@ -114,13 +116,26 @@ function stringify(
latin1 = true,
lineWidth = 80,
newline = '\n',
pathSep = '.'
pathSep = '.',
foldChars = '\f\t .'
} = {}
) {
if (!input) return ''
if (!Array.isArray(input)) input = toLines(input, pathSep, defaultKey)
const lineOpt = { indent, latin1, lineWidth, newline: '\\' + newline }
const commentOpt = { indent: commentPrefix, latin1, lineWidth, newline }
const lineOpt = {
indent,
latin1,
lineWidth,
newline: '\\' + newline,
foldChars
}
const commentOpt = {
indent: commentPrefix,
latin1,
lineWidth,
newline,
foldChars
}
return input
.map(line => {
switch (true) {
Expand Down
136 changes: 136 additions & 0 deletions tests/folding.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const { stringify } = require('../lib/index')

describe('Folding', () => {
test('long value starts on new line', () => {
const lines = [
['k0', '0 val0'],
['k1', '1 value1'],
['key', 'v0'],
['key1', 'value1'],
['key2', 'value2 continues']
]
expect(stringify(lines, { indent: ' ', lineWidth: 8 })).toBe(
`\
k0 = \\
0 val0
k1 = \\
1 \\
value1
key = v0
key1 = \\
value1
key2 = \\
value2\\
\\ \\
contin\\
ues`
)
})

test('values after long keys do not start on new line', () => {
const lines = [
['longish key', 'v0'],
['longkeyxx', '42'],
['somelongkey', 'withlongvalue']
]
expect(stringify(lines, { indent: ' ', lineWidth: 8 })).toBe(
`\
longish\\ \\
key = \\
v0
longkeyx\\
x = 42
somelong\\
key = \\
withlo\\
ngvalu\\
e`
)
})

describe('foldChars', () => {
const lines = [
['key0', '12 345678'],
['key1', '12.345678'],
['key2', '12\t345678'],
['key3', '12\f345678'],
['key4', ' 12 345678'],
['key5', '12,345678']
]
test('default', () => {
expect(stringify(lines, { indent: ' ', lineWidth: 8 })).toBe(
`\
key0 = \\
12 \\
345678
key1 = \\
12.\\
345678
key2 = \\
12\\t\\
345678
key3 = \\
12\\f\\
345678
key4 = \\
\\ 12 \\
345678
key5 = \\
12,345\\
678`
)
})

test('empty', () => {
expect(
stringify(lines, { indent: ' ', lineWidth: 8, foldChars: '' })
).toBe(
`\
key0 = \\
12 345\\
678
key1 = \\
12.345\\
678
key2 = \\
12\\t34\\
5678
key3 = \\
12\\f34\\
5678
key4 = \\
\\ 12 3\\
45678
key5 = \\
12,345\\
678`
)
})

test('custom', () => {
expect(
stringify(lines, { indent: ' ', lineWidth: 8, foldChars: ',' })
).toBe(
`\
key0 = \\
12 345\\
678
key1 = \\
12.345\\
678
key2 = \\
12\\t34\\
5678
key3 = \\
12\\f34\\
5678
key4 = \\
\\ 12 3\\
45678
key5 = \\
12,\\
345678`
)
})
})
})
50 changes: 0 additions & 50 deletions tests/long-folds.tests.js

This file was deleted.

0 comments on commit bd344fc

Please sign in to comment.