Skip to content

Instantly share code, notes, and snippets.

View Bondifrench's full-sized avatar

Dominik Dumaine Bondifrench

View GitHub Profile
@Bondifrench
Bondifrench / Gist test
Created February 21, 2014 03:57
Javascript: PubSub test
//Works in modern browsers +IE9, but Modernizer has a polyfill baked in for function.bind
//From Sublime Nettuts tutorial, original credit from Paul Irish
var o=$( {});
$.subscribe = o.on.bind('event name', eventData, function(event) {
/* Act on the event */
});
$.unsubscribe = o.off.bind();
$.publish = o.trigger.bind();
@Bondifrench
Bondifrench / jQuery PubSub
Created February 21, 2014 04:09
Javascript: jQuery PubSub
(function ($) {
var o = $({
on: 'subscribe',
trigger: 'publish',
off: 'unsubscribe'
}, function (key, api) {
$[api] = function() {
o[key].apply[o,arguments]
}
}
@Bondifrench
Bondifrench / gist:11553534
Created May 6, 2014 04:57
Sequelize multiple rows query
fin_item
.findAll({where: Sequelize.and({cik: 1800}, {fiscalyear: 2013}, {periodfocus: 'FY'}, {rootconcept: 'Statement of Income'}), order: 'preorder'})
.success(function (result) {
console.log(result.length);
for (var i=0;i<result.length;i++) {console.log(result[i].values);}
})
.error(function (err) {
console.error(err);
})
@Bondifrench
Bondifrench / Sequelize test
Created May 6, 2014 05:01
Sequelize raw query multiple rows (no DAO)
fin_item
.findAll({where: Sequelize.and({cik: 1800}, {fiscalyear: 2013}, {periodfocus: 'FY'}, {rootconcept: 'Statement of Income'}), order: 'preorder'}, {raw: true})
.success(function (result) {
console.log(result.length);
console.log(result)
})
.error(function (err) {
console.error(err);
})
@Bondifrench
Bondifrench / Excel2SQL
Last active September 25, 2017 16:48
Importing Excel table into Postgres
#!/usr/bin/env SQL
#Importing an Excel table into Postgres
# 1) Save Excel file in a .csv format with just 1 header
# ex:
example_file.csv
# 2) Save file in C:/PostgreSQL/9.3/data (important that this file is within "data")
# so Postgres has the right file permissions
# 3) Need to create an empty table schema capable of storing the data
# 4) ex:
@Bondifrench
Bondifrench / Primary_key_SQL
Created May 6, 2014 22:49
Adding primary key to existing table in Postgres
#!/usr/bin/env SQL
#Lets say you have a sec_sample table to which you want to add a id (surrogate) autoincremental PK:
ALTER TABLE sec_sample ADD column id serial;
UPDATE sec_sample SET id = nextval(pg_get_serial_sequence('sec_sample', 'id'));
ALTER TABLE sec_sample ADD PRIMARY KEY (id);
@Bondifrench
Bondifrench / FinViewsSQL
Created May 9, 2014 00:02
Pivoting Finviews data, grouping by concepts, ordering by presentation order
Select
s.concept,
sum(case when s.fiscalyear =2009 then s.value else 0 end) as "2009",
sum(case when s.fiscalyear =2010 then s.value else 0 end) as "2010",
sum(case when s.fiscalyear =2011 then s.value else 0 end) as "2011",
sum(case when s.fiscalyear =2012 then s.value else 0 end) as "2012",
sum(case when s.fiscalyear =2013 then s.value else 0 end) as "2013",
sum(case when s.fiscalyear =2014 then s.value else 0 end) as "2014"
from (Select concept, fiscalyear, value, preorder from sec_sample
where cik=1288776
@Bondifrench
Bondifrench / filter.js
Created June 4, 2014 11:05
Filter function in Javascript
var bracket = [];
for (var d=0; d<list.length;d++){
if (path.extname(list[d]) === ".".concat(ext)) {
bracket.push(list[d]);
}
}
//bracket = list.filter(function (d) {
// return path.extname(d) === ".".concat(ext);
//});
@Bondifrench
Bondifrench / forEach.js
Created June 4, 2014 11:06
forEach function in Javascript
for (var i=0;i<list.length;i++) {
console.log(list[i]);
}
//Equivalent to:
//list.forEach(function (i) {
//console.log(i)
//})
@Bondifrench
Bondifrench / recursion.js
Created June 5, 2014 00:56
Recursion using a loop and without
for (var i=0; i<num; i ++) {
operation();
}
//Equivalent to
if (num <= 0) {return;} //or just return see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
operation()
return repeat(operation, --num)