tiny-ja-date 0.5.0 → 0.7.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 CHANGED
@@ -8,6 +8,7 @@ Tiny TypeScript date utilities for Japanese UI.
8
8
  - 営業日判定
9
9
  - 次の営業日計算
10
10
  - 営業日数の加算・減算
11
+ - 2つの日付間の営業日数
11
12
 
12
13
  ## Install
13
14
 
@@ -20,6 +21,7 @@ npm install tiny-ja-date
20
21
  ```ts
21
22
  import {
22
23
  addBusinessDays,
24
+ countBusinessDays,
23
25
  formatJaDate,
24
26
  formatRelativeJaDate,
25
27
  isBusinessDay,
@@ -44,6 +46,9 @@ nextBusinessDay(new Date(2026, 4, 15))
44
46
 
45
47
  addBusinessDays("2026-05-15", 2)
46
48
  // Date for 2026-05-19
49
+
50
+ countBusinessDays("2026-05-15", "2026-05-19")
51
+ // 2
47
52
  ```
48
53
 
49
54
  ## API
@@ -80,7 +85,8 @@ formatRelativeJaDate(new Date(2026, 4, 15), new Date(2026, 4, 12))
80
85
 
81
86
  ### `isWeekendJP(date: DateInput, options?: DateOptions): boolean`
82
87
 
83
- Checks whether the date is Saturday or Sunday.
88
+ Checks whether the date is Saturday or Sunday, regardless of the weekly
89
+ closures used by the business-day functions.
84
90
 
85
91
  ```ts
86
92
  isWeekendJP(new Date(2026, 4, 16))
@@ -90,9 +96,10 @@ isWeekendJP(new Date(2026, 4, 16))
90
96
  ### `isBusinessDay(date: DateInput, options?: BusinessDayOptions): boolean`
91
97
 
92
98
  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.
99
+ weekly closures, dates in `options.holidays`, and dates skipped entirely by
100
+ the time zone. A date that starts after midnight can still be a business day.
101
+ Invalid holiday inputs throw a `TypeError`, even when the input falls on a
102
+ weekly closure.
96
103
 
97
104
  ```ts
98
105
  const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
@@ -110,9 +117,9 @@ Returns a `Date` for the start of the next business day in the selected time
110
117
  zone. If midnight is skipped, the result is the first valid time on that date.
111
118
  If a time zone skipped an entire date, the search continues to the following date.
112
119
 
113
- The search starts the day after the input and skips Saturdays, Sundays, and
114
- any local calendar dates supplied in `options.holidays`. Each holiday accepts
115
- the same `DateInput` type as the input date.
120
+ The search starts the day after the input and skips weekly closures and dates
121
+ in `options.holidays`. Each holiday accepts the same `DateInput` type as the
122
+ input date.
116
123
 
117
124
  ```ts
118
125
  nextBusinessDay(new Date(2026, 4, 15))
@@ -125,7 +132,7 @@ nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
125
132
  ### `addBusinessDays(date: DateInput, amount: number, options?: BusinessDayOptions): Date`
126
133
 
127
134
  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
135
+ starts the day after or before the input and skips weekly closures, dates in
129
136
  `options.holidays`, and dates skipped entirely by the time zone.
130
137
  `addBusinessDays(date, 1, options)` is equivalent to `nextBusinessDay(date, options)`.
131
138
 
@@ -145,6 +152,56 @@ addBusinessDays("2026-11-24", -1, options)
145
152
  // Start of 2026-11-20 in Tokyo
146
153
  ```
147
154
 
