Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / rgbToLab.js
Created February 5, 2016 10:39
RGB to LAB
function rgbToLab( R, G, B ) {
R = ( R / 255 );
G = ( G / 255 );
B = ( B / 255 );
if ( R > 0.04045 ) R = Math.pow( ( R + 0.055 ) / 1.055, 2.4 );
else R = R / 12.92;
if ( G > 0.04045 ) G = Math.pow( ( G + 0.055 ) / 1.055, 2.4 );
else G = G / 12.92;
if ( B > 0.04045 ) B = Math.pow( ( B + 0.055 ) / 1.055, 2.4 );
@Dammmien
Dammmien / Builder.js
Last active July 7, 2018 23:14
JavaScript Builder design pattern
class MealBuilder {
init() {
this.meal = new Meal();
}
addMain(type) {
this.meal.main = new MainItem(type);
}
@Dammmien
Dammmien / dijkstra.js
Last active April 8, 2018 21:30
Implementation of Dijkstra's algorithm in JavaScript
var nodes = [
{
links: [ 1, 3 ], // node 0 is linked to node 1 and 3
weightLinks: [ 5, 15 ], // the distance between node 0 and 1 is 5, between 0 and 3 is 15
distance: Infinity
}, {
links: [ 0, 2 ], // node 1 is linked to node 0 and 2
weightLinks: [ 5, 5, ], // the distance between node 1 and 0 is 5, between 1 and 2 is 5
distance: Infinity
},
@Dammmien
Dammmien / Singleton.js
Created March 23, 2018 23:09
JavaScript Singleton design pattern
class Singleton {
constructor() {
if (typeof Singleton.instance === 'object') {
return Singleton.instance;
}
Singleton.instance = this;
return this;
@Dammmien
Dammmien / Factory.js
Last active March 23, 2018 23:06
JavaScript Factory design pattern
class Car {
constructor(size, price, maxSpeed) {
this.size = size;
this.price = price;
this.maxSpeed = maxSpeed;
}
}
class CarFactory {
@Dammmien
Dammmien / euclidean.js
Created March 10, 2018 14:03
Get euclidean distance between two points A and B
const getDistance = (A, B) => Math.sqrt( Math.pow( B.x - A.x, 2 ) + Math.pow( B.y - A.y, 2 ) );
@Dammmien
Dammmien / files.sh
Created March 9, 2018 19:38
Find unused files in a project (SVG in this example)
for FILE in $(find . -name "*.svg"); do
grep -qR $(basename "$FILE") * || echo "you can remove $FILE"
done
@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 / 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 ]
// ]
@Dammmien
Dammmien / count.js
Last active September 30, 2016 09:09
Counting the occurrences of elements in an array.
var arr = "The quick brown fox jumps over the lazy dog".split( '' );
arr.reduce( ( countMap, item ) => {
countMap[ item ] = ( countMap[ item ] || 0 ) + 1;
return countMap;
}, {} );