Skip to content

Instantly share code, notes, and snippets.

View va9iff's full-sized avatar

Vagif Valiyev va9iff

View GitHub Profile
@va9iff
va9iff / bindi.js
Last active July 8, 2024 14:57
elegant JavaScript keyboard shortcut binder
(() => {
function makeBindi(keys) {
return new Proxy({}, {
get(_, key) {
return makeBindi([...keys, key])
},
set(_, key, func) {
document.addEventListener('keydown', e => {
if (
(e.key.toLowerCase() === key) &&
@va9iff
va9iff / range.js
Last active March 27, 2023 04:14
quick one-liner for creating array from range. range(n) returns [0, 1, ..., n]; range(n, k) returns [k, k+1, ..., n]
const range = (b = 1, a = 0) => [...Array(b - a).keys()].map(i => i + a)
@va9iff
va9iff / elCls.js
Created February 3, 2023 00:29
el("div.className.another") short function for creating elements with classes
export const el = str =>{
const strs = str.split('.')
let el = document.createElement(strs.shift())
for (let cls of strs) el.classList.add(cls)
return el
}
el('div.wrapper.hidden')
@va9iff
va9iff / fancyQuery.js
Last active January 21, 2023 20:12
query with $.class syntax only in 1 line
let $ = new Proxy({}, { get: (_, c) => document.querySelector(`.${c}`) })
$.toggleBtn.onclick = e => {
$.content.classList.toggle("visible")
}
$.toggleBtn.click()
@va9iff
va9iff / one-line counter.js
Last active January 6, 2023 19:28
fancy lookin' than regular closures
const count = ((counter = 0) => () => counter++)()
console.log(count(), count(), count()) // 0 1 2
one line github files looks weird to me :d