155
+ ### `countBusinessDays(from: DateInput, to: DateInput, options?: BusinessDayOptions): number`
156
+
157
+ Counts from the day after or before `from` through `to` in the selected time
158
+ zone. The starting date is excluded, and the destination counts if it is a
159
+ business day. Counts are positive going forward and negative going backward.
160
+ The same calendar date or a range with no business days returns `0`.
161
+
162
+ Weekly closures, dates in `options.holidays`, and dates skipped entirely by the time
163
+ zone do not count. Both endpoints and all holiday inputs are validated even
164
+ when the result is `0`.
165
+
166
+ ```ts
167
+ const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
168
+
169
+ countBusinessDays("2026-11-20", "2026-11-25", options) // 2
170
+ countBusinessDays("2026-11-25", "2026-11-20", options) // -2
171
+
172
+ countBusinessDays("2026-11-20", "2026-11-22", options) // 0
173
+ countBusinessDays("2026-11-22", "2026-11-20", options) // -1
174
+ ```
175
+
176
+ ## Weekly closures
177
+
178
+ `isBusinessDay`, `nextBusinessDay`, `addBusinessDays`, and `countBusinessDays`
179
+ accept `weekendDays?: readonly number[]` in `BusinessDayOptions`. Use integers
180
+ from `0` (Sunday) through `6` (Saturday).
181
+
182
+ - Omitting `weekendDays` closes Saturday and Sunday, equivalent to `[0, 6]`.
183
+ - An explicit array replaces the default. An empty array clears all weekly
184
+ closures. `holidays` still applies to individual dates.
185
+ - Duplicate weekdays count once. Values outside the integer range throw a
186
+ `RangeError`. A non-array value throws a `TypeError`. Validation also applies
187
+ to zero-day moves and same-date counts.
188
+ - Closing all seven days makes `isBusinessDay` return `false` and
189
+ `countBusinessDays` return `0`. `nextBusinessDay` and nonzero
190
+ `addBusinessDays` calls throw a `TypeError`. A zero-day move still returns the
191
+ start of the input date.
192
+
193
+ ```ts
194
+ const options = {
195
+ timeZone: "Asia/Tokyo",
196
+ weekendDays: [0],
197
+ holidays: ["2026-11-23"],
198
+ }
199
+
200
+ isBusinessDay("2026-11-21", options) // true (Saturday)
201
+ nextBusinessDay("2026-11-20", options) // Start of 2026-11-21 in Tokyo
202
+ countBusinessDays("2026-11-20", "2026-11-24", options) // 2 (Saturday and Tuesday)
203
+ ```
204
+
148
205
  ## Time zones
149
206
 
150
207
  All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface DateOptions {
4
4
  }
5
5
  export interface BusinessDayOptions extends DateOptions {
6
6
  holidays?: readonly DateInput[];
7
+ weekendDays?: readonly number[];
7
8
  }
8
9
  export declare function formatJaDate(input: DateInput, options?: DateOptions): string;
9
10
  export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput, options?: DateOptions): string;
@@ -11,3 +12,4 @@ export declare function isWeekendJP(input: DateInput, options?: DateOptions): bo
11
12
  export declare function isBusinessDay(input: DateInput, options?: BusinessDayOptions): boolean;
12
13
  export declare function addBusinessDays(input: DateInput, amount: number, options?: BusinessDayOptions): Date;
13
14
  export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
15
+ export declare function countBusinessDays(from: DateInput, to: DateInput, options?: BusinessDayOptions): number;
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const WEEKDAYS_JA = ["日", "月", "火", "水", "木", "金", "土"];
2
+ const DEFAULT_WEEKEND_DAYS = new Set([0, 6]);
2
3
  const ONE_DAY_MS = 86400000;
3
4
  const MAX_DATE_MS = 8640000000000000;
4
5
  const GREGORIAN_CYCLE_MS = 146097 * ONE_DAY_MS;
@@ -140,12 +141,25 @@ function weekdayOf(day) {
140
141
  const weekday = (day / ONE_DAY_MS + 4) % 7;
141
142
  return (weekday + 7) % 7;
142
143
  }
