Skip to content

Instantly share code, notes, and snippets.

@s4y
s4y / waitForNetwork.m
Created February 24, 2011 17:16
On Mac OS X: Block until the network stack is ready. Useful for daemons which need the network to do anything useful.
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
// Wait for the network to be configured. Equivalent to `ipconfig waitall`. Adapted from ipconfig/client.c
static void _doneWaitingForNetwork(SCDynamicStoreRef session, CFArrayRef changes, void * arg){
CFRunLoopStop(CFRunLoopGetCurrent());
}
#define STARTUP_KEY CFSTR("Plugin:IPConfiguration")
#define NETWORK_RUN_LOOP_MODE CFSTR("WaitingForNetwork")
@s4y
s4y / massageInput.js
Created March 24, 2011 22:41
Make Safari think that an input does not contain priceless intelligence, the safety of which must be confirmed by the user before the window or tab can be closed.
function massageInput(input){
var oldDisplay = input.style.display;
input.style.display = 'none';
input.clientLeft; // trigger layout
input.style.display = oldDisplay;
}
@s4y
s4y / decodeQuotedPrintable.js
Created July 14, 2011 17:21
Decode quoted-printable-encoded text
function decodeQuotedPrintable(input){
return input.replace(/=(\n|[0-F]{2})/g, function(match, charCode){
if (charCode === '\n') {
return '';
} else {
return String.fromCharCode(parseInt(charCode, 16));
}
});
}
@s4y
s4y / backbone_NativeSortingCollection.js
Created July 19, 2011 23:12
Backbone.Collection subtype which uses native-style comparators
var NativeSortingCollection = Backbone.Collection.extend({
sortedIndex: function(candidate, comparator){
var i = 0;
while (i < this.models.length && comparator(this.models[i], candidate) <= 0) {
i++;
}
return i;
},
sortBy: function(comparator){
return this.models.sort(comparator);
@s4y
s4y / LICENSE.md
Created August 26, 2011 17:11
For Mac OS X: Get a list of running processes, and tell if a particular process is running, by name

Copyright (c) 2010 DeepTech, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE O

@s4y
s4y / gist:1215700
Created September 14, 2011 02:12
child_process.execFile example
var child_process = require('child_process');
// exec: spawns a shell.
child_process.exec('ls -lah /tmp', function(error, stdout, stderr){
console.log(stdout);
});
// execFile: executes a file with the specified arguments
child_process.execFile('ls', ['-lah', '/tmp'], function(error, stdout, stderr){
console.log(stdout);
@s4y
s4y / isType.js
Created October 10, 2011 03:05
isType: A mostly-naive implementation of, “Is this thing a Thing?”
function isType(object, type){
return object != null && type ? object.constructor.name === type.name : object === type;
}
/*!
* jQuery JavaScript Library v1.7.1pre
* http://jquery.com/
*
* Copyright 2011, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* Includes Sizzle.js
* http://sizzlejs.com/
@s4y
s4y / zeropad.js
Created April 16, 2012 03:33
Simple JavaScript zero padding function
function zeroPad(input, length, character){
var padding = length + 1 - input.length;
return (padding > 0 ? Array(length + 1 - input.length).join(character || '0') + input : input);
};
@s4y
s4y / formatDuration.js
Created April 16, 2012 03:35
JavaScript function for formatting a duration of time
function formatDuration(duration){
var seconds = Math.abs(Math.ceil(duration / 1000)),
h = (seconds - seconds % 3600) / 3600,
m = (seconds - seconds % 60) / 60 % 60,
s = seconds % 60;
return (duration < 0 ? '-' : '') + h + ':' + zeroPad(m.toString(), 2) + ':' + zeroPad(s.toString(), 2);
}