Skip to content

Commit

Permalink
Merge pull request #39 from sousousore1/support-array-parameters
Browse files Browse the repository at this point in the history
Add support for parameters as Array
  • Loading branch information
ohakutsu committed Apr 18, 2024
2 parents 60bdb19 + 5fee200 commit 04d233f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
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

0 comments on commit 04d233f

Please sign in to comment.