143
- function isWeekend(day) {
144
- const weekday = weekdayOf(day);
145
- return weekday === 0 || weekday === 6;
144
+ function weekendDaySet(input) {
145
+ if (input === undefined)
146
+ return DEFAULT_WEEKEND_DAYS;
147
+ if (!Array.isArray(input))
148
+ throw new TypeError("Expected a weekendDays array");
149
+ const days = new Set();
150
+ for (const day of input) {
151
+ if (!Number.isInteger(day) || day < 0 || day >= WEEKDAYS_JA.length) {
152
+ throw new RangeError("Expected a weekend day from 0 to 6");
153
+ }
154
+ days.add(day);
155
+ }
156
+ return days;
146
157
  }
147
- function holidayDays(holidays, formatter) {
148
- return new Set(holidays?.map((holiday) => calendarDay(holiday, formatter)));
158
+ function businessCalendar(options, formatter) {
159
+ return {
160
+ holidays: new Set(options.holidays?.map((holiday) => calendarDay(holiday, formatter))),
161
+ weekendDays: weekendDaySet(options.weekendDays),
162
+ };
149
163
  }
150
164
  function dayStart(day, formatter) {
151
165
  if (formatter)
@@ -156,8 +170,8 @@ function dayStart(day, formatter) {
156
170
  local.setFullYear(year, month - 1, dayOfMonth);
157
171
  return local.getDate() === dayOfMonth ? local : undefined;
158
172
  }
159
- function businessDayStart(candidate, holidays, formatter) {
160
- if (isWeekend(candidate) || holidays.has(candidate))
173
+ function businessDayStart(candidate, calendar, formatter) {
174
+ if (calendar.weekendDays.has(weekdayOf(candidate)) || calendar.holidays.has(candidate))
161
175
  return undefined;
162
176
  return dayStart(candidate, formatter);
163
177
  }
@@ -182,26 +196,29 @@ export function formatRelativeJaDate(input, baseInput = new Date(), options = {}
182
196
  return `${diffDays}日後`;
183
197
  }
184
198
  export function isWeekendJP(input, options = {}) {
185
- return isWeekend(calendarDay(input, createFormatter(options.timeZone)));
199
+ const day = calendarDay(input, createFormatter(options.timeZone));
200
+ return DEFAULT_WEEKEND_DAYS.has(weekdayOf(day));
186
201
  }
187
202
  export function isBusinessDay(input, options = {}) {
188
203
  const formatter = createFormatter(options.timeZone);
189
204
  const day = dateOnlyDay(input) ?? calendarDay(input, formatter);
190
- const holidays = holidayDays(options.holidays, formatter);
191
- return businessDayStart(day, holidays, formatter) !== undefined;
205
+ const calendar = businessCalendar(options, formatter);
206
+ return businessDayStart(day, calendar, formatter) !== undefined;
192
207
  }
193
208
  export function addBusinessDays(input, amount, options = {}) {
194
209
  if (!Number.isSafeInteger(amount))
195
210
  throw new RangeError("Expected a safe integer business-day amount");
196
211
  const formatter = createFormatter(options.timeZone);
197
212
  const day = calendarDay(input, formatter);
198
- const holidays = holidayDays(options.holidays, formatter);
213
+ const calendar = businessCalendar(options, formatter);
199
214
  if (amount === 0) {
200
215
  const start = dayStart(day, formatter);
201
216
  if (!start)
202
217
  throw new TypeError("Invalid date");
203
218
  return start;
204
219
  }
220
+ if (calendar.weekendDays.size === WEEKDAYS_JA.length)
221
+ throw new TypeError("No business days");
205
222
  const direction = Math.sign(amount);
206
223
  const step = direction * ONE_DAY_MS;
207
224
  // A local calendar date can extend one day beyond the UTC Date limits.
@@ -210,7 +227,7 @@ export function addBusinessDays(input, amount, options = {}) {
210
227
  if (remaining > (limit - direction * day) / ONE_DAY_MS)
211
228
  throw new TypeError("Invalid date");
212
229
  for (let candidate = day + step; Math.abs(candidate) <= limit; candidate += step) {
213
- const start = businessDayStart(candidate, holidays, formatter);
230
+ const start = businessDayStart(candidate, calendar, formatter);
214
231
  if (!start)
215
232
  continue;
216
233
  remaining -= 1;
@@ -222,3 +239,21 @@ export function addBusinessDays(input, amount, options = {}) {
222
239
  export function nextBusinessDay(input, options = {}) {
223
240
  return addBusinessDays(input, 1, options);
224
241
  }
242
+ export function countBusinessDays(from, to, options = {}) {
243
+ const formatter = createFormatter(options.timeZone);
244
+ const start = dateOnlyDay(from) ?? calendarDay(from, formatter);
245
+ const end = dateOnlyDay(to) ?? calendarDay(to, formatter);
246
+ const calendar = businessCalendar(options, formatter);
247
+ if (calendar.weekendDays.size === WEEKDAYS_JA.length)
248
+ return 0;
249
+ const direction = Math.sign(end - start);
250
+ const step = direction * ONE_DAY_MS;
251
+ let count = 0;
252
+ let day = start;
253
+ while (day !== end) {
254
+ day += step;
255
+ if (businessDayStart(day, calendar, formatter))
256
+ count += direction;
257
+ }
258
+ return count;
259
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-ja-date",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Tiny TypeScript date utilities for Japanese UI.",
5
5
  "type": "module",
6
6
  "exports": {