feat: allow object format for range parameters#222
Conversation
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>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
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.
src/search-builder.ts
Outdated
| if ("equal" in obj) { | ||
| return obj.equal.toString() as Join<T>; | ||
| } | ||
| return `${obj.min ?? ""}-${obj.max ?? ""}` as Join<T | "">; |
There was a problem hiding this comment.
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 | "">;There was a problem hiding this comment.
minかmaxのどちらかがあった時だけ値をセットするように。型でも制限すると尚良い。
There was a problem hiding this comment.
ご指摘ありがとうございます!RangeParam型の定義を修正し、min または max (あるいは equal)が必ず含まれるように制約を加えました。また、range2string内で両方指定されなかった場合に備え、undefined を返すようにしてsetで不要な空文字が設定されないようガードを追加しました。
| */ | ||
| kaiwaritu(range: RangeParam<number>): this; | ||
|
|
||
| kaiwaritu(minOrRange: number | RangeParam<number>, max?: number): this { |
There was a problem hiding this comment.
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 {- 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>
There was a problem hiding this comment.
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, andkaiwarituto 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 }のテストが追加されていません。range2stringのequal分岐が期待通り"<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.
| * @template T - 範囲指定する値の型 | ||
| */ | ||
| export type RangeParam<T extends number> = | ||
| | { min: T; max?: T } | ||
| | { min?: T; max: T } |
There was a problem hiding this comment.
RangeParam の型定義が、JSDoc/PR説明にある { min?: T, max?: T } | { equal: T } と一致していません。現在の union だと {} は型エラーになりますが、range2string 側は {} を受けて undefined を返す実装になっています。{ min?: T; max?: T } を許可する形にするか、少なくとも JSDoc 側で「min/max のどちらか必須」と明記して型と挙動を揃えてください。
| * @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 } |
| } | ||
| return `${obj.min ?? ""}-${obj.max ?? ""}` as Join<T | "">; | ||
| } | ||
| return SearchBuilderBase.array2string(n) as Join<T>; |
There was a problem hiding this comment.
range2string は配列入力をそのまま array2string に委譲しているため、数値配列の要素数が 2 以外(例: [1,2,3])でも 1-2-3 のような文字列になり、range 系パラメータとしては不正なクエリを生成します。range 用ユーティリティとしては、配列はタプル [min, max] のみ受け付ける(型を readonly [T, T] にする)か、ランタイムで長さチェックしてエラーにするなどの防止策を入れた方が安全です。
| 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>; |
There was a problem hiding this comment.
array2stringが意図しているタプルを返すかを(型かランタイムで)検証をしっかりすること
There was a problem hiding this comment.
range2string は配列入力をそのまま array2string に委譲しているため、数値配列の要素数が 2 以外(例: [1,2,3])でも 1-2-3 のような文字列になり、range 系パラメータとしては不正なクエリを生成します。range 用ユーティリティとしては、配列はタプル [min, max] のみ受け付ける(型を readonly [T, T] にする)か、ランタイムで長さチェックしてエラーにするなどの防止策を入れた方が安全です。
There was a problem hiding this comment.
ご指摘ありがとうございます!range2stringが不適切な要素数(3以上)の配列を受け付けないようにランタイムチェックを追加し、さらに安全性を高めるため、TypeScriptの型定義でも readonly [number, number] のタプル型を要求するように変更しました。
| /** | ||
| * 抽出する作品の挿絵数を指定します (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; |
There was a problem hiding this comment.
sasie / time も RangeParam を受け付けるようになりましたが、テストが一部の形(例: sasie は { min } のみ、time は { min, max } のみ)しかカバーしていません。{ max } や { equal } など、サポートするとして公開したオブジェクト形状を最低限網羅するテストを追加してください。
There was a problem hiding this comment.
ご指摘ありがとうございます。sasie および time に対しても { min: x, max: y } や { equal: z } といったオブジェクト形式での網羅的なテストケースを追加し、不足を補いました。
There was a problem hiding this comment.
💡 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".
src/search-builder.ts
Outdated
| if ("equal" in obj) { | ||
| return obj.equal.toString() as Join<T>; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
ご指摘ありがとうございます。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>
|
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>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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 likelength,sasie,time, andkaiwaritunatively accept objects matching{ min?: T, max?: T } | { equal: T }.Changes Made:
RangeParamtype to utility types.NovelSearchBuilderBaseby creating arange2stringutility which handles parsing the new object shape into themin-maxformat expected by the API.length,sasie,time, andkaiwaritu).PR created automatically by Jules for task 4859039751617875636 started by @deflis