Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/stringifyStructRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,18 @@ describe('longform serialize (in xlsx mode)', () => {
table: 'Table2',
columns: [ 'col1' ],
sections: [ 'this row' ]
}, { thisRow: true })).toBe('Table2[[#This row],[col1]]');
}, { thisRow: true })).toBe('Table2[[#This Row],[col1]]');

expect(stringifyStructRef({
table: 'Table2',
columns: [ 'col1' ],
sections: [ 'this row' ]
}, { thisRow: true })).toBe('Table2[[#This row],[col1]]');
}, { thisRow: true })).toBe('Table2[[#This Row],[col1]]');

// Keyword-only this-row (no column) also title-cases both words.
expect(stringifyStructRef({
table: 'Table2',
sections: [ 'this row' ]
}, { thisRow: true })).toBe('Table2[#This Row]');
});
});
10 changes: 6 additions & 4 deletions lib/stringifyStructRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ function needsBraces (str: string): boolean {
return /[^a-zA-Z0-9\u00a1-\uffff]/.test(str);
}

function toSentenceCase (str: string): string {
return str[0].toUpperCase() + str.slice(1).toLowerCase();
function toTitleCase (str: string): string {
// Section specifiers are title-cased per word, so the two-word `this row`
// serializes as `This Row` (Excel's canonical form), not `This row`.
return str.toLowerCase().replace(/\b\w/g, c => c.toUpperCase());
}

export function stringifySRef (refObject: ReferenceStruct, thisRow = false) {
Expand All @@ -22,7 +24,7 @@ export function stringifySRef (refObject: ReferenceStruct, thisRow = false) {
const numSections = refObject.sections?.length ?? 0;
// single section
if (numSections === 1 && !numColumns) {
s += `[#${toSentenceCase(refObject.sections[0])}]`;
s += `[#${toTitleCase(refObject.sections[0])}]`;
}
// single column
else if (!numSections && numColumns === 1) {
Expand All @@ -37,7 +39,7 @@ export function stringifySRef (refObject: ReferenceStruct, thisRow = false) {
}
else if (numSections) {
s += refObject.sections
.map(d => `[#${toSentenceCase(d)}]`)
.map(d => `[#${toTitleCase(d)}]`)
.join(',');
if (numColumns) {
s += ',';
Expand Down