Skip to content

Instantly share code, notes, and snippets.

View Oaphi's full-sized avatar
💭
☕ and 👩‍💻

Oleg Valter Oaphi

💭
☕ and 👩‍💻
  • Rocket
  • Saint Petersburg
View GitHub Profile
@Oaphi
Oaphi / withInterval.js
Created December 3, 2020 21:05
Promise-based interval runner (cancellable)
const withInterval = async ({
timeouts = [],
delay = 0,
interval = 4,
callback,
times = 1,
stopIf = () => false
}) => {
if (!times) {
return;
@Oaphi
Oaphi / gas-unicode-hex.ts
Last active December 6, 2020 05:54
Convert Unicode literal to hexadecimal byte sequence
/**
* @summary converts unicode literal \u{<code point>} into hex byte sequence \x<byte>...
*/
const fromUnicodeToHexBytes = (unicode: string) => {
//make bytes unsigned -128 +127 -> +256 or & 0xff -> 0 256
return Utilities.newBlob(unicode)
.getBytes()
.map((bt) => `\\x${(bt & 0xff).toString(16)}`)
.join("");
};
@Oaphi
Oaphi / DriveUtils.ts
Created December 7, 2020 01:33
Drive utilities
const getFolderByName = (name: string, create = false) => {
const iter = DriveApp.getFoldersByName(name);
return iter.hasNext()
? iter.next()
: create
? DriveApp.createFolder(name)
: null;
};
@Oaphi
Oaphi / WidthMeasure.js
Last active December 7, 2020 04:25
Preciser text width measure (single line, obviously needs transpiling for the browser detection to work)
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics}
* @summary measure text width
* @param {CanvasRenderingContext2D} ctx drawing context
* @returns {number}
*/
const measureWidth = (ctx, text) => {
const measurement = ctx.measureText(text);
@Oaphi
Oaphi / webpack-setup.md
Created February 5, 2021 04:05
Webpack installation notes

Quick install for HTML-based projects:

npm install --save-dev 
  webpack \
  clean-webpack-plugin \
  css-minimizer-webpack-plugin \
  html-webpack-plugin \
  html-loader
@Oaphi
Oaphi / recursiveAppend.ts
Created February 7, 2021 20:44
Recursive append Node.js utility
import { BaseEncodingOptions } from "fs";
import { appendFile, mkdir } from "fs/promises";
import { parse, sep } from "path";
const recursiveAppend = async (
path: string,
encoding: BaseEncodingOptions["encoding"] = "utf-8"
) => {
const parts = path.split(sep);
@Oaphi
Oaphi / HTMLSelectElementUtils.js
Last active February 9, 2021 00:52
Useful utilities for HTMLSelectElement
/**
* @summary extracts values from select
* @param {HTMLSelectElement} sel
* @return {string[]}
*/
const getSelectVals = ({ options }) =>
Array.from(options).map(({ value }) => value);
/**
* @summary checks if select has a value
@Oaphi
Oaphi / domHelpers.ts
Last active February 10, 2021 02:34
Common basic DOM helpers
export const remClasses = (
{ classList }: { classList: DOMTokenList },
...classes: string[]
) => classList.remove(...classes);
export const addClasses = (
{ classList }: { classList: DOMTokenList },
...classes: string[]
) => classList.add(...classes);
@Oaphi
Oaphi / TypePeculiarities.ts
Last active February 17, 2021 06:59
Peculiar types found in TS land
//@see https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
type swithcNever<T, A, B> = [T] extends [never] ? A : B;
//@see https://stackoverflow.com/a/63568058/11407695
type Flatten<T extends any[]> = T extends [infer U, ...infer R]
? U extends any[]
? [...Flatten<U>, ...Flatten<R>]
: [U, ...Flatten<R>]
: [];
@Oaphi
Oaphi / utilityTypes.ts
Last active February 17, 2021 08:04
General-purpose utility types
type Invert<T> = { [ P in keyof T as Extract<T[P], string | number | symbol> ] : P };
type test1 = Invert<{ a: "ia", b: "ib", c: 1 }>; //{ ia: "a"; ib: "b"; 1: "c"; }
type DeepPath<T extends string[], U> = T extends [infer F, ...infer L]
? F extends string
? { [P in F]: L extends string[] ? DeepPath<L,U> : never }
: {}
: U;