Skip to content

Instantly share code, notes, and snippets.

View kerimdzhanov's full-sized avatar

Dan Kerimdzhanov kerimdzhanov

View GitHub Profile
@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;
}
#!/bin/sh
echo Xcode installation path: `xcode-select --print-path`
sudo rm -rf `xcode-select --print-path` && xcode-select --install
@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' },
@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();
}
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
td.ellipsis {
position: relative;
white-space: initial;
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());
}
@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.

@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 / async-each-promise.js
Last active March 21, 2020 18:37
Iterate through an array asynchronously one by one using the ES6 promises
const array = []; // array to iterate
const promise = array.reduce((p, entry) => {
p.then(() => {
return new Promise((resolve, reject) => {...});
});
}, Promise.resolve());
promise
.then(() => console.log('done!'))
/**
* Sum two big numbers given as strings.
*
* @param {string} a
* @param {string} b
* @return {string}
*/
function sumStrings(a, b) {
var zrx = /^0+/; // remove leading zeros
a = a.replace(zrx, '').split('').reverse();