tiny-ja-date 0.4.0 → 0.5.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/README.md +28 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +34 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ Tiny TypeScript date utilities for Japanese UI.
|
|
|
7
7
|
- 土日判定
|
|
8
8
|
- 営業日判定
|
|
9
9
|
- 次の営業日計算
|
|
10
|
+
- 営業日数の加算・減算
|
|
10
11
|
|
|
11
12
|
## Install
|
|
12
13
|
|
|
@@ -18,6 +19,7 @@ npm install tiny-ja-date
|
|
|
18
19
|
|
|
19
20
|
```ts
|
|
20
21
|
import {
|
|
22
|
+
addBusinessDays,
|
|
21
23
|
formatJaDate,
|
|
22
24
|
formatRelativeJaDate,
|
|
23
25
|
isBusinessDay,
|
|
@@ -39,6 +41,9 @@ isBusinessDay("2026-05-18", { holidays: ["2026-05-18"] })
|
|
|
39
41
|
|
|
40
42
|
nextBusinessDay(new Date(2026, 4, 15))
|
|
41
43
|
// Date for 2026-05-18
|
|
44
|
+
|
|
45
|
+
addBusinessDays("2026-05-15", 2)
|
|
46
|
+
// Date for 2026-05-19
|
|
42
47
|
```
|
|
43
48
|
|
|
44
49
|
## API
|
|
@@ -117,6 +122,29 @@ nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
|
|
|
117
122
|
// Date for 2026-05-19
|
|
118
123
|
```
|
|
119
124
|
|
|
125
|
+
### `addBusinessDays(date: DateInput, amount: number, options?: BusinessDayOptions): Date`
|
|
126
|
+
|
|
127
|
+
Counts forward for a positive amount and backward for a negative amount. Counting
|
|
128
|
+
starts the day after or before the input and skips Saturdays, Sundays, dates in
|
|
129
|
+
`options.holidays`, and dates skipped entirely by the time zone.
|
|
130
|
+
`addBusinessDays(date, 1, options)` is equivalent to `nextBusinessDay(date, options)`.
|
|
131
|
+
|
|
132
|
+
Zero returns the start of the input date, including a weekend or holiday.
|
|
133
|
+
When midnight is skipped, the result is the first valid time on that date.
|
|
134
|
+
If the entire input date is skipped, zero throws a `TypeError`. Holiday inputs
|
|
135
|
+
are validated for every amount, including zero. An amount that is not a safe
|
|
136
|
+
integer throws a `RangeError`. A result beyond the `Date` range throws a `TypeError`.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
|
|
140
|
+
|
|
141
|
+
addBusinessDays("2026-11-20", 2, options)
|
|
142
|
+
// Start of 2026-11-25 in Tokyo
|
|
143
|
+
|
|
144
|
+
addBusinessDays("2026-11-24", -1, options)
|
|
145
|
+
// Start of 2026-11-20 in Tokyo
|
|
146
|
+
```
|
|
147
|
+
|
|
120
148
|
## Time zones
|
|
121
149
|
|
|
122
150
|
All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
|
package/dist/index.d.ts
CHANGED
|
@@ -9,4 +9,5 @@ export declare function formatJaDate(input: DateInput, options?: DateOptions): s
|
|
|
9
9
|
export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput, options?: DateOptions): string;
|
|
10
10
|
export declare function isWeekendJP(input: DateInput, options?: DateOptions): boolean;
|
|
11
11
|
export declare function isBusinessDay(input: DateInput, options?: BusinessDayOptions): boolean;
|
|
12
|
+
export declare function addBusinessDays(input: DateInput, amount: number, options?: BusinessDayOptions): Date;
|
|
12
13
|
export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
|
package/dist/index.js
CHANGED
|
@@ -147,16 +147,19 @@ function isWeekend(day) {
|
|
|
147
147
|
function holidayDays(holidays, formatter) {
|
|
148
148
|
return new Set(holidays?.map((holiday) => calendarDay(holiday, formatter)));
|
|
149
149
|
}
|
|
150
|
-
function
|
|
151
|
-
if (isWeekend(candidate) || holidays.has(candidate))
|
|
152
|
-
return undefined;
|
|
150
|
+
function dayStart(day, formatter) {
|
|
153
151
|
if (formatter)
|
|
154
|
-
return zonedDayStart(
|
|
155
|
-
const { year, month, day } = utcDateParts(
|
|
152
|
+
return zonedDayStart(day, formatter);
|
|
153
|
+
const { year, month, day: dayOfMonth } = utcDateParts(day);
|
|
156
154
|
const local = new Date(0);
|
|
157
155
|
local.setHours(0, 0, 0, 0);
|
|
158
|
-
local.setFullYear(year, month - 1,
|
|
159
|
-
return local.getDate() ===
|
|
156
|
+
local.setFullYear(year, month - 1, dayOfMonth);
|
|
157
|
+
return local.getDate() === dayOfMonth ? local : undefined;
|
|
158
|
+
}
|
|
159
|
+
function businessDayStart(candidate, holidays, formatter) {
|
|
160
|
+
if (isWeekend(candidate) || holidays.has(candidate))
|
|
161
|
+
return undefined;
|
|
162
|
+
return dayStart(candidate, formatter);
|
|
160
163
|
}
|
|
161
164
|
export function formatJaDate(input, options = {}) {
|
|
162
165
|
const formatter = createFormatter(options.timeZone);
|
|
@@ -187,14 +190,35 @@ export function isBusinessDay(input, options = {}) {
|
|
|
187
190
|
const holidays = holidayDays(options.holidays, formatter);
|
|
188
191
|
return businessDayStart(day, holidays, formatter) !== undefined;
|
|
189
192
|
}
|
|
190
|
-
export function
|
|
193
|
+
export function addBusinessDays(input, amount, options = {}) {
|
|
194
|
+
if (!Number.isSafeInteger(amount))
|
|
195
|
+
throw new RangeError("Expected a safe integer business-day amount");
|
|
191
196
|
const formatter = createFormatter(options.timeZone);
|
|
192
197
|
const day = calendarDay(input, formatter);
|
|
193
198
|
const holidays = holidayDays(options.holidays, formatter);
|
|
194
|
-
|
|
199
|
+
if (amount === 0) {
|
|
200
|
+
const start = dayStart(day, formatter);
|
|
201
|
+
if (!start)
|
|
202
|
+
throw new TypeError("Invalid date");
|
|
203
|
+
return start;
|
|
204
|
+
}
|
|
205
|
+
const direction = Math.sign(amount);
|
|
206
|
+
const step = direction * ONE_DAY_MS;
|
|
207
|
+
// A local calendar date can extend one day beyond the UTC Date limits.
|
|
208
|
+
const limit = MAX_DATE_MS + ONE_DAY_MS;
|
|
209
|
+
let remaining = Math.abs(amount);
|
|
210
|
+
if (remaining > (limit - direction * day) / ONE_DAY_MS)
|
|
211
|
+
throw new TypeError("Invalid date");
|
|
212
|
+
for (let candidate = day + step; Math.abs(candidate) <= limit; candidate += step) {
|
|
195
213
|
const start = businessDayStart(candidate, holidays, formatter);
|
|
196
|
-
if (start)
|
|
214
|
+
if (!start)
|
|
215
|
+
continue;
|
|
216
|
+
remaining -= 1;
|
|
217
|
+
if (remaining === 0)
|
|
197
218
|
return start;
|
|
198
219
|
}
|
|
199
220
|
throw new TypeError("Invalid date");
|
|
200
221
|
}
|
|
222
|
+
export function nextBusinessDay(input, options = {}) {
|
|
223
|
+
return addBusinessDays(input, 1, options);
|
|
224
|
+
}
|