Skip to content

Instantly share code, notes, and snippets.

View alexanderscott's full-sized avatar

Alex Ehrnschwender alexanderscott

View GitHub Profile
@alexanderscott
alexanderscott / config-encryptor
Created August 31, 2012 01:02
app.config encryption mechanism in .NET
/*
* Encrypt private data such as authentication keys and db connection strings inside app.config
* or web.config of a .NET project. Import this class inside a project and call
* ProtectConnectionString() to encrypt and UnprotectConnectionString() to decrypt the XML data.
* With a standalone executable, the config file will be built alongside the exe with .config
* appended. Manage multiple config files for different production environments.
* Important: make sure the process is single-threaded for security.
*/
using System;
@alexanderscott
alexanderscott / js-ajax-utils
Created August 31, 2012 09:37
JavaScript utility functions for cookie and alert manipulation
/*
* COOKIE HELPERS
*/
getCookie: function(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g,"");
@alexanderscott
alexanderscott / JS-sortBy
Created September 1, 2012 06:15
Useful sortBy function in JavaScript
var sort_by = function(field, reverse, primer){
var key = function (x) {return primer ? primer(x[field]) : x[field]};
return function (a,b) {
var A = key(a), B = key(b);
return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse];
}
};
@alexanderscott
alexanderscott / phoneNum-raw-format
Created September 7, 2012 00:26
Phone number formatting for mobile
/*
* Re-formats a number for phone call
* Takes (555) 555-5555 and replaces with 5555555555
* To make a call from mobile use <a href="tel:XXX-XXX-XXXX">
*/
var raw_phoneNum = formatted_phoneNum.replace(/\)\s*|\(\s*|-/g, '');
@alexanderscott
alexanderscott / zip-rm-files
Created September 7, 2012 07:26
Remove .DS_Store files from zip archive via terminal
zip -d ZIPfile.zip `unzip -l ZIPfile.zip | grep .DS_Store | awk '{print $4}'`
@alexanderscott
alexanderscott / scala-tail-recursion
Created September 14, 2012 13:05
Tail recursive functions written in Scala
/**
* Proprietary work of Alex Ehrnschwender
*/
def sum(xs: List[Int]): Int = {
var i = 0
if (xs.isEmpty) i else i + xs.head + sum(xs.tail)
}
def max(xs: List[Int]): Int = {
@alexanderscott
alexanderscott / prime-number-fxns
Created September 14, 2012 15:28
Multi-language functions for displaying prime numbers
###
# scala
##
object P extends App{
def c(M:Int)={
val p=collection.mutable.BitSet(M+1)
p(2)=true
(3 to M+1 by 2).map(p(_)=true)
for(i<-p){
var j=2*i;
@alexanderscott
alexanderscott / js-num-commas
Created October 20, 2012 15:20
comma-separated thousands fxn
/* comma-separated thousands */
// if no decimals
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// if more than 3 decimal places
function numberWithCommas(x) {
var parts = x.toString().split(".");
$.getScript("http://platform.twitter.com/widgets.js", function(){
function handleTweetEvent(event){
if (event) {
alert("This is a callback from a tweet")
}
}
twttr.events.bind('tweet', handleTweetEvent);
});
<a href="http://twitter.com/intent/tweet?url=http://test.com;via=stack">twitter</a>
/* LIKE */
FB.Event.subscribe('edge.create', function(href, widget) {
alert('You just liked the page!');
});
/* FEED DIALOG FB.UI
http://developers.facebook.com/docs/reference/javascript/FB.ui/
http://developers.facebook.com/docs/reference/dialogs/