Skip to content

Instantly share code, notes, and snippets.

View pedromcunha's full-sized avatar
🤘
Hello! Olá!

Pedro Cunha pedromcunha

🤘
Hello! Olá!
View GitHub Profile
@pedromcunha
pedromcunha / gist:e1506d3cd1b27b77095f
Last active August 29, 2015 14:04
Instantiate the CartoDB Map
cartodb.createLayer(map, {
user_name: 'dmonay',
type: 'cartodb',
sublayers: [{
sql: "SELECT * FROM bathymetry_layers",
cartocss: "#bathymetry_layers{ polygon-fill: #598cdb;polygon-opacity: 0.2;line-color: #FFF;line-width: 0;line-opacity: 1;}"
},{
sql: "SELECT * FROM pm25_na",
cartocss: $('#pm25_na').html().format('pm25_2000'),
interactivity: "cartodb_id, the_geom, pm25_2000, pm25_2001, pm25_2002, pm25_2003, pm25_2004, pm25_2005, pm25_2006, pm25_2007, pm25_2008, pm25_2009, pm25_2010, pm25_2011, pm25_2012, country"
{
sql: "SELECT * FROM pm25_na",
cartocss: $('#pm25_na').html().format('pm25_2012'),
interactivity: "cartodb_id, the_geom, pm25_2000, pm25_2001, pm25_2002, pm25_2003, pm25_2004, pm25_2005, pm25_2006, pm25_2007, pm25_2008, pm25_2009, pm25_2010, pm25_2011, pm25_2012, country"
}
<script type="cartocss/html" id="pm25_na">
/*Country Styles*/
#pm25_na{
polygon-fill: #FFFFB2;
polygon-opacity: 0.8;
line-color: #FFF;
line-width: 1;
line-opacity: 1;
}
@pedromcunha
pedromcunha / gist:4c1024f02ee4c0c31de3
Created July 21, 2014 20:24
Formatting CartoCSS function
String.prototype.format = (function (i, safe, arg) {
function format() {
var str = this,
len = arguments.length + 1;
for (i = 0; i < len; arg = arguments[i++]) {
safe = typeof arg === 'object' ? JSON.stringify(arg) : arg;
str = str.replace(RegExp('\\{' + (i - 1) + '\\}', 'g'), safe);
}
return str;
function changePowerTotal(currentPower, GenID, GenStat, GenPower) {
if(GenStat == 'on') {
currentPower += GenPower;
return alert("Generator #"+GenID+" is now on adding "+GenPower+" MW, for a total of "+currentPower+" MW!");
}
else if(GenStat == 'off') {
if (currentPower <= 0) {
currentPower = 0;
}
else {
@pedromcunha
pedromcunha / gist:2dd37cfee80790b1572a
Created July 30, 2014 03:05
Function Expressions and Instant Invocations.
var listOfSharks = ["Sea Pain", "Great Wheezy",
"DJ Chewie", "Lil' Bitey",
"Finmaster Flex", "Swim Khalifa",
"Ice Teeth", "The Notorious J.A.W."];
var listOfTargets = ["icicle bat", "snow yeti",
"killer penguin", "frost tiger",
"polar bear", "iceberg",
"blue witch", "wooly mammoth"];
function makeTargetAssigner( sharks, targets ){
return function (shark) {
@pedromcunha
pedromcunha / gist:b03261b10089a11abc3e
Created August 2, 2014 17:36
OOP JavaScript: Adding or removing objects via built in functions.
var fridge = {
addItem: function(name, type, expirationDate, healthRating) {
this[name] = {type: type, expirationDate: expirationDate, healthRating: healthRating};
},
removeItem: function(name){
var recentlyRemoved = delete this[name];
}
};
fridge.addItem("bannana", "fruit", "2 weeks", "100");
fridge.addItem("steak", "meat", "1 weeks", "70");
@pedromcunha
pedromcunha / gist:aed804e428a5dfe1635b
Created August 2, 2014 19:13
Prototypal Methods: Creating a new inherited method in the String prototype
String.prototype.checkFor = function (input) {
var result = 0;
for (var i = 0; i<this.length; i++) {//use this to check for the length of the actual string
this.charAt(i) == input ? result++ : false; //we can use a ternary conditional here.
}
return result;
};
console.log("This is a good string".checkFor("o")); //Now we can use the new method on a string and log it out.
@pedromcunha
pedromcunha / gist:591d1ed814c3f0d562a1
Created August 2, 2014 19:32
OOP Javascript: Inheritance using constructors
function Tool(name, type, use, cost) {
this.name = name;
this.type = type;
this.use = use;
this.cost = cost;
//You can also add inherited functions here for everything created using this constructor
this.useTool = function () {
alert("Alright, be careful with that "+this.name+"!");
}
}
@pedromcunha
pedromcunha / gist:791c80f3ab800ccfb49f
Last active August 29, 2015 14:04
If/Else Case Switch Conditionals
function sansTemple(floor){
var happensNext;
switch(floor) {
//Notice how you can use a stacking method to reduce redundency in cases. Now lower level also works for the basement cond.
case "lower level" :
case "basement" :
welcomeMessage = "Welcome to the basement!";
case 1 :
welcomeMessage = "You have entered the First Floor!";
break