tiny-ja-date 0.3.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 +51 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +58 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,9 @@ Tiny TypeScript date utilities for Japanese UI.
|
|
|
5
5
|
- 日本語の日付表示
|
|
6
6
|
- 日本語の相対日付表示
|
|
7
7
|
- 土日判定
|
|
8
|
+
- 営業日判定
|
|
8
9
|
- 次の営業日計算
|
|
10
|
+
- 営業日数の加算・減算
|
|
9
11
|
|
|
10
12
|
## Install
|
|
11
13
|
|
|
@@ -17,8 +19,10 @@ npm install tiny-ja-date
|
|
|
17
19
|
|
|
18
20
|
```ts
|
|
19
21
|
import {
|
|
22
|
+
addBusinessDays,
|
|
20
23
|
formatJaDate,
|
|
21
24
|
formatRelativeJaDate,
|
|
25
|
+
isBusinessDay,
|
|
22
26
|
isWeekendJP,
|
|
23
27
|
nextBusinessDay,
|
|
24
28
|
} from "tiny-ja-date"
|
|
@@ -32,8 +36,14 @@ formatRelativeJaDate(new Date(2026, 4, 15), new Date(2026, 4, 12))
|
|
|
32
36
|
isWeekendJP(new Date(2026, 4, 16))
|
|
33
37
|
// true
|
|
34
38
|
|
|
39
|
+
isBusinessDay("2026-05-18", { holidays: ["2026-05-18"] })
|
|
40
|
+
// false
|
|
41
|
+
|
|
35
42
|
nextBusinessDay(new Date(2026, 4, 15))
|
|
36
43
|
// Date for 2026-05-18
|
|
44
|
+
|
|
45
|
+
addBusinessDays("2026-05-15", 2)
|
|
46
|
+
// Date for 2026-05-19
|
|
37
47
|
```
|
|
38
48
|
|
|
39
49
|
## API
|
|
@@ -77,6 +87,23 @@ isWeekendJP(new Date(2026, 4, 16))
|
|
|
77
87
|
// true
|
|
78
88
|
```
|
|
79
89
|
|
|
90
|
+
### `isBusinessDay(date: DateInput, options?: BusinessDayOptions): boolean`
|
|
91
|
+
|
|
92
|
+
Checks the input date itself in the selected time zone. Returns `false` for
|
|
93
|
+
Saturdays, Sundays, dates in `options.holidays`, and dates skipped entirely by
|
|
94
|
+
the time zone. A weekday that starts after midnight can still be a business day.
|
|
95
|
+
Invalid holiday inputs throw a `TypeError`, even when the input falls on a weekend.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
|
|
99
|
+
|
|
100
|
+
isBusinessDay("2026-11-23", options)
|
|
101
|
+
// false
|
|
102
|
+
|
|
103
|
+
isBusinessDay("2026-11-24", options)
|
|
104
|
+
// true
|
|
105
|
+
```
|
|
106
|
+
|
|
80
107
|
### `nextBusinessDay(date: DateInput, options?: BusinessDayOptions): Date`
|
|
81
108
|
|
|
82
109
|
Returns a `Date` for the start of the next business day in the selected time
|
|
@@ -95,9 +122,32 @@ nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
|
|
|
95
122
|
// Date for 2026-05-19
|
|
96
123
|
```
|
|
97
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
|
+
|
|
98
148
|
## Time zones
|
|
99
149
|
|
|
100
|
-
All
|
|
150
|
+
All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
|
|
101
151
|
When omitted, they keep the runtime-local behavior of 0.2. The runtime's
|
|
102
152
|
`Intl.DateTimeFormat` supplies the time zone rules. Unrecognized IANA names
|
|
103
153
|
throw a `RangeError`.
|
package/dist/index.d.ts
CHANGED
|
@@ -8,4 +8,6 @@ export interface BusinessDayOptions extends DateOptions {
|
|
|
8
8
|
export declare function formatJaDate(input: DateInput, options?: DateOptions): string;
|
|
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
|
+
export declare function isBusinessDay(input: DateInput, options?: BusinessDayOptions): boolean;
|
|
12
|
+
export declare function addBusinessDays(input: DateInput, amount: number, options?: BusinessDayOptions): Date;
|
|
11
13
|
export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
|
package/dist/index.js
CHANGED
|
@@ -37,6 +37,12 @@ function checkedUtcDay(year, month, day) {
|
|
|
37
37
|
}
|
|
38
38
|
return value;
|
|
39
39
|
}
|
|
40
|
+
function dateOnlyDay(input) {
|
|
41
|
+
if (typeof input !== "string" || !DATE_ONLY.test(input))
|
|
42
|
+
return undefined;
|
|
43
|
+
const [year, month, day] = input.split("-").map(Number);
|
|
44
|
+
return checkedUtcDay(year, month, day);
|
|
45
|
+
}
|
|
40
46
|
function utcDateParts(day) {
|
|
41
47
|
const cycles = Math.floor(day / GREGORIAN_CYCLE_MS);
|
|
42
48
|
const date = new Date(day - cycles * GREGORIAN_CYCLE_MS);
|
|
@@ -79,10 +85,9 @@ function calendarDay(input, formatter) {
|
|
|
79
85
|
return utcDay(date.getFullYear(), date.getMonth() + 1, date.getDate());
|
|
80
86
|
}
|
|
81
87
|
if (typeof input === "string") {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return
|
|
85
|
-
}
|
|
88
|
+
const day = dateOnlyDay(input);
|
|
89
|
+
if (day !== undefined)
|
|
90
|
+
return day;
|
|
86
91
|
const match = OFFSET_DATE_TIME.exec(input);
|
|
87
92
|
if (!match)
|
|
88
93
|
throw new TypeError("Expected YYYY-MM-DD or an ISO date-time with an offset");
|
|
@@ -139,6 +144,23 @@ function isWeekend(day) {
|
|
|
139
144
|
const weekday = weekdayOf(day);
|
|
140
145
|
return weekday === 0 || weekday === 6;
|
|
141
146
|
}
|
|
147
|
+
function holidayDays(holidays, formatter) {
|
|
148
|
+
return new Set(holidays?.map((holiday) => calendarDay(holiday, formatter)));
|
|
149
|
+
}
|
|
150
|
+
function dayStart(day, formatter) {
|
|
151
|
+
if (formatter)
|
|
152
|
+
return zonedDayStart(day, formatter);
|
|
153
|
+
const { year, month, day: dayOfMonth } = utcDateParts(day);
|
|
154
|
+
const local = new Date(0);
|
|
155
|
+
local.setHours(0, 0, 0, 0);
|
|
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);
|
|
163
|
+
}
|
|
142
164
|
export function formatJaDate(input, options = {}) {
|
|
143
165
|
const formatter = createFormatter(options.timeZone);
|
|
144
166
|
const date = calendarDay(input, formatter);
|
|
@@ -162,26 +184,41 @@ export function formatRelativeJaDate(input, baseInput = new Date(), options = {}
|
|
|
162
184
|
export function isWeekendJP(input, options = {}) {
|
|
163
185
|
return isWeekend(calendarDay(input, createFormatter(options.timeZone)));
|
|
164
186
|
}
|
|
165
|
-
export function
|
|
187
|
+
export function isBusinessDay(input, options = {}) {
|
|
188
|
+
const formatter = createFormatter(options.timeZone);
|
|
189
|
+
const day = dateOnlyDay(input) ?? calendarDay(input, formatter);
|
|
190
|
+
const holidays = holidayDays(options.holidays, formatter);
|
|
191
|
+
return businessDayStart(day, holidays, formatter) !== undefined;
|
|
192
|
+
}
|
|
193
|
+
export function addBusinessDays(input, amount, options = {}) {
|
|
194
|
+
if (!Number.isSafeInteger(amount))
|
|
195
|
+
throw new RangeError("Expected a safe integer business-day amount");
|
|
166
196
|
const formatter = createFormatter(options.timeZone);
|
|
167
197
|
const day = calendarDay(input, formatter);
|
|
168
|
-
const holidays =
|
|
169
|
-
|
|
170
|
-
|
|
198
|
+
const holidays = holidayDays(options.holidays, formatter);
|
|
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) {
|
|
213
|
+
const start = businessDayStart(candidate, holidays, formatter);
|
|
214
|
+
if (!start)
|
|
171
215
|
continue;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return start;
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
const { year, month, day } = utcDateParts(candidate);
|
|
179
|
-
const local = new Date(0);
|
|
180
|
-
local.setHours(0, 0, 0, 0);
|
|
181
|
-
local.setFullYear(year, month - 1, day);
|
|
182
|
-
if (local.getDate() === day)
|
|
183
|
-
return local;
|
|
184
|
-
}
|
|
216
|
+
remaining -= 1;
|
|
217
|
+
if (remaining === 0)
|
|
218
|
+
return start;
|
|
185
219
|
}
|
|
186
220
|
throw new TypeError("Invalid date");
|
|
187
221
|
}
|
|
222
|
+
export function nextBusinessDay(input, options = {}) {
|
|
223
|
+
return addBusinessDays(input, 1, options);
|
|
224
|
+
}
|