Docs/@nepali-utils/core

@nepali-utils/core

Framework-agnostic TypeScript library for Bikram Sambat operations, location data, validation, and currency formatting.

Getting started

@nepali-utils/core is a zero-dependency TypeScript library providing utilities for the Bikram Sambat (Nepali) calendar system. It covers dates from BS 2000 to BS 2100 (AD 1943–2043) with high accuracy.

The library is organized into several modules:

  • Dates — Conversion, formatting, comparison, calendar data
  • Location — Provinces, districts, municipalities, wards for Nepal
  • Validation — Nepali mobile number validation with carrier detection
  • Currency — Nepali currency formatting and number-to-words

The library is framework-agnostic and works with any JavaScript/TypeScript project.

Installation

terminal
bash
1
npm install @nepali-utils/core
terminal
bash
1
pnpm add @nepali-utils/core
terminal
bash
1
yarn add @nepali-utils/core

Date conversion

Convert between Bikram Sambat and Gregorian (AD) dates. All conversion functions are accurate for BS 2000–2100 (AD 1943–2043).

BS → AD

1
2
3
4
import { bsToAd } from "@nepali-utils/core"
const date = bsToAd({ year: 2081, month: 5, day: 15 })
// → Date (JavaScript Date object)

AD → BS

1
2
3
4
import { adToBs } from "@nepali-utils/core"
const bsDate = adToBs(new Date())
// → { year: 2081, month: 5, day: 15 }

Today's date

1
2
3
4
import { getTodayBs } from "@nepali-utils/core"
const today = getTodayBs()
// → { year: 2081, month: 5, day: 15 }

Day of week

1
2
3
4
import { getBsDayOfWeek } from "@nepali-utils/core"
const dayOfWeek = getBsDayOfWeek({ year: 2081, month: 5, day: 15 })
// → 0 (Sunday) through 6 (Saturday)

Formatting

Format BS dates with custom patterns in English or Nepali. Pattern tokens include YYYY, YY, MMMM, MMM, MM, and DD.

Date formatting

1
2
3
4
5
6
7
8
9
10
11
12
13
import { formatBsDate } from "@nepali-utils/core"
formatBsDate(date, "en", { pattern: "YYYY MMMM DD" })
// → "2081 Bhadra 15"
formatBsDate(date, "ne", { pattern: "YYYY MMMM DD" })
// → "२०८१ भदौ १५"
formatBsDate(date, "en", { pattern: "YYYY-MM-DD" })
// → "2081-05-15"
formatBsDate(date, "en", { pattern: "MMMM DD, YYYY" })
// → "Bhadra 15, 2081"

Nepali numerals

1
2
3
4
5
6
import { toNepaliNumber, parseNepaliNumber } from "@nepali-utils/core"
toNepaliNumber(2081) // → "२०८१"
toNepaliNumber(12345) // → "१२३४५"
parseNepaliNumber("२०८१") // → 2081

Month navigation

1
2
3
4
5
6
7
import { getPrevMonth, getNextMonth, MONTHS } from "@nepali-utils/core"
getPrevMonth(2081, 5) // → { year: 2081, month: 4 }
getNextMonth(2081, 5) // → { year: 2081, month: 6 }
MONTHS.en // → ["Baisakh", "Jestha", "Ashad", ...]
MONTHS.ne // → ["बैशाख", "जेष्ठ", "असार", ...]

Calendar data

Generate calendar grids and query month/year data for building custom calendar UIs.

Calendar grid

1
2
3
4
5
import { generateCalendarGrid, type CalendarDay } from "@nepali-utils/core"
const grid: CalendarDay[] = generateCalendarGrid(2081, 5)
// Returns an array of CalendarDay objects with day, month, year,
// isCurrentMonth, isToday, dayOfWeek, and date properties.

Month/year info

