系統

檢查 Email 格式

為了發 EDM 整理網站的會員 email。

發現好多奇怪的 email….

到底是怎麼通過驗證的呢?!

問了 ChatGPT,修改一下,下面是用 Node.js 驗證 email 格式的 code,有用到 isemail 這個 package。

email.txt 裡的 email 為一行一個。

const fs = require('fs');

const isEmail = require('isemail');

let emails;

fs.readFile('./email.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Err:', err);
    return;
  }

  emails = data.split(/\r?\n|\r/).filter((line) => line.length > 0);

  console.log('emails.length:', emails.length);

  emails.forEach((email) => {
    if (!isEmail.validate(email)) {
      console.log(email);
    }

    // if (isEmail.validate(email, { errorLevel: true }) !== 0) {
    //  console.log(email);
    // }
  });
});
系統

使用 awk 擷取 Log 檔的特定欄位

想要從 Apache 的 log 檔擷取特定欄位。

下面是問 ChatGPT 的回答:

// 要使用 awk 指令從 Apache 日誌中提取第一欄(IP 地址)和第四欄(時間戳)的數據
// 可以使用以下命令
// '{print $1, $4}' 意思是印出第一欄和第四欄
$ awk '{print $1, $4}' log_file

// 不過,第四欄的時間戳會包含方括號,你可以用 awk 去除方括號,使輸出更乾淨
// gsub(/\[|\]/, "", $4) 是 awk 的一個函數調用,用來替換 $4 欄中的方括號 [ 和 ]
// 將其替換為空字符串 ""
$ awk '{gsub(/\[|\]/, "", $4); print $1, $4}' log_file

下面是我自己修改後的寫法:

// 欄位之間的 \t 是 tab 的意思
$ awk '{gsub(/\[|\]/, "", $4); gsub(/\"/, "", $7); print $1 "\t" $4 "\t" $7 "\t" $9 "\t" $10}' log_file
網頁

[JS] 日期

取得格式為 YYYY-MM-DD 的今天日期。

下面是問 ChatGPT 的結果:

const today = new Date();

const year = today.getFullYear();

// 月份從 0 開始,所以要加 1,並確保兩位數
const month = String(today.getMonth() + 1).padStart(2, '0');

// 確保日期為兩位數
const day = String(today.getDate()).padStart(2, '0');

const formattedDate = `${year}-${month}-${day}`;

// 2024-05-23
console.log(formattedDate);

在網路上還看到其他方式:

const today = new Date();

const year = today.getFullYear();
const month = ('0' + (today.getMonth() + 1)).slice(-2);
const date = ('0' + today.getDate()).slice(-2);

const hours = ('0' + today.getHours()).slice(-2);
const minutes = ('0' + today.getMinutes()).slice(-2);
const seconds = ('0' + today.getSeconds()).slice(-2);

// 2024-05-23 10:55:11
console.log(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`);

———

取得最近七天日期。

下面是問 ChatGPT 的結果:

const today = new Date();

for (let i = 0; i < 7; i++) {
  const tempDate = new Date();

  // 設定日期為今天減去 i 天
  tempDate.setDate(today.getDate() - i);

  const year = tempDate.getFullYear();

  // 月份從 0 開始,所以要加 1,並確保兩位數
  const month = String(tempDate.getMonth() + 1).padStart(2, '0');

  // 確保日期為兩位數
  const day = String(tempDate.getDate()).padStart(2, '0');

  const formattedDate = `${year}-${month}-${day}`;
  
  console.log(formattedDate);
}

在網路上看到的方式:

const today = new Date();

for (let i = 0; i < 7; i++) {
  const newDate = new Date(today.getTime() - i * 24 * 60 * 60 * 1000);
  
  const year = newDate.getFullYear();
  const month = String(newDate.getMonth() + 1).padStart(2, '0');
  const day = String(newDate.getDate()).padStart(2, '0');
  
  const formattedDate = `${year}-${month}-${day}`;
  
  console.log(formattedDate);
}


其他參考連結:

網頁

[Vue] Global State with Composables

在 Vue Composition API 下,想要有全域狀態,但又不用 Pinia,可以使用下面的方法:

// 檔名:useTest.js

import { ref, readonly } from 'vue';

// 有 import 這個檔案的 .vue 都可以使用這個 ref
const global = ref(0);

export function useTest() {
  // import 這個檔案的 .vue 檔,各自有這個 ref
  const count = ref(0);

  const countPlus = () => {
    count.value++;
  };

  const globalPlus = () => {
    global.value++;
  };

  return {
    // 設定全域 ref 唯讀,只能透過 return 的 function 修改
    global: readonly(global),
    globalPlus,
    count,
    countPlus
  };
}


參考連結:

返回頂端