Détail du package

qwerasd

jqzhang-xg93ISC1.1.1

A TypeScript library for detecting consecutive keyboard characters (qwerty patterns, numbers, letters). Perfect for password strength validation.

keyboard, consecutive, qwerty, detection

readme

qwerasd

🎯 檢測鍵盤連續字的 TypeScript 工具包

不僅可以檢測 qwe123abc 等基本連續字,更能智慧識別 abc124qwe789password123 等複雜混合字串中的連續字片段,適用於密碼強度驗證、表單驗證等場景。

✨ 功能特色

  • 檢測 QWERTY 鍵盤連續字:qweasdzxc
  • 檢測垂直連續字:qazwsxedc
  • 檢測反向連續字:ewqtsrcba
  • 檢測數字連續字:123456789
  • 檢測字母連續字:abcXYZ
  • 🆕 智慧混合字串檢測abc124qwe789hello123 等複雜組合

📦 安裝

npm install qwerasd

🚀 快速開始

useQwerty 檢測器

useQwerty 是本套件的核心功能,提供完整的鍵盤連續字檢測能力:

import { useQwerty } from "qwerasd";

const detector = useQwerty(3); // 設定最小長度為 3

// 檢測各種連續字
detector.detect("qwe").isQwerty(); // true - QWERTY 鍵盤連續字
detector.detect("123").isNumber(); // true - 數字連續字
detector.detect("abc").isLowercase(); // true - 小寫字母連續字
detector.detect("XYZ").isUppercase(); // true - 大寫字母連續字

// 檢測任何類型的連續字
detector.detect("qwe").isConsecutive(); // true
detector.detect("hello").isConsecutive(); // false

// 獲取詳細結果
const result = detector.detect("qwer").getResults();
// { isQwerty: true, isUppercase: false, isLowercase: false,
//   isNumber: false, isConsecutive: true }

🆕 智慧混合字串檢測

新版本支援檢測複雜字串中的連續字片段,大幅提升實用性:

const detector = useQwerty(3);

// ✨ 混合字串檢測 - 核心新功能
detector.detect("abc124").isConsecutive(); // true - 檢測到 "abc"
detector.detect("qwe789").isConsecutive(); // true - 檢測到 "qwe" 和 "789"
detector.detect("hello123").isConsecutive(); // true - 檢測到 "123"
detector.detect("password456").isConsecutive(); // true - 檢測到 "456"

// 詳細分析混合字串
const mixedResult = detector.detect("abc124").getResults();
// { isQwerty: false, isLowercase: true, isNumber: false, isConsecutive: true }

// 複合連續字檢測
detector.detect("qwe123").getResults();
// { isQwerty: true, isNumber: true, isConsecutive: true }
// 同時檢測到 QWERTY 和數字連續字!

工作原理:檢測器會智慧地將長字串分割成指定長度的片段,然後檢查每個片段是否為連續字,讓您能輕鬆發現隱藏在複雜字串中的弱點模式。

進階檢測選項

// 檢測反向連續字
detector.detect("ewq", true); // true - qwe 的反向
detector.detect("321", true); // true - 123 的反向

// 檢測垂直連續字(僅適用於 QWERTY)
detector.detect("qaz", false, true); // true - q→a→z 垂直排列
detector.detect("wsx", false, true); // true - w→s→x 垂直排列

單次檢測場景

如果只需要簡單的單次檢測,可以從 qwerasd/utils 導入這些便利函數:

import { isQwerty, isNumber, isUppercase, isLowercase } from "qwerasd/utils";

// 快速檢測 QWERTY 鍵盤連續字
isQwerty("qwe"); // true
isQwerty("asd"); // true
isQwerty("hello"); // false

// 快速檢測數字連續字
isNumber("123"); // true
isNumber("456"); // true
isNumber("135"); // false

// 快速檢測字母連續字
isUppercase("ABC"); // true
isLowercase("xyz"); // true

// 支援反向和垂直檢測
isQwerty("ewq", true); // true - 反向檢測
isQwerty("qaz", false, true); // true - 垂直檢測

