@@ -28,10 +28,13 @@ export const findErrorData = (el: HTMLElement): ErrorData | null => {
28
28
const errors : number [ ] = [ ] ;
29
29
30
30
td . forEach ( ( td , i ) => {
31
- let cellType = getCellType ( td . textContent , true ) ;
32
- if ( cellType === CELL_TYPE . ERROR ) {
31
+ const cellType = td . textContent ;
32
+ if (
33
+ cellType !== CELL_TYPE . TEXT &&
34
+ cellType != CELL_TYPE . NUMBER &&
35
+ cellType !== CELL_TYPE . TAG
36
+ )
33
37
errors . push ( i ) ;
34
- }
35
38
} ) ;
36
39
37
40
if ( errors . length === 0 ) {
@@ -76,17 +79,11 @@ export const findAppData = (
76
79
let cellType = "" ;
77
80
//Set header type based off of the first row's specified cell type
78
81
if ( i === 1 ) {
79
- cellType = getCellType ( td . textContent , true ) ;
82
+ cellType = td . textContent ;
80
83
headers [ j ] . type = cellType ;
81
84
return ;
82
85
} else {
83
- //If empty then just set it to the type it's supposed to be.
84
- //We do this to allow blank cells
85
- if ( td . textContent === "" ) {
86
- cellType = headers [ j ] . type ;
87
- } else {
88
- cellType = getCellType ( td . textContent , false ) ;
89
- }
86
+ cellType = findCellType ( td . textContent , headers [ j ] . type ) ;
90
87
}
91
88
92
89
//Check if doesn't match header
@@ -168,7 +165,7 @@ export const loadData = (
168
165
el : HTMLElement ,
169
166
settings : NltSettings
170
167
) : AppData | ErrorData => {
171
- let data = findErrorData ( el ) ;
168
+ const data = findErrorData ( el ) ;
172
169
if ( data !== null ) {
173
170
return data ;
174
171
} else {
@@ -345,38 +342,33 @@ export const writeContentToDataString = (
345
342
return data ;
346
343
} ;
347
344
348
- export const getCellType = ( textContent : string , firstRow : boolean ) => {
349
- if ( firstRow ) {
350
- switch ( textContent ) {
351
- case CELL_TYPE . TEXT :
352
- return CELL_TYPE . TEXT ;
353
- case CELL_TYPE . NUMBER :
354
- return CELL_TYPE . NUMBER ;
355
- case CELL_TYPE . TAG :
356
- return CELL_TYPE . TAG ;
357
- case CELL_TYPE . MULTI_TAG :
358
- return CELL_TYPE . MULTI_TAG ;
359
- default :
360
- return CELL_TYPE . ERROR ;
345
+ export const findCellType = ( textContent : string , expectedType : string ) => {
346
+ //If empty then just set it to the type it's supposed to be.
347
+ //We do this to allow blank cells
348
+ if ( textContent === "" ) return expectedType ;
349
+
350
+ const numTags = countNumTags ( textContent ) ;
351
+ if ( numTags === 1 ) {
352
+ //If we have a tag like "#test test" the first will match, but it's technically invalid
353
+ if ( textContent . match ( / \s / ) ) {
354
+ return CELL_TYPE . ERROR ;
355
+ } else {
356
+ return CELL_TYPE . TAG ;
361
357
}
358
+ } else if ( numTags > 1 ) {
359
+ return CELL_TYPE . MULTI_TAG ;
362
360
} else {
363
361
if ( textContent . match ( / ^ \d + $ / ) ) {
364
- return CELL_TYPE . NUMBER ;
362
+ if ( expectedType === CELL_TYPE . TEXT ) return CELL_TYPE . TEXT ;
363
+ else return CELL_TYPE . NUMBER ;
365
364
} else {
366
- const numTags = countNumTags ( textContent ) ;
367
- if ( numTags === 1 ) {
368
- return CELL_TYPE . TAG ;
369
- } else if ( numTags > 1 ) {
370
- return CELL_TYPE . MULTI_TAG ;
371
- } else {
372
- return CELL_TYPE . TEXT ;
373
- }
365
+ return CELL_TYPE . TEXT ;
374
366
}
375
367
}
376
368
} ;
377
369
378
370
export const countNumTags = ( textContent : string ) : number => {
379
- return ( textContent . match ( / # \w + / g) || [ ] ) . length ;
371
+ return ( textContent . match ( / # [ a - z A - z 0 - 9 - _ ] + / g) || [ ] ) . length ;
380
372
} ;
381
373
382
374
export const hasLink = ( content : string ) : boolean => {
0 commit comments