Developer Reference & Snippets

Open Source Guides & Code Snippets

Quick-start references for thutil open-source projects and standalone zero-dependency utility functions.

1. thutil Open Source Projects

GitHub @thutil

Explore our open source codebases and try the live web applications below:

finfly

AirDrop Alternative for direct P2P transfer via WebRTC

Open finfly ↗
node-shorten-link

Minimal and fast URL shortener service on Node.js

self-file-convert

Image format conversion tool (PNG, WebP, JPG, AVIF)

View GitHub ↗

2. Thai National ID Validation (Modulo 11 Checksum)

Copy and paste this zero-dependency TypeScript/JavaScript function directly into your application:

function validateThaiCitizenId(id: string): boolean {
  const cleanId = id.replace(/[^0-9]/g, '');
  if (cleanId.length !== 13) return false;
  if (cleanId.charAt(0) === '0') return false;

  let sum = 0;
  for (let i = 0; i < 12; i++) {
    sum += parseInt(cleanId.charAt(i), 10) * (13 - i);
  }

  const checkDigit = (11 - (sum % 11)) % 10;
  return checkDigit === parseInt(cleanId.charAt(12), 10);
}

3. CRC16-CCITT Calculation (EMVCo Standard)

Standalone CRC16-CCITT algorithm for computing QR code payloads:

function calculateCRC16(data: string): string {
  let crc = 0xFFFF;
  for (let i = 0; i < data.length; i++) {
    crc ^= data.charCodeAt(i) << 8;
    for (let j = 0; j < 8; j++) {
      if ((crc & 0x8000) !== 0) {
        crc = ((crc << 1) ^ 0x1021) & 0xFFFF;
      } else {
        crc = (crc << 1) & 0xFFFF;
      }
    }
  }
  return crc.toString(16).toUpperCase().padStart(4, '0');
}