getLocationFromPostalCode resolves a 10-digit Iranian postal code to its state and city.
Function
Return type
LocationInfo on a match, null for anything invalid or unmatched. Unlike getPlaceByIranNationalId, it never returns undefined.
Behaviour
- Validate the code: must be exactly 10 digits, numeric only, and not all-repeating (e.g.
"1111111111"is rejected). - Take the first 5 digits as the prefix.
- Scan the internal range table and return the
state/cityof the first range whose[start, end]contains the prefix. - Return
nullif validation fails or the prefix matches no range.
Pitfalls
- Overlapping ranges resolve by table order, not by “closest match.” Prefix ranges for different cities in the same state can overlap, so the function returns whichever range appears first in the table. Always check the actual return value for a prefix rather than eyeballing the ranges — e.g.
"45138xxxxx"falls inside both the زنجان-city range (45131–45541) and the ابهر range (45511–45741), but since the زنجان range is listed first, that’s what you get back. - No Persian/Arabic digit normalization. Persian-digit input won’t match — pre-convert with
autoConvertDigitsToENfirst, same asgetPlaceByIranNationalId. - Can’t distinguish “malformed input” from “well-formed but unknown location” — both return
null. The internal validator isn’t exported separately. - Repeating-digit codes are always rejected, even if the prefix would otherwise fall inside a valid range.
Source
src/modules/getLocationFromPostalCode/index.ts, validator.ts, postalCodeRanges.skip.ts · Tests: test/getLocationFromPostalCode.spec.ts