1
2
3
4
5
6
7
8
9
10
import { getDaysInBsMonth, getDaysInBsYear, isValidBsYear, getBsYearRange, validateBsDate } from "@nepali-utils/core"
getDaysInBsMonth(2081, 5) // → number of days in Bhadra 2081 (e.g. 32)
getDaysInBsYear(2081) // → total days in BS year 2081 (e.g. 365)
isValidBsYear(2081) // → true
getBsYearRange() // → { min: 2000, max: 2100 }
// Validate a BS date — throws if invalid
validateBsDate(2081, 5, 15) // → undefined (valid)
validateBsDate(2081, 13, 1) // → throws "Invalid BS month: 13"

Date comparison

Compare BS dates with equality and ordering checks.

1
2
3
4
5
6
7
8
import { isSameBsDate, isBeforeBsDate, isAfterBsDate } from "@nepali-utils/core"
const d1 = { year: 2081, month: 5, day: 15 }
const d2 = { year: 2081, month: 6, day: 1 }
isSameBsDate(d1, d1) // → true
isBeforeBsDate(d1, d2) // → true
isAfterBsDate(d2, d1) // → true

Location data

Complete dataset for Nepal's administrative divisions including all 7 provinces, 77 districts, municipalities, and wards. All data includes English and Nepali names.

Provinces

All 7 provinces with Nepali and English names, identified by UUID:

1
2
3
4
5
6
7
8
9
import { provinces, getProvinceById, getProvinceByNameEn } from "@nepali-utils/core"
provinces // → ProvinceInfo[] (all 7 provinces)
getProvinceById("1a2d297a-b4d4-4062-9801-62813c324753")
// → { id: "...", name: "कोशी प्रदेश", nameEn: "Koshi Province" }
getProvinceByNameEn("Bagmati Province")
// → { id: "...", name: "बागमती प्रदेश", nameEn: "Bagmati Province" }

Districts

All 77 districts, each linked to a province via provinceId:

1
2
3
4
5
6
7
8
9
import { districts, getDistrictById, getDistrictsByProvince } from "@nepali-utils/core"
districts // → DistrictInfo[] (all 77 districts)
getDistrictById("6accfa6f-c4ab-4b95-b0bf-aaf740375f62")
// → { id: "...", provinceId: "...", name: "ताप्लेजुङ", nameEn: "Taplejung", ... }
getDistrictsByProvince("1a2d297a-b4d4-4062-9801-62813c324753")
// → DistrictInfo[] (districts in Koshi province)

Municipalities

1
2
3
4
5
6
7
8
9
10
import { municipalities, getMunicipalityById, getMunicipalitiesByDistrict } from "@nepali-utils/core"
getMunicipalityById(1)
// → { id: 1, districtId: 1, nameEn: "...", nameNe: "...", type: "...", ... }
getMunicipalitiesByDistrict(1)
// → MunicipalityInfo[]
getMunicipalitiesByProvince(1)
// → MunicipalityInfo[] (all municipalities in Koshi)

Wards

1
2
3
4
5
6
7
8
9
10
import { wards, getWardsByMunicipality, getWardsByDistrict, getWardsByProvince } from "@nepali-utils/core"
getWardsByMunicipality(1)
// → WardInfo[]
getWardsByDistrict(1)
// → WardInfo[]
getWardsByProvince(1)
// → WardInfo[]

Validation

Validate Nepali mobile phone numbers with carrier detection and formatting.

Phone validation

1
2
3
4
5
6
7
8
9
10
11
import { isValidNepaliMobile, getPhoneType, formatNepaliPhone } from "@nepali-utils/core"
isValidNepaliMobile("9841234567") // → true
isValidNepaliMobile("12345") // → false
getPhoneType("9841234567") // → "NTC" (Nepal Telecom)
getPhoneType("9861234567") // → "NTC"
getPhoneType("9851234567") // → "Ncell"
formatNepaliPhone("9841234567")
// → "984-1234567"

Currency

Format amounts in Nepali currency (रु) and convert numbers to Nepali words.

Currency formatting

