zim-utils
v1.0.3
Published
A collection of utility functions specifically for Zimbabwean data formatting and validation.
Downloads
3
Readme
zim-utils
A collection of utility functions specifically for Zimbabwean data formatting and validation.
Features
- Phone Number Formatting: Format Zimbabwean phone numbers to the international format.
- Zimbabwean ID Validation: Validate Zimbabwean National ID numbers in the format
XX-XXXXXXXXXX.
Installation
To install the zim-utils package, use npm:
npm install zim-utilsUsage
1. Phone Number Formatting
You can use the formatPhone function to format Zimbabwean phone numbers into the international format.
Function: formatPhone
import { formatPhone } from 'zim-utils';
const formattedNumber = formatPhone('0771234567');
console.log(formattedNumber); // Output: +263 77 123 4567This function takes a Zimbabwean phone number (e.g., 0771234567) and returns it in the international format with a country code of +263.
2. Zimbabwean ID Validation
You can use the isValidZimID function to validate Zimbabwean National IDs.
Function: isValidZimID
import { isValidZimID } from 'zim-utils';
const id = '38-34343444R38';
console.log(isValidZimID(id)); // true
const invalidId = '38-34343444R3';
console.log(isValidZimID(invalidId)); // falseThis function checks if a given Zimbabwean ID matches the expected format: XX-XXXXXXXXXX (two digits, followed by a hyphen, eight digits, one letter, and two digits at the end).
Functions Overview
formatPhone(phoneNumber: string): string
- Description: Formats Zimbabwean phone numbers to the international format
+263. - Arguments:
phoneNumber: A Zimbabwean phone number in the format07xxxxxxxx.
- Returns: A formatted phone number as a string in the international format.
isValidZimID(id: string): boolean
- Description: Validates Zimbabwean National IDs with the format
XX-XXXXXXXXXX. - Arguments:
id: A string representing the Zimbabwean ID in the formatXX-XXXXXXXXXX.
- Returns:
trueif the ID matches the format,falseotherwise.
Example
Here’s a complete example that demonstrates both functions:
import { formatPhone, isValidZimID } from 'zim-utils';
// Format a Zimbabwean phone number
const formattedPhone = formatPhone('0771234567');
console.log(formattedPhone); // +263 77 123 4567
// Validate a Zimbabwean National ID
const zimId = '38-34343444R38';
const isValid = isValidZimID(zimId);
console.log(isValid); // true
const invalidZimId = '38-34343444R3';
console.log(isValidZimID(invalidZimId)); // falseContribution
If you would like to contribute to this package, feel free to fork the repository, make your changes, and create a pull request. Any improvements or additions are welcome!
License
This package is licensed under the MIT License.
