Skip to content

decodeURI and encodeURI -> add #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 15, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Other improvements:
Breaking changes:

New features:
- Added `encodeURI`, `decodeURI` functions (#4 by @srghma)

Bugfixes:

Expand Down
3 changes: 2 additions & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let upstream =
https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20220808/packages.dhall
sha256:60eee64b04ca0013fae3e02a69fc3b176105c6baa2f31865c67cd5f881a412fd

in upstream
50 changes: 36 additions & 14 deletions src/JSURI.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
// A helper which transforms the result ofencodeURIComponent to be compliant
// with RFC3896, as described in the MDN documentation here:
// A helper which transforms the result of encodeURIComponent to be compliant
// with RFC3986, as described in the MDN documentation here:
//
// https://web.archive.org/web/20201206001047/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
function toRFC3896(input) {
function encodeURIComponent_to_RFC3986(input) {
return input.replace(/[!'()*]/g, function (c) {
return "%" + c.charCodeAt(0).toString(16);
});
}

export const _encodeURIComponent = function encode(fail, succeed, input) {
// A helper which transforms the result of encodeURI to be compliant
// with RFC3986, as described in the MDN documentation here:
//
// https://web.archive.org/web/20210117175449/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_ipv6
function encodeURI_to_RFC3986(input) {
return input.replace(/%5B/g, "[").replace(/%5D/g, "]");
}

export function _encodeURIComponent(fail, succeed, input) {
try {
return succeed(toRFC3896(encodeURIComponent(input)));
return succeed(encodeURIComponent_to_RFC3986(encodeURIComponent(input)));
} catch (err) {
return fail(err);
}
};
}

export const _encodeFormURLComponent = function encode(fail, succeed, input) {
export function _encodeFormURLComponent(fail, succeed, input) {
try {
return succeed(toRFC3896(encodeURIComponent(input)).replace(/%20/g, "+"));
return succeed(encodeURIComponent_to_RFC3986(encodeURIComponent(input)).replace(/%20/g, "+"));
} catch (err) {
return fail(err);
}
};
}

function _decodeURIComponent(fail, succeed, input) {
export function _decodeURIComponent(fail, succeed, input) {
try {
return succeed(decodeURIComponent(input));
} catch (err) {
return fail(err);
}
}

export {_decodeURIComponent};

export const _decodeFormURLComponent = function encode(fail, succeed, input) {
export function _decodeFormURLComponent(fail, succeed, input) {
return _decodeURIComponent(fail, succeed, input.replace(/\+/g, " "));
};
}

export function _encodeURI(fail, succeed, input) {
try {
return succeed(encodeURI_to_RFC3986(encodeURI(input)));
} catch (err) {
return fail(err);
}
}

export function _decodeURI(fail, succeed, input) {
try {
return succeed(decodeURI(input));
} catch (err) {
return fail(err);
}
}
12 changes: 12 additions & 0 deletions src/JSURI.purs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module JSURI
( encodeURIComponent
, encodeFormURLComponent
, encodeURI
, decodeURIComponent
, decodeFormURLComponent
, decodeURI
) where

import Prelude
Expand Down Expand Up @@ -87,3 +89,13 @@ foreign import _decodeFormURLComponent :: Fn3 (String -> Maybe String) (String -
-- | ```
decodeFormURLComponent :: String -> Maybe String
decodeFormURLComponent = runFn3 _decodeFormURLComponent (const Nothing) Just

foreign import _encodeURI :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String)

encodeURI :: String -> Maybe String
encodeURI = runFn3 _encodeURI (const Nothing) Just

foreign import _decodeURI :: Fn3 (String -> Maybe String) (String -> Maybe String) String (Maybe String)

decodeURI :: String -> Maybe String
decodeURI = runFn3 _decodeURI (const Nothing) Just