fix: initial (-) added to YAML output

This commit is contained in:
Chesterkxng
2025-04-06 01:47:10 +02:00
parent f6fd1d9a04
commit cdda783e47
2 changed files with 13 additions and 10 deletions

View File

@@ -15,32 +15,34 @@ describe('main', () => {
it('should return empty string for empty input', () => { it('should return empty string for empty input', () => {
const result = main('', defaultOptions); const result = main('', defaultOptions);
expect(result).toBe(''); expect(result).toEqual('');
}); });
it('should return this if header is set to false', () => { it('should return this if header is set to false', () => {
const options = { ...defaultOptions, headerRow: false }; const options = { ...defaultOptions, headerRow: false };
const result = main('John,30\nEmma,50', options); const result = main('John,30\nEmma,50', options);
expect(result).toBe(' - John\n - 30\n-\n - Emma\n - 50'); expect(result).toEqual('-\n - John\n - 30\n-\n - Emma\n - 50');
}); });
it('should return this header is set to true', () => { it('should return this header is set to true', () => {
const options = { ...defaultOptions }; const options = { ...defaultOptions };
const result = main('Name,Age\nJohn,30\nEmma,50', options); const result = main('Name,Age\nJohn,30\nEmma,50', options);
expect(result).toBe(' Name: John\n Age: 30\n-\n Name: Emma\n Age: 50'); expect(result).toEqual(
'-\n Name: John\n Age: 30\n-\n Name: Emma\n Age: 50'
);
}); });
it('should return this header is set to true and comment flag set', () => { it('should return this header is set to true and comment flag set', () => {
const options = { ...defaultOptions, commentcharacter: '#' }; const options = { ...defaultOptions, commentcharacter: '#' };
const result = main('Name,Age\nJohn,30\n#Emma,50', options); const result = main('Name,Age\nJohn,30\n#Emma,50', options);
expect(result).toBe(' Name: John\n Age: 30'); expect(result).toEqual('-\n Name: John\n Age: 30');
}); });
it('should return this header is set to true and spaces is set to 3', () => { it('should return this header is set to true and spaces is set to 3', () => {
const options = { ...defaultOptions, spaces: 3 }; const options = { ...defaultOptions, spaces: 3 };
const result = main('Name,Age\nJohn,30\nEmma,50', options); const result = main('Name,Age\nJohn,30\nEmma,50', options);
expect(result).toBe( expect(result).toEqual(
' Name: John\n Age: 30\n-\n Name: Emma\n Age: 50' '-\n Name: John\n Age: 30\n-\n Name: Emma\n Age: 50'
); );
}); });
}); });

View File

@@ -19,18 +19,19 @@ function toYaml(
const lines = Object.entries(obj) const lines = Object.entries(obj)
.map(([key, value]) => `${indent}${key}: ${value}`) .map(([key, value]) => `${indent}${key}: ${value}`)
.join('\n'); .join('\n');
return `${lines}`; return `-\n${lines}`;
}) })
.join('\n-\n'); .join('\n');
} }
// If input is string[][]. // If input is string[][].
if (Array.isArray(input) && Array.isArray(input[0])) { if (Array.isArray(input) && Array.isArray(input[0])) {
return (input as string[][]) return (input as string[][])
.map((row) => { .map((row) => {
return row.map((cell) => `${indent}- ${cell}`).join('\n'); const inner = row.map((cell) => `${indent}- ${cell}`).join('\n');
return `-\n${inner}`;
}) })
.join('\n-\n'); .join('\n');
} }
return 'invalid input'; return 'invalid input';