Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / wget.sh
Last active March 13, 2024 13:58
wget cheat sheet
# POST a JSON file and redirect output to stdout
wget -q -O - --header="Content-Type:application/json" --post-file=foo.json http://127.0.0.1
# Download a complete website
wget -m -r -linf -k -p -q -E -e robots=off http://127.0.0.1
# But it may be sufficient
wget -mpk http://127.0.0.1
# Download all images of a website
@Dammmien
Dammmien / flexbox.css
Created February 25, 2016 18:07
CSS flexbox
.container{
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
flex-wrap: nowrap | wrap | wrap-reverse;
flex-flow: <‘flex-direction’> || <‘flex-wrap’>;
justify-content: flex-start | flex-end | center | space-between | space-around; // horizontally
align-items: flex-start | flex-end | center | baseline | stretch; // vertical-align
align-content: flex-start | flex-end | center | space-between | space-around | stretch; //multi line repartition
}
@Dammmien
Dammmien / kadane.js
Created February 15, 2016 18:09
Kadane algorithm In computer science, the maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers which has the largest sum. For example, for the sequence of values −2, 1, −3, 4, −1, 2, 1, −5, 4. The contiguous subarray with the largest sum is 4, −1, 2, 1, with sum 6.
var a = [ -2, 1, -3, 4, -1, 2, 1, -5, 4 ],
now = 0,
prev = 0;
for ( var i = 0; i < a.length; i++ ) {
prev = Math.max( 0, prev + a[ i ] );
now = Math.max( prev, now );
}
console.log( now ); // 6
@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 / 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;
}, {} );
@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 / dfs.js
Last active February 22, 2022 11:37
Depth First Search (DFS) Graph Traversal in Javascript
const nodes = [
{
links: [ 1 ], // node 0 is linked to node 1
visited: false
}, {
links: [ 0, 2 ], // node 1 is linked to node 0 and 2
visited: false
},
...
];
@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 / 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 );