Skip to content

Instantly share code, notes, and snippets.

@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 ;
}
@Dammmien
Dammmien / perfect_centering.css
Last active December 21, 2015 00:48
Absolute Horizontal And Vertical Centering
#foo{
height: 150px;
width: 300px;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
@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 / time_tracker.js
Last active September 30, 2016 13:24
TimeTracker
class TimeTracker {
constructor() {
this.timers = {};
}
logLoadTimes() {
var loadTimes = chrome.loadTimes();
console.log( `commitLoadTime .............. ${( loadTimes.commitLoadTime - loadTimes.startLoadTime ).toFixed( 2 )}s` );
console.log( `finishDocumentLoadTime ...... ${( loadTimes.finishDocumentLoadTime - loadTimes.startLoadTime ).toFixed( 2 )}s` );
@Dammmien
Dammmien / bfs.js
Last active February 22, 2022 11:35
Breadth First Search (BFS) Graph Traversal in Javascript
var nodes = [
{
links: [ 1 ], // node 0 is linked to node 1
visited: false
}, {
links: [ 0, 2 ], // node 1 is linked to node 0 and 2
path: [],
visited: false
},
...
@Dammmien
Dammmien / average.js
Last active October 3, 2019 09:06
Average of an array ES6
var average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
average( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] );
// 5
@Dammmien
Dammmien / Matrix.js
Last active July 9, 2023 17:33
Easily create an empty matrix in javascript
var matrix = [],
H = 4, // 4 rows
W = 6; // of 6 cells
for ( var y = 0; y < H; y++ ) {
matrix[ y ] = [];
for ( var x = 0; x < W; x++ ) {
matrix[ y ][ x ] = "foo";
}
}
@Dammmien
Dammmien / chunk.js
Last active September 30, 2016 09:14
Split an array into chunks
let partition = ( a, n ) => a.length ? [ a.splice( 0, n ), ...partition( a, n ) ] : [];
partition( [ 1, 2, 3, 4, 5, 6, 7, 8 ], 2 );
// [
// [ 1, 2 ],
// [ 3, 4 ],
// [ 5, 6 ],
// [ 7, 8 ]
// ]