Skip to content

Instantly share code, notes, and snippets.

View dgolosov's full-sized avatar

Dima Golosov dgolosov

  • Israel
  • 16:17 (UTC +03:00)
View GitHub Profile
@dgolosov
dgolosov / exception.js
Created February 11, 2024 16:29
Exception
class AppError extends Error {
constructor(message, data, options) {
super(message, options);
this.name = this.constructor.name;
this.data = data;
if (options && options.cause) {
const stackOffset = (this.message.match(/\n/g) || []).length + 2;
const afterCauseStack = this.stack
.split("\n")
@dgolosov
dgolosov / nginx.conf
Last active December 8, 2022 06:30
NginX JSON Errors
error_page 400 /400.json;
location /400.json {
internal;
default_type application/json;
return 400 '{ "code": 400, "message":"Bad Request" }';
}
error_page 401 /401.json;
location /401.json {
internal;
@dgolosov
dgolosov / requestAnimationFrame.ts
Last active May 10, 2022 06:05
requestAnimationFrame usage example
function animate(animation: AnimationFunction, ...args: unknown[]): Promise<void> {
let start
return new Promise<void>((resolve) => {
function step(timestamp) {
start ??= timestamp
const isRunning = animation(timestamp - start, ...args)
if (isRunning) {
window.requestAnimationFrame(step)
} else {
@dgolosov
dgolosov / arrays.js
Last active February 20, 2022 11:53
Working with arrays in JS
// The most perfomance way to populate a new array
[...Array(1000)].map((_, i) => i); // [0,...,999]
// Grouping array elements by condition
function groupBy(list, groupingCondition) {
const groups = [];
let currentGroup = [];
for (const item of list) {
if (currentGroup.length) {