Skip to content

Instantly share code, notes, and snippets.

View kubarskii's full-sized avatar
☀️
Working from home

Alexander Kubarskii kubarskii

☀️
Working from home
  • EPAM Systems
  • Russia, Saint-Petersburg/Turkey, Antalya
View GitHub Profile
@kubarskii
kubarskii / test-lib.js
Last active March 24, 2021 22:03
A try to create some kind of small testing lib that compares expected result and actual (made in educational purposes). Currently tests works properly only with sync functions.
function testExecutor(value) {
const A = function (x) {
this.value = x;
this.success = false;
};
A.prototype.toBe = function (x) {
if (JSON.stringify(x) === JSON.stringify(this.value) && typeof this.value === typeof x) {
this.success = true;
console.log(`%cPassed: ${JSON.stringify(x)} equals ${JSON.stringify(this.value)}`, 'color: green;');
const memo = (function () {
const cache = new Map()
return function (fn, ctx = null, ...params) {
if (cache.has([fn, params, ctx].toString())) {
return cache.get([fn, params, ctx].toString())
} else {
const res = fn.call(ctx, ...params);
cache.set([fn, params, ctx].toString(), res);
return res;
}
@kubarskii
kubarskii / trampoline.js
Created May 13, 2021 22:05
Trampoline example
function sum(n, s = 0) {
return (n) ? () => sum(n - 1, s + n) : s
}
function trampoline(fn) {
return function (...args) {
let res = fn.apply(null, args)
while (typeof res === 'function') {
res = res()
}
@kubarskii
kubarskii / redux-architecture.js
Last active February 12, 2024 20:34
redux architecture implementation example
class Observer {
#initialValue;
#subscriptions = [];
constructor(initialValue) {
this.#initialValue = initialValue;
}
subscribe(fn) {
this.#subscriptions.push(fn);
@kubarskii
kubarskii / opaque.ts
Created November 10, 2021 14:50
Opaque type using typescript
declare namespace Tag {
const OpaqueTagSymbol: unique symbol;
class OpaqueTag<S extends symbol> {
private [OpaqueTagSymbol]: S;
}
export type OpaqueType<T, S extends symbol> = T & OpaqueTag<S>;
}
export declare type Opaque<T, S extends symbol> = Tag.OpaqueType<T, S>;
import React, {PureComponent} from 'react'
class Boundary extends PureComponent<any, any> {
static getDerivedStateFromError(error: any) {
return {hasError: true, error: error.toString()}
}
state = {
hasError: false,
error: '',
@kubarskii
kubarskii / video.html
Last active September 29, 2023 00:41
Render video as ascii
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>