diff --git a/bun.lockb b/bun.lockb index 8d516a0..8f47daa 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/dist/assets/index-CtF5sz_F.js b/dist/assets/index-CtF5sz_F.js new file mode 100644 index 0000000..2160bf4 --- /dev/null +++ b/dist/assets/index-CtF5sz_F.js @@ -0,0 +1 @@ +(function(){const n=document.createElement("link").relList;if(n&&n.supports&&n.supports("modulepreload"))return;for(const e of document.querySelectorAll('link[rel="modulepreload"]'))s(e);new MutationObserver(e=>{for(const i of e)if(i.type==="childList")for(const a of i.addedNodes)a.tagName==="LINK"&&a.rel==="modulepreload"&&s(a)}).observe(document,{childList:!0,subtree:!0});function r(e){const i={};return e.integrity&&(i.integrity=e.integrity),e.referrerPolicy&&(i.referrerPolicy=e.referrerPolicy),e.crossOrigin==="use-credentials"?i.credentials="include":e.crossOrigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function s(e){if(e.ep)return;e.ep=!0;const i=r(e);fetch(e.href,i)}})();var b=Object.defineProperty,h=(t,n)=>{for(var r in n)b(t,r,{get:n[r],enumerable:!0,configurable:!0,set:s=>n[r]=()=>s})},l={};h(l,{default:()=>f});function u(t){if(typeof t=="number"&&(t=t.toString()),!/^\d{13}$/.test(t))return!1;let n=0;for(let r=0;r<12;r++)n+=Number(t[r])*(13-r);return(11-n%11)%10===Number(t[12])}typeof B<"u"&&typeof B.exports<"u"?(B.exports=u,l.default=u,l.isValidThaiID=u):typeof window<"u"&&(window.isValidThaiID=u);var f=u;document.addEventListener("DOMContentLoaded",()=>{const t=document.getElementById("citizenid"),n=document.getElementById("button"),r=document.getElementById("errorMessage"),s=document.getElementById("successMessage"),e=IMask(t,{mask:"0-0000-00000-00-0"});t.addEventListener("input",i),t.addEventListener("keypress",a),n.addEventListener("click",m);function i(){const o=e.unmaskedValue,p=13,g=/^[0-9]\d*$/,c=o.length===p,y=g.test(o),d=f(o);n.disabled=!(c&&y&&d),t.setAttribute("aria-invalid",String(!d)),r.setAttribute("aria-hidden",String(d)),s.setAttribute("aria-hidden",String(!d)),r.style.display=c?d?"none":"block":"none",s.style.display=c&&d?"block":"none"}function a(o){if(o.code==="Enter")return o.preventDefault(),!1}function m(o){o.preventDefault(),o.stopImmediatePropagation(),window.alert("Your citizen ID submit value is: "+e.unmaskedValue)}}); diff --git a/dist/browser/validator.js b/dist/browser/validator.js index e1c1ad9..6c8681b 100644 --- a/dist/browser/validator.js +++ b/dist/browser/validator.js @@ -1 +1 @@ -var C=(j,k)=>()=>(k||j((k={exports:{}}).exports,k),k.exports);var E=C((B,A)=>{function z(j){if(typeof j==="number")j=j.toString();if(!/^\d{13}$/.test(j))return!1;let k=0;for(let q=0;q<12;q++)k+=Number(j[q])*(13-q);return(11-k%11)%10===Number(j[12])}if(typeof A!=="undefined"&&typeof A.exports!=="undefined")A.exports=z,B.default=z,B.isValidThaiID=z;else if(typeof window!=="undefined")window.isValidThaiID=z});export default E(); +var E=Object.defineProperty;var F=(j,k)=>{for(var A in k)E(j,A,{get:k[A],enumerable:!0,configurable:!0,set:(q)=>k[A]=()=>q})};var C={};F(C,{default:()=>G});function z(j){if(typeof j==="number")j=j.toString();if(!/^\d{13}$/.test(j))return!1;let k=0;for(let q=0;q<12;q++)k+=Number(j[q])*(13-q);return(11-k%11)%10===Number(j[12])}if(typeof B!=="undefined"&&typeof B.exports!=="undefined")B.exports=z,C.default=z,C.isValidThaiID=z;else if(typeof window!=="undefined")window.isValidThaiID=z;var G=z;export{G as default}; diff --git a/dist/esm/validator.js b/dist/esm/validator.js index e1c1ad9..6c8681b 100644 --- a/dist/esm/validator.js +++ b/dist/esm/validator.js @@ -1 +1 @@ -var C=(j,k)=>()=>(k||j((k={exports:{}}).exports,k),k.exports);var E=C((B,A)=>{function z(j){if(typeof j==="number")j=j.toString();if(!/^\d{13}$/.test(j))return!1;let k=0;for(let q=0;q<12;q++)k+=Number(j[q])*(13-q);return(11-k%11)%10===Number(j[12])}if(typeof A!=="undefined"&&typeof A.exports!=="undefined")A.exports=z,B.default=z,B.isValidThaiID=z;else if(typeof window!=="undefined")window.isValidThaiID=z});export default E(); +var E=Object.defineProperty;var F=(j,k)=>{for(var A in k)E(j,A,{get:k[A],enumerable:!0,configurable:!0,set:(q)=>k[A]=()=>q})};var C={};F(C,{default:()=>G});function z(j){if(typeof j==="number")j=j.toString();if(!/^\d{13}$/.test(j))return!1;let k=0;for(let q=0;q<12;q++)k+=Number(j[q])*(13-q);return(11-k%11)%10===Number(j[12])}if(typeof B!=="undefined"&&typeof B.exports!=="undefined")B.exports=z,C.default=z,C.isValidThaiID=z;else if(typeof window!=="undefined")window.isValidThaiID=z;var G=z;export{G as default}; diff --git a/dist/index.html b/dist/index.html index 0fefad4..f151999 100644 --- a/dist/index.html +++ b/dist/index.html @@ -14,7 +14,7 @@ crossorigin="anonymous" referrerpolicy="no-referrer" > - + diff --git a/package.json b/package.json index 5127fe3..a5b9319 100644 --- a/package.json +++ b/package.json @@ -48,12 +48,13 @@ "license": "MIT", "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.2", - "@types/node": "^22.3.0", - "eslint": "^9.9.0", + "@types/bun": "^1.1.9", + "@types/node": "^22.5.5", + "eslint": "^9.10.0", "nyc": "^17.0.0", - "standard": "^17.1.0", - "typescript": "^5.5.4", - "vite": "^5.4.0" + "standard": "^17.1.2", + "typescript": "^5.6.2", + "vite": "^5.4.6" }, "standard": { "ignore": [ diff --git a/src/validator.ts b/src/validator.ts index 022427e..3e4a2c5 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -25,7 +25,7 @@ function isValidThaiID(id: string | number): boolean { } // Export the function. -if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { +if (typeof module !== 'undefined' && typeof module.exports !== 'undefined' || window === undefined) { module.exports = isValidThaiID exports.default = isValidThaiID exports.isValidThaiID = isValidThaiID @@ -33,4 +33,7 @@ if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { // Support legacy version of the browser. // @ts-ignore window.isValidThaiID = isValidThaiID +} else { + // @ts-ignore + global.isValidThaiID = isValidThaiID } diff --git a/tests/validator.test.js b/tests/validator.test.js deleted file mode 100644 index 3b653fb..0000000 --- a/tests/validator.test.js +++ /dev/null @@ -1,52 +0,0 @@ -import { describe, it, expect } from 'bun:test' -import validateThaiID from '../src/validator' - -describe('validateThaiID Function', () => { - it('Case 1: Valid ID Correct pattern', () => { - expect(validateThaiID('1112034563562')).toBe(true) - }) - - it('Case 2: Invalid ID Wrong pattern', () => { - expect(validateThaiID('1101700230705')).toBe(false) - }) - - it('Case 3: Invalid ID Less than 13 characters', () => { - expect(validateThaiID('110170023073')).toBe(false) - }) - - it('Case 4: Invalid ID Mixed characters', () => { - expect(validateThaiID('11017002070d3')).toBe(false) - }) - - it('Case 5: Invalid ID Mixed characters', () => { - expect(validateThaiID('rytege54fsfsf')).toBe(false) - }) - - it('Case 6: Invalid ID Just 0', () => { - expect(validateThaiID('0')).toBe(false) - }) - - it('Case 7: Invalid ID Special character', () => { - expect(validateThaiID('-')).toBe(false) - }) - - it('Case 8: Invalid ID Blank character', () => { - expect(validateThaiID('')).toBe(false) - }) - - it('Case 9: Invalid ID NULL', () => { - expect(validateThaiID(null)).toBe(false) - }) - - it('Case 10: Invalid ID Only text character', () => { - expect(validateThaiID('blablabla')).toBe(false) - }) - - it('Case 11: Invalid ID Undefined input', () => { - expect(validateThaiID(undefined)).toBe(false) - }) - - it('Case 12: First digit of ID start with 0', () => { - expect(validateThaiID('0113200420331')).toBe(true) - }) -}) diff --git a/tests/validator.test.ts b/tests/validator.test.ts new file mode 100644 index 0000000..18827bd --- /dev/null +++ b/tests/validator.test.ts @@ -0,0 +1,25 @@ +import { describe, it, expect } from 'bun:test'; +// @ts-ignore +import isValidThaiID from '../src/validator'; + +describe('isValidThaiID Function', () => { + it('Case 1: Valid ID Correct pattern', () => { + expect(isValidThaiID('1112034563562')).toBe(true); + }); + + it('Case 2: Invalid ID Wrong pattern', () => { + expect(isValidThaiID('1101700230705')).toBe(false); + }); + + it('Case 3: Invalid ID Less than 13 characters', () => { + expect(isValidThaiID('110170023073')).toBe(false); + }); + + it('Case 4: Invalid ID Mixed characters', () => { + expect(isValidThaiID('11017002070d3')).toBe(false); + }); + + it('Case 5: Invalid ID Mixed characters', () => { + expect(isValidThaiID('rytege54fsfsf')).toBe(false); + }); +}); \ No newline at end of file diff --git a/vite.config.js b/vite.config.js deleted file mode 100644 index 3e4738d..0000000 --- a/vite.config.js +++ /dev/null @@ -1,16 +0,0 @@ -module.exports = { - test: { - coverage: { - provider: 'istanbul' - } - }, - build: { - minify: 'esbuild', - outDir: './dist', - lib: { - entry: './lib/validator.js', - name: 'validateThaiID', - fileName: (format) => `validator.${format}.js` - } - } -}