tiny-ja-date 0.7.0 → 0.9.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
@@ -9,6 +9,7 @@ Tiny TypeScript date utilities for Japanese UI.
9
9
  - 次の営業日計算
10
10
  - 営業日数の加算・減算
11
11
  - 2つの日付間の営業日数
12
+ - 期間内の営業日一覧
12
13
 
13
14
  ## Install
14
15
 
@@ -22,6 +23,7 @@ npm install tiny-ja-date
22
23
  import {
23
24
  addBusinessDays,
24
25
  countBusinessDays,
26
+ createBusinessCalendar,
25
27
  formatJaDate,
26
28
  formatRelativeJaDate,
27
29
  isBusinessDay,
@@ -49,6 +51,10 @@ addBusinessDays("2026-05-15", 2)
49
51
 
50
52
  countBusinessDays("2026-05-15", "2026-05-19")
51
53
  // 2
54
+
55
+ const calendar = createBusinessCalendar({ timeZone: "Asia/Tokyo", weekendDays: [0] })
56
+ calendar.isBusinessDay("2026-11-21")
57
+ // true
52
58
  ```
53
59
 
54
60
  ## API
@@ -173,10 +179,61 @@ countBusinessDays("2026-11-20", "2026-11-22", options) // 0
173
179
  countBusinessDays("2026-11-22", "2026-11-20", options) // -1
174
180
  ```
175
181
 
182
+ ### `listBusinessDays(from: DateInput, to: DateInput, options?: BusinessDayOptions): Date[]`
183
+
184
+ Lists business days from `from` through `to`, including either endpoint when it
185
+ is a business day. The dates appear in the requested direction. Equal endpoints
186
+ return one date if it is a business day, or an empty array otherwise.
187
+
188
+ Weekly closures, individual holidays, and dates skipped entirely by the time zone
189
+ are excluded. Each `Date` represents the day's first valid time in the selected
190
+ time zone. Each call returns a fresh array with fresh `Date` objects.
191
+
192
+ ```ts
193
+ const options = {
194
+ timeZone: "Asia/Tokyo",
195
+ weekendDays: [0],
196
+ holidays: ["2026-11-23"],
197
+ }
198
+
199
+ listBusinessDays("2026-11-20", "2026-11-24", options)
200
+ // Starts of November 20, 21, and 24 in Tokyo
201
+
202
+ listBusinessDays("2026-11-24", "2026-11-20", options)
203
+ // Starts of November 24, 21, and 20 in Tokyo
204
+ ```
205
+
206
+ ### `createBusinessCalendar(options?: BusinessDayOptions): BusinessCalendar`
207
+
208
+ Creates a reusable calendar with `isBusinessDay(date)`, `nextBusinessDay(date)`,
209
+ `addBusinessDays(date, amount)`, `countBusinessDays(from, to)`, and
210
+ `listBusinessDays(from, to)` methods.
211
+ Each method follows the same rules as the standalone function. The factory
212
+ validates the time zone, weekend days, and holidays when creating the calendar.
213
+ Method arguments are validated on each call.
214
+
215
+ The calendar stores the rules as they were at creation. Editing the original
216
+ options, arrays, or holiday `Date` objects has no effect on an existing calendar.
217
+ Create a new calendar when the rules change. The default uses the runtime's
218
+ local time zone and closes Saturday and Sunday.
219
+
220
+ ```ts
221
+ const calendar = createBusinessCalendar({
222
+ timeZone: "Asia/Tokyo",
223
+ weekendDays: [0],
224
+ holidays: ["2026-11-23"],
225
+ })
226
+
227
+ calendar.isBusinessDay("2026-11-21") // true
228
+ calendar.countBusinessDays("2026-11-20", "2026-11-24") // 2
229
+ calendar.listBusinessDays("2026-11-20", "2026-11-24")
230
+ // Starts of November 20, 21, and 24 in Tokyo
231
+ ```
232
+
176
233
  ## Weekly closures
177
234
 
178
- `isBusinessDay`, `nextBusinessDay`, `addBusinessDays`, and `countBusinessDays`
179
- accept `weekendDays?: readonly number[]` in `BusinessDayOptions`. Use integers
235
+ `isBusinessDay`, `nextBusinessDay`, `addBusinessDays`, `countBusinessDays`, and
236
+ `listBusinessDays` accept `weekendDays?: readonly number[]` in `BusinessDayOptions`. Use integers
180
237
  from `0` (Sunday) through `6` (Saturday).
181
238
 
182
239
  - Omitting `weekendDays` closes Saturday and Sunday, equivalent to `[0, 6]`.
@@ -186,7 +243,8 @@ from `0` (Sunday) through `6` (Saturday).
186
243
  `RangeError`. A non-array value throws a `TypeError`. Validation also applies
187
244
  to zero-day moves and same-date counts.
188
245
  - Closing all seven days makes `isBusinessDay` return `false` and
189
- `countBusinessDays` return `0`. `nextBusinessDay` and nonzero
246
+ `countBusinessDays` return `0`. `listBusinessDays` returns an empty array.
247
+ `nextBusinessDay` and nonzero
190
248
  `addBusinessDays` calls throw a `TypeError`. A zero-day move still returns the
191
249
  start of the input date.
192
250
 
package/dist/index.d.ts CHANGED
@@ -6,6 +6,13 @@ export interface BusinessDayOptions extends DateOptions {
6
6
  holidays?: readonly DateInput[];
7
7
  weekendDays?: readonly number[];
8
8
  }
9
+ export interface BusinessCalendar {
10
+ isBusinessDay(input: DateInput): boolean;
11
+ nextBusinessDay(input: DateInput): Date;
12
+ addBusinessDays(input: DateInput, amount: number): Date;
13
+ countBusinessDays(from: DateInput, to: DateInput): number;
14
+ listBusinessDays(from: DateInput, to: DateInput): Date[];
15
+ }
9
16
  export declare function formatJaDate(input: DateInput, options?: DateOptions): string;
10
17
  export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput, options?: DateOptions): string;
