Skip to content

Instantly share code, notes, and snippets.

View MD4's full-sized avatar
🔥
🔥🔥🔥

Martin DEQUATREMARE MD4

🔥
🔥🔥🔥
View GitHub Profile
@MD4
MD4 / promiseRetry.ts
Created November 14, 2019 15:20
Typescript promise retry
function retry<T>(promise: () => Promise<T>, maxTries = 3): Promise<T> {
return promise().catch(() =>
maxTries > 1 ? retry(promise, maxTries - 1) : promise()
);
}
const countSelectedIssuePoints = () => $('.js-issue.ghx-selected .aui-badge.ghx-statistic-badge')
.map((_, b) => +$(b).text())
.get()
.reduce(
(a,b) => a + (b || 0),
0,
);
countSelectedIssuePoints(); // 3
const preFetchImageSize = (url, checkInterval = 10) => new Promise((resolve, reject) => {
const image = new Image();
image.src = url;
image.onerror = reject;
const interval = setInterval(() => {
if (image.width + image.naturalWidth) {
clearInterval(interval);
resolve({
const formatToDuration = milliseconds => {
const seconds = Math.round(milliseconds / 1000);
const s = Math.round(seconds % 60);
const m = (Math.floor(seconds / 60) % 60);
const h = (Math.floor(seconds / 60 / 60) % 24);
const d = (Math.floor(seconds / 60 / 60 / 24));
return [
(d ? `${d}d` : ''),
(h ? `${h}h` : ''),
@MD4
MD4 / parseHex.js
Created January 29, 2018 10:00
Hex string to ascii, feat @PaGury
const parseHex = hexString => hexString
.match(/.{1,2}/g)
.map(hex => String.fromCharCode(parseInt(hex, 16)))
.join('');
@MD4
MD4 / observe.on.js
Last active June 14, 2017 13:52
Refacto tool which highlight every use of a variable on an object.
/**
* Example:
* observe('myProp').on(myObject);
*/
global.observe = (varName) => ({
on: (object) => {
let value = object[varName];
Object.defineProperty(
object,
@MD4
MD4 / smoothTop.js
Last active April 27, 2017 11:53
Really simple smooth scroll to top
const smoothScrollToTop = (element = document.body, interpolationFactor = 1.2) =>
Math.round(element.scrollTop /= interpolationFactor) ?
setTimeout(smoothScrollToTop, 16.666, element) && true : false;
@MD4
MD4 / :monkey: jojoShuffle.js
Last active January 2, 2017 13:29
Adds a shuffle method in array prototype.
Array.prototype.shuffle = function() {
return this
.reduce(
([result, rest]) => {
const random = (Math.random() * rest.length) >> 0;
return [
result
.concat(rest[random]),
rest
.slice(0, random)
@MD4
MD4 / jojoPromiseSeriesV2.js
Last active July 20, 2016 13:23
🐒 Better promise.series simple implementation ! It now gives the result array.
'use strict';
Promise.series = promises =>
((results = []) =>
promises
.reduce(
(promise, current) => promise
.then(current)
.then(function () {
results.push(arguments);
@MD4
MD4 / wni-es7.js
Created July 20, 2016 09:31
🆕 What's new in ES7 ?
var todo = {a: [1, 2, 3]};
console.log((todo === { ...todo, a: [...todo.a] })); // false
console.log((todo.a === { ...todo, a: [...todo.a] }.a)); // false
console.log(todo, { ...todo, a: [...todo.a] }) // {"a":[1,2,3]} {"a":[1,2,3]}
console.log([1,2,3,4].includes(1)) // true
console.log([1,2,3,4].includes(5)) // false
console.log(2 ** 8) // 256