UNPKG

aus-postcode

Version:

A package containing utility functions around postcode definitions in Australia.

71 lines (70 loc) 1.52 kB
// src/constants/states.ts var australianCapitalTerritory = { name: "Australian Capital Territory", abbreviation: "ACT" }; var newSouthWales = { name: "New South Wales", abbreviation: "NSW" }; var victoria = { name: "Victoria", abbreviation: "VIC" }; var queensland = { name: "Queensland", abbreviation: "QLD" }; var southAustralia = { name: "South Australia", abbreviation: "SA" }; var westernAustralia = { name: "Western Australia", abbreviation: "WA" }; var tasmania = { name: "Tasmania", abbreviation: "TAS" }; var northernTerritory = { name: "Northern Territory", abbreviation: "NT" }; // src/index.ts var postcodeToPostalState = (postcode) => { if (/^02\d{2}$/.test(postcode)) { return australianCapitalTerritory; } if (/^26\d{2}$/.test(postcode) && parseInt(postcode) < 2619) { return australianCapitalTerritory; } if (/^29\d{2}$/.test(postcode) && parseInt(postcode) < 2921) { return australianCapitalTerritory; } if (/^[1|2]\d{3}$/.test(postcode)) { return newSouthWales; } if (/^[3|8]\d{3}$/.test(postcode)) { return victoria; } if (/^[4|9]\d{3}$/.test(postcode)) { return queensland; } if (/^5\d{3}$/.test(postcode)) { return southAustralia; } if (/^6\d{3}$/.test(postcode)) { return westernAustralia; } if (/^7\d{3}$/.test(postcode)) { return tasmania; } if (/^0[8|9]\d{2}$/.test(postcode)) { return northernTerritory; } throw new Error("Invalid postcode"); }; export { postcodeToPostalState };