All files / utils dateParser.ts

100% Statements 13/13
66.66% Branches 4/6
100% Functions 4/4
100% Lines 13/13

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 25 26 27 28 29 30 31    7x     12x 12x       5x       3x 3x 3x       5x 5x   5x   5x 2x   5x    
import { FunctionDeclaration, SyntaxKind } from 'ts-morph';
 
const dateRegExp = /['"]?(\d{1,2}\/\d{1,2}\/\d{1,4})["']?.*,/;
 
export function getDate(commentString: string | undefined): string | undefined {
  const matchResult = commentString?.match(dateRegExp);
  return matchResult?.[1];
}
 
export function extractDateFromComments(funDecl: FunctionDeclaration): string | undefined {
  return extractDateFromInlineComment(funDecl) || extractDateFromBlockComments(funDecl);
}
 
function extractDateFromBlockComments(funDecl: FunctionDeclaration): string | undefined {
  const returnStatement = funDecl.getFirstDescendantByKind(SyntaxKind.ReturnStatement);
  const comment = returnStatement?.getChildren()[1]?.getText();
  return getDate(comment);
}
 
function extractDateFromInlineComment(funDecl: FunctionDeclaration): string | undefined {
  const returnStatement = funDecl.getFirstDescendantByKind(SyntaxKind.ReturnStatement);
  const callExp = returnStatement?.getExpressionIfKind(SyntaxKind.CallExpression);
 
  const comments = callExp?.getArguments()[0]?.getTrailingCommentRanges() ?? [];
  let dateString;
  for (const comment of comments) {
    dateString = getDate(comment.getText()) || dateString;
  }
  return dateString;
}