Skip to content

Instantly share code, notes, and snippets.

@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 / 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
}
function download(filename, text) {
Object.assign(document.createElement("a"), {
download: filename,
href: "data:text/plain;charset=utf-8," + encodeURIComponent(text),
}).click();
}
const secs2Time = (secs) => {
const hours = Math.floor(secs / 60 / 60).toString().padStart(2, 0)
return hours + ':' + new Date(secs * 1000).toISOString().substr(14, 5)
}
async function fetchLink(q) {
const htmlStr = await fetch(
'https://duckduckgo.com/?q=' + q + '&ia=web'
).then((r) => r.text())
return `https://links.duckduckgo.com/${htmlStr.match(/\/(d.js.+?)&p_ent/)[1]}`
}
async function fetchDuck(q) {
function transform({ u, t, a, i }) {
return {
@Ivannnnn
Ivannnnn / css.js
Last active August 30, 2022 07:07
function css(obj) {
return Object.keys(obj)
.map((key) => {
const newKey = key.replace(
/[A-Z]/,
(match, index) => "-" + match.toLowerCase()
);
return `${newKey}:${obj[key]};`;
})
.join("\n");
const parseToDOM = Range.prototype.createContextualFragment.bind(
document.createRange()
);
const functionalize = (arr) => {
const result = {};
for (let funcOrArr of arr) {
const [func, indexOfData = 0] =
funcOrArr.constructor.name === "Array" ? funcOrArr : [funcOrArr];
const name = func.name || func;
result[name] =
(...args) =>
(data) => {
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));
const { buildQueryStr, timeTaken } = require("./helpers.js");
async function getSource({ q, ...params } = {}, page = 1) {
if (!q) throw Error("{ q } is a manadatory parameter!");
const url =
`https://www.google.com/search?` +
buildQueryStr({
function scrollToBottom() {
const scrollingElement = document.scrollingElement || document.body;
scrollingElement.scrollTop = scrollingElement.scrollHeight;
}