Skip to content

Instantly share code, notes, and snippets.

@gilbox
gilbox / encodeURIObject.js
Last active December 28, 2015 03:18
Encodes a JavaScript object (with key/values) into a URI (ie: key1=value1&key2=value2...)
function encodeURIObject(o) {
var a = [];
for (k in o) {
a.push(encodeURIComponent(k) + '=' + encodeURIComponent(o[k]))
}
return a.join('&');
}
// test:
var uriStringTest = encodeURIObject({
@gilbox
gilbox / stack-trace.js
Created November 13, 2013 16:45
A one-liner for a doing a quick stack trace in JavaScript
console.log((new Error('STACK TRACE')).stack);
@gilbox
gilbox / gist:7683414
Created November 27, 2013 21:21
Get a jQuery selector for any element on a page in Chrome (or any WebKit browser). [assuming jQuery has been loaded on the page]
// right-click any element and choose copy xpath, it will look something like this:
//*[@id="list-results"]/div[2]/div[1]/div/ol/li[5]/div/a
var MY_XPATH = '//*[@id="list-results"]/div[2]/div[1]/div/ol/li[5]/div/a'
// then do this:
$($x(MY_XPATH))
@gilbox
gilbox / getURLParameter.js
Created December 13, 2013 23:34
Parse GET parameters from the current page's URL
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
svc = function allServices(mod, r) {
var inj = angular.element(document).injector().get;
if (!r) r = {};
angular.forEach(angular.module(mod).requires, function(m) {allServices(m,r)});
angular.forEach(angular.module(mod)._invokeQueue, function(a) {
try { r[a[2][0]] = inj(a[2][0]); } catch (e) {}
});
return r;
}();
@gilbox
gilbox / trackFn.js
Last active August 29, 2015 14:03
Function tracking for console debugging
/**
* Track a function attached to an object.
* For example to track: myObject.myFunc()
* trackFn(myObject, 'myFunc');
*
* You can also track multiple functions at once:
* trackFn(myObject, ['myFunc1','myFunc2','myFunc3']);
*
* optionally set st=true to get a stack trace
* optionally you can supply a msg argument
function handlePathChange() {
actionCreator.handlePathChange({newPath:location.pathname + location.search});
}
// setup event listener for back/forward buttons
var eventInfo = window.addEventListener ? ['addEventListener', ''] : ['attachEvent', 'on'];
window[eventInfo[0]](eventInfo[1] + 'popstate', handlePathChange, false);
@gilbox
gilbox / gist:12095aa0bc6822736f8f
Last active September 1, 2015 21:49
ui-router VS flux+vanillaJS
//
// ui-router
//
$stateProvider.state('person', {
url: '/person/{personId}',
resolve: ($stateParams, AppObject) => {
return AppObject.getPerson( $stateParams.personId )
},
controller() { /* ... */ }
@gilbox
gilbox / transit-immutable.js
Created February 13, 2015 00:16
Use transit-js Immutable.js with automatic conversion of plain JS objects to immutables
var Transit = require('transit-js'),
Imm = require('immutable');
// gil: It's possible that the following reader/writer configuration is incomplete.
// This is a modified version of:
// https://gist.github.com/Tvaroh/52efbe8f4541ca537908
//
// ... the original version of this utilized built-in JS (ES6?) types for
// better efficiency+speed. However, it didn't work with nested Map + OrderedMap.
// Improved efficiency+speed can be achieved by re-implementing with some native types.
@gilbox
gilbox / gist:a7ff39cb75796c4413cb
Created March 27, 2015 16:08
Use transit-js with Immutable.js and plain JS Objects mixed in
var Transit = require('transit-js'),
Imm = require('immutable');
// gil: It's possible that the following reader/writer configuration is incomplete.
// This is a modified version of:
// https://gist.github.com/Tvaroh/52efbe8f4541ca537908
var reader = Transit.reader('json', {
mapBuilder: {
init: function () { return {}; },