File tree Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Expand file tree Collapse file tree 3 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ import {
2626 removeNewLinesAndExtraSpaces ,
2727 toFixedLocale ,
2828} from "./str" ;
29+ import { containsEncodedComponents } from "./url" ;
2930import { getUUID } from "./uuid" ;
3031
3132export {
@@ -57,4 +58,5 @@ export {
5758 toFixedLocale ,
5859 getUUID ,
5960 getSearchQuerySelector ,
61+ containsEncodedComponents ,
6062} ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11import { expect } from "chai" ;
22
33import { deepClone } from "../src/utils/clone" ;
4+ import { containsEncodedComponents } from "../src/utils/url" ;
45
56describe ( "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+ } ) ;
You can’t perform that action at this time.
0 commit comments