Skip to content

Instantly share code, notes, and snippets.

View bbars's full-sized avatar

Denis Borzenko bbars

  • St. Petersburg, Russia
View GitHub Profile
@bbars
bbars / permute.js
Created March 17, 2023 14:57
Generate all combinations of provided element set
function* permute(permutation) {
let length = permutation.length;
let c = Array(length).fill(0);
let i = 1;
let k;
let p;
yield permutation;
while (i < length) {
if (c[i] < i) {
@bbars
bbars / Enum.js
Created February 21, 2022 17:47
Base class to implement enums in JS. Name is string, enum is an instance of derived class of Enum, value is a property
class Enum {
constructor(name, value) {
if (this.constructor._nameValue.has(name)) {
throw new Error(`Duplicate name: ${name}`);
}
if (this.constructor._valueName.has(value)) {
throw new Error(`Duplicate value: ${value}`);
}
Object.defineProperties(this, {
name: {
@bbars
bbars / formatRepeatingDecimal.js
Created September 25, 2020 17:39
Format repeating decimals as 1/3 -> "0.3(3)"
function formatRepeatingDecimal(num) {
num = num.toString();
var p = num.split('.');
if (p.length === 1) {
return p[0];
}
// match parts (prefix, repeating, suffix):
var m = /^(\d*?)(\d{1,})\2{2,}(\d*?)$/.exec(p[1]);
if (!m) {
@bbars
bbars / pixelsToImage.js
Created December 5, 2019 18:35
Generate proper header and append pixel colors - and you have a valid BMP image just for free!
var bytes = [
0xff0000, 0x00ff00, 0x0000ff,
0x0000cc, 0xcc0000, 0x00cc00,
0x00ee00, 0x0000ee, 0xee0000,
];
var img = pixelsToImage(bytes);
console.log(img.src);
img.width = 90;
img.height = 90;
import (
"strings"
)
type SortedStrArray []string
func NewSortedStrArray(arr []string) (*SortedStrArray) {
res := SortedStrArray(arr)
return &res
}
Function.prototype.expectResult = async function expectResult(interval, timeout, expectValue) {
if (interval <= 0)
interval = 1000;
var fn = this;
var timeStart = Date.now();
var tries = 0;
function check() {
var value, error, asExpected;
try {
value = fn();
@bbars
bbars / spherebot_spindle_centering.js
Created January 21, 2019 17:24
Centering formula for spindle in 12-holes sphere of Spherebot
function p2d(a, r) {
return [
r * Math.cos(a),
r * Math.sin(a),
];
}
function p_distance(a0, r0, a1, r1) {
var d0 = p2d(a0, r0);
var d1 = p2d(a1, r1);
<?php // I don't know why anybody may need it
class Promise {
protected $resolvers = [];
protected $catchers = [];
protected $value = null;
protected $error = null;
protected $status = 'idle';
public function __construct($run, $manualStart = false) {
@bbars
bbars / generate-svg-avatar.js
Last active January 14, 2023 14:05
Deterministic kaleidoscope-like images generator depends on the seed argument (uses crc32 algorithm to generate seeded random sequence).
var generateSvgAvatar = (function () {
return function (seed, raw) {
var rands = hash320('' + seed);
var randsHex = rands.map(function (v) {
return ('00' + v.toString(16)).slice(-2);
}).join('');
rands = rands.concat(rands);
var globalRotation = 360 * rands[rands[0] % 20] / 0xff;
var particles = [];
var n = 20;
@bbars
bbars / object-enable-chaining.js
Last active August 15, 2018 19:35
Example: Object.enableChaining(context2d) .setFillStyle('#333') .fillRect(0, 0, 30, 30) .rect(5, 5, 20, 20) .setFillStyle('#f90') .fill()
Object.enableChaining = function (obj) {
if (!obj || typeof obj !== 'object')
throw new TypeError("Argument obj is not an Object");
var names = {};
var protos = [];
var cur = obj;
var descr;
while (cur && protos.indexOf(cur) < 0) {
descr = Object.getOwnPropertyDescriptors(cur);
for (var k in descr) {