Skip to content

Instantly share code, notes, and snippets.

View Lavrend's full-sized avatar

Dmitriy Lavrentev Lavrend

  • Russian Federation
View GitHub Profile
@Lavrend
Lavrend / vue-click-outside.js
Last active June 9, 2021 09:16
The directive responds to click outside the block
/**
* The directive responds to click outside the block
* @example <my-component v-click-outside="callback" />
*/
const events = ['click'];
const instances = new Map();
const onClickOutside = ({ event, el, handler, middleware }) => {
const isClickOutside = event.target !== el && !el.contains(event.target);
@Lavrend
Lavrend / .md
Created February 5, 2021 12:32 — forked from iAdramelk/.md
Длинная телега про Бутстрап

Английская версия: https://evilmartians.com/chronicles/bootstrap-an-intervention

Вводная часть

У CSS есть несколько базовых проблем, которые позволяют очень быстро отстрелить себе ногу при неправильном использовании:

  1. Глобальный неймспейс – в серверном программировании все что написано в файле, в файле и остается. Все же что написано в css и js засирает глобальное пространство имен со всеми вытекающими. В JS эту проблему сейчас побороли всякими модульными системами, а вот с css сложнее. В идеальном мире это должен починить Shadow DOM и настоящие Web Components, но пока их нет единственный способ с этим бороться – следовать какой-то системе именований селекторов, которая по возможности уменьшает и исключает возможные конфликты.

  2. Каскадность – если на один элемент может сработать несколько правил, то они все и сработают последовательно. Если есть элемент h1.title, на него сработают все правила для тегов h1 и все правила для класса .title. Так как весь html состоит из тегов, то правил которые п

@Lavrend
Lavrend / index.js
Created January 13, 2021 21:38 — forked from DawidMyslak/index.js
Combining Vue.js and Knockout.js together
// skip import for KO (Knockout.js) here, it should be available in global deps
import Vue from 'vue';
import ProjectsConversationWrapper from 'ProjectsConversationWrapper';
import { getProjectConversation } from 'api';
// unique component identifier
const uniqueId = 'chat-project-room';
const bindingHandlerInit = async (element, valueAccessor) => {
const projectId = parseInt(ko.unwrap(valueAccessor()), 10);
@Lavrend
Lavrend / UndoStack.js
Created January 13, 2021 10:47 — forked from dsamarin/UndoStack.js
Easy undo-redo in JavaScript.
function UndoItem (perform, data) {
this.perform = perform;
this.data = data;
}
/**
* UndoStack:
* Easy undo-redo in JavaScript.
**/
@Lavrend
Lavrend / lodash-check-equals.js
Last active May 26, 2020 14:19
Check difference for deep arrays of objects
const listA = [{id: 1, b: { c: [1,3,2]}}, {id: 2, b: { c: [4,5,6]}}, {id: 3, b: { c: [4,5,6]}}];
const listB = [{id: 1, b: { c: [1,2,3]}}, {id: 3, b: { c: [4,5,6]}}, {id: 2, b: { c: [4,5,6]}}, {id: 4, b: { c: [7,8,9]}}];
const isEqualLists = (listA = [], listB = [], sortKey = 'id') => {
const sortedA = _.sortBy(listA, (item) => item[sortKey]);
const sortedB = _.sortBy(listB, (item) => item[sortKey]);
const maxArr = _.maxBy([sortedA, sortedB], (arr) => arr.length);
const minArr = _.minBy([sortedA, sortedB], (arr) => arr.length);
@Lavrend
Lavrend / is-palindrome.js
Created May 16, 2019 23:16
Palindrome check
const isPalindrome = (str = '') => {
const reg = /[^A-Za-z0-9]/g;
const baseStr = str.toLowerCase().replace(reg, '');
const reverseStr = baseStr.split('').reverse().join('');
return reverseStr === baseStr;
};
// Test
@Lavrend
Lavrend / find-removed-number.js
Created May 16, 2019 23:03
Finds the number taken from the mixed array of numbers from 1 to N
// Values for test
const N = 100;
const pos = 34;
const baseArr = Array.from({ length: N }, (v, k) => k + 1);
const removedArr = baseArr.filter(item => item !== pos + 1).sort(() => 0.5 - Math.random());
// -----------------------------------------------------------------------------------------
// Main method
const getRemovedNumber = (arr, maxNumber) => {
const baseSum = (maxNumber * (maxNumber + 1)) / 2;
@Lavrend
Lavrend / uniq-chars.js
Created May 16, 2019 20:54
Checks if words are the same letter
const arrSame = ['va5ya', '5avya', '5ayva'];
const arrDiff = ['cat', 'act', 'car'];
const isUniqChars = (arr) => {
const sortFn = (a, b) => {
if (a > b) return 1;
if (b > a) return -1;
return 0;
@Lavrend
Lavrend / tree-sum.js
Created May 16, 2019 18:24
Sum of all tree vertices
const tree = {
value: 3,
child: [
{
value: 1,
child: null
},
{
value: 3,
@Lavrend
Lavrend / infinity-sum.js
Created May 16, 2019 15:03
infinite function call: Sum(a)(b,c)...(x,y,z) => a + (b + c) + ... + (x + y + z)
function sum(...args) {
let total = 0;
const reduceSum = (arr = [], start = 0) => {
return arr.reduce((memo, val) => memo + val, start);
};
const fn = (...args) => {
const newSum = reduceSum(args, total);
total = newSum;