Skip to content

Instantly share code, notes, and snippets.

View alekstar79's full-sized avatar
🎯
Focusing

Aleksey Tarasenko alekstar79

🎯
Focusing
View GitHub Profile
@alekstar79
alekstar79 / random-string.js
Last active May 24, 2024 17:03
Random string generator
const caps = Array.from('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
lower = Array.from('abcdefghijklmnopqrstuvwxyz'),
numbers = Array.from('0123456789')
export function symbols(set = 'all')
{
return set === 'all' ? Array.from([...caps,...lower,...numbers]) : Array.from({ caps, lower, numbers }[set])
}
export function rndstring(length = 7, signs = 'all')
@alekstar79
alekstar79 / url-mime.js
Last active May 24, 2024 17:01
Base64data extractor
/**
* @param {String} data
* @return {{data: String|null, type: String|null}}
*/
export function dataURLMime(data)
{
const decompose = { type: null, data: null },
regexp = /data:(image\/.+);base64,/
decompose.data = data.replace(regexp, (h, t) => {
function getUserAgent({ brands = [] })
{
let agent
test: for (const { brand, version } of brands) switch (true) {
case ['Chromium','Google Chrome','CriOS'].includes(brand):
agent = `Chrome/${version}`
break
case ['Firefox'].includes(brand):
/**
* @param {Function} job
* @return {function(*=): Promise<unknown>}
*/
export function workerInit(job)
{
const url = window.URL.createObjectURL(new Blob(
[`"use strict";\nself.onmessage = ${job.toString()}`],
{ type: 'text/javascript' }
))
/**
* @param {Function} fn
* @return {Function}
*/
export function createCachedFunction(fn)
{
const handler = {
cache: {},
apply(target, that, args)
/** @see https://gist.github.com/alekstar79/11125248f8dc609f5c8dcba3e0f75b5e */
import { createCachedFunction } from './cached.mjs'
/** @see https://gist.github.com/alekstar79/042a211b62203f1439f2012ce6aa9f55 */
import { workerInit } from './worker.mjs'
/**
* @param {function} worker
* @return {function(*=): Promise<unknown>}
* @example
const h2sl = (hex, isString) => {
// Convert hex to RGB first
let r = 0, g = 0, b = 0
if (hex.length == 4) {
r = "0x" + hex[1] + hex[1]
g = "0x" + hex[2] + hex[2]
b = "0x" + hex[3] + hex[3]
} else if (hex.length == 7) {
r = "0x" + hex[1] + hex[2]
function pipe(...fn) {
return function piped(...args) {
return fn.reduce((result, f) => [f.call(this, ...result)], args)[0]
}
}
function pick(obj, keys) {
return keys.reduce((acc, key) => {
if (obj.hasOwnProperty(key)) {
acc[key] = obj[key]
}
return acc
}, {})
}
function omit(obj, keys) {
return Object.keys(obj)
.filter(key => !keys.includes(key))
.reduce((acc, key) => {
acc[key] = obj[key]
return acc
}, {})
}