Changeset View
Changeset View
Standalone View
Standalone View
webroot/rsrc/externals/javelin/core/util.js
Show First 20 Lines • Show All 289 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Print a message to the browser debugging console (like Firebug). | * Print a message to the browser debugging console (like Firebug). | ||||
* | * | ||||
* @param string Message to print to the browser debugging console. | * @param string Message to print to the browser debugging console. | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
JX.log = function(message) { | JX.log = function(message) { | ||||
// "JX.log()" accepts "Error" in addition to "string". Only try to | |||||
// treat the argument as a "sprintf()" pattern if it's a string. | |||||
if (typeof message === 'string') { | |||||
message = JX.sprintf.apply(null, arguments); | |||||
} | |||||
window.console.log(message); | window.console.log(message); | ||||
}; | }; | ||||
JX.sprintf = function(pattern) { | |||||
var argv = Array.prototype.slice.call(arguments); | |||||
argv.reverse(); | |||||
// Pop off the pattern argument. | |||||
argv.pop(); | |||||
var len = pattern.length; | |||||
var output = ''; | |||||
for (var ii = 0; ii < len; ii++) { | |||||
var c = pattern.charAt(ii); | |||||
if (c !== '%') { | |||||
output += c; | |||||
continue; | |||||
} | |||||
ii++; | |||||
var next = pattern.charAt(ii); | |||||
if (next === '%') { | |||||
// This is "%%" (that is, an escaped "%" symbol), so just add a literal | |||||
// "%" to the result. | |||||
output += '%'; | |||||
continue; | |||||
} | |||||
if (next === 's') { | |||||
if (!argv.length) { | |||||
throw new Error( | |||||
'Too few arguments to "JX.sprintf(...)" for pattern: ' + pattern); | |||||
} | |||||
output += '' + argv.pop(); | |||||
continue; | |||||
} | |||||
if (next === '') { | |||||
amckinley: Wow, `charAt` returns the empty string instead of null or something? | |||||
Done Inline ActionsYou betcha! >> ''.charAt(420.69) << "" You might already know this one, too: >> typeof null << "object" Javascript is basically worse than PHP but has a much better PR team. epriestley: You betcha!
```
>> ''.charAt(420.69)
<< ""
```
You might already know this one, too:
```
>>… | |||||
throw new Error( | |||||
'Pattern passed to "JX.sprintf(...)" ends with "%": ' + pattern); | |||||
} | |||||
throw new Error( | |||||
'Unknown conversion "%' + c + '" passed to "JX.sprintf(...)" in ' + | |||||
'pattern: ' + pattern); | |||||
} | |||||
if (argv.length) { | |||||
throw new Error( | |||||
'Too many arguments to "JX.sprintf()" for pattern: ' + pattern); | |||||
} | |||||
return output; | |||||
}; | |||||
Not Done Inline ActionsWhat is this; a Facebook interview?!? amckinley: What is this; a Facebook interview?!? | |||||
if (__DEV__) { | if (__DEV__) { | ||||
window.alert = (function(native_alert) { | window.alert = (function(native_alert) { | ||||
var recent_alerts = []; | var recent_alerts = []; | ||||
var in_alert = false; | var in_alert = false; | ||||
return function(msg) { | return function(msg) { | ||||
if (in_alert) { | if (in_alert) { | ||||
JX.log( | JX.log( | ||||
Show All 37 Lines |
Wow, charAt returns the empty string instead of null or something?