Skip to content

Instantly share code, notes, and snippets.

View ClementParis016's full-sized avatar
🐿️
Stretching some <div>s to make them flex

Clément Paris ClementParis016

🐿️
Stretching some <div>s to make them flex
View GitHub Profile
@ClementParis016
ClementParis016 / mapObject.js
Last active February 1, 2017 15:00
Map an object with Object.keys and Array.prototype.reduce
const weekDays = {
monday: "Monday",
tuesday: "Tuesday",
wednesday: "Wednesday",
thursday: "Thursday",
friday: "Friday"
};
const mappedWeekDays = Object.keys(weekDays).reduce((mapper, day) => {
mapper[day] = {
@ClementParis016
ClementParis016 / fault-tolerant-promise-all.js
Last active February 21, 2022 05:23
Fault-tolerant Promise.all
const p1 = new Promise(resolve => resolve({ data: 1, status: 'OK' }));
const p2 = new Promise((resolve, reject) => reject({ data: 2, status: 'OK' }));
const p3 = new Promise(resolve => resolve({ data: 3, status: 'OK' }));
const promisesCatcher = promises => promises.map(promise => promise.catch(err => ({ data: err, status: 'ERROR' })));
Promise.all(
promisesCatcher([p1, p2, p3])
).then(values => {
for (let result of values) {
@ClementParis016
ClementParis016 / base64tofile.js
Created September 30, 2017 16:37
Convert a base64 image to a file with Node.js
const fs = require('fs');
const rawBase64 = ''; // typically 'data:image/png;base64,.......'
const [, ext, data] = rawBase64.match(/^data:image\/([a-z]+);base64,(.*)$/);
fs.writeFileSync(`image.${ext}`, data, 'base64');
@ClementParis016
ClementParis016 / rewrite-gmail-img-src.js
Last active November 29, 2018 15:27
Use original images URLs in Gmail
const REGEX = /https:\/\/.*\.googleusercontent\.com\/.*#(https?:\/\/.*)/i;
// Rewrite img source on <img> tags
Array.from(
document.querySelectorAll('img')
).forEach(img => {
const match = img.src.match(REGEX);
if (!match) {
return;
@ClementParis016
ClementParis016 / pdf-viewer.jsx
Created November 29, 2018 14:55
Render raw binary PDF data in a PDF viewer
// Originally tweeted at https://twitter.com/ClementParis016/status/1065000952749539329
// Fetch raw binary PDF data
const response = await fetch('https://some.pdf');
// Extract response body as Blob
const blob = await response.blob();
// Create an URL pointing to the object
const url = URL.createObjectURL(blob);
// Render a PDF viewer!
@ClementParis016
ClementParis016 / getObjectKeys.js
Last active December 17, 2018 16:13
Get deep JavaScript Object keys with dot notation
// It may have a lot of possible use case but the one I did it for initially was
// to find which keys were missing between two JSON translations files
function getKeys(obj) {
const keys = [];
const walk = (o, parent = null) => {
for (const k in o) {
const current = parent ? parent + '.' + k : k;
keys.push(current);
@ClementParis016
ClementParis016 / swap.js
Created February 1, 2019 10:32
Lodash's missing swap util to replace an element from a collection
import { map, iteratee } from 'lodash';
/**
* Replaces elements from collection that matches predicate with replacement.
*
* @param {Array} collection The collection to replace in
* @param {*} predicate The condition to determine which elements of collection
* should be replaced
* @param {*} replacement The value to use as a replacement of elements of
* collection that matches the predicate
@ClementParis016
ClementParis016 / index.js
Last active May 5, 2022 09:34
Extract commits from GitHub org to CSV
const { Octokit } = require("@octokit/rest");
const fastCsv = require("fast-csv");
const fs = require("fs");
const consola = require("consola");
process.on("unhandledRejection", (error, promise) => {
consola.fatal(`Unhandled rejection! ${error.name}: ${error.message}`, {
error,
promise,
});