Skip to content

Instantly share code, notes, and snippets.

View aminnairi's full-sized avatar
😁

Amin NAIRI aminnairi

😁
View GitHub Profile
@aminnairi
aminnairi / functions-exercise.php
Last active January 10, 2022 10:59
Functions exercises in PHP
<?php
$cart = [
["product" => "paper", "quantity" => 7, "price" => 1.26],
["product" => "pen", "quantity" => 2, "price" => 0.12],
["product" => "eraser", "quantity" => 5, "price" => 0.89]
];
$stocks = [
["product" => "paper", "quantity" => 88],
@aminnairi
aminnairi / asynchronous-recursive-generator.js
Last active December 17, 2021 15:15
Asynchronous Recursive Generator
async function* webservice(entity, index = 1) {
const response = await fetch(`https://jsonplaceholder.typicode.com/${entity}/${index}`);
if (response.ok) {
yield response.json();
yield* webservice(entity, index + 1);
}
}
for await (const user of webservice("users")) {
@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^