Skip to content

Commit c7bd997

Browse files
sasaplus1claude
andauthored
allow empty string as field value (#609)
* test: value can be an empty string Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: allow empty string as field value Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d7bc908 commit c7bd997

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/formatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function objectToRecord(record: LtsvRecord, strict: boolean): string {
3030

3131
const value = record[label];
3232

33-
if (!value) {
34-
throw new TypeError('value must be a non-empty string');
33+
if (typeof value !== 'string') {
34+
throw new TypeError('value must be a string');
3535
}
3636

3737
if (strict && !isValidLabel(label)) {

test/formatter.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,37 @@ describe('formatter', () => {
9191
}) === 'label:\x00\x09\x0A\x0D'
9292
);
9393
});
94+
95+
it('should generate record if value is empty string', () => {
96+
assert(format({ label: '' }) === 'label:');
97+
});
98+
99+
it('should generate record if multiple values contain empty strings', () => {
100+
assert(format({ l1: '', l2: 'v2' }) === 'l1:\tl2:v2');
101+
});
102+
103+
it('should generate record from array with empty values', () => {
104+
assert(
105+
format([
106+
{ l1: '', l2: 'v2' },
107+
{ l3: 'v3', l4: '' }
108+
]) === 'l1:\tl2:v2\nl3:v3\tl4:'
109+
);
110+
});
111+
112+
it('should throw error if value is not a string', () => {
113+
/* eslint-disable @typescript-eslint/no-explicit-any */
114+
assert.throws(() => {
115+
format({ label: undefined as any });
116+
}, TypeError);
117+
assert.throws(() => {
118+
format({ label: null as any });
119+
}, TypeError);
120+
assert.throws(() => {
121+
format({ label: 123 as any });
122+
}, TypeError);
123+
/* eslint-enable @typescript-eslint/no-explicit-any */
124+
});
94125
});
95126

96127
describe('#formatStrict()', () => {
@@ -175,6 +206,10 @@ describe('formatter', () => {
175206
formatStrict({ label: '\x0D' });
176207
}, SyntaxError);
177208
});
209+
210+
it('should generate record if value is empty string in strict mode', () => {
211+
assert(formatStrict({ label: '' }) === 'label:');
212+
});
178213
});
179214

180215
describe('#stringify()', () => {

0 commit comments

Comments
 (0)