💡 提示:您也可以從主模組導入這些函數 import { isQwerty } from "qwerasd",兩種方式都可以使用。

📚 API 參考

useQwerty

const detector = useQwerty(最小長度);

detector.detect(str, 反向檢測?, 垂直檢測?)
  .isQwerty()       // 是否為 QWERTY 鍵盤連續字
  .isNumber()       // 是否為數字連續字
  .isUppercase()    // 是否為大寫字母連續字
  .isLowercase()    // 是否為小寫字母連續字
  .isConsecutive()  // 是否為任何類型的連續字
  .getResults()     // 取得詳細結果對象

參數說明:

  • 最小長度:檢測的最小字符長度,小於此長度的字串會返回 false
  • str:要檢測的字串
  • 反向檢測:是否包含反向字串檢測(可選,預設 false)
  • 垂直檢測:是否包含垂直排列檢測(可選,預設 false,僅適用於 QWERTY)

額外功能:直接檢測函數 (qwerasd/utils)

這些函數提供快速的單次檢測,適合簡單使用場景:

// 從 utils 模組導入
import { isQwerty, isUppercase, isLowercase, isNumber } from "qwerasd/utils";

// QWERTY 鍵盤連續字檢測
isQwerty(str, 反向檢測?, 垂直檢測?)

// 字母數字連續字檢測
isUppercase(str, 反向檢測?)   // 大寫字母 ABC, DEF...
isLowercase(str, 反向檢測?)   // 小寫字母 abc, xyz...
isNumber(str, 反向檢測?)      // 數字 123, 456...

實用範例

🔐 智慧密碼強度檢測

利用新的混合字串檢測功能,更精確地識別密碼中的弱點:

import { useQwerty } from "qwerasd";

// 創建檢測器,設定最小連續長度為 3
const detector = useQwerty(3);

function advancedPasswordCheck(password: string) {
  const result = detector.detect(password, true, true);

  return {
    password,
    hasWeakPattern: result.isConsecutive(),
    weakTypes: {
      qwerty: result.isQwerty(), // 鍵盤連續字
      numbers: result.isNumber(), // 數字連續字
      letters: result.isLowercase() || result.isUppercase(),
    },
    details: result.getResults(),
  };
}

// ✨ 測試各種複雜密碼場景
advancedPasswordCheck("mypassword123");
// { hasWeakPattern: true, weakTypes: { numbers: true, letters: false, qwerty: false } }

advancedPasswordCheck("secure_abc_2024");
// { hasWeakPattern: true, weakTypes: { letters: true, numbers: false, qwerty: false } }

advancedPasswordCheck("loginqwe456");
// { hasWeakPattern: true, weakTypes: { qwerty: true, numbers: true, letters: false } }

advancedPasswordCheck("K7#mX!9$pL");
// { hasWeakPattern: false, weakTypes: { qwerty: false, numbers: false, letters: false } }

實際應用場景

  • ✅ 檢測 abc123 類型的常見弱密碼組合
  • ✅ 識別 qwerty456 等鍵盤+數字的懶惰模式
  • ✅ 發現隱藏在長密碼中的連續字片段
  • ✅ 提供具體的改進建議(避免哪種類型的連續字)

快速檢測(使用 utils 模組)

import { isQwerty, isNumber } from "qwerasd/utils";

// 簡單的弱密碼模式檢測
function hasSimpleWeakPattern(password: string) {
  const commonPatterns = ["qwe", "asd", "zxc", "123", "456", "abc"];
  return commonPatterns.some(
    (pattern) =>
      password.includes(pattern) ||
      isQwerty(pattern, true) ||
      isNumber(pattern, true)
  );
}

hasSimpleWeakPattern("password123"); // true
hasSimpleWeakPattern("x9mK#2p"); // false

🔧 開發

npm test           # 運行測試
npm run build      # 構建發布版本

📄 授權

ISC


💡 提示:這個工具特別適合用於密碼強度檢測,不僅能識別明顯的連續字符(如 123qwe),更能發現隱藏在複雜密碼中的弱點模式(如 MyPassword123login_qwe_2024),幫助建立更安全的密碼策略。