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 September 21, 2024 11:44
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;
}
@kerimdzhanov
kerimdzhanov / mysql_data_types_size.md
Last active December 28, 2015 21:49
MySQL data types size reference tables

Textual types

      Type | Maximum length | Size (bytes)
-----------+----------------+------------------
  TINYTEXT |            255 | (2^8−1)
      TEXT |         65,535 | (2^16−1) = 64 KiB
MEDIUMTEXT |     16,777,215 | (2^24−1) = 16 MiB
  LONGTEXT |  4,294,967,295 | (2^32−1) =  4 GiB
@kerimdzhanov
kerimdzhanov / raf.js
Last active December 31, 2015 00:29 — forked from paulirish/rAF.js
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
@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();
}
/**
* 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();
@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 / chaijs-cheatsheet.js
Last active July 6, 2016 23:52
Chai.js expectations cheatsheet
// instead of using `expect(object).to.exist`, assert things
// through `.to.be.a(type)` which make failures more informative
expect(result).to.be.an('object')
.that.includes({
foo: 'bar',
baz: 'quux'
});
// to assert property value types i.e. date:
expect(result)
@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!'))
@kerimdzhanov
kerimdzhanov / mocha.conf.js
Last active August 3, 2018 08:21
Setup mocha/chai.js/sinon stack w/ es6 features
const chai = require('chai');
// globalize sinon
global.sinon = require('sinon');
// initialize chai plugins
chai.use(require('sinon-chai'));
chai.use(require('chai-as-promised'));
chai.use(require('chai-datetime'));
@kerimdzhanov
kerimdzhanov / next-second.js
Last active October 16, 2018 14:26
Executes a given `fn` at the beginning of the next second
/**
* Executes a given `fn` at the beginning of the next second.
* @param {function} fn – a function to execute
* @return {number} setTimeout's resulting timeout id, to give ability to cancel execution
*/
function nextSecond(fn) {
const time = (new Date()).getTime();
return setTimeout(fn, (Math.ceil(time / 1000) * 1000) - time);
}