Skip to content

Instantly share code, notes, and snippets.

const secs2Time = (secs) => {
const hours = Math.floor(secs / 60 / 60).toString().padStart(2, 0)
return hours + ':' + new Date(secs * 1000).toISOString().substr(14, 5)
}
function download(filename, text) {
Object.assign(document.createElement("a"), {
download: filename,
href: "data:text/plain;charset=utf-8," + encodeURIComponent(text),
}).click();
}
@Ivannnnn
Ivannnnn / chainable.js
Created December 16, 2021 10:36
Chainable
function chainable(obj) {
const proxy = new Proxy(obj, {
get(target, prop) {
return typeof target[prop] === 'function'
? (...args) => target[prop](...args) || proxy
: target[prop]
},
})
return proxy
}
@Ivannnnn
Ivannnnn / withMemoization.js
Created December 14, 2021 21:54
withMemoization
function withMemoization(func, identifierFunc) {
const cache = new Map()
return (...args) => {
const identifier = identifierFunc(...args)
if (cache.has(identifier)) {
return cache.get(identifier)
}
const result = func(...args)
cache.set(identifier, result)
@Ivannnnn
Ivannnnn / functional.js
Last active October 12, 2022 11:13
Some functional programming functions
const SORT_STRATEGIES = {
asc: (a, b) => (a - b > 0 ? 1 : -1),
desc: (a, b) => (a - b < 0 ? 1 : -1),
};
const pipe = (...fns) =>
fns.reduce(
(f, g) =>
(...args) =>
g(f(...args))
@Ivannnnn
Ivannnnn / createTimer.js
Last active December 12, 2021 13:30
Timer
const createInterval = () => {
let interval = null
const start = (cb, delay) => !interval && (interval = setInterval(cb, delay))
const stop = () => {
clearInterval(interval)
interval = null
}
return { start, stop }
}
@Ivannnnn
Ivannnnn / renderWithEvents.html
Last active December 12, 2021 12:12
render html with events
<div id="app"></div>
<script>
const $ = (...args) => {
if (args.length > 1) args.reverse()
const [query, parent = document] = args
return Array.from(parent.querySelectorAll(query))
}
$.create = (html) => {
@Ivannnnn
Ivannnnn / timeTaken.js
Last active August 31, 2022 10:36
Execution time of function
const timeTaken =
(fn) =>
async (...args) => {
console.time(fn.name);
const res = await fn(...args);
console.timeEnd(fn.name);
return res;
};
@Ivannnnn
Ivannnnn / getGlobals.js
Last active August 15, 2022 12:26
get globals of a webpage
const getGlobals = (() => {
const diff = (a, b) => a.filter((v) => !b.includes(v))
const windowProps = JSON.parse(
'["window","self","document","name","location","customElements","history","locationbar","menubar","personalbar","scrollbars","statusbar","toolbar","status","closed","frames","length","top","opener","parent","frameElement","navigator","origin","external","screen","innerWidth","innerHeight","scrollX","pageXOffset","scrollY","pageYOffset","visualViewport","screenX","screenY","outerWidth","outerHeight","devicePixelRatio","clientInformation","screenLeft","screenTop","defaultStatus","defaultstatus","styleMedia","onsearch","isSecureContext","performance","onappinstalled","onbeforeinstallprompt","crypto","indexedDB","webkitStorageInfo","sessionStorage","localStorage","onbeforexrselect","onabort","onblur","oncancel","oncanplay","oncanplaythrough","onchange","onclick","onclose","oncontextmenu","oncuechange","ondblclick","ondrag","ondragend","ondragenter","ondragleave","ondragover","ondragstart","o
@Ivannnnn
Ivannnnn / prop.js
Last active December 14, 2021 19:43
Get object properties by path without error when null
function prop(value, path) {
path = path
.replace(/\[(\d)\]/g, (_, v) => '.' + v)
.replace(/^\./, '')
.split('.')
let current = value
for (let prop of path) {
if (current[prop]) current = current[prop]
else return current[prop]
}