country-state-city-js
Version:
A fast & light weight JS library to get countries geographical information including states, cities, iso codes, etc.
22 lines (20 loc) • 743 B
text/typescript
import { countryType, returnT, CityFnType } from "../types"
import { PATH, PATH_LITE } from "../utils"
export const City: CityFnType = cityHandler()
export const CityLite: CityFnType = cityHandler('lite')
function cityHandler(type?: 'lite'): CityFnType {
const fn: CityFnType = function (cointryIso2: countryType, stateIso: string): returnT {
if (!cointryIso2) {
throw new TypeError("Country iso2 type missing")
}
if (!stateIso) {
throw new TypeError("State iso type missing")
}
try {
return require(`${type === 'lite' ? PATH_LITE : PATH}/cities/${cointryIso2}/${stateIso}/index.json`)
} catch (e) {
return []
}
}
return fn
}