Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
jasdeepkhalsa / dedupe.js
Created March 23, 2013 13:54
De-duplicate an array in JavaScript
var arr = [1,1,2];
var arr = arr.filter(function (v, i, a) { return a.indexOf (v) == i }); // dedupe array
@eranation
eranation / gist:3241616
Created August 2, 2012 22:51
MongoDB Aggregation Framework way for "select count (distinct fieldName) from someTable" for large collections
//equivalent of "select count (distinct fieldName) from someTable"
db.someCollection.aggregate([{ $group: { _id: "$fieldName"} },{ $group: { _id: 1, count: { $sum: 1 } } } ])
@ravisorg
ravisorg / build-emoji-regexp.php
Last active November 6, 2023 08:18
Generate a PHP compatible regular expression to match emoji from the most recent unicode data.
<?php
/**
* Uses the data from unicode.org's emoji-data.txt to build a PHP compatible Regular Expression
* along the lines of:
* (?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\x{FE0F}?)
*
* To use: php build-hashtag-regexp.php <emoji-data.txt>
* Output will be the generated regular expression.
*
@dalethedeveloper
dalethedeveloper / gist:1503252
Created December 20, 2011 21:00
Mobile Device Detection via User Agent RegEx

#Mobile Device Detection via User Agent RegEx

Yes, it is nearly 2012 and this exercise has been done to death in every imaginable language. For my own purposes I needed to get the majority of non-desktop devices on to a trimmed down, mobile optimized version of a site. I decided to try and chase down an up-to-date RegEx of the simplest thing that could possibly work.

I arrived at my current solution after analyzing 12 months of traffic over 30+ US based entertainment properties (5.8M+ visitors) from Jan - Dec 2011.

The numbers solidified my thoughts on the irrelevancy of including browsers/OSes such as Nokia, Samsung, Maemo, Symbian, Ipaq, Avant, Zino, Bolt, Iris, etc. The brass tacks of the matter is that you certainly could support these obscure beasts, but are you really going to test your site on them? Heck, could you even find one?! Unless the folks that pay you are die hard Treo users my guess is "No".

Interestingly enough my research shows that /Mobile/ is more efficient than **/iP(

@clarkenheim
clarkenheim / mongodb_distinct_count.js
Last active December 12, 2023 09:22
MongoDB equivalent of an SQL query to get the distinct values of a field in a collection including the count of documents which have each distinct value (distinct with count)
//equivalent of MySQL SELECT COUNT(*) AS cnt, fieldName FROM someTable GROUP BY fieldName;
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", cnt:{$sum:1}}}]);
//as above but ordered by the count descending
//eg: SELECT COUNT(*) AS cnt, fieldName FROM someTable GROUP BY fieldName ORDER BY cnt DESC;
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", cnt:{$sum:1}}}, {$sort:{'cnt':-1}}]);
@phette23
phette23 / current-dir-in-iterm-tab-title.sh
Last active January 4, 2024 10:20
Set the iTerm tab title to the current directory, not full path.
# put this in your .bash_profile
if [ $ITERM_SESSION_ID ]; then
export PROMPT_COMMAND='echo -ne "\033];${PWD##*/}\007"; ':"$PROMPT_COMMAND";
fi
# Piece-by-Piece Explanation:
# the if condition makes sure we only screw with $PROMPT_COMMAND if we're in an iTerm environment
# iTerm happens to give each session a unique $ITERM_SESSION_ID we can use, $ITERM_PROFILE is an option too
# the $PROMPT_COMMAND environment variable is executed every time a command is run
# see: ss64.com/bash/syntax-prompt.html
@simahawk
simahawk / broadcaster.py
Created December 4, 2017 18:28
python icecast source streamer
# -*- coding: utf-8 -*-
# Inspired by https://github.com/turlando/airhead/blob/master/airhead/broadcaster.py
import click
import os
import shouty
import sys
import logging
logger = logging.getLogger('[broadcaster]')
@staringispolite
staringispolite / asciiputsonglasses
Last active March 22, 2024 06:39
Ascii art sunglasses meme
Puts on glasses:
(•_•)
( •_•)>⌐■-■
(⌐■_■)
Takes off glasses ("mother of god..."):
(⌐■_■)
( •_•)>⌐■-■
@clarkenheim
clarkenheim / Zip Codes to DMAs
Last active April 8, 2024 12:16
TSV file containing zip codes and the DMA they fall in to. Method: calculate the centre point of every zip code geo boundary, plot those points on a DMA boundary map, find the containing DMA of each zip centroid
This file has been truncated, but you can view the full file.
zip_code dma_code dma_description
01001 543 SPRINGFIELD - HOLYOKE
01002 543 SPRINGFIELD - HOLYOKE
01003 543 SPRINGFIELD - HOLYOKE
01004 543 SPRINGFIELD - HOLYOKE
01005 506 BOSTON (MANCHESTER)
01007 543 SPRINGFIELD - HOLYOKE
01008 543 SPRINGFIELD - HOLYOKE
01009 543 SPRINGFIELD - HOLYOKE
01010 543 SPRINGFIELD - HOLYOKE
@lucasfais
lucasfais / gist:1207002
Created September 9, 2011 18:46
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt