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

Overhaul querystring module #62

Merged
merged 1 commit into from
Sep 24, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.scalajs.nodejs

import io.scalajs.RawOptions

import scala.scalajs.js
import scala.scalajs.js.|

/**
Expand All @@ -13,21 +14,12 @@ package object querystring {
* Query String Enrichment
* @param qs the given [[QueryString]]
*/
implicit class QueryStringEnrichment(val qs: QueryString) extends AnyVal {

/**
* The querystring.parse() method parses a URL query string (str) into a collection of key and value pairs.
* @param str The URL query string to parse
* @param sep The substring used to delimit key and value pairs in the query string. Defaults to '&'.
* @param eq The substring used to delimit keys and values in the query string. Defaults to '='.
* @param options The given [[QueryDecodeOptions options]]
* @return
*/
implicit final class QueryStringEnrichment(private val qs: QueryString) extends AnyVal {
@inline
def parseAs[T](str: String,
sep: String = null,
eq: String = null,
options: QueryDecodeOptions | RawOptions = null): T = {
def parseAs[T <: js.Object](str: String,
sep: String = null,
eq: String = null,
options: QueryDecodeOptions | RawOptions = null): T = {
qs.parse(str, sep, eq, options).asInstanceOf[T]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.scalajs.nodejs.querystring

import io.scalajs.JSON
import io.scalajs.nodejs.Assert
import io.scalajs.nodejs.querystring.QueryStringTest.MyParams
import org.scalatest.FunSpec
Expand All @@ -16,36 +15,32 @@ class QueryStringTest extends FunSpec {

it("should escape(...)") {
val result = QueryString.escape("""https://www.google.com/#q=node?key=1234""")
info(s"result: ${JSON.stringify(result)}")
assert(
QueryString
.escape("""https://www.google.com/#q=node?key=1234""") == "https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234"
)
assert(result === "https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234")
}

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

it("should stringify(...)") {
val result = QueryString.stringify(new MyParams(foo = "1", bar = "2"))
info(s"result: ${JSON.stringify(result)}")
assert(result == "foo=1&bar=2")
assert(result === "foo=1&bar=2")
}

it("should unescape(...)") {
val result = QueryString.unescape("https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234")
info(s"result: ${JSON.stringify(result)}")
assert(
QueryString
.unescape("https%3A%2F%2Fwww.google.com%2F%23q%3Dnode%3Fkey%3D1234") == """https://www.google.com/#q=node?key=1234"""
)
assert(result === """https://www.google.com/#q=node?key=1234""")
}

}

describe("QueryStringEnrichment") {
it("parseAs") {
val result: MyParams = QueryString.parseAs("foo=1&bar=2")
assert(result.foo === "1")
assert(result.bar === "2")
}
}
}

/**
Expand Down