Skip to content

Instantly share code, notes, and snippets.

View dented's full-sized avatar
🎯
Focusing

Gram dented

🎯
Focusing
View GitHub Profile
//
// Regular Expression for URL validation
//
// Author: Diego Perini
// Updated: 2010/12/05
// License: MIT
//
// Copyright (c) 2010-2013 Diego Perini (http://www.iport.it)
//
// Permission is hereby granted, free of charge, to any person
@dented
dented / move_files_to_individual_folders.rb
Last active August 29, 2015 14:27
Move a tree of files in a single folder to a new list of folders with individual files
require 'fileutils'
directory = './files'
new_directory = './new'
files = Dir["#{directory}/*"]
Dir.mkdir(new_directory) if !Dir.exists?(new_directory)
files.each do |file|
file_name = File.basename(file)
directory_name = File.basename(file_name, File.extname(file_name))
directory_path = "#{new_directory}/#{directory_name}"
@dented
dented / is_native.js
Created August 24, 2015 08:45
Check if Method is Native
;(function() {
// Used to resolve the internal `[[Class]]` of values
var toString = Object.prototype.toString;
// Used to resolve the decompiled source of functions
var fnToString = Function.prototype.toString;
// Used to detect host constructors (Safari > 4; really typed array specific)
var reHostCtor = /^\[object .+?Constructor\]$/;
@dented
dented / efficient_dynamic_style_insert.js
Created August 24, 2015 08:47
Efficiently insert styles to page, instead of inlining each element which is more expensive
var sheet = (function() {
// Create the <style> tag
var style = document.createElement('style');
// Add a media (and/or media query) here if you'd like!
// style.setAttribute('media', 'screen')
// style.setAttribute('media', 'only screen and (max-width : 1024px)')
// WebKit hack :(
style.appendChild(document.createTextNode(''));
@dented
dented / needle.js
Created August 31, 2015 03:41
Find Value in Array
var indexOf = function(needle) {
if(typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function(needle) {
var i = -1,
index = -1;
for(i = 0; i < this.length; i++) {
if(this[i] === needle) {
index = i;
@dented
dented / fatty-images.js
Last active September 2, 2015 08:17
Lazy Load Images trials
var images = document.querySelectorAll('img[data-src]'),
imageLength = images.length;
Array.prototype.forEach.call(images, function(image) {
image.setAttribute('src', image.getAttribute('data-src'));
image.onload = function() {
image.removeAttribute('data-src');
};
});
@dented
dented / my_error.js
Created September 2, 2015 08:44
Custom Error in JavaScript
function MyError(code, msg) {
this.message = msg;
this.statusCode = code;
this.name = 'HttpError';
const err = Error(msg);
this.stack = err.stack;
}
MyError.prototype = Object.create(Error.prototype);
MyError.prototype.constructor = MyError;
@dented
dented / application_controller.rb
Last active September 6, 2015 07:52
Rails Redirect Old to New
class ApplicationController
before_filter :redirect_if_old
# before_action :redirect_if_old # if Rails 4+
protected
def redirect_if_old
if request.host == 'old.com'
redirect_to "#{request.protocol}new.io#{request.fullpath}", :status => :moved_permanently
end
@dented
dented / custom_errors.js
Created September 8, 2015 10:09
Custom Error Constructor
Error.custromConstructor = (function() {
function define(obj, prop, value) {
Object.defineProperty(obj, prop, {
value: value,
configurable: true,
enumerable: false,
writable: true
});
}
@dented
dented / factory.js
Created September 8, 2015 10:11
Factory
(function (factory) {
if (typeof define === "function" && define.amd) {
define( ["jquery"], factory );
} else {
factory( window.jQuery );
}
}(function ($) {
}));