formatNepaliCurrency accepts options for language, symbol, and decimal control:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { formatNepaliCurrency } from "@nepali-utils/core"
// Default — Nepali numerals with रु symbol
formatNepaliCurrency(12345.67)
// → "रु १२,३४५.६७"
// English output
formatNepaliCurrency(12345.67, { lang: "en" })
// → "Rs. 12,345.67"
// Hide symbol
formatNepaliCurrency(1000, { symbol: false })
// → "१,०००.००"
// Whole number only (no decimals)
formatNepaliCurrency(1000, { decimal: false })
// → "रु १,०००"
// Negative amounts
formatNepaliCurrency(-500)
// → "रु -५००.००"

Number to words

Converts numbers to Nepali words using the traditional place value system (सय, हजार, लाख, करोड):

1
2
3
4
5
6
7
8
9
10
import { toNepaliWords } from "@nepali-utils/core"
toNepaliWords(0) // → "शून्य"
toNepaliWords(5) // → "पाँच"
toNepaliWords(100) // → "एक सय"
toNepaliWords(1000) // → "एक हजार"
toNepaliWords(12345) // → "बाह्र हजार तीन सय पैंतालीस"
toNepaliWords(100000) // → "एक लाख"
toNepaliWords(10000000) // → "एक करोड"
toNepaliWords(-50) // → "ऋण पचास"

Constants

MONTHS

All 12 Bikram Sambat months with English and Nepali names:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { MONTHS } from "@nepali-utils/core"
MONTHS
// → [
// { value: 1, nameEn: "Baisakh", nameNe: "बैशाख" },
// { value: 2, nameEn: "Jestha", nameNe: "जेष्ठ" },
// { value: 3, nameEn: "Ashadh", nameNe: "आषाढ" },
// { value: 4, nameEn: "Shrawan", nameNe: "श्रावण" },
// { value: 5, nameEn: "Bhadra", nameNe: "भाद्र" },
// { value: 6, nameEn: "Ashwin", nameNe: "आश्विन" },
// { value: 7, nameEn: "Kartik", nameNe: "कार्तिक" },
// { value: 8, nameEn: "Mangsir", nameNe: "मंसिर" },
// { value: 9, nameEn: "Poush", nameNe: "पौष" },
// { value: 10, nameEn: "Magh", nameNe: "माघ" },
// { value: 11, nameEn: "Falgun", nameNe: "फाल्गुन" },
// { value: 12, nameEn: "Chaitra", nameNe: "चैत्र" },
// ]

WEEKDAYS

7 weekdays with short/long names in both languages:

1
2
3
4
5
6
7
import { WEEKDAYS } from "@nepali-utils/core"
WEEKDAYS[0]
// → { value: 0, shortEn: "Sun", shortNe: "आइत", longEn: "Sunday", longNe: "आइतबार" }
WEEKDAYS[6]
// → { value: 6, shortEn: "Sat", shortNe: "शनि", longEn: "Saturday", longNe: "शनिबार" }

API reference

Conversion

FunctionSignatureDescription
bsToAd(year, month, day)(y: number, m: number, d: number) => DateBS → AD. Throws if date is invalid or outside BS 2000–2100.
adToBs(date)(date: Date) => DateBSAD → BS. Throws if date is before BS 2000-01-01 (AD 1943-04-14).
getTodayBs()() => DateBSCurrent date in BS. Always succeeds.
getBsDayOfWeek(year, month, day)(y: number, m: number, d: number) => numberDay of week: 0 (Sunday) through 6 (Saturday). Throws on invalid date.

Formatting

FunctionSignatureDescription
formatBsDate(date, lang?, opts?)(d: DateBS, lang?: BsLang, opts?: BsFormatOptions) => stringFormat with pattern (YYYY-MM-DD, DD MMMM YYYY, etc.). Default pattern: YYYY-MM-DD.
toNepaliNumber(num)(num: number) => stringArabic → Nepali digits (e.g. 2081 → २०८१). Handles negative numbers.
parseNepaliNumber(str)(str: string) => numberNepali → Arabic digits (e.g. २०८१ → 2081). Non-digits pass through.
getPrevMonth(year, month)(y: number, m: number) => { year, month }Previous month. Wraps from Baisakh (1) to Chaitra (12) of previous year.
getNextMonth(year, month)(y: number, m: number) => { year, month }Next month. Wraps from Chaitra (12) to Baisakh (1) of next year.

