Skip to content

feat: allow object format for range parameters#222

Open
deflis wants to merge 4 commits intomasterfrom
feature/range-param-object-4859039751617875636
Open

feat: allow object format for range parameters#222
deflis wants to merge 4 commits intomasterfrom
feature/range-param-object-4859039751617875636

Conversation

@deflis
Copy link
Copy Markdown
Owner

@deflis deflis commented Apr 10, 2026

This PR introduces the ability to use object formats for setting range-based filter parameters, enhancing readability and developer experience.

Previously, users had to use [min, max] tuples and manage omit limits. Now, fields like length, sasie, time, and kaiwaritu natively accept objects matching { min?: T, max?: T } | { equal: T }.

Changes Made:

  • Added RangeParam type to utility types.
  • Enhanced parameter processing inside NovelSearchBuilderBase by creating a range2string utility which handles parsing the new object shape into the min-max format expected by the API.
  • Updated typing and overloading for the related parameters (length, sasie, time, and kaiwaritu).
  • Added robust unit test coverage.

PR created automatically by Jules for task 4859039751617875636 started by @deflis

Add support for specifying range parameters (`length`, `sasie`, `time`, `kaiwaritu`) using an object format (`{ min?: number, max?: number } | { equal: number }`). This provides a more intuitive and readable way to specify minimum/maximum constraints compared to relying strictly on arrays with `null`/`undefined`.

- Created `RangeParam` type in `src/util/type.ts`.
- Implemented `range2string` utility in `SearchBuilderBase`.
- Updated `length`, `sasie`, `time`, and `kaiwaritu` API signatures.
- Added test coverage in `test/search-builder.test.ts`.

Co-authored-by: deflis <206113+deflis@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the RangeParam type to support object-based range specifications (using min, max, or equal) for search parameters such as length, kaiwaritu, sasie, and time. It also adds a range2string utility to handle the conversion of these parameters into the API's hyphen-separated string format. Feedback suggests refining range2string to return an empty string instead of "-" when an empty object is provided, and updating the kaiwaritu method signature to include readonly number[] for consistency with other methods.

if ("equal" in obj) {
return obj.equal.toString() as Join<T>;
}
return `${obj.min ?? ""}-${obj.max ?? ""}` as Join<T | "">;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When RangeParam is an empty object {}, this currently returns "-". This might be interpreted as an invalid value by the Narou API. It is safer to return an empty string "" when both min and max are undefined.

      return (obj.min !== undefined || obj.max !== undefined
        ? `${obj.min ?? ""}-${obj.max ?? ""}`
        : "") as Join<T | "">;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minかmaxのどちらかがあった時だけ値をセットするように。型でも制限すると尚良い。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます!RangeParam型の定義を修正し、min または max (あるいは equal)が必ず含まれるように制約を加えました。また、range2string内で両方指定されなかった場合に備え、undefined を返すようにしてsetで不要な空文字が設定されないようガードを追加しました。

*/
kaiwaritu(range: RangeParam<number>): this;

