Test for URI encoding

by amindunited

JavaScript

// isEncoded Check
const isEncoded = (val) => {
	// If there are whitespace characters, it's not encoded
	if (val.match(/\s/gm)) { return false; }
  // Generally most unencoded strings do not have more than one '%'
	return !!val.match(/%.*%/gm);
}

const aerial = '🚡';
const encodeURIed = encodeURIComponent(aerial);
const btoaed = btoa(encodeURIed);
const atobed = atob(btoaed);
const decodeURIed = decodeURIComponent(atobed);

console.log('original', aerial);
console.log('encodeURI', encodeURIed);
console.log('btoa', btoaed);
console.log('atob', atobed);
console.log('btoa', decodeURIed);

const encodedStrings = [
	'🚡',
  encodeURIComponent('🚡'),
	"Storm Warnings in Western australia",
	encodeURIComponent("Storm Warnings in Western australia"),
  "20% of our customers are people",
  encodeURIComponent("20% of our customers are people"),
];

console.log('encoded strings', encodedStrings);

encodedStrings.forEach((str) => {
	console.log(str, isEncoded(str));
});