Skip to content

Instantly share code, notes, and snippets.

View saginadir's full-sized avatar
:octocat:

Sagi saginadir

:octocat:
View GitHub Profile
@saginadir
saginadir / new_ebs_ec2.md
Last active February 25, 2016 10:15
Attache new EBS to instance after it was created
@saginadir
saginadir / keep_ratio.js
Last active September 12, 2016 07:39
Sometimes it's needed to keep a ratio of an image/canvas but still display it in a middle of a div that might not be the same ratio of the image/canvas, this function returns the new size of the image/canvas to cover the div, with negative margins for top/left if needed.
let myDiv = $('.stage');
let myImage = $('.stage img');
let stage = {
width: myDiv.width(),
height: myDiv.height(),
}
let item = {
width: myImage.width(),
@saginadir
saginadir / css_animations.js
Created September 28, 2016 14:40
CSS animations on the fly generation with JavaScript
// We are creating a new style tag
let newStyleSheet = document.createElement("style");
// Appending the style tag to the head.
document.head.appendChild(newStyleSheet);
// Writing to the style sheet some css rules including keyframes and the animation class
newStyleSheet.appendChild(document.createTextNode(`
@keyframes my-fun-keyframes {
/* My keyframes for the animation I want to create */
@saginadir
saginadir / worker_from_blob.js
Last active October 3, 2016 10:12
Inline Web Worker, from XHR or string
let inlineJs = `function(){ console.log("Some JS Code!") }`;
let workerBlob = window.URL.createObjectURL(new Blob([inlineJs]));
let worker = new Worker(workerBlob);
/**
* XHR with worker
*/
let worker; // Keep this in the global scope
var request = new XMLHttpRequest();
const players = {itamar: 1200, eddie: 1210, eli: 1183, uriel: 1201};
const newPlayers = [];
for (var player in players) {
if (players.hasOwnProperty(player)) {
newPlayers.push({
player,
rank: players[player],
});
}
}
const players = {itamar: 1200, eddie: 1210, eli: 1183, uriel: 1201};
const sortedPlayers =
Object.keys(players) // Convert all object keys to an array
.map(p => ({name: p, rank: players[p]})) // Map to wanted object
.sort(function(a, b) { // Sort!
return b.rank - a.rank;
});
console.log(sortedPlayers); // Done!
const players = {itamar: 1200, eddie: 1210, eli: 1183, uriel: 1201};
const newPlayers = _.map(players, (v, k) => ({name: k, rank: v}));
const sortedPlayers = _.orderBy(newPlayers, ['rank'], ['desc'])
console.log(sortedPlayers); // Done!
const players = {itamar: 1200, eddie: 1210, eli: 1183, uriel: 1201};
const sortedPlayers = _.orderBy(_.map(players, (v, k) => ({name: k, rank: v})), ['rank'], ['desc']);
console.log(sortedPlayers);
const res = _({itamar: 1200, eddie: 1210, eli: 1183, uriel: 1201})
.map((v, k) => ({name: k, rank: v}))
.orderBy(['rank'], ['desc']);
console.log(res); // Not working.
const res = _({itamar: 1200, eddie: 1210, eli: 1183, uriel: 1201})
.map((v, k) => ({name: k, rank: v}))
.orderBy(['rank'], ['desc'])
.value();
console.log(res); // Done!