genthaiid
v2.0.3
Published
better thaiid
Maintainers
Readme
genthaiid
Generate, validate, and complete Thai national ID numbers in JavaScript.
The package is ESM-only and exports the generator as the default export.
Installation
npm install genthaiidUsage
import gen, {
random,
verify,
completeid,
randomCompleteID,
getLastDigit,
getRndInteger,
thaiIdRegex,
} from "genthaiid";
const id = gen();
console.log(id); // 13-digit Thai ID
console.log(verify(id)); // true
console.log(thaiIdRegex.test(id)); // true
console.log(random(3)); // ["...", "...", "..."]
console.log(randomCompleteID("1100")); // "1100XXXXXXXX"
console.log(getLastDigit("110170020345")); // checksum digit
console.log(getRndInteger(0, 9)); // random integer between 0 and 9
console.log(completeid("11017002034_5"));
// returns every matching ID that fits the placeholderAPI
gen()
Returns a random 13-digit Thai ID number as a string.
random(count)
Returns an array of generated Thai ID numbers.
- If
countis a number, it usesparseInt(count). - If
countis a numeric string, it decrements the string safely and generates that many IDs. - If
countis invalid, omitted, or not numeric, it returns an array with one generated ID.
Examples:
random(2); // ["...", "..."]
random("2"); // ["...", "..."]
random(null); // ["..."]randomCompleteID(startDigits)
Generates a random 13-digit Thai ID that starts with the provided digits.
startDigitsmust be between 4 and 12 digits.- The first digit must be between
1and8(Thai ID rule). - Remaining digits are randomly generated.
- Returns a valid 13-digit Thai ID string that always passes
verify(). - Returns
undefinedif the input is invalid (fewer than 4 digits, first digit is0or9, or contains non-digits).
Examples:
randomCompleteID("1100"); // "1100XXXXXXXX7" (random fill)
randomCompleteID("1234567"); // "1234567XXXXX2" (random fill)
randomCompleteID("110"); // undefined (too few digits)
randomCompleteID("9100"); // undefined (first digit must be 1-8)verify(id, checkfirstdigit = true, checklastdigit = true)
Checks whether a value is a valid Thai ID.
- Requires exactly 13 digits.
- By default, validates both the first digit rule and the checksum digit.
checkfirstdigitenforces the first digit to be between1and8.checklastdigitenforces the calculated checksum.
completeid(idWithPlaceholders, checkfirstdigit = true, checklastdigit = true)
Completes a partial 13-digit Thai ID using _ as a placeholder and returns every valid match.
Examples:
completeid("11017002034_5");
completeid("1____________");Returns:
string[]when multiple candidates are checked and valid matches are foundstringwhen the original input is already validundefinedwhen the input is not a 13-digit pattern
getLastDigit(firstTwelveDigits)
Calculates the checksum digit from the first 12 digits of a Thai ID.
getRndInteger(min, max)
Returns a random integer between min and max, inclusive.
thaiIdRegex
Regular expression for the first-digit rule:
/^[1-8]\d{12}$/TypeScript
The package ships with declarations in index.d.ts.
License
MIT
