This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
const array = [ | |
['name', 'some name'], | |
['size', 1746], | |
['format', 'some format'] | |
]; | |
convert array to an object as: | |
{ | |
name: 'some name', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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)); |