Skip to content

Commit 380c08c

Browse files
committed
last build version
1 parent b4d7ed1 commit 380c08c

3 files changed

Lines changed: 164 additions & 0 deletions

File tree

dist/harfizer.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type Lexicon = string[][];
1515
* @property {string[]} [customDecimalSuffixes] - Custom array of suffixes for the fractional part.
1616
* @property {string} [customNegativeWord] - Custom word to denote negative numbers.
1717
* @property {string} [customZeroWord] - Custom word to represent zero.
18+
* @property {string} [customTimePrefix] - Custom prefix for time conversion (e.g. "ساعت" or any other text).
1819
*/
1920
export interface ConversionOptions {
2021
useNegativeWord?: boolean;
@@ -23,6 +24,7 @@ export interface ConversionOptions {
2324
customDecimalSuffixes?: string[];
2425
customNegativeWord?: string;
2526
customZeroWord?: string;
27+
customTimePrefix?: string;
2628
}
2729
/**
2830
* Interface defining the conversion methods.
@@ -98,6 +100,23 @@ export declare class HarfizerConverter implements IConverter {
98100
* @throws {Error} Throws an error if the input format is invalid, the number is too large, or out of range.
99101
*/
100102
convert(input: InputNumber, options?: ConversionOptions): string;
103+
/**
104+
* Converts a date string (Solar/Jalali or Gregorian) to its word representation.
105+
*
106+
* @param {string} dateStr - The date string in the format 'YYYY/MM/DD' or 'YYYY-MM-DD'.
107+
* @param {'jalali' | 'gregorian'} [calendar='jalali'] - The calendar type, either 'jalali' or 'gregorian'.
108+
* @returns {string} The word representation of the date.
109+
* @throws {Error} Throws an error if the date format is invalid or the month is out of range.
110+
*/
111+
convertDateToWords(dateStr: string, calendar?: "jalali" | "gregorian"): string;
112+
/**
113+
* Converts a digital time string (HH:mm) to its Persian word representation.
114+
*
115+
* @param {string} timeStr - The time string in "HH:mm" format.
116+
* @returns {string} The word representation of the time.
117+
* @throws {Error} Throws an error if the time format is invalid or if time values are out of range.
118+
*/
119+
convertTimeToWords(timeStr: string): string;
101120
/**
102121
* Returns the default separator from the configuration or default constant.
103122
*

dist/harfizer.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,106 @@ class HarfizerConverter {
191191
: negativeWord;
192192
return ((isNegative ? negPrefix : "") + wordParts.join(separator) + fractionPart);
193193
}
194+
/**
195+
* Converts a date string (Solar/Jalali or Gregorian) to its word representation.
196+
*
197+
* @param {string} dateStr - The date string in the format 'YYYY/MM/DD' or 'YYYY-MM-DD'.
198+
* @param {'jalali' | 'gregorian'} [calendar='jalali'] - The calendar type, either 'jalali' or 'gregorian'.
199+
* @returns {string} The word representation of the date.
200+
* @throws {Error} Throws an error if the date format is invalid or the month is out of range.
201+
*/
202+
convertDateToWords(dateStr, calendar = "jalali") {
203+
// Split the date string by '/' or '-' and validate the format.
204+
const parts = dateStr.split(/[-\/]/);
205+
if (parts.length !== 3) {
206+
throw new Error("Invalid date format. Expected format 'YYYY/MM/DD' or 'YYYY-MM-DD'.");
207+
}
208+
const [yearStr, monthStr, dayStr] = parts;
209+
// Parse month as a number and validate it.
210+
const monthNum = parseInt(monthStr, 10);
211+
if (isNaN(monthNum) || monthNum < 1 || monthNum > 12) {
212+
throw new Error("Invalid month in date.");
213+
}
214+
// Define month names for Jalali and Gregorian calendars.
215+
const jalaliMonths = [
216+
"فروردین",
217+
"اردیبهشت",
218+
"خرداد",
219+
"تیر",
220+
"مرداد",
221+
"شهریور",
222+
"مهر",
223+
"آبان",
224+
"آذر",
225+
"دی",
226+
"بهمن",
227+
"اسفند",
228+
];
229+
const gregorianMonths = [
230+
"ژانویه",
231+
"فوریه",
232+
"مارس",
233+
"آوریل",
234+
"مه",
235+
"ژوئن",
236+
"ژوئیه",
237+
"اوت",
238+
"سپتامبر",
239+
"اکتبر",
240+
"نوامبر",
241+
"دسامبر",
242+
];
243+
// Get the month name based on the calendar type.
244+
const monthName = calendar === "gregorian"
245+
? gregorianMonths[monthNum - 1]
246+
: jalaliMonths[monthNum - 1];
247+
// Convert the day and year to words using the convert method.
248+
const dayWords = this.convert(dayStr);
249+
const yearWords = this.convert(yearStr);
250+
// Construct and return the final word representation of the date.
251+
return `${dayWords} ${monthName} ${yearWords}`;
252+
}
253+
/**
254+
* Converts a digital time string (HH:mm) to its Persian word representation.
255+
*
256+
* @param {string} timeStr - The time string in "HH:mm" format.
257+
* @returns {string} The word representation of the time.
258+
* @throws {Error} Throws an error if the time format is invalid or if time values are out of range.
259+
*/
260+
convertTimeToWords(timeStr) {
261+
// Split the time string using colon (":") as the separator.
262+
const parts = timeStr.split(":");
263+
if (parts.length !== 2) {
264+
throw new Error("Invalid time format. Expected format 'HH:mm'.");
265+
}
266+
const [hourStr, minuteStr] = parts;
267+
// Parse hours and minutes.
268+
const hour = parseInt(hourStr, 10);
269+
const minute = parseInt(minuteStr, 10);
270+
if (isNaN(hour) || isNaN(minute)) {
271+
throw new Error("Invalid time format. Hours and minutes should be numbers.");
272+
}
273+
// Validate hour and minute ranges.
274+
if (hour < 0 || hour > 23) {
275+
throw new Error("Invalid hour value. Hour should be between 0 and 23.");
276+
}
277+
if (minute < 0 || minute > 59) {
278+
throw new Error("Invalid minute value. Minute should be between 0 and 59.");
279+
}
280+
// Retrieve the custom time prefix from configuration or use the default "ساعت".
281+
const timePrefix = this.config.customTimePrefix ?? "ساعت";
282+
// Convert the hour and minute values to their word representations.
283+
const hourWords = this.convert(hour);
284+
const minuteWords = this.convert(minute);
285+
// Construct the final time string in words.
286+
// If minutes are zero, return only the hour part.
287+
if (minute === 0) {
288+
return `${timePrefix} ${hourWords}`;
289+
}
290+
else {
291+
return `${timePrefix} ${hourWords} و ${minuteWords} دقیقه`;
292+
}
293+
}
194294
/**
195295
* Returns the default separator from the configuration or default constant.
196296
*

dist/harfizer.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,49 @@ describe("HarfizerConverter", () => {
9292
// Expect custom zero word "zero"
9393
expect(zeroResult).toBe("zero");
9494
});
95+
describe("Date conversion", () => {
96+
test("Convert Jalali date to words using slash format (1404/03/24)", () => {
97+
const result = converter.convertDateToWords("1404/03/24");
98+
// Expected output: "بیست و چهار خرداد یک هزار و چهارصد و چهار"
99+
expect(result).toBe("بیست و چهار خرداد یک هزار و چهارصد و چهار");
100+
});
101+
test("Convert Jalali date to words using dash format (1404-03-24)", () => {
102+
const result = converter.convertDateToWords("1404-03-24");
103+
// Expected output: "بیست و چهار خرداد یک هزار و چهارصد و چهار"
104+
expect(result).toBe("بیست و چهار خرداد یک هزار و چهارصد و چهار");
105+
});
106+
test("Convert Gregorian date to words (2023-04-05)", () => {
107+
const result = converter.convertDateToWords("2023-04-05", "gregorian");
108+
// For day "05" -> "پنج" and month "04" -> "آوریل", year "2023" -> "دو هزار و بیست و سه"
109+
// Expected output: "پنج آوریل دو هزار و بیست و سه"
110+
expect(result).toBe("پنج آوریل دو هزار و بیست و سه");
111+
});
112+
});
113+
describe("convertTimeToWords", () => {
114+
let converter;
115+
beforeEach(() => {
116+
converter = new harfizer_1.HarfizerConverter();
117+
});
118+
test("should convert time '09:05' with default prefix", () => {
119+
const result = converter.convertTimeToWords("09:05");
120+
// Expected conversion: "ساعت نه و پنج دقیقه"
121+
expect(result).toBe("ساعت نه و پنج دقیقه");
122+
});
123+
test("should convert time '18:00' with default prefix", () => {
124+
const result = converter.convertTimeToWords("18:00");
125+
// Expected conversion: "ساعت هجده"
126+
expect(result).toBe("ساعت هجده");
127+
});
128+
test("should convert time with custom time prefix", () => {
129+
const customConverter = new harfizer_1.HarfizerConverter({
130+
customTimePrefix: "زمان",
131+
});
132+
const result = customConverter.convertTimeToWords("09:05");
133+
// Expected conversion with custom prefix: "زمان نه و پنج دقیقه"
134+
expect(result).toBe("زمان نه و پنج دقیقه");
135+
});
136+
test("should throw error for invalid time format", () => {
137+
expect(() => converter.convertTimeToWords("9-05")).toThrow("Invalid time format. Expected format 'HH:mm'.");
138+
});
139+
});
95140
});

0 commit comments

Comments
 (0)