Skip to content

Instantly share code, notes, and snippets.

View luis-almeida's full-sized avatar

Luis Almeida luis-almeida

  • Zendesk
  • Copenhagen, Denmark
View GitHub Profile
@luis-almeida
luis-almeida / gist:7507544
Created November 17, 2013 00:54
Triggering an event in vanilla JavaScript
// http://stackoverflow.com/a/2490876
var event;
if (document.createEvent) {
event = document.createEvent("HTMLEvents");
event.initEvent("dataavailable", true, true);
} else {
event = document.createEventObject();
event.eventType = "dataavailable";
}
function moveCursorToEnd(el) {
el.focus();
if (el.setSelectionRange) {
var len = el.value.length * 2;
el.setSelectionRange(len, len);
} else el.value = el.value;
el.scrollTop = 999999;
}
<snippet>
<content><![CDATA[console.log($1);$0]]></content>
<tabTrigger>cs</tabTrigger>
<scope>text.html,source.js</scope>
<description>console.log()</description>
</snippet>
<!-- Save file in /packages/User/console-log.sublime-snippet -->
<!-- Restart Sublime Text 2 -->
<!-- Type "cl" and hit tab -->
// http://stackoverflow.com/questions/1248302/javascript-object-size
function roughSizeOfObject ( object ) {
var objectList = [];
var recurse = function ( value ) {
var bytes = 0;
if ( typeof value === 'boolean' ) bytes = 4;
@luis-almeida
luis-almeida / Gradient
Created June 15, 2012 01:07
Extending jQuery with a function that returns a gradient color based on a hex
/*
* Usage:
* '#' is optional
* $.gradient( "#CCCCCC", -0.1 ); // returns a 10% darker hex color
* $.gradient( "#AAAAAA", 0.4 ); // returns a 40% lighter hex color
* $.gradient( "NTAF2S", 0.3 ); // returns false
*/
$.gradient = function ( hex, opacity ) {
if ( !/^#?[0-9a-f]{3,6}$/i.test( hex ) ) return false;
@luis-almeida
luis-almeida / gist:2882007
Created June 6, 2012 13:57 — forked from lucasfais/gist:1207002
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
@luis-almeida
luis-almeida / jQuery "inview" selector
Created June 2, 2012 12:04
Extending jQuery with an "inview" selector to select elements that are in the visible part of the page (at least partially).
$.extend($.expr[':'], {
inview: function ( el ) {
var $e = $( el ),
$w = $( window ),
wt = $w.scrollTop(),
wb = wt + $w.height(),
et = $e.offset().top,
eb = et + $e.height();
return eb >= wt && et <= wb;