Skip to content

Commit 4506f6a

Browse files
committed
feat: add parseNepaliFormat method on NepaliDate
- updated README for parsing docs - renamed parse function to simpleParse (internal)
1 parent 8a82798 commit 4506f6a

File tree

8 files changed

+102
-18
lines changed

8 files changed

+102
-18
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ For the list of month names and their abbreviations, you can refer to the table
194194
| 10 | Fal | Falgun |
195195
| 11 | Cha | Chaitra |
196196

197+
#### Parsing Dates
198+
199+
You can parse a date-time string into a `NepaliDate` object using the following methods:
200+
201+
- `new NepaliDate(dateString, formatStr)`: Parses an Nepali date-time string according to the specified format.
202+
- `NepaliDate.parseNepaliFormat(dateStringNe, formatStr)`: Parses a Nepali date-time string in Devanagari form according to the specified format.
203+
- `NepaliDate.parseEnglishDate(dateString, formatStr)`: Parses an English date-time string according to the specified format.
204+
197205
#### Converting to JavaScript Date object
198206

199207
You can get the equivalent JavaScript `Date` object of a `NepaliDate` object using the `getDateObject()` method.

src/NepaliDate.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
nepaliDateToString,
99
} from './format'
1010

11-
import { parse, parseFormat, parseEnglishDateFormat } from './parse'
11+
import {
12+
simpleParse,
13+
parseFormat,
14+
parseEnglishDateFormat,
15+
parseNepaliFormat,
16+
} from './parse'
1217
import { getDate, getNepalDateAndTime } from './utils'
1318
import { validateTime } from './validators'
1419

@@ -170,7 +175,7 @@ class NepaliDate {
170175
}
171176

