Skip to content

Commit 0175888

Browse files
committed
Parse years and months as string fields
Closes #10
1 parent 902a1a8 commit 0175888

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/DataSource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
133133
* @param values - The field values.
134134
* @returns the detected field type and potentially converted values.
135135
*/
136-
const detectFieldType = (values: any[]): [FieldType, any[]] => {
136+
export const detectFieldType = (values: any[]): [FieldType, any[]] => {
137137
// If all values are valid ISO 8601, the assume that it's a time field.
138-
const isValidISO = values.every(value => isValid(parseISO(value)));
138+
const isValidISO = values.every(value => value.length >= 10 && isValid(parseISO(value)));
139139
if (isValidISO) {
140140
return [FieldType.time, values.map(_ => parseISO(_).valueOf())];
141141
}

src/detectFieldType.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { detectFieldType } from './DataSource';
2+
3+
test('years and months gets parsed as string to reduce false positives', () => {
4+
expect(detectFieldType(['2005', '2006'])).toEqual(['string', ['2005', '2006']]);
5+
expect(detectFieldType(['2005-01', '2006-01'])).toEqual(['string', ['2005-01', '2006-01']]);
6+
});
7+
8+
test('iso8601 gets parsed as time', () => {
9+
expect(detectFieldType(['2005-01-02', '2006-01-02'])).toEqual(['time', [1104620400000, 1136156400000]]);
10+
expect(detectFieldType(['2006-01-02T15:06:13Z', '2006-01-02T15:07:13Z'])).toEqual([
11+
'time',
12+
[1136214373000, 1136214433000],
13+
]);
14+
});

0 commit comments

Comments
 (0)