Skip to content

Instantly share code, notes, and snippets.

View petermihailov's full-sized avatar
👋

Peter Mihailov petermihailov

👋
View GitHub Profile
@petermihailov
petermihailov / checkForUndefinedCSSClasses.js
Created February 15, 2023 16:33 — forked from broofa/checkForUndefinedCSSClasses.js
ES module for detecting undefined CSS classes (uses mutation observer to monitor DOM changes). `console.warn()`s undefined classes.
/**
* Sets up a DOM MutationObserver that watches for elements using undefined CSS
* class names. Performance should be pretty good, but it's probably best to
* avoid using this in production.
*
* Usage:
*
* import cssCheck from './checkForUndefinedCSSClasses.js'
*
* // Call before DOM renders (e.g. in <HEAD> or prior to React.render())
export const linear = t => t;
export const easeInQuad = t => t * t;
export const easeOutQuad = t => t * (2 - t);
export const easeInOutQuad = t => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
export const easeInCubic = t => t * t * t;
export const easeOutCubic = t => (--t) * t * t + 1;
export const easeInOutCubic = t => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
export const easeInQuart = t => t * t * t * t;
export const easeOutQuart = t => 1 - (--t) * t * t * t;
export const easeInOutQuart = t => t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t;
function resolveHostname(url) {
const a = document.createElement('a');
a.href = url;
const { hostname } = a;
return hostname.indexOf('www.') === 0 ? hostname.slice(4) : hostname;
}
// test
resolveHostname('http://localhost:3000/some/route?some=arg#hash'); // localhost
resolveHostname('https://www.cosmo.ru/beauty/star_beauty/?utm_source=pulse_mail_ru&utm_referrer=https%3A%2F%2Fpulse.mail.ru'); // cosmo.ru
{
const add = (...args) => {
const sum = (...args) => {
sum.result = args.reduce((res, n) => res + n, sum.result);
return sum;
}
sum.result = 0;
sum.valueOf = () => sum.result;
sum(...args);
[8, 42, 38, 111, 2, 39, 1].forEach(num => setTimeout(() => console.log(num), num));
const handleClickOutside = ({trigger, popup, cb}) => (e) => {
const {target} = e;
if (
!popup.contains(target)
&& !trigger.contains(target)
&& target !== trigger
) {
cb(e);
}
Promise.resolve()
.then(() => setTimeout(() => console.log('1'), 0))
.then(() => console.log('2'))
.then(() => console.log('3'))
Promise.resolve()
.then(() => console.log('4'))
.then(() => console.log('5'))
.then(() => console.log('6'))
@petermihailov
petermihailov / shuffle.js
Created March 27, 2019 19:37
Перемешать массив
[1, 2, 3, 4, 5].sort(() => 0.5 - Math.random());
@petermihailov
petermihailov / effects.js
Last active February 14, 2020 14:44
Продающий лендинг всего за несколько секунд!
(() => {
const elements = document.querySelectorAll('a, button, img');
const head = document.querySelector('head');
const link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css';
head.appendChild(link);
const effects = ["bounce","flash","pulse","rubberBand","shake","swing","tada","wobble","jello","heartBeat","flip","hinge","jackInTheBox"];
const popChars = (str, count) => ({
val: str.substr(-count),
str: str.slice(0, -count),
});
const checkPrev = (str, val) => new RegExp(--val + '$').test(str);
const stepSequence = (str, digit) => {
let step = popChars(str, digit)
while (checkPrev(step.str, step.val)) {