Skip to content

Instantly share code, notes, and snippets.

View dmjcomdem's full-sized avatar
🎯
Focusing

Dimonskiy dmjcomdem

🎯
Focusing
View GitHub Profile
function throttle(fn, delay) {
let last;
let timer;
return () => {
const now = +new Date;
if (last && now < last + delay) {
clearTimeout(timer);
@dmjcomdem
dmjcomdem / text-fill.css
Created July 7, 2017 21:37
text transparent
.class {
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
@dmjcomdem
dmjcomdem / reducers.js
Created July 8, 2017 19:13
advanced-reduce
let selected = [
{ price: 20 },
{ price: 45 },
{ price: 67 },
{ price: 1305 }
];
let reducers = {
rubles : (state, item) => { return state.rubles += item.price },
dollars: (state, item) => { return state.dollars += item.price / 71.6024 },
@dmjcomdem
dmjcomdem / placeholder.sass
Created July 11, 2017 07:48
style placeholder
.form-control {
&::placeholder {
color: #ccc;
opacity: 1;
text-overflow: ellipsis !important;
transition: opacity .3s ease-in-out;
}
&:focus::-webkit-input-placeholder {opacity: 0;}
@dmjcomdem
dmjcomdem / createElement.js
Created July 24, 2017 20:46
Создание DOM-элемента, с указанными атрибутами
function createElement(tag, props, ...children) {
const element = document.createElement(tag);
Object.keys(props).forEach(key => {
if (key.startsWith('data-')) {
element.setAttribute(key, props[key]);
} else {
element[key] = props[key];
}
});
@dmjcomdem
dmjcomdem / transliterate.js
Created July 26, 2017 08:47
Example transliterate object
var rus = "щ ш ч ц ю я ё ж ъ ы э а б в г д е з и й к л м н о п р с т у ф х ь".split(/ +/g),
eng = "shh sh ch cz yu ya yo zh `` y' e` a b v g d e z i j k l m n o p r s t u f x `".split(/ +/g);
function translit(text) {
var text = text.toLowerCase();
for(x = 0; x < rus.length; x++) {
text = text.split(rus[x]).join(eng[x]);
}
return text;
}
@dmjcomdem
dmjcomdem / CeasarsCipher.js
Created July 26, 2017 12:36
Простой способ Ceasars Cipher Encryptor (сдвиг позиции буквы в алфавите )
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase(),
otherCharacters = '-=~\"\'#$%&*^:<>?/!{(|)}.1234567890\, ';
var shiftedAlphabet = alphabet.slice(3);
shiftedAlphabet += alphabet.slice(0, 3);
shiftedAlphabet += otherCharacters;
alphabet += otherCharacters;
var str = 'I am amazing'.toLowerCase(),
out = '';
$(document).ready(function(){
$("#menu").on("click","a", function (event) {
//отменяем стандартную обработку нажатия по ссылке
event.preventDefault();
//забираем идентификатор бока с атрибута href
var id = $(this).attr('href'),
//узнаем высоту от начала страницы до блока на который ссылается якорь
top = $(id).offset().top;
@dmjcomdem
dmjcomdem / validNumber.js
Created August 31, 2017 12:23
valid card number
input.addEventListener('input', function() {
var _arrValue = [];
var _value = this.value.replace(/\D/gi, '');
if(_value.length > 16) {
_value = _value.substr(0,16)
}
for(let i = 0; i < _value.length; i+=4) {
_arrValue.push(_value.substr(i, 4));
let a = 'xxx-xxx-xxx xx'.replace(/x/g, (n=>s=>x=>s[n++])(0)('92795008123'.replace(/\D/g,'')));
console.log(a); // 927-950-081 23
// видно, что (0)('92795008123'.replace(/\D/g,'') - это вызов функции типа нашей add(1)(2)
// '92795008123'.replace(/\D/g,'' - тут просто замена не цифр (\D) на '', то есть оставление только цифр
// вообще говоря имеет смысл только когда '92795008123' приходит из внешнего кода
// перепишем (n=>s=>x=>s[n++]) на стандартные function:
function repl(n) { // 1) n = 0 - индекс в строке s
return function (s) { // 2) s = '92795008123' - форматируемая строка