Calendar data

FunctionSignatureDescription
generateCalendarGrid(year, month, ...)(y: number, m: number, sel?: DateBS, today?: DateBS, disabledFn?: fn) => CalendarDay[][]Generates 6×7 grid (weeks × days) with padding, today/selected/disabled state.
getDaysInBsMonth(year, month)(y: number, m: number) => numberDays in a BS month (29–32). Throws if month is outside 1–12 or year unsupported.
getDaysInBsYear(year)(y: number) => numberTotal days in a BS year (354–366). Throws if year outside BS 2000–2100.
isValidBsYear(year)(y: number) => booleanTrue if year is in BS 2000–2100. Never throws.
getBsYearRange()() => { min: number; max: number }Returns { min: 2000, max: 2100 }.
validateBsDate(year, month, day)(y: number, m: number, d: number) => voidThrows descriptive Error if month is outside 1–12 or day is outside valid range.

Comparison

FunctionSignatureDescription
isSameBsDate(a, b)(a: DateBS, b: DateBS) => booleanTrue if year, month, and day are all equal.
isBeforeBsDate(a, b)(a: DateBS, b: DateBS) => booleanTrue if a is chronologically before b (compares year → month → day).
isAfterBsDate(a, b)(a: DateBS, b: DateBS) => booleanTrue if a is chronologically after b.

Location

ExportTypeDescription
provincesProvinceInfo[]All 7 provinces (Koshi, Madhesh, Bagmati, Gandaki, Lumbini, Karnali, Sudurpashchim)
getProvinceById(id)ProvinceInfo | undefinedLookup by UUID. Returns undefined if not found.
getProvinceByNameEn(name)ProvinceInfo | undefinedLookup by English name (e.g. 'Bagmati Province'). Case-sensitive.
districtsDistrictInfo[]All 77 districts
getDistrictById(id)DistrictInfo | undefinedLookup by UUID.
getDistrictsByProvince(provinceId)DistrictInfo[]Districts in a province. Empty array if no match.
municipalitiesMunicipalityInfo[]All municipalities (nagarpalika and gaupalika)
getMunicipalityById(id)MunicipalityInfo | undefinedLookup by UUID.
getMunicipalitiesByDistrict(districtId)MunicipalityInfo[]Municipalities in a district.
getMunicipalitiesByProvince(provinceId)MunicipalityInfo[]Municipalities in a province.
wardsWardInfo[]All wards across all municipalities
getWardsByMunicipality(id)WardInfo[]Wards in a municipality.
getWardsByDistrict(id)WardInfo[]Wards in a district.
getWardsByProvince(id)WardInfo[]Wards in a province.

Validation

FunctionSignatureDescription
isValidNepaliMobile(phone)(phone: string) => booleanValidates 10-digit Nepali mobile. Strips +977/977 prefix, spaces, dashes. Checks prefix against 16 known operator codes.
getPhoneType(phone)(phone: string) => 'mobile' | 'landline' | 'unknown'Detects mobile (9X, 10 digits), landline (area code match), or unknown.
formatNepaliPhone(phone)(phone: string) => stringFormats mobile as XXX-XXX-XXXX or returns national number for landline.

Currency

FunctionSignatureDescription
formatNepaliCurrency(amount, opts?)(amount: number, opts?: NepaliCurrencyOptions) => stringFormat with रु/Rs. symbol, Indian number grouping, Devanagari or English digits, optional decimals.
toNepaliWords(num)(num: number) => stringNumber to Nepali words (सय, हजार, लाख, करोड). Handles 0 and negatives.

NepaliCurrencyOptions

OptionTypeDefaultDescription
lang"en" | "ne""ne"Output language for numerals
symbolbooleantrueShow रु / Rs. prefix
decimalbooleantrueShow .XX decimal places

Still have questions?

Check the source on GitHub or open an issue.