Reproducing Javascript’s escape() function in Groovy

From time to time, I’ve seen people looking for a way to reproduce Javascript’s escape() function in this or that language. It’s not a complex function – escape() simply replaces characters that aren’t alphanumeric (and a few symbols) with their Unicode values in hexadecimal. Anyway, I recently had a need to escape something using Groovy. Here’s the method.


/* A method to mimic Javascript's escape() function.
* It replaces 'special characters' with their Unicode hexadecimal
* equivalent. This simple method is not designed to handle
* double byte character sets. Feel free to improve it.
*/
String escapeLikeJavascript(String sourceText) {
def specialCharsRegex = /[^\w@*-+.\/]/
return sourceText.replaceAll(specialCharsRegex, {
"%${Integer.toHexString(it.codePointAt(0)).toUpperCase().padLeft(2, '0')}"
})
}

Advertisements

Why You Should Never Set GROOVY_HOME

Your Records Held For Ransom?

Daniel Solove coined the phrase ‘practical obscurity’:

“Practical obscurity” refers to the privacy an individual enjoys when personal information contained in public records is relatively hard to obtain. If a would-be harasser must travel some distance, wait in line and deal face-to-face with clerks to obtain a victim’s home address and phone number, he or she might think twice about taking action.

Having recently had to deal with a family member being stalked by someone who was able to obtain her home address via property tax records indexed by Google, I am a firm believer that the power of modern search technology makes it too easy to correlate a person’s name with information that could be used to harm them, and that our laws are insufficient to address this issue. Indeed, our laws seem to be on the side of businesses who would like to make a profit by selling your contact information.

Personally, I feel that a person owns his or her own contact information, and that the law should protect the consumer.

Case in point: US-Person.com sells background searches. They advertise by spamming the search engines with computer-generated pages containing the names of every person they have records for. There are many companies like them, but while most of their competitors offer some solution that lets consumers request to have their information removed, US-Person.com seems to be somewhat unique in that they want to charge consumers for the privelege. For $10, they will remove your information from their website for one year.

If you’re concerned about stalking or domestic violence, that seems almost like extortion. However, as far as I know, there are no federal laws requiring public information aggregators like US-Person.com to offer consumers an easy-to-find method to opt out at no charge.

It’s an issue that I think more consumers should be aware of, and one which I think we should all be talking to our legislators about.

#privacy

R.I.P. Yahoo Search

Yahoo Search 1994 – 2009

#seo

UI Designs

If we can build electronic thermostats that are smart enough to regulate temperature for four different time periods during workdays and four others on the weekend, why do we still make thermostats that have to be manually switched between ‘heat’ and ‘cool’?

#design

Life at 1.5 Mbps

I’m taking a class this week on test automation, led by Jared Richardson, one of the No Fluff Just Stuff team, and author of Ship It!  Jared is full of good advice, and not just about coding.  He’s suggested that putting up a blog is a smart career move and a good way to get a lot of eyes on your code when you need help.

And he must be right, because I got my first comment before I’d even finished typing in my first post!