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 / 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) => {
@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 / array-shift.js
Last active May 24, 2024 17:03
Array Shift
export function shift(arr, direction, n)
{
const times = n > arr.length ? n % arr.length : n
return arr.concat(arr.splice(0, (direction > 0 ? arr.length - times : times)))
}
@alekstar79
alekstar79 / rgb-to-hex.js
Last active May 24, 2024 17:04
RGB to HEX
/**
* @param {{r: number, g: number, b: number}}
* @return {string}
*/
export function rgbToHex({ r, g, b })
{
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
}
@alekstar79
alekstar79 / open.js
Last active May 24, 2024 17:12
Working with blob-events-fs
const nodes = []
export function removeNodes()
{
nodes.forEach(el => el.parentNode && el.parentNode.removeChild(el))
nodes.splice(0, nodes.length)
}
/**
* @param {String} link
@alekstar79
alekstar79 / format.js
Last active May 24, 2024 17:14
Calculating and formatting file size
/**
* @param {Number} bytes
* @param {Number} decimals
* @return {string}
*/
export function format(bytes, decimals = 2)
{
if (bytes === 0) return '0 B'
const dim = ['B','KB','MB','GB','TB','PB','EB','ZB','YB'],
@alekstar79
alekstar79 / declination.js
Last active May 24, 2024 17:15
Numeric declination
/**
* @example declination(5, ['комментарий','комментария','комментариев'])
* @param {Number} n The number for which the ending will be calculated
* @param {Array} titles Word variants for 1,2,5
* @returns {String}
*/
export function declination(n, titles)
{
return titles[(n % 10 === 1 && n % 100 !== 11) ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2]
}
@alekstar79
alekstar79 / fullscreen.js
Last active May 24, 2024 17:24
Cross-browser fullscreen controller
const document = typeof window !== 'undefined' && typeof window.document !== 'undefined' ? window.document : {}
export const fullscreen = (function(fullscreen) {
const isEnabled = false, fn = {}
if (!fullscreen.some(map => {
if (map[1] in document) {
for (let i = 0; i < map.length; i++) {
fn[fullscreen[0][i]] = map[i]
}
@alekstar79
alekstar79 / renderer.js
Last active May 24, 2024 17:27
Zoom renderer
export function Renderer({ minScale, maxScale, scaleSensitivity = 10 })
{
const getInitialState = () => ({
minScale,
maxScale,
scaleSensitivity,
transformation: {
originX: 0,
originY: 0,
translateX: 0,
@alekstar79
alekstar79 / lorem.js
Last active May 24, 2024 17:25
Lorem Ipsum Generator
/** "Lorem ipsum" style text. */
/**
* Produces a random number
* @return {Number} Random number
*/
export function gauss()
{
return (Math.random() * 2 - 1) + (Math.random() * 2 - 1) + (Math.random() * 2 - 1)
}