Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / super_array.js
Last active August 29, 2015 14:07
Super Array
var SA = function( a ) {
a.has = function( v ) {
var i = a.length;
for ( i; i--; )
if ( a[ i ] === v )
return true;
return false;
};
@Dammmien
Dammmien / super_dom.js
Last active August 29, 2015 14:07
Super DOM
var SD = {
create: function( tag, attrs ) {
tag = document.createElement( tag );
for ( var attr in attrs ) tag[ attr ] = attrs[ attr ];
return tag;
},
get: function( selector, context, undefined ) {
var matches = {
@Dammmien
Dammmien / loadTime.js
Created October 10, 2014 15:09
Log load time in Google Chrome
(function() {
console.clear();
var loadTimes = chrome.loadTimes();
console.log( "requestTime................. " + ( loadTimes.startLoadTime - loadTimes.requestTime ).toFixed( 2 ) + 's' );
console.log( "commitLoadTime.............. " + ( loadTimes.commitLoadTime - loadTimes.startLoadTime ).toFixed( 2 ) + 's' );
console.log( "finishDocumentLoadTime...... " + ( loadTimes.finishDocumentLoadTime - loadTimes.startLoadTime ).toFixed( 2 ) + 's' );
console.log( "finishLoadTime.............. " + ( loadTimes.finishLoadTime - loadTimes.startLoadTime ).toFixed( 2 ) + 's' );
})()
@Dammmien
Dammmien / chunkString.js
Created December 11, 2015 14:30
Split a string into chunks
"abcdefghijklmnopqrstuvwxyz".match( /.{1,4}/g );
// [ "abcd", "efgh", "ijkl", "mnop", "qrst", "uvwx", "yz" ]
@Dammmien
Dammmien / shuffle.js
Created December 14, 2015 14:59
Shuffle an array with the Fisher–Yates shuffle in javascript
function shuffle( a ) {
var m = a.length, t, i;
while ( m ) {
i = Math.floor( Math.random() * m-- );
t = a[ m ];
a[ m ] = a[ i ];
a[ i ] = t;
}
@Dammmien
Dammmien / hex_and_rgb_converter.js
Created December 14, 2015 17:31
RGB to Hex and Hex to RGB
var rgbToHex = ( r, g, b ) => "#" + ( ( 1 << 24 ) + ( r << 16 ) + ( g << 8 ) + b ).toString( 16 ).slice( 1 );
var hexToRgb = ( hex ) => [ parseInt( hex.substring( 1, 3 ), 16 ), parseInt( hex.substring( 3, 5 ), 16 ), parseInt( hex.substring( 5, 7 ), 16 ) ];
rgbToHex( 175, 25, 70 ); // #af1946
hexToRgb( "#af1946" ); // [ 175, 25, 70 ]
@Dammmien
Dammmien / random_hex.js
Created December 14, 2015 17:04
Random Hex Color in JavaScript
'#' + Math.floor( Math.random() * 16777215 ).toString( 16 );
@Dammmien
Dammmien / concat_and_dedupe.js
Last active December 15, 2015 17:00
Concatenate two arrays and remove duplicate
// if no duplicate in the first list
a1.concat( a2.filter( i => a1.indexOf( i ) === -1 ) );
// if duplicate in the first list
a1.concat( a2 ).reduce( function( p, c, i, a ) {
if ( p.indexOf( c ) === -1 ) p.push( c );
return p;
}, [] );
@Dammmien
Dammmien / factorial.js
Created December 15, 2015 17:09
Factorial
var factorial = ( i, a ) => i < 2 ? a || 1 : factorial( i - 1, ( a || 1 ) * i );
console.log( factorial( 5 ) ); // 120
@Dammmien
Dammmien / Detect Touch Device.js
Created May 23, 2013 09:28
Detect Touch Device
var ua = navigator.userAgent,
isApple = /iPad/i.test(ua) || /iPhone/i.test(ua),
isAndroid = ua.toLowerCase().indexOf("android") > -1;
if(isAndroid || isApple) {
// do something ;
}