Skip to content

Commit fb73471

Browse files
Merge pull request #21 from Exabyte-io/feature/SOF-6228
feat: add containsEncodedComponents function to utils
2 parents 86aa45f + 21a57ae commit fb73471

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/utils/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
removeNewLinesAndExtraSpaces,
2727
toFixedLocale,
2828
} from "./str";
29+
import { containsEncodedComponents } from "./url";
2930
import { getUUID } from "./uuid";
3031

3132
export {
@@ -57,4 +58,5 @@ export {
5758
toFixedLocale,
5859
getUUID,
5960
getSearchQuerySelector,
61+
containsEncodedComponents,
6062
};

src/utils/url.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** Check whether URL component is encoded already.
2+
* @see [MDN]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent}
3+
* @param {String} x
4+
* @returns {boolean}
5+
*/
6+
export function containsEncodedComponents(x) {
7+
// ie ?,=,&,/ etc
8+
return decodeURI(x) !== decodeURIComponent(x);
9+
}

tests/utils.tests.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from "chai";
22

33
import { deepClone } from "../src/utils/clone";
4+
import { containsEncodedComponents } from "../src/utils/url";
45

56
describe("deepClone", () => {
67
const obj = {
@@ -22,3 +23,13 @@ describe("deepClone", () => {
2223
expect(other).not.to.haveOwnProperty("object");
2324
});
2425
});
26+
27+
describe("containsEncodedComponents", () => {
28+
const decodedComponent = "a test with // special = characters?";
29+
const encodedComponent = encodeURIComponent(decodedComponent);
30+
31+
it("identifies whether a string is URL encoded", () => {
32+
expect(containsEncodedComponents(encodedComponent)).to.be.true;
33+
expect(containsEncodedComponents(decodedComponent)).to.be.false;
34+
});
35+
});

0 commit comments

Comments
 (0)