Skip to content

Instantly share code, notes, and snippets.

View kerimdzhanov's full-sized avatar

Dan Kerimdzhanov kerimdzhanov

View GitHub Profile
@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 / 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 / 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);
/**
* 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 / 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 / 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 / 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 / random.js
Last active November 12, 2023 15:11
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;
}