Skip to content

Instantly share code, notes, and snippets.

@henrahmagix
henrahmagix / TimeInMinutes.js
Created January 22, 2013 14:17
A lightweight alternative to the Date object when only dealing with time in hours and minutes
var TimeInMinutes = function(hours, minutes) {
return this.init(hours, minutes);
};
TimeInMinutes.prototype = {
hours: 0,
minutes: 0,
days: 0,
time: 0,
init: function(hours, minutes) {
if (hours) this.hours = hours;
@henrahmagix
henrahmagix / jquery-build-html
Created January 29, 2013 09:23
Verbose method to build HTML using jQuery. No more insanely long strings!
// Found at http://usejquery.com/posts/the-jquery-cross-domain-ajax-guide.
// Modified to remove undefined variables. Docs at http://api.jquery.com/jQuery/#jQuery2
jQuery('<li/>', { //build an li element
html: jQuery('<a/>', { //with an A element inside it
href: 'http://henrahmagix.github.com', //set the href for the link
text: 'Henrahmagix On GitHub' //and the text
})
})
@henrahmagix
henrahmagix / anonymous-functions.md
Created February 4, 2013 10:28
An explanation of the advantages of anonymous functions in Javscript, and the pitfalls one faces when not using them.

Javascript Anonymous Functions

Anonymous functions allow us to compartmentalise our code and avoid [polluting the global scope][global scope]. The difference between using and not using an anonymous function is explained with a basic example of changing the background color to red when the DOM is ready.

Global Functions

Here we use the onload method of document.body.

index.html

@henrahmagix
henrahmagix / rgb-array.js
Last active October 16, 2019 02:59
Get an rgb() array of any hex or rgb color.
(function() {
function getRgbArray(color, alpha) {
// Always return an array, either empty or filled.
var rgb = [];
var hex;
// Get an array of rgb(a) values.
if (color.substr(0, 1) === '#') {
/* HEX STRING */
// If the first character is # we're dealing with a hex string. Get
(function() {
function getRgbArray(color, alpha) {
// Always return an array, either empty or filled.
var rgb = [];
var hex;
// Get an array of rgb(a) values.
if (color.substr(0, 1) === '#') {
/* HEX STRING */
// If the first character is # we're dealing with a hex string. Get
@henrahmagix
henrahmagix / config.rb
Created March 1, 2013 15:37
An arbitrary long CSS selector splitter to go in your `config.rb` for Compass projects.
# Split selectors that are too long so IE8 doesn't ignore them.
on_stylesheet_saved do |path|
CssSelectorSplitter.split(path) unless path[/\d+$/]
end
# The following is a default config.rb created by `compass create`.
# Set this to the root of your project when deployed:
http_path = "/"
css_dir = "stylesheets"
@henrahmagix
henrahmagix / slides.md
Last active December 15, 2015 08:49
Quick and easy way to write a slides presentation

Building Slides In Markdown

By Henry Blyth

Uses Markdown Preview by [@revolunet][], and the fantastic [Gistdeck][] by [@nzoschke][] and [@seaofclouds][].

Things you need

  • [Sublime Text 2][sublime] (ST2)
  • [Markdown Preview ST2 package, my custom fork][md preview]
@henrahmagix
henrahmagix / Gruntfile-with-coffee-delete.coffee
Last active December 18, 2015 16:09
Delete compiled JS files when the source Coffee file gets deleted on a grunt watch.
module.exports = (grunt) ->
coffeeDir = 'coffee/'
jsDir = 'project/'
grunt.initConfig
coffee:
project:
expand: true
cwd: coffeeDir
{
"auto_complete_commit_on_tab": true,
"bold_folder_labels": true,
"ensure_newline_at_eof_on_save": true,
"folder_exclude_patterns":
[
".svn",
".git",
".hg",
"CVS",
@henrahmagix
henrahmagix / map-with-filter-differences.js
Last active December 20, 2015 06:49
Difference in map between underscore/lodash and jQuery/Zepto.
// Difference in map between underscore/lodash and jQuery/Zepto.
//
// $ allows filtering at the same time; _ does not.
//
// In your client-side JavaScript apps that use _ and $, this can come as a
// surprise since the assumption is that all your map, each, filter, etc. needs
// are fulfilled by _, and you'd be right to use them for speed since they
// alias native methods where available. However, sometimes $ can be useful,
// and this is one such case where that's true.
//