Skip to content

Instantly share code, notes, and snippets.

View cschlyter's full-sized avatar

Carlos Schlyter cschlyter

  • Florianópolis - SC
View GitHub Profile
@cschlyter
cschlyter / sortSelectOption.js
Last active December 5, 2023 06:54
[Javascript] Sort alphabetically the option elements of a select element by text.
function sortSelectOptions(selectElement) {
var options = $(selectElement + " option");
options.sort(function(a,b) {
if (a.text.toUpperCase() > b.text.toUpperCase()) return 1;
else if (a.text.toUpperCase() < b.text.toUpperCase()) return -1;
else return 0;
});
$(selectElement).empty().append( options );
@cschlyter
cschlyter / toggleCheckbox.js
Created March 22, 2013 13:45
[javascript] toggle checkbox on/off
checkbox.attr("checked", !checkbox.attr("checked"));
@cschlyter
cschlyter / dropall.js
Created April 3, 2013 00:56
MongoDB: Drop all mongodb databases
// Usage: $ mongo dropAll.js
var dbs = db.getMongo().getDBNames()
for(var i in dbs){
db = db.getMongo().getDB( dbs[i] );
print( "dropping db " + db.getName() );
db.dropDatabase();
@cschlyter
cschlyter / attribute_to_file.php
Created April 24, 2013 17:07
[PHP] save attibute contents to a file.
$var_str = var_export($page, true);
file_put_contents('K:\filename.txt', $var_str);
@cschlyter
cschlyter / mongo_group_by_day.js
Created May 2, 2013 14:45
MongoDB: group data by day (group)
db.link_access.group(
{
keyf: function(doc) {
var date = new Date(doc.date);
var dateKey = (date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear()+'';
return {'day': dateKey};
},
cond: {short_id: "N"},
initial: {count:0},
reduce: function(obj, prev) {prev.count++;}
@cschlyter
cschlyter / findDatesInRange.js
Created June 5, 2013 13:42
MongoDB: Find dates in a range.
var start = new Date(2013, 5, 4)
var end = new Date(2013, 5, 6)
db.mention_stat.find({"verification": {"$gte": start, "$lt": end}})
@cschlyter
cschlyter / mongo_group_by_day.js
Created June 12, 2013 16:46
MongoDB: group by day (aggregate)
var mention_id = 620996;
db.mentionStats.aggregate([
{ $match: {'mention_id': mention_id}},
{ $group: {'_id': {
'year': { '$year': "$verification_date" },
'month': { '$month': "$verification_date" },
'day': { '$dayOfMonth': "$verification_date" }
},
'retweets': { $last: "$retweets" }}},
@cschlyter
cschlyter / mongoimport.sh
Created July 22, 2013 03:22
[MongoDB] mongoimport example
mongoimport --db pcat --collection products < products.json
@cschlyter
cschlyter / mongo_regex.js
Created August 5, 2013 23:59
[MongoDB] query db using regex.
db.zips.find({city : { $regex : /^[0-9]/ }})
@cschlyter
cschlyter / toTitleCase.js
Created November 21, 2013 16:29
Convert string to title case. input: fuel output: Fuel
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}