Skip to content

Instantly share code, notes, and snippets.

View kerimdzhanov's full-sized avatar

Dan Kerimdzhanov kerimdzhanov

View GitHub Profile
@kerimdzhanov
kerimdzhanov / custom-error.js
Last active March 21, 2020 18:37 — forked from justmoon/custom-error.js
Creating custom Error classes in Node.js
'use strict';
module.exports = function CustomError(message, extra) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.extra = extra;
};
require('util').inherits(module.exports, Error);
@kerimdzhanov
kerimdzhanov / README.md
Created April 1, 2020 00:29
JavaScript Throttling vs Debouncing

Throttling vs Debouncing

Throttling is a straightforward reduction of the trigger rate. It will cause the event listener to ignore some portion of the events while still firing the listeners at a constant (but reduced) rate.

Unlike throttling, debouncing is a technique of keeping the trigger rate at exactly 0 until a period of calm and then triggering the listener exactly once.

In other words

With Throttling the original function is called at most once per specified period.

export class CancellableRequest {
private controller = new AbortController();
public fetch(input: RequestInfo, init: RequestInit = {}): Promise<Response> {
init.signal = this.controller.signal;
return fetch(input, init)
.then(res => res.json());
}
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
td.ellipsis {
position: relative;
white-space: initial;
@kerimdzhanov
kerimdzhanov / polling-callback.js
Last active October 16, 2020 15:46
Javascript polling functions
// The polling function
function poll(fn, callback, timeout, interval) {
var endTime = Number(new Date()) + (timeout || 2000);
interval = interval || 100;
(function p() {
// If the condition is met, we're done!
if (fn()) {
callback();
}
@kerimdzhanov
kerimdzhanov / conventional-changelog-config.js
Last active July 23, 2021 13:06
Conventional changelog config example
'use strict';
const config = require('conventional-changelog-conventionalcommits');
module.exports = config({
types: [
{ type: 'feat', section: 'Features' },
{ type: 'feature', section: 'Features' },
{ type: 'fix', section: 'Bug Fixes' },
{ type: 'perf', section: 'Performance Improvements' },
#!/bin/sh
echo Xcode installation path: `xcode-select --print-path`
sudo rm -rf `xcode-select --print-path` && xcode-select --install
@kerimdzhanov
kerimdzhanov / random.js
Last active April 20, 2024 03:21
JavaScript: get a random number from a specific range
/**
* Get a random floating point number between `min` and `max`.
*
* @param {number} min - min number
* @param {number} max - max number
* @return {number} a random floating point number
*/
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}