172177
private parseFromString(value: string) {
173-
const parsedResult = parse(value)
178+
const parsedResult = simpleParse(value)
174179
this.set(
175180
parsedResult[0], // Year
176181
parsedResult[1], // Month
@@ -250,7 +255,7 @@ class NepaliDate {
250255
*
251256
* @returns {Date} The equivalent JavaScript Date object.
252257
*/
253-
getDateObject() {
258+
getDateObject(): Date {
254259
return this.timestamp
255260
}
256261

@@ -624,8 +629,27 @@ class NepaliDate {
624629
}
625630

626631
/**
627-
* Creates a NepaliDate instance by parsing a provided English Date and Time string
628-
* with the given format.
632+
* Parses a Nepali date-time string in Devanagari form according to the
633+
* specified format and returns a `NepaliDate` instance.
634+
*
635+
* @param dateStringNe - The Nepali Date and time string in Devanagari.
636+
* @param format - The format of the provided date-time string.
637+
* @example
638+
* const dateStringNe = '२०८०/०८/१२ १४-०५-२३.७८९'
639+
* const format = 'YYYY/MM/DD HH-mm-ss.SSS'
640+
* const nepaliDate = NepaliDate.parseNepaliFormat(dateStringNe, format)
641+
*/
642+
static parseNepaliFormat(dateStringNe: string, format: string): NepaliDate {
643+
const [year, month0, day, hour, minute, second, ms] = parseNepaliFormat(
644+
dateStringNe,
645+
format
646+
)
647+
return new NepaliDate(year, month0, day, hour, minute, second, ms)
648+
}
649+
650+
/**
651+
* Parses an English date-time string according to the specified format
652+
* and returns a `NepaliDate` instance.
629653
*
630654
* @param dateString - The English Date and time string.
631655
* @param format - The format of the provided date-time string.

src/parse/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export { parseFormat, parse } from './parse'
1+
export { parseFormat, simpleParse } from './parse'
22
export { parseEnglishDateFormat } from './parseEnglishDate'
3+
export { parseNepaliFormat } from './parseNepaliFormat'

src/parse/parse.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
/**
2-
* parse.ts
3-
*
4-
* This module provides methods for parsing dates and times from strings.
5-
*
6-
* Functions:
7-
*
8-
* parse(dateTimeString)
9-
* - Parses date and time from the given string.
10-
*
11-
* Further extension is needed in this module as there are limited formats supported for parsing.
12-
* Developers should consider extending the module to support additional date and time formats.
2+
* This module provides features for parsing Nepali Date strings (in English).
133
*/
144

155
import {
@@ -92,7 +82,7 @@ function parseTimeString(timeString: string): number[] {
9282
* @throws {Error} if date or time string is invalid
9383
* @returns return array of date information [hour, minute, second, ms].
9484
*/
95-
export function parse(dateTimeString: string): number[] {
85+
export function simpleParse(dateTimeString: string): number[] {
9686
const [dateString, timeString] = dateTimeString.split(' ', 2)
9787
const [year, month0, day] = parseDateString(dateString)
9888
const [hour, minute, second, ms] = parseTimeString(timeString)

src/parse/parseEnglishDate.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* This module provides methods for parsing English Date strings.
3+
*/
4+
15
import {
26
ENGLISH_MONTHS_EN,
37
ENGLISH_MONTHS_SHORT_EN,

src/parse/parseNepaliFormat.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* This module provides methods for parsing Nepali Date strings (in Devanagari).
3+
*/
4+
5+
import {
6+
NEPALI_MONTHS_EN,
7+
NEPALI_MONTHS_NE,
8+
NUM_NE,
9+
WEEKDAYS_LONG_EN,
10+
WEEKDAYS_LONG_NE,
11+
WEEKDAYS_SHORT_EN,
12+
WEEKDAYS_SHORT_NE,
13+
} from '../constants'
14+
import { parseFormat } from './parse'
15+
16+
function convertNepaliStringToEnglish(dateStringNe: string): string {
17+
const regexLookup: Record<string, string> = {
18+
एम: 'AM',
19+
पिम: 'PM',
20+
}
21+
NEPALI_MONTHS_NE.forEach((ne, i) => (regexLookup[ne] = NEPALI_MONTHS_EN[i]))
22+
WEEKDAYS_LONG_NE.forEach((ne, i) => (regexLookup[ne] = WEEKDAYS_LONG_EN[i]))
23+
WEEKDAYS_SHORT_NE.forEach((ne, i) => (regexLookup[ne] = WEEKDAYS_SHORT_EN[i]))
24+
NUM_NE.forEach((ne, i) => (regexLookup[ne] = String(i)))
25+
26+
// create a regex pattern
27+
const pattern = new RegExp(Object.keys(regexLookup).join('|'), 'g')
28+
29+
// replace the match
30+
return dateStringNe.replace(pattern, match => regexLookup[match])
31+
}
32+
33+
export function parseNepaliFormat(dateStringNe: string, format: string): number[] {
34+
const convertedDateString = convertNepaliStringToEnglish(dateStringNe)
35+
return parseFormat(convertedDateString, format)
36+
}

tests/NepaliDate.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ describe('NepaliDate', () => {
6767
}).toThrow('Date out of range')
6868
})
6969

70+
it('should initialize by parsing Nepali Date string in Devanagari with given format', () => {
71+
const n1 = NepaliDate.parseNepaliFormat(
72+
'२०८०/०८/१२ १४-०५-२३.७८९',
73+
'YYYY/MM/DD HH-mm-ss.SSS'
74+
)
75+
expect(n1.toString()).toBe('2080-08-12 14:05:23.789')
76+
})
77+
7078
it('should initialize by parsing English Date string with given format', () => {
7179
const n1 = NepaliDate.parseEnglishDate(
7280
'2024 August 13 14-05-23.789',

tests/parse/parseNepaliFormat.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { parseNepaliFormat } from '../../src/parse'
2+
3+
describe('parseNepaliFormat', () => {
4+
// another test will be covered from NepaliDate.parseNepaliFormat test
5+
it('parses the Nepali date-time string in Devanagari', () => {
6+
expect(
7+
parseNepaliFormat(
8+
'साल: २०८०, महिना: जेठ, गते(दिन): ३२, बार: बिहिबार, बार(आधा): बिहि, समय: १:३४:५६ पिम',
9+
'साल: YYYY, महिना: MMMM, गते(दिन): DD, बार: dddd, बार(आधा): ddd, समय: hh:mm:ss A'
10+
)
11+
).toEqual([2080, 1, 32, 13, 34, 56, 0])
12+
})
13+
})

0 commit comments

Comments
 (0)