Skip to content

Instantly share code, notes, and snippets.

View mkoryak's full-sized avatar
🐈
Working

Misha Koryak mkoryak

🐈
Working
View GitHub Profile
@mkoryak
mkoryak / jquery.values.js
Created October 9, 2012 19:19
jquery.values - load and save form values
/* jQuery.values: get or set all of the name/value pairs from child input controls
* @argument data {array} If included, will populate all child controls.
* @returns element if data was provided, or array of values if not
*/
$.fn.values = function(data) {
var els = this.find(':input').get();
if(arguments.length === 0) {
// return all data
@mkoryak
mkoryak / aopJs
Created November 2, 2012 15:38
Instrument all of object's functions with fn.before() and fn.after() functions
//requires underscore.js 1.3+
var wrapFunctions = function(obj){
_.each(obj, function(func, funcName){
if(_.isFunction(func)){
obj[funcName] = _.wrap(func, function(fn){
var wrappedArgs = _.toArray(arguments).slice(1);
wrappedArgs = obj[funcName].before && obj[funcName].before.apply(this, wrappedArgs) || wrappedArgs;
var ret = fn.apply(obj, wrappedArgs);
obj[funcName].after && obj[funcName].after.apply(this, wrappedArgs);
return ret;
@mkoryak
mkoryak / _deferred_shim.js
Created November 13, 2012 16:24
easy to use deferred shim
//requires jQuery.Deferred
_deferred = (function(){
var that = {};
var deferred = {};
var lazy = function(name) {
if(!deferred[name]){
deferred[name] = $.Deferred();
}
return deferred[name];
@mkoryak
mkoryak / gist:4119372
Created November 20, 2012 17:20
add custom error messages to jquery.validate
/**
*
* you can use the message attribute to provide custom verbose error messages. The error message contains a token {} which will be replaced by the input label.
*
* Ex:
* <label for='firstName'>First Name</label><input id='firstName' name='first_name' class='verbose-error' message='Please enter your {}'/>
* the error message will be: Please enter your First Name
*/
jQuery.validator.addMethod("verbose-error", function(value, element, param) {
var $elem = $(element);
@mkoryak
mkoryak / html5location.js
Last active April 9, 2017 17:48
Using HTML5 location API and google reverse geocoding to get browser location. Dependencies: 1) underscore.js 2) https://github.com/estebanav/javascript-mobile-desktop-geolocation 3) <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
if(geoPosition.init()){
var foundLocation = function(city, state, country, lat, lon){
//do stuff with your location! any of the first 3 args may be null
console.log(arguments);
}
var geocoder = new google.maps.Geocoder();
geoPosition.getCurrentPosition(function(r){
var findResult = function(results, name){
@mkoryak
mkoryak / searchAndWrap.js
Created January 17, 2013 22:54
regexp search nested objects and wrap hits
var searchAndWrap = function(obj, regex, blacklist){
var found = false;
_(obj).each(function(val, key){
if(!_.include(blacklist || [], key)){
if(_.isObject(val)){
if(searchAndWrap(val, regex, blacklist)){
found = true;
}
} else {
val = val.toString();
@mkoryak
mkoryak / dedent.js
Created March 5, 2013 22:42
Remove any common leading whitespace from every line in 'text'
/**
* Remove any common leading whitespace from every line in `text`
* Ported from http://hg.python.org/cpython/file/2.7/Lib/textwrap.py
* @param text
* @return {*}
*/
var dedent = function(text){
var leadingWhitespaceRE = /(^[ \t]*)(?:[^ \t\n])/;
var margin = null;
var i;
@mkoryak
mkoryak / post-receive
Created April 2, 2013 15:31
git post-receive hook for a simple deployment of a nodejs app
#!/bin/sh
echo "Deploying..."
unset GIT_DIR
cd /home/deploy/srv/destructly/
GIT_WORK_TREE=/home/deploy/srv/destructly/
git checkout -f master
pwd
echo "Stopping service"
sudo forever stop `forever list | grep destructly.js | awk '{ print $2 }' | sed 's/\[\(.*\)\]/\1/'`
echo "Installing dependencies"
@mkoryak
mkoryak / format.bas
Created May 29, 2013 00:49
This is one of the first programs I wrote. The file has a modified date of 1/9/1998
CLS
GOSUB sub1
IF quest$ = "y" THEN
GOSUB sub2
ELSE
END
END IF
GOSUB sub3
@mkoryak
mkoryak / gist:6023894
Created July 17, 2013 19:55
functional
var Person = function(){
var that = {};
var privateVar = 'BLEEEEEE'; //how do i do this with prototypical inhereitance?
that.talk = function(){
return "i said "+privateVar;
}
return that;
};