Skip to content

Instantly share code, notes, and snippets.

View glebcha's full-sized avatar
👨‍💻
do my best

Gleb glebcha

👨‍💻
do my best
View GitHub Profile
@glebcha
glebcha / depcheck.sh
Last active April 25, 2023 10:11
Find unused packages from package.json
#!/bin/bash
# Enhanced version of https://stackoverflow.com/a/52371112/1835055
DIRNAME=${1:-.}
cd $DIRNAME
FILES=$(mktemp)
PACKAGES=$(mktemp)
@glebcha
glebcha / getType.js
Created April 21, 2020 07:59
Check common js types
const modifier = type => item => Object.prototype.toString.call(item) === `[object ${type}]`;
const checkTypes = ['String', 'Function', 'Number', 'Boolean', 'Object', 'Symbol'];
const is = checkTypes.reduce((checkers, type) => ({ ...checkers, [type]: modifier(type) }), {});
is.Function(null)
@glebcha
glebcha / detect_failed_promise.js
Last active May 25, 2021 14:48
Fast concept of failed promise detection
function bulkRequest(urls) {
const promises = urls.reduce((result, url) => {
const isValidUrl = Object.prototype.toString.call(url) === '[object String]';
if (isValidUrl) {
result.push(
fetch(url)
.catch(({ message }) => console.warn(`Bulk Request: ${ message }`))
.then(response => ({ [url]: response }))
)
@glebcha
glebcha / structuralClone.js
Created July 18, 2018 11:33
Deep object clone without functional properties
function structuralClone(obj) {
return new Promise(resolve => {
const {port1, port2} = new MessageChannel();
port2.onmessage = ev => resolve(ev.data);
port1.postMessage(obj);
});
}
var obj = {a: 1, b: {
c: b

@kangax's ES6 quiz, explained

@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).

Here we go with the explanations:

Question 1:
(function(x, f = () => x) {
const EXTENSION_TYPE = {
0x01: 'PlainText',
0xF9: 'GraphicControl',
0xFE: 'Comment',
0xFF: 'Application'
};
/**
* Returns total length of data blocks sequence
*
@glebcha
glebcha / quizez.js
Last active January 19, 2018 18:27
Challenges in a hard way
// Find summ of all elements in array => result: 32
var multiArr = [1, '2x', ['3', ['x2', ['10', '10'], '5'], '1x']];
function summ(arr, result) {
var result = result || 0;
for (var i = 0; i < arr.length; i++) {
var number = arr[i];
var notNumber = isNaN(parseInt(number));
var isArray = Object.prototype.toString.call(number) === '[object Array]';
@glebcha
glebcha / linkedList.js
Last active August 9, 2017 14:59
Linked List
function Node(data) {
this.data = data;
this.next = null;
this.prev = null;
}
function linkedList() {
this.head = null;
this.size = 0;
}
@glebcha
glebcha / WebstormTweak
Created May 22, 2017 05:04 — forked from abhisheksliam/WebstormTweak
Speed up WebStorm
Here is my recipe how to speed up WebStorm:
Go to Preferences and do next:
Appearance & Behaviour > System Settings > Updates: disable auto update
Appearance & Behaviour > System Settings > Using Statistics: Uncheck allowing sending data
Editor > Live Templates: disable all, leave only what you are really use
Editor > Emmet: disable all emmets
Editor > Intentions: I leave only: CSS, Declaration, JavaScript and Language Injection
Plugins: leave only next (* - can be also disabled in case don't need them):
CoffeeScript *
@glebcha
glebcha / iterate_better.js
Created January 13, 2016 18:37
examples of iterators usage
//Iterate through object
const obj = {
foo: 'bar',
comes: 'is',
from: 'object'
};
for(let key in obj) {
if(obj.hasOwnProperty(key)) {
let valueOfKey = obj[key];
console.log('%s: %s', key, valueOfKey);