All files / skin-lr2/src csv.ts

100% Statements 11/11
87.5% Branches 7/8
100% Functions 7/7
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  128x       131x   665x 665x       665x 5x   660x 4182x       938x 938x    
export function decodeText(bytes: Uint8Array): string {
  return new TextDecoder('shift_jis').decode(bytes).replace(/^\ufeff/u, '');
}
 
export function parseRows(text: string): string[][] {
  return text
    .split(/\r?\n/u)
    .map((line) => parseRow(stripComment(line).trim()))
    .filter((row) => row.length > 0 && row[0]?.startsWith('#'));
}
 
export function parseRow(line: string): string[] {
  if (!line) {
    return [];
  }
  const delimiter = line.includes('\t') ? '\t' : ',';
  return line.split(delimiter).map((value) => value.trim().replace(/^["']|["']$/gu, ''));
}
 
export function stripComment(line: string): string {
  const index = line.indexOf('//');
  return index >= 0 ? line.slice(0, index) : line;
}