thaizip 0.7.6 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -42,6 +42,12 @@ var MAX_POSTING_BUDGET = 1e6;
42
42
  function typeErr(table, id, field, expected, value) {
43
43
  return new TypeError(`[thaizip] ${table} ${id}: expected ${expected} for ${field}, got ${typeof value}`);
44
44
  }
45
+ function assertId(table, id, field, value) {
46
+ if (typeof value !== "number") throw typeErr(table, id, field, "number", value);
47
+ if (!Number.isSafeInteger(value)) {
48
+ throw new TypeError(`[thaizip] ${table} ${id}: expected safe integer for ${field}, got ${value}`);
49
+ }
50
+ }
45
51
  function typeName(value) {
46
52
  return value === null ? "null" : typeof value;
47
53
  }
@@ -68,7 +74,7 @@ function validateRawData(data) {
68
74
  if (p === null || typeof p !== "object") {
69
75
  throw new TypeError(`[thaizip] province[${i}]: expected object, got ${typeName(p)}`);
70
76
  }
71
- if (typeof p.id !== "number") throw typeErr("province", p.id, "id", "number", p.id);
77
+ assertId("province", p.id, "id", p.id);
72
78
  if (provinceById.has(p.id)) throw new TypeError(`[thaizip] duplicate province id: ${p.id}`);
73
79
  provinceById.set(p.id, p);
74
80
  if (typeof p.name_th !== "string") throw typeErr("province", p.id, "name_th", "string", p.name_th);
@@ -82,12 +88,12 @@ function validateRawData(data) {
82
88
  if (a === null || typeof a !== "object") {
83
89
  throw new TypeError(`[thaizip] amphure[${i}]: expected object, got ${typeName(a)}`);
84
90
  }
85
- if (typeof a.id !== "number") throw typeErr("amphure", a.id, "id", "number", a.id);
91
+ assertId("amphure", a.id, "id", a.id);
86
92
  if (amphureById.has(a.id)) throw new TypeError(`[thaizip] duplicate amphure id: ${a.id}`);
87
93
  amphureById.set(a.id, a);
88
94
  if (typeof a.name_th !== "string") throw typeErr("amphure", a.id, "name_th", "string", a.name_th);
89
95
  if (typeof a.name_en !== "string") throw typeErr("amphure", a.id, "name_en", "string", a.name_en);
90
- if (typeof a.province_id !== "number") throw typeErr("amphure", a.id, "province_id", "number", a.province_id);
96
+ assertId("amphure", a.id, "province_id", a.province_id);
91
97
  if (a.name_th.length > MAX_NAME_LENGTH) throw new RangeError(`[thaizip] amphure ${a.id}: name_th too long`);
92
98
  if (a.name_en.length > MAX_NAME_LENGTH) throw new RangeError(`[thaizip] amphure ${a.id}: name_en too long`);
93
99
  }
@@ -100,7 +106,7 @@ function validateRawData(data) {
100
106
  if (t === null || typeof t !== "object") {
101
107
  throw new TypeError(`[thaizip] tambon[${i}]: expected object, got ${typeName(t)}`);
102
108
  }
103
- if (typeof t.id !== "number") throw typeErr("tambon", t.id, "id", "number", t.id);
109
+ assertId("tambon", t.id, "id", t.id);
104
110
  if (seenTambonIds.has(t.id)) throw new TypeError(`[thaizip] duplicate tambon id: ${t.id}`);
105
111
  seenTambonIds.add(t.id);
106
112
  if (typeof t.name_th !== "string") throw typeErr("tambon", t.id, "name_th", "string", t.name_th);
@@ -111,7 +117,7 @@ function validateRawData(data) {
111
117
  if (zipType !== "string" && zipType !== "number") {
112
118
  throw typeErr("tambon", t.id, "zip_code", "string or number", t.zip_code);
113
119
  }
114
- if (typeof t.amphure_id !== "number") throw typeErr("tambon", t.id, "amphure_id", "number", t.amphure_id);
120
+ assertId("tambon", t.id, "amphure_id", t.amphure_id);
115
121
  const zipLength = String(t.zip_code).length;
116
122
  if (zipLength > MAX_ZIP_LENGTH) throw new RangeError(`[thaizip] tambon ${t.id}: zip_code too long`);
117
123
  if (t.deleted_at) continue;
@@ -421,7 +427,7 @@ function searchThaiAddress(index, query, options) {
421
427
  if (ZIP_CODE_RE.test(normalized)) {
422
428
  if (normalized.length < 2) return [];
423
429
  return lookupByZipCode(index, normalized, {
424
- zipLimit: options?.zipLimit ?? Infinity
430
+ zipLimit: options?.zipLimit ?? (normalized.length >= 5 ? Infinity : limit)
425
431
  });
426
432
  }
427
433
  if (normalized.length < 3) return [];
@@ -583,5 +589,3 @@ exports.normalizeThaiAddressText = normalizeThaiAddressText;
583
589
  exports.resolveThaiAddress = resolveThaiAddress;
584
590
  exports.searchThaiAddress = searchThaiAddress;
585
591
  exports.validateRawData = validateRawData;
586
- //# sourceMappingURL=index.cjs.map
587
- //# sourceMappingURL=index.cjs.map
package/dist/index.d.cts CHANGED
@@ -101,9 +101,11 @@ type SearchOptions = {
101
101
  limit?: number;
102
102
  threshold?: number;
103
103
  /**
104
- * Maximum results for all-digit (zip code) queries. A zip code maps to at most
105
- * a few dozen tambons, so this defaults to `Infinity` — capping zip results at
106
- * the text-autocomplete `limit` silently hides valid subdistricts.
104
+ * Maximum results for all-digit (zip code) queries. When omitted, a complete
105
+ * 5-digit zip returns every matching tambon (at most a few dozen — capping
106
+ * those at the text-autocomplete `limit` would silently hide valid
107
+ * subdistricts), while a partial prefix (2–4 digits, which can match hundreds
108
+ * of records) is capped at `limit`. Pass `Infinity` to return every prefix match.
107
109
  */
108
110
  zipLimit?: number;
109
111
  /**
@@ -145,6 +147,10 @@ type BuildIndexOptions = {
145
147
  * Validate that every consumed field has the expected runtime type and throw a
146
148
  * descriptive `TypeError` if not. Defaults to `true`; set to `false` to skip
147
149
  * the checks when the data source is already trusted (e.g. the bundled index).
150
+ *
151
+ * `false` also skips the row, name-length and posting-budget limits that bound
152
+ * memory and CPU use, so never disable it for data you do not control
153
+ * (user uploads, third-party APIs, a CMS editable by others).
148
154
  */
149
155
  validate?: boolean;
150
156
  };
package/dist/index.d.ts CHANGED
@@ -101,9 +101,11 @@ type SearchOptions = {
101
101
  limit?: number;
102
102
  threshold?: number;
103
103
  /**
104
- * Maximum results for all-digit (zip code) queries. A zip code maps to at most
105
- * a few dozen tambons, so this defaults to `Infinity` — capping zip results at
106
- * the text-autocomplete `limit` silently hides valid subdistricts.
104
+ * Maximum results for all-digit (zip code) queries. When omitted, a complete
105
+ * 5-digit zip returns every matching tambon (at most a few dozen — capping
106
+ * those at the text-autocomplete `limit` would silently hide valid
107
+ * subdistricts), while a partial prefix (2–4 digits, which can match hundreds
108
+ * of records) is capped at `limit`. Pass `Infinity` to return every prefix match.
107
109
  */
108
110
  zipLimit?: number;
109
111
  /**
@@ -145,6 +147,10 @@ type BuildIndexOptions = {
145
147
  * Validate that every consumed field has the expected runtime type and throw a
146
148
  * descriptive `TypeError` if not. Defaults to `true`; set to `false` to skip
147
149
  * the checks when the data source is already trusted (e.g. the bundled index).
150
+ *
151
+ * `false` also skips the row, name-length and posting-budget limits that bound
152
+ * memory and CPU use, so never disable it for data you do not control
153
+ * (user uploads, third-party APIs, a CMS editable by others).
148
154
  */
149
155
  validate?: boolean;
150
156
  };
package/dist/index.js CHANGED
@@ -40,6 +40,12 @@ var MAX_POSTING_BUDGET = 1e6;
40
40
  function typeErr(table, id, field, expected, value) {
41
41
  return new TypeError(`[thaizip] ${table} ${id}: expected ${expected} for ${field}, got ${typeof value}`);
42
42
  }
43
+ function assertId(table, id, field, value) {
44
+ if (typeof value !== "number") throw typeErr(table, id, field, "number", value);
45
+ if (!Number.isSafeInteger(value)) {
46
+ throw new TypeError(`[thaizip] ${table} ${id}: expected safe integer for ${field}, got ${value}`);
47
+ }
48
+ }
43
49
  function typeName(value) {
44
50
  return value === null ? "null" : typeof value;
45
51
  }
@@ -66,7 +72,7 @@ function validateRawData(data) {
66
72
  if (p === null || typeof p !== "object") {
67
73
  throw new TypeError(`[thaizip] province[${i}]: expected object, got ${typeName(p)}`);
68
74
  }
69
- if (typeof p.id !== "number") throw typeErr("province", p.id, "id", "number", p.id);
75
+ assertId("province", p.id, "id", p.id);
70
76
  if (provinceById.has(p.id)) throw new TypeError(`[thaizip] duplicate province id: ${p.id}`);
71
77
  provinceById.set(p.id, p);
72
78
  if (typeof p.name_th !== "string") throw typeErr("province", p.id, "name_th", "string", p.name_th);
@@ -80,12 +86,12 @@ function validateRawData(data) {
80
86
  if (a === null || typeof a !== "object") {
81
87
  throw new TypeError(`[thaizip] amphure[${i}]: expected object, got ${typeName(a)}`);
82
88
  }
83
- if (typeof a.id !== "number") throw typeErr("amphure", a.id, "id", "number", a.id);
89
+ assertId("amphure", a.id, "id", a.id);
84
90
  if (amphureById.has(a.id)) throw new TypeError(`[thaizip] duplicate amphure id: ${a.id}`);
85
91
  amphureById.set(a.id, a);
86
92
  if (typeof a.name_th !== "string") throw typeErr("amphure", a.id, "name_th", "string", a.name_th);
87
93
  if (typeof a.name_en !== "string") throw typeErr("amphure", a.id, "name_en", "string", a.name_en);
88
- if (typeof a.province_id !== "number") throw typeErr("amphure", a.id, "province_id", "number", a.province_id);
94
+ assertId("amphure", a.id, "province_id", a.province_id);
89
95
  if (a.name_th.length > MAX_NAME_LENGTH) throw new RangeError(`[thaizip] amphure ${a.id}: name_th too long`);
90
96
  if (a.name_en.length > MAX_NAME_LENGTH) throw new RangeError(`[thaizip] amphure ${a.id}: name_en too long`);
91
97
  }
@@ -98,7 +104,7 @@ function validateRawData(data) {
98
104
  if (t === null || typeof t !== "object") {
99
105
  throw new TypeError(`[thaizip] tambon[${i}]: expected object, got ${typeName(t)}`);
100
106
  }
101
- if (typeof t.id !== "number") throw typeErr("tambon", t.id, "id", "number", t.id);
107
+ assertId("tambon", t.id, "id", t.id);
102
108
  if (seenTambonIds.has(t.id)) throw new TypeError(`[thaizip] duplicate tambon id: ${t.id}`);
103
109
  seenTambonIds.add(t.id);
104
110
  if (typeof t.name_th !== "string") throw typeErr("tambon", t.id, "name_th", "string", t.name_th);
@@ -109,7 +115,7 @@ function validateRawData(data) {
109
115
  if (zipType !== "string" && zipType !== "number") {
110
116
  throw typeErr("tambon", t.id, "zip_code", "string or number", t.zip_code);
111
117
  }
112
- if (typeof t.amphure_id !== "number") throw typeErr("tambon", t.id, "amphure_id", "number", t.amphure_id);
118
+ assertId("tambon", t.id, "amphure_id", t.amphure_id);
113
119
  const zipLength = String(t.zip_code).length;
114
120
  if (zipLength > MAX_ZIP_LENGTH) throw new RangeError(`[thaizip] tambon ${t.id}: zip_code too long`);
115
121
  if (t.deleted_at) continue;
@@ -419,7 +425,7 @@ function searchThaiAddress(index, query, options) {
419
425
  if (ZIP_CODE_RE.test(normalized)) {
420
426
  if (normalized.length < 2) return [];
421
427
  return lookupByZipCode(index, normalized, {
422
- zipLimit: options?.zipLimit ?? Infinity
428
+ zipLimit: options?.zipLimit ?? (normalized.length >= 5 ? Infinity : limit)
423
429
  });
424
430
  }
425
431
  if (normalized.length < 3) return [];
@@ -571,5 +577,3 @@ function resolveThaiAddress(record) {
571
577
  }
572
578
 
573
579
  export { applyRomanizationAliases, buildThaiAddressIndex, formatThaiAddressSuggestion, listAmphures, listProvinces, listTambons, lookupByZipCode, normalizeThaiAddressText, resolveThaiAddress, searchThaiAddress, validateRawData };
574
- //# sourceMappingURL=index.js.map
575
- //# sourceMappingURL=index.js.map
package/dist/react.cjs CHANGED
@@ -225,7 +225,7 @@ function searchThaiAddress(index, query, options) {
225
225
  if (ZIP_CODE_RE.test(normalized)) {
226
226
  if (normalized.length < 2) return [];
227
227
  return lookupByZipCode(index, normalized, {
228
- zipLimit: options?.zipLimit ?? Infinity
228
+ zipLimit: options?.zipLimit ?? (normalized.length >= 5 ? Infinity : limit)
229
229
  });
230
230
  }
231
231
  if (normalized.length < 3) return [];
@@ -402,5 +402,3 @@ function useThaiAddressAutocomplete(options) {
402
402
  }
403
403
 
404
404
  exports.useThaiAddressAutocomplete = useThaiAddressAutocomplete;
405
- //# sourceMappingURL=react.cjs.map
406
- //# sourceMappingURL=react.cjs.map
package/dist/react.d.cts CHANGED
@@ -79,6 +79,7 @@ type UseThaiAddressAutocompleteOptions = {
79
79
  limit?: number;
80
80
  debounce?: number;
81
81
  threshold?: number;
82
+ /** Maximum results for zip code queries; see `SearchOptions.zipLimit` for the default. */
82
83
  zipLimit?: number;
83
84
  /** Seed value for `query` on first render. Defaults to `''`. */
84
85
  initialQuery?: string;
package/dist/react.d.ts CHANGED
@@ -79,6 +79,7 @@ type UseThaiAddressAutocompleteOptions = {
79
79
  limit?: number;
80
80
  debounce?: number;
81
81
  threshold?: number;
82
+ /** Maximum results for zip code queries; see `SearchOptions.zipLimit` for the default. */
82
83
  zipLimit?: number;
83
84
  /** Seed value for `query` on first render. Defaults to `''`. */
84
85
  initialQuery?: string;
package/dist/react.js CHANGED
@@ -223,7 +223,7 @@ function searchThaiAddress(index, query, options) {
223
223
  if (ZIP_CODE_RE.test(normalized)) {
224
224
  if (normalized.length < 2) return [];
225
225
  return lookupByZipCode(index, normalized, {
226
- zipLimit: options?.zipLimit ?? Infinity
226
+ zipLimit: options?.zipLimit ?? (normalized.length >= 5 ? Infinity : limit)
227
227
  });
228
228
  }
229
229
  if (normalized.length < 3) return [];
@@ -400,5 +400,3 @@ function useThaiAddressAutocomplete(options) {
400
400
  }
401
401
 
402
402
  export { useThaiAddressAutocomplete };
403
- //# sourceMappingURL=react.js.map
404
- //# sourceMappingURL=react.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thaizip",
3
- "version": "0.7.6",
3
+ "version": "0.7.8",
4
4
  "description": "Fast, fuzzy Thai address autocomplete library (headless + React)",
5
5
  "keywords": [
6
6
  "thai",
@@ -55,17 +55,17 @@
55
55
  "typecheck": "tsc --noEmit"
56
56
  },
57
57
  "devDependencies": {
58
- "@testing-library/react": "^16.3.2",
59
- "@types/node": "^25.5.2",
60
- "@types/react": "^19.2.14",
61
- "@vitest/coverage-v8": "^4.1.11",
62
- "jsdom": "^29.0.1",
63
- "react": "^19.2.4",
64
- "react-dom": "^19.2.4",
58
+ "@testing-library/react": "^16.3.3",
59
+ "@types/node": "^26.6.5",
60
+ "@types/react": "^19.3.0",
61
+ "@vitest/coverage-v8": "^5.0.4",
62
+ "jsdom": "^30.1.2",
63
+ "react": "^19.3.0",
64
+ "react-dom": "^19.3.0",
65
65
  "tsup": "^8.5.1",
66
- "tsx": "^4.23.13",
66
+ "tsx": "^4.23.15",
67
67
  "typescript": "^6.0.2",
68
- "vitest": "^4.1.11"
68
+ "vitest": "^5.0.4"
69
69
  },
70
70
  "overrides": {
71
71
  "esbuild": "^0.28.1"