Skip to content

Instantly share code, notes, and snippets.

@aminnairi
aminnairi / asynchronous-recursive-generator.js
Created December 17, 2021 14:21
Asynchronous Recursive Generator
// Générateur asynchrone récursif
async function* webservice(entity, index = 1) {
const response = await fetch(`https://jsonplaceholder.typicode.com/${entity}/${index}`);
if (response.ok) {
yield response.json();
// Yield Star (yield*)
// Permet de renvoyer la valeur résolue d'un générateur dans un générateur
// Équivalent d'applatir une imbrication de génerateur les uns dans les autres
@aminnairi
aminnairi / asynchronous-generator.js
Last active December 17, 2021 14:16
Asynchronous Generator example using the JSONPlaceholder API
// Générateur asynchrone
async function* webservice(entity) {
let index = 1;
while (true) {
try {
const response = await fetch(`https://jsonplaceholder.typicode.com/${entity}/${index}`);
if (!response.ok) {
throw new Error("Not found");
@aminnairi
aminnairi / index.js
Created December 16, 2021 10:43
Vibration API
// Si le navigateur n'a pas implémenté correctment l'API de Vibration
if (!window.navigator.vibrate || typeof window.navigator.vibrate !== "function") {
const message = "Vibration not supported by this device, sorry!";
alert(message);
throw new Error(message);
}
// Récupération d'un bouton pour pouvoir faire vibrer l'appareil
const vibrateButton = window.document.getElementById("vibrate");
@aminnairi
aminnairi / pip.fish
Created February 28, 2021 12:53
pip.fish
#!/usr/bin/env fish
# File: $HOME/.config/fish/functions/pip.fish
# Author: Amin NAIRI <https://github.com/aminnairi>
# Usage: pip DOCKER_ARGUMENTS -- NODE_ARGUMENTS
# Example: pip -- install package-name
# Example: pip --publish 8080:8080 -- install package-name
function pip
# A local array of Node.js arguments to proxy
set -l pip_arguments
@aminnairi
aminnairi / python.fish
Last active March 1, 2021 17:36
python.fish
#!/usr/bin/env fish
# File: $HOME/.config/fish/functions/python.fish
# Author: Amin NAIRI <https://github.com/aminnairi>
# Usage: python DOCKER_ARGUMENTS -- ENTRYPOINT_ARGUMENTS
# Example: python -- --version
# Example: python --tag 8.0.0 -- --version
# Example: python -- script.mjs
# Example: python --tag 8.0.0 -- script.mjs
# Example: python -- --experimental-json-modules module.mjs
@aminnairi
aminnairi / challenge.py
Created January 12, 2021 22:07
Challenge: write a function difference that compute the difference between
def difference(first, second):
intersection = []
for item in first:
if not item in second:
intersection.append(item)
for item in second:
if not item in first:
intersection.append(item)
const asciiTable = text => {
const rows = text.split("\n");
const nonEmptyRows = rows.filter(row => 0 !== row.trim().length);
const normalizedRows = nonEmptyRows.map(row => row.trim());
const rowsColumns = normalizedRows.map(row => row.split("|"));
const initialPadding = [];
const padding = rowsColumns.reduce((pad, row) => row.map((column, index) => {
if ("undefined" === typeof pad[index]) {
return column.length;
@aminnairi
aminnairi / deploy.sh
Last active September 8, 2020 14:28
GitHub Pages deploy script for Docker Compose project using Yarn
#!/bin/sh
# sh deploy.sh
docker-compose exec node yarn build
git add --force dist
git commit -m ":package: new build"
git subtree split --prefix dist -b gh-pages
git push --force origin gh-pages:gh-pages
git reset --hard HEAD^
@aminnairi
aminnairi / docker-compose.yaml
Last active May 26, 2020 12:34
Docker Compose settings for Elastic Search + Kibana
version: "3"
# docker-compose up --detach
# docker-compose down --remove-orphans --volumes --timeout 0
services:
elasticsearch:
restart: always
image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
environment:
- "discovery.type=single-node"
@aminnairi
aminnairi / capitalize.js
Created June 9, 2019 10:15
Capitalize files in a directory recursively
const { readdirSync, statSync, renameSync } = require('fs');
function capitalize(input) {
return input.slice(0, 1).toUpperCase() + input.slice(1);
}
function recursivelyCapitalizeFilesAndDirectories(path) {
for (const file of readdirSync(path)) {
const capitalizedFile = capitalize(file);