khmer-utils
Version:
A Khmer utility library for number formatting, currency conversion, date localization, transliteration, and text normalization.
138 lines (96 loc) โข 3.1 kB
Markdown
A lightweight utility library to help developers format numbers, currencies, and dates using **Khmer Unicode**, along with utilities for transliteration, normalization, and Khmer-specific input validation.
Install via [npm](https://www.npmjs.com/):
```bash
npm install khmer-utils
```
- โ
Convert Arabic numbers to Khmer numerals
- โ
Format currency into Khmer Riel (`แ`) or `แแแ`
- โ
Format `Date` objects using Khmer digits
- โ
Normalize Khmer text (trim, reduce spaces, normalize Unicode)
- โ
Transliterate Khmer script to Latin phonetics
- โ
Validate Khmer phone numbers & national IDs
## ๐ Usage
### 1. Importing
```js
const {
formatNumber,
formatCurrency,
formatDate,
normalizeText,
khmerToLatin,
validateMobile,
validateLandline,
validateNationalID,
} = require("khmer-utils");
```
Convert Arabic numbers to Khmer numerals.
```js
formatNumber(12345); // "แกแขแฃแคแฅ"
formatNumber("123,456.78"); // "แกแขแฃ,แคแฅแฆ.แงแจ"
```
Format numbers as Khmer Riel currency.
```js
formatCurrency(12345);
// Output: "แกแข,แฃแคแฅแ"
formatCurrency(12345.678, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
currencySymbol: " แแแ",
});
// Output: "แกแข,แฃแคแฅ.แฆแจ แแแ"
```
Format JavaScript `Date` objects into `DD/MM/YYYY` using Khmer digits.
```js
formatDate(new Date(2023, 2, 15));
// Output: "แกแฅ/แฃ/แขแ แขแฃ"
formatDate(new Date(2025, 0, 1), { delimiter: "-" });
// Output: "แก-แก-แขแ แขแฅ"
```
Normalize Khmer or mixed-language text by:
- Removing extra spaces
- Lowercasing
- Unicode NFC normalization
```js
normalizeText(" แแฝแแแแธ แแทแแแแแ ");
// Output: "แแฝแแแแธ แแทแแแแแ"
```
Transliterate basic Khmer script to Latin characters.
```js
khmerToLatin("แแแแแ");
// Output: "kkhggng"
```
```js
validateMobile("+855-12-3456789"); // true
validateMobile("+855-123-456789"); // false
```
```js
validateLandline("+855-23-123456"); // true
validateLandline("+855-23-12345"); // false
```
```js
validateNationalID("1234567890"); // true
validateNationalID("123456789"); // false
```
MIT License
Feel free to reach out if you want to <a href="https://github.com/Seavleu/khmer-utils">contribute</a> or report <a href="https://github.com/Seavleu/khmer-utils/issues">issues</a>!
1. Fork this repo
2. Create a new branch: `git checkout -b my-change`
3. Make your changes
4. Submit a pull request
If you find this useful for Khmer development or localization, please โญ๏ธ the repo and share it!
Created with โค๏ธ by Seavleu Heang