Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 67a175d

Browse files
authored
Merge pull request #62 from exoego/querystring
Overhaul querystring module
2 parents 589672c + 036c037 commit 67a175d

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

app/current/src/main/scala/io/scalajs/nodejs/querystring/package.scala

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.scalajs.nodejs
22

33
import io.scalajs.RawOptions
44

5+
import scala.scalajs.js
56
import scala.scalajs.js.|
67

78
/**
@@ -13,21 +14,12 @@ package object querystring {
1314
* Query String Enrichment
1415
* @param qs the given [[QueryString]]
1516
*/
16-
implicit class QueryStringEnrichment(val qs: QueryString) extends AnyVal {
17-
18-
/**
19-
* The querystring.parse() method parses a URL query string (str) into a collection of key and value pairs.
20-
* @param str The URL query string to parse
21-
* @param sep The substring used to delimit key and value pairs in the query string. Defaults to '&'.
22-
* @param eq The substring used to delimit keys and values in the query string. Defaults to '='.
23-
* @param options The given [[QueryDecodeOptions options]]
24-
* @return
25-
*/
17+
implicit final class QueryStringEnrichment(private val qs: QueryString) extends AnyVal {
2618
@inline
27-
def parseAs[T](str: String,
28-
sep: String = null,
29-
eq: String = null,
30-
options: QueryDecodeOptions | RawOptions = null): T = {
19+
def parseAs[T <: js.Object](str: String,
20+
sep: String = null,
21+
eq: String = null,
22+
options: QueryDecodeOptions | RawOptions = null): T = {
3123
qs.parse(str, sep, eq, options).asInstanceOf[T]
3224
}
3325

app/nodejs-v8/src/test/scala/nodejs/querystring/QueryStringTest.scala

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.scalajs.nodejs.querystring
22

3-
import io.scalajs.JSON
43
import io.scalajs.nodejs.Assert
54
import io.scalajs.nodejs.querystring.QueryStringTest.MyParams
65
import org.scalatest.FunSpec
@@ -16,36 +15,32 @@ class QueryStringTest extends FunSpec {
1615

1716
it("should escape(...)") {
1817
val result = QueryString.escape("""https://www.google.com/#q=node?key=1234""")
19-
info(s"result: ${JSON.stringify(result)}")
20-
assert(
21-
QueryString
22-
.escape("""https://www.google.com/#q=node?key=1234""") == "https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234"
23-
)
18+
assert(result === "https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234")
2419
}
2520

2621
it("should parse(...)") {
2722
val result = QueryString.parse("""https://www.google.com/#q=node?key=1234""")
28-
info(s"result: ${JSON.stringify(result)}")
2923
Assert.deepEqual(result, js.Dictionary("https://www.google.com/#q" -> "node?key=1234"))
3024
}
3125

3226
it("should stringify(...)") {
3327
val result = QueryString.stringify(new MyParams(foo = "1", bar = "2"))
34-
info(s"result: ${JSON.stringify(result)}")
35-
assert(result == "foo=1&bar=2")
28+
assert(result === "foo=1&bar=2")
3629
}
3730

3831
it("should unescape(...)") {
3932
val result = QueryString.unescape("https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234")
40-
info(s"result: ${JSON.stringify(result)}")
41-
assert(
42-
QueryString
43-
.unescape("https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234") == """https://www.google.com/#q=node?key=1234"""
44-
)
33+
assert(result === """https://www.google.com/#q=node?key=1234""")
4534
}
46-
4735
}
4836

37+
describe("QueryStringEnrichment") {
38+
it("parseAs") {
39+
val result: MyParams = QueryString.parseAs("foo=1&bar=2")
40+
assert(result.foo === "1")
41+
assert(result.bar === "2")
42+
}
43+
}
4944
}
5045

5146
/**

0 commit comments

Comments
 (0)