Skip to content

Commit

Permalink
fix: Node.URL.URLSearchParams keys & values return undefined (#22)
Browse files Browse the repository at this point in the history
* fix: Node.URL.URLSearchParams keys & values return undefined

* docs: update changelog

* test: add tests

* test: formatting

* chore: fmt

* chore: add npm fmt script

---------

Co-authored-by: Orion Kindel <[email protected]>
  • Loading branch information
cakekindel and cakekindel committed Oct 8, 2023
1 parent 9c5a840 commit b9d3333
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Breaking changes:
New features:

Bugfixes:
- `URLSearchParams.keys` and `.values` returned undefined due to a bug in the foreign code (#22 by @cakekindel)

Other improvements:
- Rename `uneffectfulHref` to `hrefPure` (used in `Show` instance) (#21 by @JordanMartinez)
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"purescript-tuples": "^7.0.0"
},
"devDependencies": {
"purescript-assert": "^6.0.0"
"purescript-assert": "^6.0.0",
"purescript-ordered-collections": "^3.1.1"
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict",
"test": "pulp test -- --censor-lib --strict"
"test": "pulp test -- --censor-lib --strict",
"fmt": "purs-tidy format-in-place 'src/**/*.purs' 'test/**/*.purs'"
},
"devDependencies": {
"eslint": "^7.15.0",
"pulp": "16.0.0-0",
"purescript-psa": "^0.8.2",
"purs-tidy": "^0.10.0",
"rimraf": "^3.0.2"
}
}
4 changes: 2 additions & 2 deletions src/Node/URL/URLSearchParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const entriesImpl = (params, tuple) => {
return arr;
};
export const keysImpl = (params) => {
Array.from(params.keys());
return Array.from(params.keys());
};
export const valuesImpl = (params) => {
Array.from(params.values());
return Array.from(params.values());
};
50 changes: 46 additions & 4 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,53 @@ import Prelude

import Effect (Effect)
import Node.URL as URL
import Node.URL.URLSearchParams as URL.Search
import Foreign (unsafeReadTagged)
import Data.Bifunctor (lmap)
import Control.Monad.Except (runExcept)
import Control.Monad.Error.Class (liftEither)
import Effect.Exception (error)
import Data.Traversable (for)
import Data.Tuple (Tuple(..))
import Data.Map as Map
import Test.Assert (assertEqual)

main Effect Unit
main = do
let urlStr = "http://example.com/"
url <- URL.new urlStr
urlStr' <- URL.format url
assertEqual { expected: urlStr, actual: urlStr' }
-- Simple roundtrip
do
let urlStr = "http://example.com/"
url <- URL.new urlStr
urlStr' <- URL.format url
assertEqual { expected: urlStr, actual: urlStr' }

-- URLSearchParams
do
let urlStr = "http://example.com/?k&v=a&k&v=b"
url <- URL.new urlStr
search <- URL.searchParams url
keys <- URL.Search.keys search
valuesForeign <- URL.Search.values search

let
getStringOrThrow =
liftEither
<<< lmap (error <<< show)
<<< runExcept
<<< unsafeReadTagged "String"

values <- for valuesForeign getStringOrThrow

map <- Map.fromFoldable <$> for keys \k -> do
vs <- URL.Search.getAll k search
pure $ Tuple k vs

assertEqual { actual: keys, expected: [ "k", "v", "k", "v" ] }
assertEqual { actual: values, expected: [ "", "a", "", "b" ] }
assertEqual
{ actual: map
, expected: Map.fromFoldable
[ Tuple "k" [ "", "" ]
, Tuple "v" [ "a", "b" ]
]
}

0 comments on commit b9d3333

Please sign in to comment.