|
1 | 1 |
|
2 | | -# Harfizer - Number, Date & Time to Persian Words |
3 | | -**تبدیل عدد، تاریخ و زمان به حروف فارسی با TypeScript** |
4 | 2 |
|
5 | | -📘 English & فارسی Documentation |
| 3 | +# Harfizer — Say Your Numbers, Dates, and Times Out Loud (in Any Language!) |
6 | 4 |
|
7 | | ---- |
8 | | - |
9 | | -## 📦 Overview | نمای کلی |
| 5 | +**Harfizer** is a powerful package for converting numbers, dates, and times into words. It supports multiple languages including [🇮🇷 Persian](./docs/persian.md), [🇫🇷 French](./docs/french.md), [🇯🇵 Japanese](./docs/japanese.md), [🇨🇳 Chinese](./docs/chinese.md), [🇷🇺 Russian](./docs/russian.md), [🇩🇪 German](./docs/german.md), and [🇪🇸 Spanish](./docs/spanish.md). By using language-specific plugins, Harfizer allows you to easily convert numeric and temporal values into their textual representations. |
| 6 | +If you prefer a language other than English, please check the respective documentation by clicking on the links provided above or at the end of this document. |
10 | 7 |
|
11 | | -**Harfizer** is a modern, TypeScript-native package for converting numbers into their **Persian word** representation. |
12 | | -It supports integers, decimals, and negative numbers with rich customization options (such as separators, lexicons, and decimal suffixes). |
13 | 8 |
|
14 | | -In addition to number conversion, Harfizer now supports converting dates and digital time strings into Persian words. |
15 | | -This means you can now convert both dates (Solar/Jalali or Gregorian) and digital time (HH:mm) to their Persian word equivalents. |
| 9 | +## Table of Contents |
| 10 | +- [Harfizer — Say Your Numbers, Dates, and Times Out Loud (in Any Language!)](#harfizer--say-your-numbers-dates-and-times-out-loud-in-any-language) |
| 11 | + - [Table of Contents](#table-of-contents) |
| 12 | + - [Installation](#installation) |
| 13 | + - [Usage](#usage) |
| 14 | + - [Methods](#methods) |
| 15 | + - [`convertNumber(input: InputNumber, options?: ConversionOptions): string`](#convertnumberinput-inputnumber-options-conversionoptions-string) |
| 16 | + - [`convertTripleToWords(num: InputNumber, lexicon?: any, _separator?: string): string`](#converttripletowordsnum-inputnumber-lexicon-any-_separator-string-string) |
| 17 | + - [`convertDateToWords(dateStr: string, calendar?: "jalali" | "gregorian"): string`](#convertdatetowordsdatestr-string-calendar-jalali--gregorian-string) |
| 18 | + - [`convertTimeToWords(timeStr: string): string`](#converttimetowordstimestr-string-string) |
| 19 | + - [Examples](#examples) |
| 20 | + - [Additional Options](#additional-options) |
| 21 | + - [Other Language Plugins Documentation](#other-language-plugins-documentation) |
| 22 | + - [License](#license) |
16 | 23 |
|
17 | | -**Harfizer** یک پکیج تایپاسکریپتی مدرن برای تبدیل اعداد به **حروف فارسی** است. |
18 | | -این پکیج از اعداد صحیح، اعشاری و منفی پشتیبانی میکند و با ارائه گزینههای متنوع، امکان سفارشیسازی کامل خروجی (مانند جداکنندهها، واژگان و پسوندهای اعشاری) را فراهم میسازد. |
| 24 | +## Installation |
19 | 25 |
|
20 | | -همچنین، Harfizer از تبدیل تاریخها و زمان دیجیتال به حروف فارسی پشتیبانی میکند. |
21 | | -این بدان معناست که اکنون میتوانید تاریخ (شمسی/میلادی) و زمان (به فرمت HH:mm) را به معادل حروف فارسی آنها تبدیل کنید. |
22 | | - |
23 | | ---- |
24 | | - |
25 | | -## 🛠 Installation | نصب |
| 26 | +Install Harfizer via npm: |
26 | 27 |
|
27 | 28 | ```bash |
28 | 29 | npm install harfizer |
29 | 30 | ``` |
30 | 31 |
|
31 | | ---- |
32 | | - |
33 | | -## 🚀 Basic Usage | استفاده ساده |
| 32 | +## Usage |
34 | 33 |
|
35 | | -### Converting Numbers | تبدیل عدد |
| 34 | +Import the plugin and the `CoreConverter` from the package: |
36 | 35 |
|
37 | | -```ts |
38 | | -import { HarfizerConverter } from 'harfizer'; |
| 36 | +```typescript |
| 37 | +import { CoreConverter, EnglishLanguagePlugin } from 'harfizer'; |
39 | 38 |
|
40 | | -console.log(HarfizerConverter.toWords(1234)); |
41 | | -// Output: "یک هزار و دویست و سی و چهار" |
| 39 | +const englishPlugin = new EnglishLanguagePlugin(); |
| 40 | +const converter = new CoreConverter(englishPlugin); |
42 | 41 | ``` |
43 | 42 |
|
44 | | -```ts |
45 | | -console.log(HarfizerConverter.toWords("10500.25")); |
46 | | -// Output: "ده هزار و پانصد ممیز بیست و پنج صدم" |
47 | | -``` |
48 | | - |
49 | | -### Converting Dates | تبدیل تاریخ |
50 | | - |
51 | | -```ts |
52 | | -import { HarfizerConverter } from 'harfizer'; |
| 43 | +## Methods |
53 | 44 |
|
54 | | -const converter = new HarfizerConverter(); |
55 | | - |
56 | | -// Convert a Jalali (Solar) date: |
57 | | -console.log(converter.convertDateToWords("1404-03-24")); |
58 | | -// Expected Output: "بیست و چهار خرداد یک هزار و چهارصد و چهار" |
59 | | - |
60 | | -// Convert a Gregorian date: |
61 | | -console.log(converter.convertDateToWords("2023-04-05", "gregorian")); |
62 | | -// Expected Output: "پنج آوریل دو هزار و بیست و سه" |
63 | | -``` |
| 45 | +### `convertNumber(input: InputNumber, options?: ConversionOptions): string` |
| 46 | +Converts a given number (integer or decimal, possibly negative) into its English textual form. It handles the fractional part digit-by-digit using the word "point". |
64 | 47 |
|
65 | | -### Converting Time | تبدیل زمان |
| 48 | +**Parameters:** |
| 49 | +- **input:** A number, numeric string, or bigint. |
| 50 | +- **options (optional):** An object to customize conversion: |
| 51 | + - `customZeroWord` – overrides the default word for zero. |
| 52 | + - `customNegativeWord` – overrides the default negative word. |
| 53 | + - `customSeparator` – overrides the default token separator. |
66 | 54 |
|
67 | | -```ts |
68 | | -import { HarfizerConverter } from 'harfizer'; |
| 55 | +**Returns:** |
| 56 | +A string representing the number in words. |
69 | 57 |
|
70 | | -const converter = new HarfizerConverter(); |
| 58 | +**Example:** |
71 | 59 |
|
72 | | -console.log(converter.convertTimeToWords("09:05")); |
73 | | -// Expected Output: "ساعت نه و پنج دقیقه" |
| 60 | +```typescript |
| 61 | +converter.convertNumber("123"); |
| 62 | +// Output: "one hundred twenty-three" |
74 | 63 |
|
75 | | -console.log(converter.convertTimeToWords("18:00")); |
76 | | -// Expected Output: "ساعت هجده" |
| 64 | +converter.convertNumber("-456.78"); |
| 65 | +// Output: "minus four hundred fifty-six point seven eight" |
77 | 66 | ``` |
78 | 67 |
|
79 | 68 | --- |
80 | 69 |
|
81 | | -## ➖ Negative Numbers | اعداد منفی |
| 70 | +### `convertTripleToWords(num: InputNumber, lexicon?: any, _separator?: string): string` |
| 71 | +Converts a three-digit (or fewer) number into its English textual representation. |
82 | 72 |
|
83 | | -```ts |
84 | | -console.log(HarfizerConverter.toWords(-72)); |
85 | | -// Output: "منفی هفتاد و دو" |
86 | | -``` |
87 | | - |
88 | | -```ts |
89 | | -console.log(HarfizerConverter.toWords("-0.01")); |
90 | | -// Output: "منفی یک صدم" |
91 | | -``` |
92 | | - |
93 | | ---- |
94 | | - |
95 | | -## ⚙️ Custom Options | تنظیمات سفارشی |
| 73 | +**Parameters:** |
| 74 | +- **num:** A numeric value (up to 3 digits). |
96 | 75 |
|
97 | | -You can customize the output of Harfizer using the following options: |
| 76 | +**Returns:** |
| 77 | +A string representing the three-digit number (e.g., "four hundred fifty-six"). |
98 | 78 |
|
99 | | -| Option | Type | Default | توضیح فارسی | |
100 | | -|------------------------|--------------|--------------|----------------------------------------| |
101 | | -| `useNegativeWord` | `boolean` | `true` | استفاده از کلمه "منفی" | |
102 | | -| `customSeparator` | `string` | `" و "` | جداکننده بین بخشها | |
103 | | -| `customLexicon` | `Lexicon` | پیشفرض فارسی | واژگان سفارشی | |
104 | | -| `customDecimalSuffixes`| `string[]` | پیشفرض فارسی | پسوندهای اعشاری | |
105 | | -| `customNegativeWord` | `string` | `"منفی "` | واژه منفی دلخواه | |
106 | | -| `customZeroWord` | `string` | `"صفر"` | معادل صفر دلخواه | |
107 | | -| `customTimePrefix` | `string` | `"ساعت"` | پیشوند زمان برای تبدیل زمان | |
| 79 | +**Example:** |
108 | 80 |
|
109 | | -### Examples | مثالها |
110 | | - |
111 | | -#### ✅ `useNegativeWord` |
112 | | - |
113 | | -```ts |
114 | | -HarfizerConverter.toWords(-45, { useNegativeWord: false }); |
115 | | -// خروجی: "چهل و پنج" |
116 | | -``` |
117 | | - |
118 | | -#### ✅ `customSeparator` |
119 | | - |
120 | | -```ts |
121 | | -HarfizerConverter.toWords(123456, { customSeparator: "، " }); |
122 | | -// خروجی: "یکصد و بیست و سه هزار، چهارصد و پنجاه و شش" |
| 81 | +```typescript |
| 82 | +converter.convertTripleToWords(789); |
| 83 | +// Output: "seven hundred eighty-nine" |
123 | 84 | ``` |
124 | 85 |
|
125 | | -#### ✅ `customNegativeWord` |
126 | | - |
127 | | -```ts |
128 | | -HarfizerConverter.toWords(-12, { customNegativeWord: "عدد منفی " }); |
129 | | -// خروجی: "عدد منفی دوازده" |
130 | | -``` |
131 | | - |
132 | | -#### ✅ `customZeroWord` |
133 | | - |
134 | | -```ts |
135 | | -HarfizerConverter.toWords(0, { customZeroWord: "هیچ" }); |
136 | | -// خروجی: "هیچ" |
137 | | -``` |
138 | | - |
139 | | -#### ✅ `customDecimalSuffixes` |
140 | | - |
141 | | -```ts |
142 | | -HarfizerConverter.toWords("0.04", { |
143 | | - customDecimalSuffixes: ["", "دهم", "صدم", "هزارم", "دههزارم"] |
144 | | -}); |
145 | | -// خروجی: "چهار صدم" |
146 | | -``` |
147 | | - |
148 | | -#### ✅ `customLexicon` |
149 | | - |
150 | | -```ts |
151 | | -const funnyLexicon = [...]; // واژگان سفارشی خودتان |
152 | | - |
153 | | -HarfizerConverter.toWords(12, { customLexicon: funnyLexicon }); |
154 | | -// خروجی: بر اساس واژگان سفارشی |
155 | | -``` |
156 | | - |
157 | | -#### ✅ `customTimePrefix` (برای تبدیل زمان) |
158 | | - |
159 | | -```ts |
160 | | -import { HarfizerConverter, ConversionOptions } from 'harfizer'; |
| 86 | +--- |
161 | 87 |
|
162 | | -const options: ConversionOptions = { customTimePrefix: "زمان" }; |
163 | | -const customConverter = new HarfizerConverter(options); |
| 88 | +### `convertDateToWords(dateStr: string, calendar?: "jalali" | "gregorian"): string` |
| 89 | +Converts a Gregorian date string (formatted as "YYYY/MM/DD" or "YYYY-MM-DD") into its English textual representation in the format "Month Day, Year". |
164 | 90 |
|
165 | | -console.log(customConverter.convertTimeToWords("09:05")); |
166 | | -// خروجی مورد انتظار: "زمان نه و پنج دقیقه" |
167 | | -``` |
| 91 | +**Parameters:** |
| 92 | +- **dateStr:** The date string. |
| 93 | +- **calendar (optional):** For English, use "gregorian" (default is "gregorian"). |
168 | 94 |
|
169 | | ---- |
| 95 | +**Returns:** |
| 96 | +A string representing the date in words. |
170 | 97 |
|
171 | | -## 🧱 Lexicon Structure | ساختار واژگان |
| 98 | +**Example:** |
172 | 99 |
|
173 | | -```ts |
174 | | -type Lexicon = [ |
175 | | - units[], // یک تا نه |
176 | | - tenToTwenty[], // ده تا بیست |
177 | | - tens[], // بیست تا نود |
178 | | - hundreds[], // یکصد تا نهصد |
179 | | - scales[] // هزار، میلیون، میلیارد و ... |
180 | | -]; |
| 100 | +```typescript |
| 101 | +converter.convertDateToWords("2023/04/05"); |
| 102 | +// Output: "April 5, two thousand twenty-three" |
181 | 103 | ``` |
182 | 104 |
|
183 | 105 | --- |
184 | 106 |
|
185 | | -## 💼 Advanced Usage | استفاده پیشرفته |
| 107 | +### `convertTimeToWords(timeStr: string): string` |
| 108 | +Converts a time string in "HH:mm" format into its English textual representation. |
| 109 | +If minutes are zero, returns "It is <hour> o'clock"; otherwise, returns "It is <hour> o'clock and <minute> minutes". |
186 | 110 |
|
187 | | -### Instance-based usage: |
| 111 | +**Parameters:** |
| 112 | +- **timeStr:** A time string in "HH:mm" format. |
188 | 113 |
|
189 | | -```ts |
190 | | -const converter = new HarfizerConverter({ customZeroWord: "هیچ" }); |
| 114 | +**Returns:** |
| 115 | +A string representing the time in words. |
191 | 116 |
|
192 | | -console.log(converter.convert("0.75")); |
193 | | -// خروجی: "هفتاد و پنج صدم" |
194 | | -``` |
| 117 | +**Example:** |
195 | 118 |
|
196 | | -### Convert Triple Digits Only | تبدیل تنها ارقام سه رقمی |
| 119 | +```typescript |
| 120 | +converter.convertTimeToWords("09:00"); |
| 121 | +// Output: "It is nine o'clock" |
197 | 122 |
|
198 | | -```ts |
199 | | -console.log(converter.convertTripleToWords(215)); |
200 | | -// خروجی: "دویست و پانزده" |
| 123 | +converter.convertTimeToWords("09:05"); |
| 124 | +// Output: "It is nine o'clock and five minutes" |
201 | 125 | ``` |
202 | 126 |
|
203 | | ---- |
| 127 | +## Examples |
204 | 128 |
|
205 | | -## 📆 Date Conversion | تبدیل تاریخ |
| 129 | +Here is an example of using the `EnglishLanguagePlugin` with `CoreConverter`: |
206 | 130 |
|
207 | | -Harfizer supports converting dates to their Persian word representation using the `convertDateToWords` method. |
208 | | -This method accepts a date string in either `YYYY/MM/DD` or `YYYY-MM-DD` format and an optional calendar type: |
209 | | -- `"jalali"` for Solar dates (default) |
210 | | -- `"gregorian"` for Gregorian dates |
| 131 | +```typescript |
| 132 | +import { CoreConverter, EnglishLanguagePlugin } from 'harfizer'; |
211 | 133 |
|
212 | | -```ts |
213 | | -import { HarfizerConverter } from 'harfizer'; |
| 134 | +const englishPlugin = new EnglishLanguagePlugin(); |
| 135 | +const converter = new CoreConverter(englishPlugin); |
214 | 136 |
|
215 | | -const converter = new HarfizerConverter(); |
| 137 | +console.log(converter.convertNumber("123")); |
| 138 | +// Output: "one hundred twenty-three" |
216 | 139 |
|
217 | | -// Convert a Jalali (Solar) date: |
218 | | -console.log(converter.convertDateToWords("1404-03-24")); |
219 | | -// Expected Output: "بیست و چهار خرداد یک هزار و چهارصد و چهار" |
| 140 | +console.log(converter.convertDateToWords("2023/04/05")); |
| 141 | +// Output: "April 5, two thousand twenty-three" |
220 | 142 |
|
221 | | -// Convert a Gregorian date: |
222 | | -console.log(converter.convertDateToWords("2023-04-05", "gregorian")); |
223 | | -// Expected Output: "پنج آوریل دو هزار و بیست و سه" |
| 143 | +console.log(converter.convertTimeToWords("09:05")); |
| 144 | +// Output: "It is nine o'clock and five minutes" |
224 | 145 | ``` |
225 | 146 |
|
226 | | ---- |
227 | | - |
228 | | -## ⏰ Time Conversion | تبدیل زمان |
| 147 | +## Additional Options |
229 | 148 |
|
230 | | -Harfizer also converts digital time strings to Persian words using the `convertTimeToWords` method. |
231 | | -It accepts a time string in the format `"HH:mm"` and returns its Persian word representation. |
232 | | -A custom time prefix can be provided via `customTimePrefix` (default is `"ساعت"`). |
| 149 | +The `convertNumber` method accepts an optional `ConversionOptions` object for customizing conversion: |
233 | 150 |
|
234 | | -```ts |
235 | | -import { HarfizerConverter } from 'harfizer'; |
| 151 | +```typescript |
| 152 | +const options = { |
| 153 | + customZeroWord: "nil", |
| 154 | + customNegativeWord: "negative", |
| 155 | + customSeparator: " " |
| 156 | +}; |
236 | 157 |
|
237 | | -const converter = new HarfizerConverter(); |
| 158 | +console.log(converter.convertNumber("-123", options)); |
| 159 | +// Output: "negative one hundred twenty-three" |
| 160 | +``` |
238 | 161 |
|
239 | | -console.log(converter.convertTimeToWords("09:05")); |
240 | | -// Expected Output: "ساعت نه و پنج دقیقه" |
| 162 | +## Other Language Plugins Documentation |
241 | 163 |
|
242 | | -console.log(converter.convertTimeToWords("18:00")); |
243 | | -// Expected Output: "ساعت هجده" |
244 | | -``` |
| 164 | +For documentation on other language plugins, please refer to the following files: |
245 | 165 |
|
246 | | ---- |
| 166 | +- [🇬🇧 EnglishLanguagePlugin Documentation](./README.md) |
| 167 | +- [🇮🇷 PersianLanguagePlugin Documentation](./docs/persian.md) |
| 168 | +- [🇫🇷 FrenchLanguagePlugin Documentation](./docs/french.md) |
| 169 | +- [🇯🇵 JapaneseLanguagePlugin Documentation](./docs/japanese.md) |
| 170 | +- [🇨🇳 ChineseLanguagePlugin Documentation](./docs/chinese.md) |
| 171 | +- [🇷🇺 RussianLanguagePlugin Documentation](./docs/russian.md) |
| 172 | +- [🇩🇪 GermanLanguagePlugin Documentation](./docs/german.md) |
| 173 | +- [🇪🇸 SpanishLanguagePlugin Documentation](./docs/spanish.md) |
247 | 174 |
|
248 | | -## 📏 Limitations | محدودیتها |
249 | 175 |
|
250 | | -- **عدد:** حداکثر عدد ورودی 66 رقم (برای اعداد صحیح) |
251 | | -- **اعشار:** پشتیبانی از حداکثر 11 رقم اعشار |
252 | | -- **تاریخ:** فرمتهای پشتیبانی شده `YYYY/MM/DD` و `YYYY-MM-DD` |
253 | | -- **زمان:** فرمت پشتیبانی شده برای زمان تنها `"HH:mm"` است. |
254 | | -- ورودی فقط باید عددی (برای تبدیل عدد) یا رشتههای معتبر برای تاریخ و زمان باشد. |
255 | 176 |
|
256 | | ---- |
| 177 | +## License |
257 | 178 |
|
258 | | -## 📚 License | مجوز |
| 179 | +This package is licensed under the MIT License. |
259 | 180 |
|
260 | | -**MIT License** |
261 | | -کاملاً متنباز و رایگان برای استفاده تجاری و شخصی. |
0 commit comments