Skip to content

Instantly share code, notes, and snippets.

View farskid's full-sized avatar
🎯
Focusing

Farzad Yousefzadeh farskid

🎯
Focusing
View GitHub Profile
@farskid
farskid / tree.js
Created October 24, 2017 22:13
Generate a tree from flatten array of data in Javascript
function generateTree(list, id = 'slug', parentId = 'parent_slug', childrenId = 'children') {
var tree = [];
var lookup = {};
list.forEach(function(item) {
lookup[item[id]] = item;
item[childrenId] = [];
});
list.forEach(function(item) {
if (item[parentId] != null) {
@farskid
farskid / emojis.txt
Created November 10, 2017 11:36
Emojis in git commits
Emojis on GitHub commits
Table of emojis:
🎨 :art: when improving the format/structure of the code
📰 :newspaper: when creating a new file
📝 :pencil: when performing minor changes/fixing the code or language
🐎 :racehorse: when improving performance
📚 :books: when writing docs
🐛 :bug: when reporting a bug, with @FIXMEComment Tag
@farskid
farskid / base64.js
Created November 10, 2017 23:02
Create base64 out of image in Javascript
function base64Image(src) {
return new Promise(function(resolve, reject) {
var img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.onload = function() {
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
@farskid
farskid / export-csv.js
Last active November 10, 2017 23:06
Export to CSV from client side in Javascript
/*
* @param {string} content
* @param {string} fileName
*/
function downloadWithContent(content, fileName) {
// Create a virtual <a> element
var $a = document.createElement('a');
// Attach proper href
$a.attr('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent('\uFEFF') + encodeURIComponent(content));
// Attach download prop
@farskid
farskid / express_with_used_port_management_server.js
Last active December 30, 2018 20:30
Express server manage used port
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
// Start the server using managed startup process
startServer()
// Since express is just a wrapper around http and extends the EventEmitter, one can catch the port error using on('error').
function startServer(app, port) {
// process.env props are usually sent as string
@farskid
farskid / conver.js
Created December 7, 2017 21:13
Create a key value object from a 2d array in javascript
/*
const array = [
['name', 'some name'],
['size', 1746],
['format', 'some format']
];
convert array to an object as:
{
name: 'some name',
@farskid
farskid / minMaxSum.js
Created December 21, 2017 22:35
Minimum and Maximum values that can be calculated by summing exactly `n-1` of the `n` integers
function maxMinSum(intArray) {
var sum = sumOfArray(intArray);
var sums = intArray.map(num => sum - num);
return [minOfArray(sums), maxOfArray(sums)].join(' ');
}
function sumOfArray(intArray) {
return intArray.reduce((total, curr) => total += curr, 0);
}
@farskid
farskid / stairs.js
Created December 22, 2017 21:50
Javascript draw stairs by number
function draw(n) {
var steps = new Array(n).fill('#');
var len = steps.length;
steps = steps.map((val, i) => {
return new Array(len - i - 1).fill(' ').join('') + val + new Array(i).fill('#').join('');
});
return steps.join('\n');
}
/*
@farskid
farskid / anagram.js
Created December 24, 2017 03:25
Number of deletions needed to make two random strings, anagrams of each other in Javascript
function anagrams(a, b) {
var freqA = charFreq(a);
var freqB = charFreq(b);
var deletions = 0;
getChars().forEach(char => {
deletions += Math.max(freqA[char], freqB[char]) - Math.min(freqA[char], freqB[char]);
});
return deletions;
@farskid
farskid / by-iframe.js
Created January 16, 2018 20:39
Find out global variables added by scripts on a website
/*
Method1: Create an iframe and compare it's window object with website's window
*/
const iframe = document.createElement('iframe');
document.body.append(iframe);
const virgin = Object.keys(iframe.contentWindow);
const current = Object.keys(window);
const added = current.filter(key => !virgin.includes(key));