Skip to content

Commit

Permalink
fix: tags being joined with encoded characters
Browse files Browse the repository at this point in the history
  • Loading branch information
AtoraSuunva committed Jul 30, 2022
1 parent 76199a8 commit 608d945
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# booru Changelog

## 2.6.1

- Fixed tags being incorrectly joined with an encoded `+`/`,` (#93)

## 2.6.0

- Added credential support
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "booru",
"version": "2.6.0",
"version": "2.6.1",
"description": "Search (and do other things) on a bunch of different boorus!",
"author": "AtoraSuunva (https://github.com/AtoraSuunva/)",
"license": "MIT",
Expand Down
17 changes: 11 additions & 6 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ export function searchURI(
page = 0,
credentials: BooruCredentials = {},
): string {
const query = querystring({
[site.tagQuery]: expandTags(tags).join(site.tagJoin),
limit,
[site.paginate]: page,
...credentials,
})
const query = querystring(
{
[site.tagQuery]: expandTags(tags),
limit,
[site.paginate]: page,
...credentials,
},
{
arrayJoin: site.tagJoin,
},
)

return (
`http${site.insecure ? '' : 's'}://` +
Expand Down
24 changes: 20 additions & 4 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ export function compareArrays(arr1: string[], arr2: string[]): string[] {
type URIEncodable = string | number | boolean
type QueryValue = URIEncodable | URIEncodable[]

interface QuerystringOptions {
arrayJoin?: string
}

interface EncodeURIQueryValueOptions {
arrayJoin?: string
}

/**
* Turns an object into a query string, correctly encoding uri components
*
Expand All @@ -204,11 +212,16 @@ type QueryValue = URIEncodable | URIEncodable[]
* @param query An object with key/value pairs that will be turned into a string
* @returns A string that can be appended to a url (after `?`)
*/
export function querystring(query: Record<string, QueryValue>): string {
export function querystring(
query: Record<string, QueryValue>,
{ arrayJoin = '+' }: QuerystringOptions = {},
): string {
return Object.entries(query)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIQueryValue(value)}`,
`${encodeURIComponent(key)}=${encodeURIQueryValue(value, {
arrayJoin,
})}`,
)
.join('&')
}
Expand All @@ -219,9 +232,12 @@ export function querystring(query: Record<string, QueryValue>): string {
* @param value The value to encode
* @returns An encoded value that can be passed to a querystring
*/
export function encodeURIQueryValue(value: QueryValue): string {
export function encodeURIQueryValue(
value: QueryValue,
{ arrayJoin = '+' }: EncodeURIQueryValueOptions = {},
): string {
if (Array.isArray(value)) {
return value.map(encodeURIComponent).join('+')
return value.map(encodeURIComponent).join(arrayJoin)
} else {
return encodeURIComponent(value)
}
Expand Down

0 comments on commit 608d945

Please sign in to comment.