Skip to content

Instantly share code, notes, and snippets.

@davidbarredo
davidbarredo / IE8_and_below_patch.js
Last active August 29, 2015 14:26
Determine whether an array contains a value
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;
@davidbarredo
davidbarredo / emptyArray.js
Last active September 15, 2015 07:36
Empty an array methods
A = [];
// This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array.
A.length = 0;
// This will clear the existing array by setting its length to 0
A.splice(0,A.length);
// Using .splice() will work perfectly, but since .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggests that this has no effect on performance whatsoever.
while(A.length > 0) {
@davidbarredo
davidbarredo / utilRegex.js
Last active October 7, 2015 09:26
Util regular expressions
//for positive integers
/^\+?[1-9]\d*$/.test();
//for positive integers and zero
/^\+?(0|[1-9]\d*)$/.test();
//for positive/negative integers and zero
/^\-?(0|[1-9]\d*)$/.test();
//to match specific chars
@davidbarredo
davidbarredo / copyWithElementInvisible.js
Created October 7, 2015 09:44
Copy info to clipboard with javascript
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
//
// *** This styling is an extra step which is likely not required. ***
//
// Why is it here? To ensure:
// 1. the element is able to have focus and selection.
// 2. if element was to flash render it has minimal visual impact.
// 3. less flakyness with selection and copying which **might** occur if
@davidbarredo
davidbarredo / extendBackbone.js
Created October 8, 2015 13:50
How to extend properly Backbone views, models and collections
var obj = {
foo: function() {
// do awesome things
},
bar: function() {
// do magic stuff
}
};
var collection = Backbone.Collection.extend({});
@davidbarredo
davidbarredo / imageData.js
Created November 15, 2013 08:22
Image data on change input file type
var url = window.URL || window.webkitURL;
// Change inputFileID with your own
document.getElementById("inputFileID").onchange = function() {
if ( this.disabled ) {
// File upload not allowed
console.log( 'Your browser does not support File upload.' );
} else {
var fileChosen = this.files[0];
var image = new Image();
image.onload = function() {
@davidbarredo
davidbarredo / dateExtensions.js
Last active December 28, 2015 18:19
Util date extensions
// Remember to prototype your own custom Date
var customDate = Date
// Date extension to to make monday day 0 (week start on monday)
customDate.prototype.getDayL = function () {
if ( this.getDay() == 0 ) {
return 6;
}else{
return this.getDay() - 1;
}
@davidbarredo
davidbarredo / insertGist.js
Created November 19, 2013 12:35
Dynamically add a script of a github gist
/* Create script element */
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://gist.github.com/7544705.js';
/* Backup document.write function */
if(!document._write) document._write = document.write;
/* Override document.write function */
document.write = function (str) {
document.getElementById('scriptCont').innerHTML += str;
};
@davidbarredo
davidbarredo / endResize.js
Created November 19, 2013 18:42
Detect end of window resize (with timer help)
window.onresize = function() {
if ( this.resizeTimer ) clearTimeout( this.resizeTimer );
this.resizeTimer = setTimeout ( function() {
/* Your code here */
}, 500 );
}
@davidbarredo
davidbarredo / app.js
Created November 21, 2016 18:32 — forked from Turbo87/app.js
webpack + font-awesome test
require('font-awesome/css/font-awesome.css');
document.body.innerHTML = '<i class="fa fa-fw fa-question"></i>';