11
18
  export declare function isWeekendJP(input: DateInput, options?: DateOptions): boolean;
@@ -13,3 +20,5 @@ export declare function isBusinessDay(input: DateInput, options?: BusinessDayOpt
13
20
  export declare function addBusinessDays(input: DateInput, amount: number, options?: BusinessDayOptions): Date;
14
21
  export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
15
22
  export declare function countBusinessDays(from: DateInput, to: DateInput, options?: BusinessDayOptions): number;
23
+ export declare function listBusinessDays(from: DateInput, to: DateInput, options?: BusinessDayOptions): Date[];
24
+ export declare function createBusinessCalendar(options?: BusinessDayOptions): BusinessCalendar;
package/dist/index.js CHANGED
@@ -155,7 +155,7 @@ function weekendDaySet(input) {
155
155
  }
156
156
  return days;
157
157
  }
158
- function businessCalendar(options, formatter) {
158
+ function businessDayRules(options, formatter) {
159
159
  return {
160
160
  holidays: new Set(options.holidays?.map((holiday) => calendarDay(holiday, formatter))),
161
161
  weekendDays: weekendDaySet(options.weekendDays),
@@ -170,8 +170,8 @@ function dayStart(day, formatter) {
170
170
  local.setFullYear(year, month - 1, dayOfMonth);
171
171
  return local.getDate() === dayOfMonth ? local : undefined;
172
172
  }
173
- function businessDayStart(candidate, calendar, formatter) {
174
- if (calendar.weekendDays.has(weekdayOf(candidate)) || calendar.holidays.has(candidate))
173
+ function businessDayStart(candidate, rules, formatter) {
174
+ if (rules.weekendDays.has(weekdayOf(candidate)) || rules.holidays.has(candidate))
175
175
  return undefined;
176
176
  return dayStart(candidate, formatter);
177
177
  }
@@ -202,22 +202,21 @@ export function isWeekendJP(input, options = {}) {
202
202
  export function isBusinessDay(input, options = {}) {
203
203
  const formatter = createFormatter(options.timeZone);
204
204
  const day = dateOnlyDay(input) ?? calendarDay(input, formatter);
205
- const calendar = businessCalendar(options, formatter);
206
- return businessDayStart(day, calendar, formatter) !== undefined;
205
+ const rules = businessDayRules(options, formatter);
206
+ return businessDayStart(day, rules, formatter) !== undefined;
207
207
  }
208
- export function addBusinessDays(input, amount, options = {}) {
208
+ function validateBusinessDayAmount(amount) {
209
209
  if (!Number.isSafeInteger(amount))
210
210
  throw new RangeError("Expected a safe integer business-day amount");
211
- const formatter = createFormatter(options.timeZone);
212
- const day = calendarDay(input, formatter);
213
- const calendar = businessCalendar(options, formatter);
211
+ }
212
+ function moveBusinessDays(day, amount, rules, formatter) {
214
213
  if (amount === 0) {
215
214
  const start = dayStart(day, formatter);
216
215
  if (!start)
217
216
  throw new TypeError("Invalid date");
218
217
  return start;
219
218
  }
220
- if (calendar.weekendDays.size === WEEKDAYS_JA.length)
219
+ if (rules.weekendDays.size === WEEKDAYS_JA.length)
221
220
  throw new TypeError("No business days");
222
221
  const direction = Math.sign(amount);
223
222
  const step = direction * ONE_DAY_MS;
@@ -227,7 +226,7 @@ export function addBusinessDays(input, amount, options = {}) {
227
226
  if (remaining > (limit - direction * day) / ONE_DAY_MS)
228
227
  throw new TypeError("Invalid date");
229
228
  for (let candidate = day + step; Math.abs(candidate) <= limit; candidate += step) {
230
- const start = businessDayStart(candidate, calendar, formatter);
229
+ const start = businessDayStart(candidate, rules, formatter);
231
230
  if (!start)
232
231
  continue;
233
232
  remaining -= 1;
@@ -236,15 +235,17 @@ export function addBusinessDays(input, amount, options = {}) {
236
235
  }
237
236
  throw new TypeError("Invalid date");
238
237
  }
238
+ export function addBusinessDays(input, amount, options = {}) {
239
+ validateBusinessDayAmount(amount);
240
+ const formatter = createFormatter(options.timeZone);
241
+ const day = calendarDay(input, formatter);
242
+ return moveBusinessDays(day, amount, businessDayRules(options, formatter), formatter);
243
+ }
239
244
  export function nextBusinessDay(input, options = {}) {
240
245
  return addBusinessDays(input, 1, options);
241
246
  }
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)
247
+ function countBusinessDaysBetween(start, end, rules, formatter) {
248
+ if (rules.weekendDays.size === WEEKDAYS_JA.length)
248
249
  return 0;
249
250
  const direction = Math.sign(end - start);
250
251
  const step = direction * ONE_DAY_MS;
@@ -252,8 +253,58 @@ export function countBusinessDays(from, to, options = {}) {
252
253
  let day = start;
253
254
  while (day !== end) {
254
255
  day += step;
255
- if (businessDayStart(day, calendar, formatter))
256
+ if (businessDayStart(day, rules, formatter))
256
257
  count += direction;
257
258
  }
258
259
  return count;
259
260
  }
261
+ export function countBusinessDays(from, to, options = {}) {
262
+ const formatter = createFormatter(options.timeZone);
263
+ const start = dateOnlyDay(from) ?? calendarDay(from, formatter);
264
+ const end = dateOnlyDay(to) ?? calendarDay(to, formatter);
265
+ return countBusinessDaysBetween(start, end, businessDayRules(options, formatter), formatter);
266
+ }
267
+ function listBusinessDaysBetween(from, to, rules, formatter) {
268
+ const start = dateOnlyDay(from) ?? calendarDay(from, formatter);
269
+ const end = dateOnlyDay(to) ?? calendarDay(to, formatter);
270
+ if (rules.weekendDays.size === WEEKDAYS_JA.length)
271
+ return [];
272
+ const days = [];
273
+ const step = start <= end ? ONE_DAY_MS : -ONE_DAY_MS;
274
+ for (let day = start;; day += step) {
275
+ const businessDay = businessDayStart(day, rules, formatter);
276
+ if (businessDay)
277
+ days.push(businessDay);
278
+ if (day === end)
279
+ return days;
280
+ }
281
+ }
282
+ export function listBusinessDays(from, to, options = {}) {
283
+ const formatter = createFormatter(options.timeZone);
284
+ return listBusinessDaysBetween(from, to, businessDayRules(options, formatter), formatter);
285
+ }
286
+ export function createBusinessCalendar(options = {}) {
287
+ const formatter = createFormatter(options.timeZone);
288
+ const rules = businessDayRules(options, formatter);
289
+ return {
290
+ isBusinessDay(input) {
291
+ const day = dateOnlyDay(input) ?? calendarDay(input, formatter);
292
+ return businessDayStart(day, rules, formatter) !== undefined;
293
+ },
294
+ nextBusinessDay(input) {
295
+ return moveBusinessDays(calendarDay(input, formatter), 1, rules, formatter);
296
+ },
297
+ addBusinessDays(input, amount) {
298
+ validateBusinessDayAmount(amount);
299
+ return moveBusinessDays(calendarDay(input, formatter), amount, rules, formatter);
300
+ },
301
+ countBusinessDays(from, to) {
302
+ const start = dateOnlyDay(from) ?? calendarDay(from, formatter);
303
+ const end = dateOnlyDay(to) ?? calendarDay(to, formatter);
304
+ return countBusinessDaysBetween(start, end, rules, formatter);
305
+ },
306
+ listBusinessDays(from, to) {
307
+ return listBusinessDaysBetween(from, to, rules, formatter);
308
+ },
309
+ };
310
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-ja-date",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "Tiny TypeScript date utilities for Japanese UI.",
5
5
  "type": "module",
6
6
  "exports": {