kaiwaritu(minOrRange: number | RangeParam<number>, max?: number): this {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with length, sasie, and time, the kaiwaritu method should also support readonly number[] in its signature. The implementation already correctly handles arrays via range2string because typeof array === "object" is true.

  kaiwaritu(
    minOrRange: number | readonly number[] | RangeParam<number>,
    max?: number
  ): this {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会話率も同じシグネチャのほうがいい

- Ensure type `RangeParam` strictly requires at least `min` or `max` when using range object notation.
- Make `range2string` return `undefined` if neither bound is present (although type system restricts this).
- Add undefined checks before setting the parameter value to avoid empty parameters in queries.

Co-authored-by: deflis <206113+deflis@users.noreply.github.com>
@deflis deflis marked this pull request as ready for review April 12, 2026 10:28
Copilot AI review requested due to automatic review settings April 12, 2026 10:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the novel search builder API by allowing range-based filter parameters to be passed as objects (e.g. { min, max } / { equal }) instead of only numeric tuples, improving readability and ergonomics.

Changes:

  • Added a new RangeParam<T> utility type for expressing range/equality object inputs.
  • Introduced SearchBuilderBase.range2string() to normalize object/array/single-number inputs into the API’s expected string format.
  • Updated length, sasie, time, and kaiwaritu to accept the new object form and added unit tests for the new behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/util/type.ts Adds the RangeParam<T> type used by the builder public API.
src/search-builder.ts Adds range2string and updates range-accepting builder methods to support object inputs.
test/search-builder.test.ts Adds tests validating the new object input format for selected parameters.
Comments suppressed due to low confidence (1)

src/search-builder.ts:369

  • 新しいオブジェクト形式は RangeParam 的に { equal: number } も許容していますが、kaiwaritu については { equal } のテストが追加されていません。range2stringequal 分岐が期待通り "<value>" になることをテストで担保してください。
  /**
   * 抽出する作品の会話率を%単位で範囲指定またはオブジェクトで指定します (kaiwaritu)。
   * @param range 範囲指定オブジェクト
   * @return {this}
   */
  kaiwaritu(range: RangeParam<number>): this;

  kaiwaritu(minOrRange: number | RangeParam<number>, max?: number): this {
    if (typeof minOrRange === "object" && minOrRange !== null) {
      const val = NovelSearchBuilderBase.range2string(minOrRange);
      if (val !== undefined) {
        this.set({ kaiwaritu: val });
      }
      return this;
    }

    const min = minOrRange;
    let n: number | string;
    if (max != null) {
      n = `${min}-${max}`;
    } else {
      n = min;
    }
    this.set({ kaiwaritu: n });
    return this;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +30 to +34
* @template T - 範囲指定する値の型
*/
export type RangeParam<T extends number> =
| { min: T; max?: T }
| { min?: T; max: T }
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RangeParam の型定義が、JSDoc/PR説明にある { min?: T, max?: T } | { equal: T } と一致していません。現在の union だと {} は型エラーになりますが、range2string 側は {} を受けて undefined を返す実装になっています。{ min?: T; max?: T } を許可する形にするか、少なくとも JSDoc 側で「min/max のどちらか必須」と明記して型と挙動を揃えてください。

Suggested change
* @template T - 範囲指定する値の型
*/
export type RangeParam<T extends number> =
| { min: T; max?: T }
| { min?: T; max: T }
* `min` / `max` による範囲指定、または `equal` による完全一致を表します。
* `min` `max` はどちらも省略可能です。
* @template T - 範囲指定する値の型
*/
export type RangeParam<T extends number> =
| { min?: T; max?: T }

Copilot uses AI. Check for mistakes.
}
return `${obj.min ?? ""}-${obj.max ?? ""}` as Join<T | "">;
}
return SearchBuilderBase.array2string(n) as Join<T>;
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range2string は配列入力をそのまま array2string に委譲しているため、数値配列の要素数が 2 以外(例: [1,2,3])でも 1-2-3 のような文字列になり、range 系パラメータとしては不正なクエリを生成します。range 用ユーティリティとしては、配列はタプル [min, max] のみ受け付ける(型を readonly [T, T] にする)か、ランタイムで長さチェックしてエラーにするなどの防止策を入れた方が安全です。

Suggested change
return SearchBuilderBase.array2string(n) as Join<T>;
if (Array.isArray(n)) {
if (n.length !== 2) {
throw new TypeError(
"range2string only accepts arrays with exactly two elements: [min, max]"
);
}
return `${n[0]}-${n[1]}` as Join<T>;
}
return n.toString() as Join<T>;

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array2stringが意図しているタプルを返すかを(型かランタイムで)検証をしっかりすること

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range2string は配列入力をそのまま array2string に委譲しているため、数値配列の要素数が 2 以外(例: [1,2,3])でも 1-2-3 のような文字列になり、range 系パラメータとしては不正なクエリを生成します。range 用ユーティリティとしては、配列はタプル [min, max] のみ受け付ける(型を readonly [T, T] にする)か、ランタイムで長さチェックしてエラーにするなどの防止策を入れた方が安全です。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます!range2stringが不適切な要素数(3以上)の配列を受け付けないようにランタイムチェックを追加し、さらに安全性を高めるため、TypeScriptの型定義でも readonly [number, number] のタプル型を要求するように変更しました。

Comment on lines 372 to 397
/**
* 抽出する作品の挿絵数を指定します (sasie)。
* @param num 挿絵数、または[最小挿絵数, 最大挿絵数]
* オブジェクトによる指定 ({ min?: number, max?: number } または { equal: number }) も可能です。
* @param num 挿絵数、または[最小挿絵数, 最大挿絵数]、またはオブジェクト指定
* @return {this}
*/
sasie(num: number | readonly number[]): this {
this.set({ sasie: NovelSearchBuilderBase.array2string(num) });
sasie(num: number | readonly number[] | RangeParam<number>): this {
const val = NovelSearchBuilderBase.range2string(num);
if (val !== undefined) {
this.set({ sasie: val });
}
return this;
}

/**
* 抽出する作品の予想読了時間を分単位で指定します (time)。
* @param num 読了時間(分)、または[最小読了時間, 最大読了時間]
* オブジェクトによる指定 ({ min?: number, max?: number } または { equal: number }) も可能です。
* @param num 読了時間(分)、または[最小読了時間, 最大読了時間]、またはオブジェクト指定
* @return {this}
*/
time(num: number | readonly number[]): this {
this.set({ time: NovelSearchBuilderBase.array2string(num) });
time(num: number | readonly number[] | RangeParam<number>): this {
const val = NovelSearchBuilderBase.range2string(num);
if (val !== undefined) {
this.set({ time: val });
}
return this;
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sasie / timeRangeParam を受け付けるようになりましたが、テストが一部の形(例: sasie{ min } のみ、time{ min, max } のみ)しかカバーしていません。{ max }{ equal } など、サポートするとして公開したオブジェクト形状を最低限網羅するテストを追加してください。

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テストが足りてない

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。sasie および time に対しても { min: x, max: y }{ equal: z } といったオブジェクト形式での網羅的なテストケースを追加し、不足を補いました。

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bf7d4d1fa4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +69 to +70
if ("equal" in obj) {
return obj.equal.toString() as Join<T>;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate equal before coercing it to string

The new object-range path assumes that any object with an equal key has a numeric value, but obj.equal.toString() will throw when JS callers pass { equal: undefined } (or a widened optional value), causing query construction to fail at runtime. This regression is introduced by the new range2string branch and can be avoided by checking for null/undefined (or narrowing to a number) before calling toString and then falling back to the min/max handling or a controlled error.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equalが数値化を検証するべき

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。equal が数値であるかを検証するため、typeof obj.equal === "number" の実行時チェックを追加しました。

- Adds a runtime validation check `typeof n.equal === "number"` in `range2string` utility logic.
- Adds comprehensive testing for `{ max: x }`, `{ min: x, max: y }`, and `{ equal: x }` cases in `sasie` and `time` parameters within `search-builder.test.ts`.

Co-authored-by: deflis <206113+deflis@users.noreply.github.com>
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

…meters

- Modifies the parameter signature for range parameters (`length`, `sasie`, `time`) to accept `readonly [number, number]` instead of arbitrary `number[]` arrays to tighten type safety.
- Adds a runtime length check inside `range2string` that throws an error if an array with more than 2 elements is passed, preventing incorrectly formatted query parameters (e.g. `1-2-3`).
- Adds a unit test validating this specific error condition.

Co-authored-by: deflis <206113+deflis@users.noreply.github.com>
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants