Skip to content

Instantly share code, notes, and snippets.

View BPanchenko's full-sized avatar
:bowtie:

Boris Vasilievich Panchenko BPanchenko

:bowtie:
View GitHub Profile
@BPanchenko
BPanchenko / gist:f6ccef56ac88ef59e2dcae80d0294051
Created January 21, 2020 17:47
Declination of a word depending on the number
Number.prototype.noun = function(c, f) {
f || (f = true), c = c.split('|')
let n = this.toString().substr(-2)
let r = f ? this + ' ' : ''
return r + c[0] + ((/^[0,2-9]?[1]$/.test(n)) ? c[2] : ((/^[0,2-9]?[2-4]$/.test(n)) ? c[3] : c[1]))
}
// (5).noun('огур|цов|ец|ца') => 5 огурцов
@BPanchenko
BPanchenko / gist:b0bd201fb9e9f600ffb025911e2a50aa
Created August 4, 2023 06:27
Getting an index in an array using a adjustment number
const len = array.length // some array and its length
const idx = 0 // current index
const updateIndex = (adj: number): number => (((idx + adj) % len) + len) % len
@BPanchenko
BPanchenko / gist:b5a7fa4e2997088a716219dc166fd34d
Created April 24, 2024 21:29
Recursively Object.freeze() on objects and functions adding readonly type. Object.freeze includes Seal.
export const deepFreeze = <T>(source: T, freezeParent = true): DRo<T> => {
if (freezeParent) Object.freeze(source)
Object.getOwnPropertyNames(source).forEach(function(prop) {
if (
Object.prototype.hasOwnProperty.call(source as any, prop) &&
(source as any)[prop] !== null &&
(typeof (source as any)[prop] === 'object' || typeof (source as any)[prop] === 'function')
) {
if (Object.isFrozen((source as any)[prop])) {
/**
* Convert string literal type to camelCase.
*/
declare type CamelCase<T extends string> = T extends `${infer P1}-${infer P2}`
? `${Lowercase<P1>}${Capitalize<P2>}`
: Lowercase<T>
/**
* Utility type `Replace` replace substrings within string literal unions
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#tail-recursion-elimination-on-conditional-types
*/
type Replace<T extends string, S extends string, D extends string,
A extends string = ""> = T extends `${infer L}${S}${infer R}` ?
Replace<R, S, D, `${A}${L}${D}`> : `${A}${T}`
@BPanchenko
BPanchenko / paint.smooth-corners.js
Created November 2, 2025 17:33
(CSS.paintWorklet || paintWorklet).addModule('smooth-corners.js')
registerPaint('smooth-corners', class {
paint(ctx, size) {
ctx.fillStyle = 'black'
// n=4 draw a squircle
const n = 4
let m = n
if (n > 100) m = 100
if (n < 0.00000000001) m = 0.00000000001