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 / 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)
}
@alekstar79
alekstar79 / cancelable-promise.js
Last active May 24, 2024 16:26
Сancelable promise
const noop = () => {}
export class Cancelable extends Promise
{
constructor(executor = noop)
{
super((resolve, reject) => {
executor(v => {
/**
@alekstar79
alekstar79 / callbacks.js
Last active May 24, 2024 16:26
Asynchronous handlers
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/queueMicrotask
*/
export const queueMicrotask = (function() {
const queueMicrotask = typeof window !== 'undefined'
? window.queueMicrotask
: undefined
return queueMicrotask || (cb => {
Promise.resolve().then(cb).catch(e => setTimeout(() => { throw e }))
@alekstar79
alekstar79 / binary.js
Last active May 24, 2024 16:23
Binary Search
Object.defineProperty(Array.prototype, 'binarySearch', {
value(target, comparator) {
let l = 0,
h = this.length - 1,
m, comparison
/* default comparison method if one was not provided */
comparator = comparator || function(a, b) {
return (a < b ? -1 : (a > b ? 1 : 0))
}
@alekstar79
alekstar79 / async-array.js
Last active May 24, 2024 16:19
Async array
export class AsyncArray extends Array
{
[Symbol.asyncIterator]()
{
let i = 0
return {
next: () => new Promise(resolve => {
setTimeout(() => resolve({ value: this[i], done: i++ === this.length }))
})
@alekstar79
alekstar79 / str2ab.js
Created April 24, 2023 10:46
Convert String to ArrayBuffer
export function str2ab(str)
{
let buf = new ArrayBuffer(str.length * 2) // 2 bytes for each char
let bufView = new Uint16Array(buf)
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i)
}
return buf
@alekstar79
alekstar79 / ab2str.js
Created April 24, 2023 10:43
Convert ArrayBuffer to String
export function ab2str(buf)
{
return String.fromCharCode.apply(null, new Uint16Array(buf))
}
@alekstar79
alekstar79 / convert.php
Created February 4, 2023 09:18
Converting images to WebP in PHP
function convertToWebp(string $src, int $quality = 100): string
{
$dir = pathinfo($src, PATHINFO_DIRNAME);
$name = pathinfo($src, PATHINFO_FILENAME);
$ext = pathinfo($src, PATHINFO_EXTENSION);
$dest = "$dir/{$name}_$ext.webp";
$is_alpha = false;
switch (mime_content_type($src)) {
@alekstar79
alekstar79 / one-line-utils.js
Created February 4, 2023 07:59
One-line JavaScript functions
// Immutable Shuffle
const shuffleImmutable = (arr) => arr.slice().sort(() => Math.random() - 0.5)
// Random number in range
const randomInRange = (from, to) => Math.floor(from + Math.random() * (to - from + 1))
// Array with unique items
const uniqueItems = (arr) => [...new Set(arr)]
// Uppercase firs letter
@alekstar79
alekstar79 / Install PyQt5 on Ubuntu with python3 .md
Created January 16, 2021 18:28 — forked from r00tdaemon/Install PyQt5 on Ubuntu with python3 .md
Install PyQt5 on Ubuntu with python3. Steps to set up PyQt5 (ubuntu). With python code generation

Installation

pip3 install --user pyqt5  
sudo apt-get install python3-pyqt5  
sudo apt-get install pyqt5-dev-tools
sudo apt-get install qttools5-dev-tools

Configuring to run from terminal