Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
malaysia-postcodes
Advanced tools
List of Malaysia Postcodes with City and State (JSON)
Install the package from NPM.
npm i malaysia-postcodes
yarn add malaysia-postcodes
<!-- Regular version -->
<script src="https://cdn.jsdelivr.net/npm/malaysia-postcodes@2.2.0/dist/malaysia-postcodes.js"></script>
<!-- Minified version -->
<script src="https://cdn.jsdelivr.net/npm/malaysia-postcodes@2.2.0/dist/malaysia-postcodes.min.js"></script>
If you're using a module bundler like Webpack:
import {
allPostcodes,
getStates,
getCities,
findCities,
getPostcodes,
findPostcode,
} from "malaysia-postcodes";
Once you've included the library via the script tag, you can access its functions in two ways:
You can destructure the desired functions from window.malaysiaPostcodes
:
const {
allPostcodes,
getStates,
getCities,
findCities,
getPostcodes,
findPostcode
} = window.malaysiaPostcodes;
Alternatively, you can call the functions directly using the malaysiaPostcodes
object:
const postcodesData = malaysiaPostcodes.allPostcodes;
const states = malaysiaPostcodes.getStates();
const cities = malaysiaPostcodes.getCities("Kelantan");
const postcodes = malaysiaPostcodes.getPostcodes("Kelantan", "Pasir Mas");
const location = malaysiaPostcodes.findPostcode("17070");
Return all postcodes data with city and state
Example usage:
const postcodes = allPostcodes;
Example results:
[
{
"name": "Kelantan",
"city": [
{
"name": "Pasir Mas",
"postcode": [
"17000",
"17007",
"17009",
"17010",
"17020",
"17030",
"17040",
"17050",
"17060",
"17070"
]
},
...
]
},
...
]
Return all states data
Example usage:
const states = getStates();
Example results:
[
"Wp Kuala Lumpur",
"Johor",
"Kedah",
"Kelantan",
"Melaka",
"Negeri Sembilan",
"Pahang",
"Penang",
"Perak",
"Perlis",
"Sabah",
"Sarawak",
"Selangor",
"Terengganu",
"Wp Labuan",
"Wp Putrajaya",
];
Return all cities data based on selected state
Parameters:
selectedState
(string): The name of the state for which cities are to be retrieved.Example usage:
const cities = getCities("Kelantan");
Example results:
[
"Ayer Lanas",
"Bachok",
"Cherang Ruku",
"Dabong",
"Gua Musang",
"Jeli",
"Kem Desa Pahlawan",
"Ketereh",
"Kota Bharu",
"Kuala Balah",
"Kuala Krai",
"Machang",
"Melor",
"Pasir Mas",
"Pasir Puteh",
"Pulai Chondong",
"Rantau Panjang",
"Selising",
"Tanah Merah",
"Temangan",
"Tumpat",
"Wakaf Bharu",
];
Search for cities based on the provided query string.
Parameters:
true
, the function will search for cities that match the query exactly. If false
, it will search for cities that contain the query string. Default is true
.Example usage:
// For exact search
const cityDetailsExact = findCities("Pasir Mas");
// For non-exact search
const cityDetailsBroad = findCities("Kota", false);
Example result for an exact search:
{
"found": true,
"state": "Kelantan",
"city": "Pasir Mas",
"postcodes": ["17000", "17007", "17009", "17010", "17020", "17030", "17040", "17050", "17060", "17070"]
}
Example result for a non-exact search:
{
"found": true,
"results": [
{
"state": "Kelantan",
"city": "Kota Bharu",
"postcodes": ["15000", "15050", "15100", ...]
},
{
"state": "Sabah",
"city": "Kota Kinabalu",
"postcodes": ["88000", "88100", "88110", ...]
},
...
]
}
Example result if city not found:
{
"found": false
}
Return all postcodes data based on selected state and city
Parameters:
state
(string): The name of the state.city
(string): The name of the city within the specified state.Example usage:
const postcodes = getPostcodes("Kelantan", "Pasir Mas");
Example results:
[
"17000",
"17007",
"17009",
"17010",
"17020",
"17030",
"17040",
"17050",
"17060",
"17070",
];
Returns an array of all postcodes that start with the given prefix
Let's say you have a form where the user starts typing a postcode. As they type, you can call getPostcodesByPrefix
to dynamically show suggestions based on what the user has typed so far.
Parameters:
prefix
(string): The prefix you wish to search for. This should be a non-null string with a length between 1 and 5.Example usage:
const postcodes = getPostcodesByPrefix("170");
Example results:
[
'17000',
'17007',
'17009',
'17010',
'17020',
'17030',
'17040',
'17050',
'17060',
'17070'
];
Return state and city data based on postcode
Parameters:
postcode
(string): The postcode you wish to search for.exact
(boolean, optional): Determines the type of search. If true
(default), the function will search for an exact match of the provided postcode. If false
, it will search for postcodes that start with the given substring.Example usage:
const locationExact = findPostcode("17070");
const locationPartial = findPostcode("170", false);
Example result for an exact match when postcode is found:
{
"found": true,
"state": "Kelantan",
"city": "Pasir Mas",
"postcode": "17070",
}
Example result for a non-exact match:
{
"found": true,
"results": [
{
"state": "Wp Kuala Lumpur",
"city": "Kuala Lumpur",
"postcode": "51700"
},
{
"state": "Johor",
"city": "Pasir Gudang",
"postcode": "81700"
},
...
]
}
Example result if postcode is not found:
{
"found": false
}
Here are some practical examples demonstrating how to use the library:
Starting from version 1.1.0, malaysia-postcodes provides TypeScript type declarations out-of-the-box. This enhancement ensures a more developer-friendly experience for TypeScript users, offering better intellisense and type checking without requiring any additional installation steps.
For JavaScript users, this change won't affect your current implementation, and you can continue using the package as before.
The library is equipped with unit tests to ensure its functionality and reliability. We utilize the Jest testing framework for this purpose.
To run the tests, follow these steps:
1.Ensure you have all dependencies installed:
npm install
2.Run the test command:
npm test
For active development, you can run tests in watch mode. This will continuously monitor changes in the project and run tests accordingly, providing immediate feedback.
npm run test:watch
We strive for a high level of test coverage to ensure the library's integrity. After running tests, you can view a detailed coverage report by navigating to the coverage
directory in the project root.
npm run test:coverage
Note: The coverage
directory is not included in the repository as it is generated on-the-fly whenever tests are run with coverage.
If you spot any errors, typos or missing information, please submit a pull request.
ISC. See LICENSE for more details.
FAQs
List of Malaysia Postcodes with City and State (JSON)
The npm package malaysia-postcodes receives a total of 531 weekly downloads. As such, malaysia-postcodes popularity was classified as not popular.
We found that malaysia-postcodes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.