Skip to content

Instantly share code, notes, and snippets.

View karataev's full-sized avatar
:octocat:
¯\_(ツ)_/¯

Eugene Karataev karataev

:octocat:
¯\_(ツ)_/¯
View GitHub Profile
@karataev
karataev / walkTree.js
Created November 17, 2020 08:46
Walk a DOM tree and log a tag or text content
function padding(depth) {
return new Array(depth).fill(' ').join('');
}
function walk(node, depth) {
if (node.nodeType === 1) {
console.log(padding(depth) + node.tagName);
let children = Array.from(node.childNodes);
children.forEach(item => walk(item, depth + 1));
} else if (node.nodeType === 3) {
@karataev
karataev / ip.js
Created November 17, 2020 07:12
A prototype to find possible valid IP addresses from a string
function getValidIpAddresses(str) {
let result = [];
for (let i = 1; i < str.length; i++) {
let first = str.slice(0, i);
if (first > 255) continue;
let withoutFirst = str.slice(i);
for (let j = 1; j < withoutFirst.length; j++) {
let second = withoutFirst.slice(0, j);
if (second > 255) continue;
let withoutFirstSecond = withoutFirst.slice(j);
@karataev
karataev / helpers.js
Created November 20, 2019 05:18
Find the longest words from cubes with letters
const allWords = require('russian-words');
function getCubesLettersHeap(cubes) {
return cubes.reduce((acc, cube) => {
const letters = cube.split('');
letters.forEach(letter => {
acc[letter] = acc[letter]? acc[letter] + 1 : 1;
});
return acc;
}, {});
@karataev
karataev / promisePatterns.js
Created September 25, 2019 16:16
Promise patterns
const _ = require('lodash');
function series(arr, fn) {
let result = [];
return _.reduce(arr, (acc, item) => {
acc = acc
.then(() => fn(item))
.then(res => result.push(res));
return acc;
@karataev
karataev / README.md
Created September 23, 2019 11:47
node-gyp on windows

Installing npm packages on windows might be a tough task. One project I worked on had these dependencies: "canvas": "1.6.13", "chartjs-node": "^1.7.1",

Installing these dependencies failed with error gyp ERR! build error

I googled this issue and there are a lot of pages regarding this. One of them: https://spin.atomicobject.com/2019/03/27/node-gyp-windows/

@karataev
karataev / github-activity.js
Created August 8, 2019 12:48
Draw images on github's profile contribution graph with right mouse clicks
// execute this script in the github's page console
let colors = ['#196127', '#239a3b', '#7bc96f', '#c6e48b'];
function getRandomColor() {
let index = Math.floor(Math.random() * colors.length);
return colors[index];
}
document.addEventListener('contextmenu', e => {
e.preventDefault();
e.target.setAttribute('fill', getRandomColor())
@karataev
karataev / events.css
Created November 29, 2018 14:20
Events capture/bubble visualization
div {
position: relative;
padding: 30px;
}
label {
display: block;
}
div:after {
@karataev
karataev / facebook-post-parse.js
Created November 20, 2018 08:52
Parse list of users from a post and save it to .csv file
// open a list of users who interact with a post by clicking a number below the post
// execute function below in the browser's console
// PROFIT
(function() {
let result = [];
document.querySelectorAll('#reaction_profile_browser > li > div > div > div > div > div > div > a').forEach(item => result.push(item.textContent));
let csvContent = "data:text/csv;charset=utf-8,";
csvContent += result.join('\r\n');
let encodedUri = encodeURI(csvContent);
@karataev
karataev / bitsDiff.js
Created December 1, 2017 18:11
Разница в количестве битов двух чисел
function leftPad(str, num) {
if (str.length >= num) return str;
var pad = new Array(num - str.length + 1).join('0');
return pad + str;
}
function bitsDiff(a, b) {
var aBin = a.toString(2);
var bBin = b.toString(2);
"use strict";
console.clear();
// Yor code here ...
function dscount(str, s1, s2) {
const pattern = `${s1}${s2}`;
const regexp = new RegExp(pattern, 'gi');
let matches;
let counter = 0;
while((matches = regexp.exec(str)) != null) {