Skip to content

Instantly share code, notes, and snippets.

View dmjcomdem's full-sized avatar
🎯
Focusing

Dimonskiy dmjcomdem

🎯
Focusing
View GitHub Profile
@dmjcomdem
dmjcomdem / findHelper.js
Last active November 21, 2018 06:23
Sample find method
let moList = [{id: 1, name: 'NameMO-1'}, {id: 2, name: 'NameMO-2'}, {id: 3, name: 'NameMO-3'}];
let find = (() => {
let store = {};
return (list, id, key = 'id') => {
if (store[id]) {
return store[id];
} else {
let item = list.find(item => item[key] === id);
@dmjcomdem
dmjcomdem / deepClone.js
Created September 13, 2018 08:18
Полная копия объекта
const deepClone = obj => {
let clone = Object.assign({}, obj);
Object.keys(clone).forEach(
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
);
return Array.isArray(obj) ? (clone.length = obj.length) && Array.from(clone) : clone;
};
@dmjcomdem
dmjcomdem / autosize-input.html
Created August 16, 2018 04:36
autosize input
<input type="text" oninput="this.style.width = (this.value.length + 1 ) * 8 + 'px'">
@dmjcomdem
dmjcomdem / xPathToCss.js
Created August 1, 2018 12:27
convert Xpath to CSS
function xPathToCss(xpath) {
return xpath
.replace(/\[(\d+?)\]/g, function(s,m1){ return '['+(m1-1)+']'; })
.replace(/\/{2}/g, '')
.replace(/\/+/g, ' > ')
.replace(/@/g, '')
.replace(/\[(\d+)\]/g, ':eq($1)')
.replace(/^\s+/, '');
}
@dmjcomdem
dmjcomdem / hideMenu.js
Created July 8, 2018 16:38
Hide Menu On Scroll
let prevScrollpos = window.pageYOffset;
let navbar = document.querySelector('.navbar');
window.addEventListener('scroll', (event) => {
let currentScrollpos = window.pageYOffset;
prevScrollpos > currentScrollpos
? navbar.style.setProperty('top', 0)
: navbar.style.setProperty('top', '-100px')
prevScrollpos = currentScrollpos;
})
@dmjcomdem
dmjcomdem / decodeHtmlEntity.js
Last active June 30, 2018 09:45
Перевод html сущности в utf-8
let decodeHtmlEntity = (str) => {
return str.replace(/&#?x(.+?);/gi, (match, dec) => String.fromCharCode('0x' + dec));
};
// decodeHtmlEntity('&#x411;&#x423; &#x425;&#x41C;&#x410;&#x41E;-&#x42E;&#x433;&#x440;&#x44B; &#x41C;&#x418;&#x410;&#x426;')
// "БУ ХМАО-Югры МИАЦ"
@dmjcomdem
dmjcomdem / typographer.js
Last active October 22, 2022 17:49
Typography rus language
function typographer(
string,
words = ['в', 'во', 'без', 'до', 'из', 'к', 'ко', 'на', 'по', 'о', 'от', 'при', 'с', 'у', 'не', 'за', 'над', 'для', 'об', 'под', 'про', 'и', 'а', 'но', 'да', 'или', 'ли', 'бы', 'то', 'что', 'как', 'я', 'он', 'мы', 'они', 'ни', 'же', 'вы', 'им']
){
if (!string && typeof string !== 'string') throw Error('can\'t find string');
let pattern = new RegExp( words.map(w => ` ${w} `).join('|'), 'gi' );
return string.replace(pattern, str => str.slice(0, -1) + '&nbsp;' );
}
console.log( typographer('Экспериментаниум – идеальная площадка для проведения открытых уроков и интерактивных занятий') );
@dmjcomdem
dmjcomdem / detectDeviceType.js
Last active May 24, 2018 12:47
Detects wether the website is being opened in a mobile device or a desktop/laptop.
const detectDeviceType = () =>
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
? 'Mobile'
: 'Desktop';
detectDeviceType(); // "Mobile" or "Desktop"
@dmjcomdem
dmjcomdem / simple-grid-in-css.scss
Created May 18, 2018 12:17
Простой пример grid-in-css
$breakpoints: (
"xs": 'max-width: 768px',
"sm": 'min-width: 768px',
"md": 'min-width: 992px',
"lg": 'min-width: 1200px',
"xlg": 'min-width: 1400px'
);
$gap: 15px;
$column: 12;
@dmjcomdem
dmjcomdem / getURLParameter.js
Created May 18, 2018 07:51
Get URL parameter
let getURLParameter = (name) => decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;