All files common.js

100% Statements 28/28
100% Branches 27/27
100% Functions 8/8
100% Lines 26/26

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                    192x 5x   187x                           21x 5x       16x   34x 34x   16x                     39x             108x                   35x 6x       29x 9x     20x                 46x   40x 14x   26x 10x   16x 7x   9x 7x     2x    
/**
 * 西濃追跡チェッカー - 共通ユーティリティ関数
 */
 
/**
 * HTMLをエスケープする(XSS対策)
 * @param {string} unsafe - エスケープ対象の文字列
 * @returns {string} エスケープされた文字列
 */
export function escapeHtml(unsafe) {
  if (typeof unsafe !== 'string') {
    return '';
  }
  return unsafe
    .replace(/&/g, '&')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#039;');
}
 
/**
 * 伝票番号を抽出する
 * @param {string} text - テキスト(カンマ区切り、改行区切り、スペース区切り対応)
 * @returns {string[]} 伝票番号の配列(重複なし)
 */
export function extractTrackingNumbers(text) {
  if (typeof text !== 'string') {
    return [];
  }
  // カンマ区切り、改行区切り、スペース区切りに対応
  // 10-12桁の数字を伝票番号として抽出
  const numbers = text
    .split(/[,\n\s]+/)
    .map(n => n.trim())
    .filter(n => /^\d{10,12}$/.test(n));
 
  return [...new Set(numbers)]; // 重複を削除
}
 
/**
 * 電話番号かどうかを判定
 * @param {string} num - チェックする数字文字列
 * @returns {boolean} 電話番号の場合はtrue
 */
export function isPhoneNumber(num) {
  // 電話番号のパターン(0から始まる)
  // 03, 06, 052, 0120, 0570, 0800 など
  const phonePatterns = [
    /^0[1-9]\d{8,9}$/,  // 固定電話(03, 06, 052など)
    /^0120\d{6}$/,       // フリーダイヤル
    /^0570\d{6}$/,       // ナビダイヤル
    /^0800\d{7}$/,       // フリーダイヤル
  ];
 
  return phonePatterns.some(pattern => pattern.test(num));
}
 
/**
 * 伝票番号として有効かどうかを判定
 * @param {string} num - チェックする数字文字列
 * @returns {boolean} 有効な伝票番号の場合はtrue
 */
export function isValidTrackingNumber(num) {
  // 10-12桁の数字
  if (!/^\d{10,12}$/.test(num)) {
    return false;
  }
 
  // 電話番号は除外
  if (isPhoneNumber(num)) {
    return false;
  }
 
  return true;
}
 
/**
 * ステータスタイプの判定
 * @param {string} status - ステータス文字列
 * @returns {string} ステータスタイプ(delivered, intransit, shipped, pickedup, unknown)
 */
export function getStatusType(status) {
  if (!status) return 'unknown';
 
  if (status.includes('完了') || status.includes('到着') || status.includes('お届け')) {
    return 'delivered';
  }
  if (status.includes('持出') || status.includes('配達中')) {
    return 'intransit';
  }
  if (status.includes('発送') || status.includes('出荷')) {
    return 'shipped';
  }
  if (status.includes('受付') || status.includes('集荷')) {
    return 'pickedup';
  }
 
  return 'unknown';
}