Skip to content

Commit 0b8c879

Browse files
committed
other minor bugs
1 parent 4975a66 commit 0b8c879

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

src/CsvEditorProvider.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,13 @@ class CsvEditorController {
841841
}
842842

843843
private isDate(value: string): boolean {
844-
return !isNaN(Date.parse(value));
844+
if (!value) return false;
845+
const v = value.trim();
846+
// Strictly match ISO-like date formats to avoid misclassifying plain numbers as dates.
847+
const isoDate = /^\d{4}-\d{2}-\d{2}(?:[ T]\d{2}:\d{2}(?::\d{2})?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
848+
const isoSlash = /^\d{4}\/\d{2}\/\d{2}$/;
849+
if (!(isoDate.test(v) || isoSlash.test(v))) return false;
850+
return !isNaN(Date.parse(v));
845851
}
846852

847853
private isBooleanish(value: string): boolean {
@@ -1004,4 +1010,28 @@ export class CsvEditorProvider implements vscode.CustomTextEditorProvider {
10041010
if (!sep || sep.length === 0) { delete map[uri.toString()]; } else { map[uri.toString()] = sep; }
10051011
await context.workspaceState.update(CsvEditorProvider.sepKey, map);
10061012
}
1013+
1014+
// Test helpers to access internal utilities without VS Code runtime
1015+
public static __test = {
1016+
computeColumnWidths(data: string[][]): number[] {
1017+
const c: any = new (CsvEditorController as any)({} as any);
1018+
return c.computeColumnWidths(data);
1019+
},
1020+
isDate(v: string): boolean {
1021+
const c: any = new (CsvEditorController as any)({} as any);
1022+
return c.isDate(v);
1023+
},
1024+
estimateColumnDataType(col: string[]): string {
1025+
const c: any = new (CsvEditorController as any)({} as any);
1026+
return c.estimateColumnDataType(col);
1027+
},
1028+
getColumnColor(t: string, dark: boolean, i: number): string {
1029+
const c: any = new (CsvEditorController as any)({} as any);
1030+
return c.getColumnColor(t, dark, i);
1031+
},
1032+
hslToHex(h: number, s: number, l: number): string {
1033+
const c: any = new (CsvEditorController as any)({} as any);
1034+
return c.hslToHex(h, s, l);
1035+
}
1036+
};
10071037
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ meta,Title,Animal Shenanigans CSV,,,,,,,,,
22
meta,Author,The CSV Menagerie,,,,,,,,,
33
meta,About,Fictional animals with quirky traits,,,,,,,,,
44
AnimalName,SidekickName,IsMischievous,IsNocturnal,BirthdateISO,LastVetVisit,FavoriteNumber,StepsToday,SnackBudget,AgilityScore,OptionalNote,SpareColumn
5-
Riley the Fox,Buddy Morgan,false,FALSE,2011-02-02,2024-08-12,13,1003,1.01,63.17,,
6-
Sam the Bear,Buddy Casey,true,FALSE,2012-03-03,2024-03-23,26,1006,2.02,76.34,,
7-
Morgan the Lynx,Buddy Reese,false,TRUE,2013-04-04,2024-10-06,39,1009,3.03,89.51,,
5+
Riley the Fox,Buddy Morgan,false,FALSE,2011-02-02,2024-08-12,13,1003,,63.17,,
6+
Sam the Bear,,true,FALSE,2012-03-03,2024-03-23,26,1006,2.02,76.34,,
7+
Morgan the Lynx,Buddy Reese,false,TRUE,2013-04-04,,39,1009,3.03,89.51,,
88
Jordan the Raccoon,Buddy Cameron,true,FALSE,2014-05-05,2024-05-17,52,1012,4.04,52.68,,
9-
Taylor the Panda,Buddy Sage,false,FALSE,2015-06-06,2024-12-28,65,1015,5.05,65.85,,
9+
Taylor the Panda,Buddy Sage,false,,2015-06-06,2024-12-28,65,1015,5.05,65.85,,
1010
Casey the Ferret,Buddy Remy,true,TRUE,2016-07-07,2024-07-11,78,1018,6.06,78.02,,
1111
Jamie the Capybara,Buddy Riley,false,FALSE,2017-08-08,2024-02-22,91,1021,7.07,91.19,,
12-
Avery the Wombat,Buddy Jordan,true,FALSE,2018-09-09,2024-09-05,104,1024,8.08,54.36,,
12+
Avery the Wombat,Buddy Jordan,true,FALSE,,2024-09-05,104,,8.08,54.36,,
1313
Reese the Badger,Buddy Jamie,false,TRUE,2019-10-10,2024-04-16,117,1027,9.09,67.53,,
14-
Quinn the Koala,Buddy Quinn,true,FALSE,2010-11-11,2024-11-27,130,1030,10.10,80.70,,
14+
Quinn the Koala,Buddy Quinn,true,FALSE,2010-11-11,2024-11-27,130,,10.10,80.70,,
1515
Skyler the Red Panda,Buddy Harper,false,FALSE,2011-12-12,2024-06-10,143,1033,11.11,93.87,,
16-
Cameron the Kiwi,Buddy Parker,true,TRUE,2012-01-13,2024-01-21,156,1036,12.12,56.04,,
16+
Cameron the Kiwi,Buddy Parker,true,TRUE,2012-01-13,2024-01-21,,1036,12.12,56.04,,
1717
Harper the Yak,Buddy Elliot,false,FALSE,2013-02-14,2024-08-04,169,1039,13.13,69.21,,
1818
Rowan the Bison,Buddy Sam,true,FALSE,2014-03-15,2024-03-15,182,1042,14.14,82.38,,
1919
Sage the Marten,Buddy Taylor,false,TRUE,2015-04-16,2024-10-26,195,1045,15.15,95.55,,

src/test/provider-utils.test.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,51 @@ Module.prototype.require = function (id: string) {
1515

1616
import { CsvEditorProvider } from '../CsvEditorProvider';
1717

18-
// Helper to access private methods via type casting
19-
function getPrivate<T>(obj: any, name: string): T {
20-
return obj[name] as T;
21-
}
22-
2318
describe('CsvEditorProvider utility methods', () => {
24-
const provider = new CsvEditorProvider({} as any);
2519

2620
it('computeColumnWidths returns max length per column', () => {
2721
const data = [
2822
['a', 'bb', 'ccc'],
2923
['dddd', 'ee', 'f']
3024
];
31-
const compute = getPrivate<(d: string[][]) => number[]>(provider, 'computeColumnWidths').bind(provider);
32-
const widths = compute(data);
25+
const widths = CsvEditorProvider.__test.computeColumnWidths(data);
3326
assert.deepStrictEqual(widths, [4, 2, 3]);
3427
});
3528

3629
it('isDate correctly identifies date strings', () => {
37-
const isDate = getPrivate<(v: string) => boolean>(provider, 'isDate').bind(provider);
30+
const isDate = CsvEditorProvider.__test.isDate;
3831
assert.strictEqual(isDate('2024-01-02'), true);
3932
assert.strictEqual(isDate('not-a-date'), false);
33+
assert.strictEqual(isDate('1003'), false);
34+
assert.strictEqual(isDate('2024'), false);
35+
assert.strictEqual(isDate('2024/01/02'), true);
4036
});
4137

4238
it('estimateColumnDataType detects common types', () => {
43-
const estimate = getPrivate<(c: string[]) => string>(provider, 'estimateColumnDataType').bind(provider);
39+
const estimate = CsvEditorProvider.__test.estimateColumnDataType;
4440
assert.strictEqual(estimate(['true', 'FALSE']), 'boolean');
4541
assert.strictEqual(estimate(['1', '0', '0', '1']), 'boolean');
4642
assert.strictEqual(estimate(['t', 'F', 'T', 'f']), 'boolean');
4743
assert.strictEqual(estimate(['yes', 'No', 'Y', 'n']), 'boolean');
4844
assert.strictEqual(estimate(['on', 'OFF']), 'boolean');
4945
assert.strictEqual(estimate(['2020-01-01', '1999-12-31']), 'date');
5046
assert.strictEqual(estimate(['0x1', '0x2']), 'integer');
47+
assert.strictEqual(estimate(['1003', '42', '0']), 'integer');
5148
assert.strictEqual(estimate(['1.2e0', '3.4e0']), 'float');
5249
assert.strictEqual(estimate(['', '']), 'empty');
5350
assert.strictEqual(estimate(['hello', '1a']), 'string');
5451
});
5552

5653
it('getColumnColor returns hex colors', () => {
57-
const getColor = getPrivate<(t: string, dark: boolean, i: number) => string>(provider, 'getColumnColor').bind(provider);
54+
const getColor = CsvEditorProvider.__test.getColumnColor;
5855
assert.strictEqual(getColor('empty', true, 0), '#BBB');
5956
assert.strictEqual(getColor('empty', false, 0), '#444');
6057
const hex = getColor('boolean', true, 2);
6158
assert.match(hex, /^#[0-9a-fA-F]{6}$/);
6259
});
6360

6461
it('hslToHex converts known colors', () => {
65-
const hslToHex = getPrivate<(h:number,s:number,l:number)=>string>(provider, 'hslToHex').bind(provider);
62+
const hslToHex = CsvEditorProvider.__test.hslToHex;
6663
assert.strictEqual(hslToHex(0, 100, 50), '#ff0000'); // red
6764
assert.strictEqual(hslToHex(120, 100, 50), '#00ff00'); // green
6865
assert.strictEqual(hslToHex(240, 100, 50), '#0000ff'); // blue

0 commit comments

Comments
 (0)