15

This is a serious post about unicorns!

Here is a unicorn for everyone's enjoyment

enter image description here

But in all seriousness, I was testing out some stuff for a Javascript question on strings, so I opened up the Javascript console in Chrome, and I typed in new String(), and I saw what properties/methods it had. One of these methods was formatunicorn(), with the code

function (){var d=this.toString();if(!arguments.length)return d;var a=typeof arguments[0],a="string"==a||"number"==a?Array.prototype.slice.call(arguments):arguments[0],c;for(c in a)d=d.replace(RegExp("\\{"+c+"\\}","gi"),a[c]);return d}

What is this for? I'm assuming it has something to do with a unicornify April Fool's joke. Also, since this question is about programming somewhat, is it more on-topic in SO?

  • Should I ask this on SO? I wasn't sure, since it pertains to SO, but also code. – scrblnrd3 Nov 12 '13 at 22:27
  • 1
    It just replaces {token}s in strings, for injecting parameters into routes for AJAX requests, etc. – Tim Stone Nov 12 '13 at 22:28
  • 1
    Is this something that's actually in the Stack Exchange JavaScript? It's not immediately clear. I'm trying to figure out what you're talking about. – animuson Nov 12 '13 at 22:29
  • @animuson just go to the js console, and type in "my string".formatunicorn. – scrblnrd3 Nov 12 '13 at 22:30
  • @animuson Yeah, it's a SE utility function. – Tim Stone Nov 12 '13 at 22:30
  • 3
    Cool. I just found the name interesting. – scrblnrd3 Nov 12 '13 at 22:31
  • Usage of String#formatUnicorn(): stackoverflow.com/questions/610406/… – Pang Nov 19 '15 at 8:31
26

formatUnicorn is just a utility function for subbing in values to simple string templates in various places in the Stack Exchange JavaScript, using curly braces as the token delimiter. It's used on things like request routes to inject parameters.

As for the name, we can only guess…

Unicorn, sort of...*

*It knows it's an abomination, don't stare at it!

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .