Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parameters as Array #39

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/js_rails_routes/language/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class JavaScript < Base
var query = [];
for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) {
if (keys.indexOf(param) === -1) {
query.push(param + "=" + encodeURIComponent(params[param]));
if (Array.isArray(params[param])) {
for (var value of params[param]) {
query.push(param + "[]=" + encodeURIComponent(value));
}
} else {
query.push(param + "=" + encodeURIComponent(params[param]));
}
}
}
return query.length ? route + "?" + query.join("&") : route;
Expand Down
10 changes: 8 additions & 2 deletions lib/js_rails_routes/language/typescript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ module JSRailsRoutes
module Language
class TypeScript < JavaScript
PROCESS_FUNC = <<~TYPESCRIPT
type Value = string | number
type Value = string | number | (string | number)[];
type Params<Keys extends string> = { [key in Keys]: Value } & Record<string, Value>
function process(route: string, params: Record<string, Value> | undefined, keys: string[]): string {
if (!params) return route
var query: string[] = [];
for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) {
if (keys.indexOf(param) === -1) {
query.push(param + "=" + encodeURIComponent(params[param].toString()));
if (Array.isArray(params[param])) {
for (var value of params[param] as (string | number)[]) {
query.push(param + "[]=" + encodeURIComponent(value.toString()));
}
} else {
query.push(param + "=" + encodeURIComponent(params[param].toString()));
}
}
}
return query.length ? route + "?" + query.join("&") : route;
Expand Down
8 changes: 7 additions & 1 deletion spec/js_rails_routes/language/javascript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
var query = [];
for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) {
if (keys.indexOf(param) === -1) {
query.push(param + "=" + encodeURIComponent(params[param]));
if (Array.isArray(params[param])) {
for (var value of params[param]) {
query.push(param + "[]=" + encodeURIComponent(value));
}
} else {
query.push(param + "=" + encodeURIComponent(params[param]));
}
}
}
return query.length ? route + "?" + query.join("&") : route;
Expand Down
10 changes: 8 additions & 2 deletions spec/js_rails_routes/language/typescript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@

it 'returns a typescript function' do
expect(subject).to eq <<~TYPESCRIPT
type Value = string | number
type Value = string | number | (string | number)[];
type Params<Keys extends string> = { [key in Keys]: Value } & Record<string, Value>
function process(route: string, params: Record<string, Value> | undefined, keys: string[]): string {
if (!params) return route
var query: string[] = [];
for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) {
if (keys.indexOf(param) === -1) {
query.push(param + "=" + encodeURIComponent(params[param].toString()));
if (Array.isArray(params[param])) {
for (var value of params[param] as (string | number)[]) {
query.push(param + "[]=" + encodeURIComponent(value.toString()));
}
} else {
query.push(param + "=" + encodeURIComponent(params[param].toString()));
}
}
}
return query.length ? route + "?" + query.join("&") : route;
Expand Down
Loading