var FFZ = window.FrankerFaceZ, constants = require('./constants'); var sanitize_cache = {}, sanitize_el = document.createElement('span'), place_string = function(num) { if ( num == 1 ) return '1st'; else if ( num == 2 ) return '2nd'; else if ( num == 3 ) return '3rd'; else if ( num == null ) return '---'; return num + "th"; }, brighten = function(rgb, amount) { amount = (amount === 0) ? 0 : (amount || 1); amount = Math.round(255 * -(amount / 100)); var r = Math.max(0, Math.min(255, rgb[0] - amount)), g = Math.max(0, Math.min(255, rgb[1] - amount)), b = Math.max(0, Math.min(255, rgb[2] - amount)); return [r,g,b]; }, rgb_to_css = function(rgb) { return "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")"; }, darken = function(rgb, amount) { amount = (amount === 0) ? 0 : (amount || 1); return brighten(rgb, -amount); }, get_luminance = function(rgb) { rgb = [rgb[0]/255, rgb[1]/255, rgb[2]/255]; for (var i =0; i s_ind; if ( !found && !css ) return; if ( found ) all = all.substr(0, s_ind) + all.substr(e_ind + end.length); if ( css ) all += start + css + end; element.innerHTML = all; }, get_luminance: get_luminance, brighten: brighten, darken: darken, rgb_to_css: rgb_to_css, parse_date: parse_date, number_commas: function(x) { var parts = x.toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); return parts.join("."); }, place_string: place_string, placement: function(entrant) { if ( entrant.state == "forfeit" ) return "Forfeit"; else if ( entrant.state == "dq" ) return "DQed"; else if ( entrant.place ) return place_string(entrant.place); return ""; }, sanitize: function(msg) { var m = sanitize_cache[msg]; if ( ! m ) { sanitize_el.textContent = msg; m = sanitize_cache[msg] = sanitize_el.innerHTML; sanitize_el.innerHTML = ""; } return m; }, date_string: function(date) { return date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate(); }, time_to_string: function(elapsed, separate_days, days_only) { var seconds = elapsed % 60, minutes = Math.floor(elapsed / 60), hours = Math.floor(minutes / 60), days = ""; minutes = minutes % 60; if ( separate_days ) { days = Math.floor(hours / 24); hours = hours % 24; if ( days_only && days > 0 ) return days + " days"; days = ( days > 0 ) ? days + " days, " : ""; } return days + (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds; } }