ad-bs-converter 0.4.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/README.md +145 -33
- package/dist/index.d.mts +148 -0
- package/dist/index.d.ts +148 -0
- package/dist/index.js +540 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +528 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +52 -10
- package/.npmignore +0 -8
- package/lol.js +0 -3
- package/src/converter.js +0 -342
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project
|
|
6
|
+
adheres to [Semantic Versioning](https://semver.org/).
|
|
7
|
+
|
|
8
|
+
## [0.6.0] - 2026-11-11
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Corrected the month lengths for BS years 2082 and 2083. The rows shipped since 2017 were
|
|
13
|
+
provisional predictions; they are now aligned with the published calendar and with the
|
|
14
|
+
independent `nepali-date-converter`, `@remotemerge/nepali-date-converter` and
|
|
15
|
+
`@sbmdkl/nepali-date-converter` tables. Conversions in the second half of those years were
|
|
16
|
+
previously off by one day (for example `ad2bs('2026/11/11')` returned 2083/7/26 instead of
|
|
17
|
+
2083/7/25).
|
|
18
|
+
|
|
19
|
+
| Year | Old row (Baisakh - Chaitra) | New row |
|
|
20
|
+
| ---- | ------------------------------------------------- | ------------------------------------------------- |
|
|
21
|
+
| 2082 | 31 31 32 31 31 30 30 30 29 30 30 30 (365) | 31 31 32 31 31 31 30 29 30 29 30 30 (365) |
|
|
22
|
+
| 2083 | 31 31 32 31 31 30 30 30 29 30 30 30 (365) | 31 31 32 31 31 31 30 29 30 29 30 30 (365) |
|
|
23
|
+
|
|
24
|
+
- Restored the month lengths for BS years 2004, 2062 and 2087 to the values published in 0.5.0.
|
|
25
|
+
PR #11 (Feb 2024, on `master` only, never released) corrected 2081 but at the same time rewrote
|
|
26
|
+
these three rows to values that match no reference library. The restored rows were verified
|
|
27
|
+
against two independent implementations, `nepali-date-converter` and
|
|
28
|
+
`@remotemerge/nepali-date-converter`; the yearly totals are unchanged, so only the days around
|
|
29
|
+
the affected month boundaries move.
|
|
30
|
+
|
|
31
|
+
| Year | Row on `master` (Baisakh - Chaitra) | Restored row |
|
|
32
|
+
| ---- | ----------------------------------------- | ----------------------------------------- |
|
|
33
|
+
| 2004 | 30 32 31 32 31 30 30 30 30 29 29 31 (365) | 30 32 31 32 31 30 30 30 29 30 29 31 (365) |
|
|
34
|
+
| 2062 | 31 31 31 32 31 31 29 30 29 30 29 31 (365) | 30 32 31 32 31 31 29 30 29 30 29 31 (365) |
|
|
35
|
+
| 2087 | 31 31 32 31 31 31 30 29 30 30 30 30 (366) | 31 31 32 31 31 31 30 30 29 30 30 30 (366) |
|
|
36
|
+
|
|
37
|
+
- `ad2bs()` no longer crashes with a `TypeError` when walking past the end of the calendar table
|
|
38
|
+
(in the 0.5.0 npm release this happened for every AD date after 2036-04-14). The conversion now
|
|
39
|
+
works for every day in the table and throws a descriptive `RangeError` beyond it.
|
|
40
|
+
|
|
41
|
+
### Added
|
|
42
|
+
|
|
43
|
+
- TypeScript source with bundled type declarations; the package is published as ESM (`import`)
|
|
44
|
+
and CommonJS (`require`) through an `exports` map.
|
|
45
|
+
- `ad2bs()` accepts a `Date` object; both functions accept `{ year, month, day }` objects and
|
|
46
|
+
`yyyy-mm-dd` strings in addition to `yyyy/mm/dd`.
|
|
47
|
+
- `SUPPORTED_RANGE` constant, plus `CALENDAR_DATA`, `MIN_BS_YEAR` and `MAX_BS_YEAR` exports.
|
|
48
|
+
- Exported types: `AdDate`, `AdDateInput`, `BsDate`, `BsDateFields`, `BsDateInput`, `DateParts`,
|
|
49
|
+
`SupportedRange`, `YearData`.
|
|
50
|
+
- Malformed input, impossible dates and out-of-range dates throw `RangeError` with a clear message
|
|
51
|
+
(previously the behaviour was undefined: wrong results or a `TypeError`).
|
|
52
|
+
- Test suite migrated to vitest, with a round-trip test over every day in the table and a
|
|
53
|
+
cross-check of every day in 2000 - 2090 BS against `nepali-date-converter`.
|
|
54
|
+
- GitHub Actions CI (Node 22 / 24 / 26) replacing Travis; ESLint and Prettier. A `.nvmrc`
|
|
55
|
+
pins local development to Node 26.
|
|
56
|
+
|
|
57
|
+
### Changed
|
|
58
|
+
|
|
59
|
+
- Calendar data for BS 2101 - 2199 (already on `master`, never published to npm) is included.
|
|
60
|
+
- `engines.node` is now `>=22` (Node 18 and 20 are end-of-life). The build targets Node 22 /
|
|
61
|
+
ES2022.
|
|
62
|
+
|
|
63
|
+
### Notes
|
|
64
|
+
|
|
65
|
+
- Years 2091 BS and later remain provisional (no official source yet) and may change. The
|
|
66
|
+
Feb 2024 data contribution shortened 2092 BS from 366 to 365 days, so `2092/12/30` now maps to
|
|
67
|
+
2036-04-13 (it was 2036-04-14 in 0.5.0) and 2093/1/1 is 2036-04-14; the corresponding unit
|
|
68
|
+
test was updated to the current table.
|
|
69
|
+
- With the 2004/2062/2087 rows restored, the cross-check against `nepali-date-converter` is clean
|
|
70
|
+
for every day of 2000 - 2090 BS. `test/crosscheck.test.ts` keeps a `KNOWN_DISAGREEMENTS` list
|
|
71
|
+
(currently empty) for documenting any future, deliberate divergence.
|
|
72
|
+
|
|
73
|
+
## 0.5.0 - 2017
|
|
74
|
+
|
|
75
|
+
Last release of the original CommonJS implementation.
|
|
76
|
+
|
|
77
|
+
[0.6.0]: https://github.com/techgaun/ad-bs-converter/compare/fd76e5a...master
|
package/README.md
CHANGED
|
@@ -1,50 +1,162 @@
|
|
|
1
|
-
# ad-bs-converter [](http://badge.fury.io/js/ad-bs-converter) [](http://badge.fury.io/js/ad-bs-converter) [](https://github.com/techgaun/ad-bs-converter/actions/workflows/ci.yml)
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
Convert Bikram Sambat (BS, the Nepali calendar) dates to Anno Domini (AD, Gregorian) and vice-versa.
|
|
4
|
+
|
|
5
|
+
Written in TypeScript, published as both ESM and CommonJS with bundled type declarations. Requires
|
|
6
|
+
Node 22 or newer (CI runs on Node 22, 24 and 26; `.nvmrc` points at 26) and works in any modern
|
|
7
|
+
browser bundler.
|
|
8
|
+
|
|
9
|
+
_If you are looking for an ad-bs converter for Python, check out [ad-bs-converter.py](https://github.com/techgaun/ad-bs-converter.py)._
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
5
12
|
|
|
6
|
-
### Installation
|
|
7
13
|
```bash
|
|
8
|
-
npm install ad-bs-converter
|
|
14
|
+
npm install ad-bs-converter
|
|
9
15
|
```
|
|
10
16
|
|
|
11
|
-
|
|
12
|
-
```javascript
|
|
13
|
-
var adbs = require("ad-bs-converter");
|
|
17
|
+
## Usage
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
```ts
|
|
20
|
+
// ESM / TypeScript
|
|
21
|
+
import { ad2bs, bs2ad } from 'ad-bs-converter';
|
|
22
|
+
|
|
23
|
+
// CommonJS
|
|
24
|
+
const { ad2bs, bs2ad } = require('ad-bs-converter');
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
console.log(ad2bs('1990/8/10'));
|
|
29
|
+
console.log(bs2ad('2047/4/26'));
|
|
17
30
|
```
|
|
18
31
|
|
|
19
32
|
outputs
|
|
20
33
|
|
|
21
|
-
```
|
|
22
|
-
{
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
34
|
+
```js
|
|
35
|
+
{
|
|
36
|
+
ne: {
|
|
37
|
+
year: '२०४७',
|
|
38
|
+
month: '४',
|
|
39
|
+
day: '२६',
|
|
40
|
+
strMonth: 'श्रावण',
|
|
41
|
+
strShortMonth: 'श्रा',
|
|
42
|
+
dayOfWeek: '५',
|
|
43
|
+
strDayOfWeek: 'शुक्रवार',
|
|
44
|
+
strShortDayOfWeek: 'शुक्र',
|
|
45
|
+
strMinDayOfWeek: 'शु',
|
|
46
|
+
totalDaysInMonth: '३२'
|
|
47
|
+
},
|
|
48
|
+
en: {
|
|
49
|
+
year: 2047,
|
|
50
|
+
month: 4,
|
|
51
|
+
day: 26,
|
|
52
|
+
strMonth: 'Shrawan',
|
|
53
|
+
strShortMonth: 'Shra',
|
|
54
|
+
dayOfWeek: 5,
|
|
55
|
+
strDayOfWeek: 'Shukrabaar',
|
|
56
|
+
strShortDayOfWeek: 'Shukra',
|
|
57
|
+
strMinDayOfWeek: 'Shu',
|
|
58
|
+
totalDaysInMonth: 32
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
{
|
|
62
|
+
year: 1990,
|
|
43
63
|
month: 8,
|
|
44
64
|
strMonth: 'August',
|
|
45
65
|
strShortMonth: 'Aug',
|
|
46
66
|
day: 10,
|
|
47
67
|
dayOfWeek: 5,
|
|
48
68
|
strDayOfWeek: 'Friday',
|
|
49
|
-
strShortDayOfWeek: 'Fri'
|
|
69
|
+
strShortDayOfWeek: 'Fri'
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Accepted inputs
|
|
74
|
+
|
|
75
|
+
Both functions accept a `"yyyy/mm/dd"` string (a `-` separator and leading zeros are fine) or a
|
|
76
|
+
`{ year, month, day }` object. `ad2bs` additionally accepts a JavaScript `Date`, which is read in the
|
|
77
|
+
local time zone (`getFullYear()`, `getMonth()`, `getDate()`).
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
ad2bs('2026-11-11');
|
|
81
|
+
ad2bs({ year: 2026, month: 11, day: 11 });
|
|
82
|
+
ad2bs(new Date(2026, 10, 11));
|
|
83
|
+
|
|
84
|
+
bs2ad('2083/07/25');
|
|
85
|
+
bs2ad({ year: 2083, month: 7, day: 25 });
|
|
50
86
|
```
|
|
87
|
+
|
|
88
|
+
Months and days are always 1-based. `dayOfWeek` is `0` for Sunday through `6` for Saturday.
|
|
89
|
+
|
|
90
|
+
### Errors
|
|
91
|
+
|
|
92
|
+
Malformed input, impossible dates (for example `2083/7/31`, since Kartik 2083 has 30 days) and dates
|
|
93
|
+
outside the supported range throw a `RangeError` with a descriptive message.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
try {
|
|
97
|
+
ad2bs('1900/1/1');
|
|
98
|
+
} catch (error) {
|
|
99
|
+
// RangeError: AD date 1900/1/1 is outside the supported range 1918/4/13 - 2143/4/18
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Supported range
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { SUPPORTED_RANGE } from 'ad-bs-converter';
|
|
107
|
+
|
|
108
|
+
SUPPORTED_RANGE.bs; // { start: { year: 1975, month: 1, day: 1 }, end: { year: 2199, month: 12, day: 30 } }
|
|
109
|
+
SUPPORTED_RANGE.ad; // { start: { year: 1918, month: 4, day: 13 }, end: { year: 2143, month: 4, day: 18 } }
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The calendar table lives in [`src/data.ts`](src/data.ts) and is also exported as `CALENDAR_DATA`
|
|
113
|
+
(with `MIN_BS_YEAR` / `MAX_BS_YEAR`) for anyone who needs the raw month lengths.
|
|
114
|
+
|
|
115
|
+
### A note on data accuracy
|
|
116
|
+
|
|
117
|
+
The Bikram Sambat calendar is not computed from a formula: month lengths are published by Nepal's
|
|
118
|
+
Panchanga Nirnayak Samiti year by year. This library therefore ships a lookup table.
|
|
119
|
+
|
|
120
|
+
- **1978 - 2090 BS**: considered reliable. Every day in 2000 - 2090 BS is cross-checked in CI
|
|
121
|
+
against the independent [`nepali-date-converter`](https://www.npmjs.com/package/nepali-date-converter)
|
|
122
|
+
table with no disagreements (see [`test/crosscheck.test.ts`](test/crosscheck.test.ts)).
|
|
123
|
+
- **2082 and 2083 BS** were corrected in v0.6.0; earlier releases carried provisional 2017-era
|
|
124
|
+
predictions that were off by a day in the second half of those years (see the
|
|
125
|
+
[changelog](CHANGELOG.md)).
|
|
126
|
+
- **2004, 2062 and 2087 BS**: PR #11 (2024, never published to npm) fixed 2081 but also rewrote
|
|
127
|
+
these three rows to values that match no reference library. v0.6.0 restores the original rows,
|
|
128
|
+
which agree with both `nepali-date-converter` and `@remotemerge/nepali-date-converter`.
|
|
129
|
+
- **2091 BS and later** are provisional. No official data exists yet and different sources disagree;
|
|
130
|
+
the rows are kept so conversions keep working but they may change in a future release.
|
|
131
|
+
|
|
132
|
+
If you spot a discrepancy, please open an issue with the source you are comparing against.
|
|
133
|
+
|
|
134
|
+
### TypeScript
|
|
135
|
+
|
|
136
|
+
All inputs and outputs are typed:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import type {
|
|
140
|
+
AdDate,
|
|
141
|
+
AdDateInput,
|
|
142
|
+
BsDate,
|
|
143
|
+
BsDateInput,
|
|
144
|
+
DateParts,
|
|
145
|
+
SupportedRange,
|
|
146
|
+
} from 'ad-bs-converter';
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Development
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
npm install
|
|
153
|
+
npm run typecheck # tsc --noEmit
|
|
154
|
+
npm run lint # eslint + prettier --check
|
|
155
|
+
npm test # vitest (unit, round-trip and cross-check suites)
|
|
156
|
+
npm run build # tsup -> dist/ (ESM + CJS + .d.ts)
|
|
157
|
+
npm run smoke # require()/import the built package
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
ISC
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/** A calendar date broken into its numeric parts. Months and days are 1-based. */
|
|
2
|
+
interface DateParts {
|
|
3
|
+
year: number;
|
|
4
|
+
month: number;
|
|
5
|
+
day: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Input accepted by {@link bs2ad}: a `"yyyy/mm/dd"` (or `"yyyy-mm-dd"`) string
|
|
9
|
+
* or a {@link DateParts} object describing a Bikram Sambat date.
|
|
10
|
+
*/
|
|
11
|
+
type BsDateInput = string | DateParts;
|
|
12
|
+
/**
|
|
13
|
+
* Input accepted by {@link ad2bs}: a `"yyyy/mm/dd"` (or `"yyyy-mm-dd"`) string,
|
|
14
|
+
* a {@link DateParts} object, or a JavaScript `Date` (interpreted in the local
|
|
15
|
+
* time zone, i.e. via `getFullYear()`, `getMonth()` and `getDate()`).
|
|
16
|
+
*/
|
|
17
|
+
type AdDateInput = string | DateParts | Date;
|
|
18
|
+
/**
|
|
19
|
+
* One localisation of a Bikram Sambat date. `T` is `string` for the Nepali
|
|
20
|
+
* (Devanagari numeral) version and `number` for the English version.
|
|
21
|
+
*/
|
|
22
|
+
interface BsDateFields<T extends string | number> {
|
|
23
|
+
year: T;
|
|
24
|
+
month: T;
|
|
25
|
+
day: T;
|
|
26
|
+
/** Full month name, e.g. `"श्रावण"` / `"Shrawan"`. */
|
|
27
|
+
strMonth: string;
|
|
28
|
+
/** Abbreviated month name, e.g. `"श्रा"` / `"Shra"`. */
|
|
29
|
+
strShortMonth: string;
|
|
30
|
+
/** Day of week, `0` for Sunday through `6` for Saturday. */
|
|
31
|
+
dayOfWeek: T;
|
|
32
|
+
/** Full weekday name, e.g. `"शनिवार"` / `"Shanibaar"`. */
|
|
33
|
+
strDayOfWeek: string;
|
|
34
|
+
/** Short weekday name, e.g. `"शनि"` / `"Shani"`. */
|
|
35
|
+
strShortDayOfWeek: string;
|
|
36
|
+
/** Minimal weekday name, e.g. `"श"` / `"Sha"`. */
|
|
37
|
+
strMinDayOfWeek: string;
|
|
38
|
+
/** Number of days in the BS month the date falls in. */
|
|
39
|
+
totalDaysInMonth: T;
|
|
40
|
+
}
|
|
41
|
+
/** Result of {@link ad2bs}. */
|
|
42
|
+
interface BsDate {
|
|
43
|
+
/** Nepali (Devanagari) representation. Numbers are strings of Devanagari digits. */
|
|
44
|
+
ne: BsDateFields<string>;
|
|
45
|
+
/** English representation. Numbers are plain JavaScript numbers. */
|
|
46
|
+
en: BsDateFields<number>;
|
|
47
|
+
}
|
|
48
|
+
/** Result of {@link bs2ad}. */
|
|
49
|
+
interface AdDate {
|
|
50
|
+
year: number;
|
|
51
|
+
/** 1-based Gregorian month. */
|
|
52
|
+
month: number;
|
|
53
|
+
/** Full month name, e.g. `"August"`. */
|
|
54
|
+
strMonth: string;
|
|
55
|
+
/** Abbreviated month name, e.g. `"Aug"`. */
|
|
56
|
+
strShortMonth: string;
|
|
57
|
+
day: number;
|
|
58
|
+
/** Day of week, `0` for Sunday through `6` for Saturday. */
|
|
59
|
+
dayOfWeek: number;
|
|
60
|
+
/** Full weekday name, e.g. `"Friday"`. */
|
|
61
|
+
strDayOfWeek: string;
|
|
62
|
+
/** Short weekday name, e.g. `"Fri"`. */
|
|
63
|
+
strShortDayOfWeek: string;
|
|
64
|
+
}
|
|
65
|
+
/** Inclusive bounds of the dates this library can convert. */
|
|
66
|
+
interface SupportedRange {
|
|
67
|
+
bs: {
|
|
68
|
+
start: DateParts;
|
|
69
|
+
end: DateParts;
|
|
70
|
+
};
|
|
71
|
+
ad: {
|
|
72
|
+
start: DateParts;
|
|
73
|
+
end: DateParts;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Inclusive bounds of the dates this library can convert, in both calendars.
|
|
79
|
+
* Derived from the calendar table, so it grows automatically when rows are added.
|
|
80
|
+
*/
|
|
81
|
+
declare const SUPPORTED_RANGE: Readonly<SupportedRange>;
|
|
82
|
+
/**
|
|
83
|
+
* Converts a Bikram Sambat date to the Gregorian (AD) calendar.
|
|
84
|
+
*
|
|
85
|
+
* @param input `"yyyy/mm/dd"` string or `{ year, month, day }` object.
|
|
86
|
+
* @throws {RangeError} if the input is malformed, not a real BS date, or
|
|
87
|
+
* outside {@link SUPPORTED_RANGE}.
|
|
88
|
+
*/
|
|
89
|
+
declare function bs2ad(input: BsDateInput): AdDate;
|
|
90
|
+
/**
|
|
91
|
+
* Converts a Gregorian (AD) date to the Bikram Sambat calendar.
|
|
92
|
+
*
|
|
93
|
+
* @param input `"yyyy/mm/dd"` string, `{ year, month, day }` object, or a
|
|
94
|
+
* `Date` (read in the local time zone).
|
|
95
|
+
* @throws {RangeError} if the input is malformed, not a real date, or outside
|
|
96
|
+
* {@link SUPPORTED_RANGE}.
|
|
97
|
+
*/
|
|
98
|
+
declare function ad2bs(input: AdDateInput): BsDate;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Bikram Sambat calendar table.
|
|
102
|
+
*
|
|
103
|
+
* Each row is keyed by BS year and holds the number of days in each of the
|
|
104
|
+
* twelve months (Baisakh to Chaitra) followed by the total number of days in
|
|
105
|
+
* that year.
|
|
106
|
+
*
|
|
107
|
+
* Data provenance:
|
|
108
|
+
* - 1975-2092: original table shipped with this package (npm 0.5.0).
|
|
109
|
+
* - 2082 and 2083 were corrected in 0.6.0: they were provisional
|
|
110
|
+
* predictions when first added in 2017.
|
|
111
|
+
* - 2081 was corrected by PR #11 (2024). The same PR also rewrote 2004,
|
|
112
|
+
* 2062 and 2087 to values that match no reference library; 0.6.0
|
|
113
|
+
* restores the 0.5.0 rows, which agree with nepali-date-converter and
|
|
114
|
+
* @remotemerge/nepali-date-converter.
|
|
115
|
+
* - 2093-2100: https://nepalipatro.com.np/calendar/monthly/bs/2093/1
|
|
116
|
+
* - 2101-2199: community contribution (PR #11, see git history).
|
|
117
|
+
*
|
|
118
|
+
* Years after 2090 BS have not been officially published by the Nepal
|
|
119
|
+
* Panchanga Nirnayak Samiti and are therefore provisional; they may change.
|
|
120
|
+
*
|
|
121
|
+
* The module keeps the table in a separate file so that updating it is a
|
|
122
|
+
* one-line change per year. `test/data.test.ts` verifies that every row is
|
|
123
|
+
* internally consistent (month lengths sum to the yearly total).
|
|
124
|
+
*/
|
|
125
|
+
/** Twelve month lengths followed by the total number of days in the year. */
|
|
126
|
+
type YearData = readonly [
|
|
127
|
+
number,
|
|
128
|
+
number,
|
|
129
|
+
number,
|
|
130
|
+
number,
|
|
131
|
+
number,
|
|
132
|
+
number,
|
|
133
|
+
number,
|
|
134
|
+
number,
|
|
135
|
+
number,
|
|
136
|
+
number,
|
|
137
|
+
number,
|
|
138
|
+
number,
|
|
139
|
+
number
|
|
140
|
+
];
|
|
141
|
+
declare const CALENDAR_DATA: Readonly<Record<number, YearData>>;
|
|
142
|
+
/** First BS year present in the table. */
|
|
143
|
+
declare const MIN_BS_YEAR = 1975;
|
|
144
|
+
/** Last BS year present in the table. */
|
|
145
|
+
declare const MAX_BS_YEAR = 2199;
|
|
146
|
+
|
|
147
|
+
export { CALENDAR_DATA, MAX_BS_YEAR, MIN_BS_YEAR, SUPPORTED_RANGE, ad2bs, bs2ad };
|
|
148
|
+
export type { AdDate, AdDateInput, BsDate, BsDateFields, BsDateInput, DateParts, SupportedRange, YearData };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/** A calendar date broken into its numeric parts. Months and days are 1-based. */
|
|
2
|
+
interface DateParts {
|
|
3
|
+
year: number;
|
|
4
|
+
month: number;
|
|
5
|
+
day: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Input accepted by {@link bs2ad}: a `"yyyy/mm/dd"` (or `"yyyy-mm-dd"`) string
|
|
9
|
+
* or a {@link DateParts} object describing a Bikram Sambat date.
|
|
10
|
+
*/
|
|
11
|
+
type BsDateInput = string | DateParts;
|
|
12
|
+
/**
|
|
13
|
+
* Input accepted by {@link ad2bs}: a `"yyyy/mm/dd"` (or `"yyyy-mm-dd"`) string,
|
|
14
|
+
* a {@link DateParts} object, or a JavaScript `Date` (interpreted in the local
|
|
15
|
+
* time zone, i.e. via `getFullYear()`, `getMonth()` and `getDate()`).
|
|
16
|
+
*/
|
|
17
|
+
type AdDateInput = string | DateParts | Date;
|
|
18
|
+
/**
|
|
19
|
+
* One localisation of a Bikram Sambat date. `T` is `string` for the Nepali
|
|
20
|
+
* (Devanagari numeral) version and `number` for the English version.
|
|
21
|
+
*/
|
|
22
|
+
interface BsDateFields<T extends string | number> {
|
|
23
|
+
year: T;
|
|
24
|
+
month: T;
|
|
25
|
+
day: T;
|
|
26
|
+
/** Full month name, e.g. `"श्रावण"` / `"Shrawan"`. */
|
|
27
|
+
strMonth: string;
|
|
28
|
+
/** Abbreviated month name, e.g. `"श्रा"` / `"Shra"`. */
|
|
29
|
+
strShortMonth: string;
|
|
30
|
+
/** Day of week, `0` for Sunday through `6` for Saturday. */
|
|
31
|
+
dayOfWeek: T;
|
|
32
|
+
/** Full weekday name, e.g. `"शनिवार"` / `"Shanibaar"`. */
|
|
33
|
+
strDayOfWeek: string;
|
|
34
|
+
/** Short weekday name, e.g. `"शनि"` / `"Shani"`. */
|
|
35
|
+
strShortDayOfWeek: string;
|
|
36
|
+
/** Minimal weekday name, e.g. `"श"` / `"Sha"`. */
|
|
37
|
+
strMinDayOfWeek: string;
|
|
38
|
+
/** Number of days in the BS month the date falls in. */
|
|
39
|
+
totalDaysInMonth: T;
|
|
40
|
+
}
|
|
41
|
+
/** Result of {@link ad2bs}. */
|
|
42
|
+
interface BsDate {
|
|
43
|
+
/** Nepali (Devanagari) representation. Numbers are strings of Devanagari digits. */
|
|
44
|
+
ne: BsDateFields<string>;
|
|
45
|
+
/** English representation. Numbers are plain JavaScript numbers. */
|
|
46
|
+
en: BsDateFields<number>;
|
|
47
|
+
}
|
|
48
|
+
/** Result of {@link bs2ad}. */
|
|
49
|
+
interface AdDate {
|
|
50
|
+
year: number;
|
|
51
|
+
/** 1-based Gregorian month. */
|
|
52
|
+
month: number;
|
|
53
|
+
/** Full month name, e.g. `"August"`. */
|
|
54
|
+
strMonth: string;
|
|
55
|
+
/** Abbreviated month name, e.g. `"Aug"`. */
|
|
56
|
+
strShortMonth: string;
|
|
57
|
+
day: number;
|
|
58
|
+
/** Day of week, `0` for Sunday through `6` for Saturday. */
|
|
59
|
+
dayOfWeek: number;
|
|
60
|
+
/** Full weekday name, e.g. `"Friday"`. */
|
|
61
|
+
strDayOfWeek: string;
|
|
62
|
+
/** Short weekday name, e.g. `"Fri"`. */
|
|
63
|
+
strShortDayOfWeek: string;
|
|
64
|
+
}
|
|
65
|
+
/** Inclusive bounds of the dates this library can convert. */
|
|
66
|
+
interface SupportedRange {
|
|
67
|
+
bs: {
|
|
68
|
+
start: DateParts;
|
|
69
|
+
end: DateParts;
|
|
70
|
+
};
|
|
71
|
+
ad: {
|
|
72
|
+
start: DateParts;
|
|
73
|
+
end: DateParts;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Inclusive bounds of the dates this library can convert, in both calendars.
|
|
79
|
+
* Derived from the calendar table, so it grows automatically when rows are added.
|
|
80
|
+
*/
|
|
81
|
+
declare const SUPPORTED_RANGE: Readonly<SupportedRange>;
|
|
82
|
+
/**
|
|
83
|
+
* Converts a Bikram Sambat date to the Gregorian (AD) calendar.
|
|
84
|
+
*
|
|
85
|
+
* @param input `"yyyy/mm/dd"` string or `{ year, month, day }` object.
|
|
86
|
+
* @throws {RangeError} if the input is malformed, not a real BS date, or
|
|
87
|
+
* outside {@link SUPPORTED_RANGE}.
|
|
88
|
+
*/
|
|
89
|
+
declare function bs2ad(input: BsDateInput): AdDate;
|
|
90
|
+
/**
|
|
91
|
+
* Converts a Gregorian (AD) date to the Bikram Sambat calendar.
|
|
92
|
+
*
|
|
93
|
+
* @param input `"yyyy/mm/dd"` string, `{ year, month, day }` object, or a
|
|
94
|
+
* `Date` (read in the local time zone).
|
|
95
|
+
* @throws {RangeError} if the input is malformed, not a real date, or outside
|
|
96
|
+
* {@link SUPPORTED_RANGE}.
|
|
97
|
+
*/
|
|
98
|
+
declare function ad2bs(input: AdDateInput): BsDate;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Bikram Sambat calendar table.
|
|
102
|
+
*
|
|
103
|
+
* Each row is keyed by BS year and holds the number of days in each of the
|
|
104
|
+
* twelve months (Baisakh to Chaitra) followed by the total number of days in
|
|
105
|
+
* that year.
|
|
106
|
+
*
|
|
107
|
+
* Data provenance:
|
|
108
|
+
* - 1975-2092: original table shipped with this package (npm 0.5.0).
|
|
109
|
+
* - 2082 and 2083 were corrected in 0.6.0: they were provisional
|
|
110
|
+
* predictions when first added in 2017.
|
|
111
|
+
* - 2081 was corrected by PR #11 (2024). The same PR also rewrote 2004,
|
|
112
|
+
* 2062 and 2087 to values that match no reference library; 0.6.0
|
|
113
|
+
* restores the 0.5.0 rows, which agree with nepali-date-converter and
|
|
114
|
+
* @remotemerge/nepali-date-converter.
|
|
115
|
+
* - 2093-2100: https://nepalipatro.com.np/calendar/monthly/bs/2093/1
|
|
116
|
+
* - 2101-2199: community contribution (PR #11, see git history).
|
|
117
|
+
*
|
|
118
|
+
* Years after 2090 BS have not been officially published by the Nepal
|
|
119
|
+
* Panchanga Nirnayak Samiti and are therefore provisional; they may change.
|
|
120
|
+
*
|
|
121
|
+
* The module keeps the table in a separate file so that updating it is a
|
|
122
|
+
* one-line change per year. `test/data.test.ts` verifies that every row is
|
|
123
|
+
* internally consistent (month lengths sum to the yearly total).
|
|
124
|
+
*/
|
|
125
|
+
/** Twelve month lengths followed by the total number of days in the year. */
|
|
126
|
+
type YearData = readonly [
|
|
127
|
+
number,
|
|
128
|
+
number,
|
|
129
|
+
number,
|
|
130
|
+
number,
|
|
131
|
+
number,
|
|
132
|
+
number,
|
|
133
|
+
number,
|
|
134
|
+
number,
|
|
135
|
+
number,
|
|
136
|
+
number,
|
|
137
|
+
number,
|
|
138
|
+
number,
|
|
139
|
+
number
|
|
140
|
+
];
|
|
141
|
+
declare const CALENDAR_DATA: Readonly<Record<number, YearData>>;
|
|
142
|
+
/** First BS year present in the table. */
|
|
143
|
+
declare const MIN_BS_YEAR = 1975;
|
|
144
|
+
/** Last BS year present in the table. */
|
|
145
|
+
declare const MAX_BS_YEAR = 2199;
|
|
146
|
+
|
|
147
|
+
export { CALENDAR_DATA, MAX_BS_YEAR, MIN_BS_YEAR, SUPPORTED_RANGE, ad2bs, bs2ad };
|
|
148
|
+
export type { AdDate, AdDateInput, BsDate, BsDateFields, BsDateInput, DateParts, SupportedRange, YearData };
|