Skip to content

Instantly share code, notes, and snippets.

View AitorAlejandro's full-sized avatar

Aitor Alejandro Herrera AitorAlejandro

View GitHub Profile
* {
box-sizing: border-box;
}
.ldPmWA {
position: relative;
margin: auto;
width: 370px;
border-radius: 30px;
box-shadow: rgb(85 85 85) 0px 0px 2.4em inset;
@AitorAlejandro
AitorAlejandro / axiosApi.ts
Last active June 23, 2021 06:29 — forked from Mr-Malomz/api.ts
Axios API Request with TypeScript
import axios, { AxiosResponse } from 'axios';
import { PostType } from '../models/post.interface';
const instance = axios.create({
baseURL: 'http://jsonplaceholder.typicode.com/',
timeout: 15000,
});
const responseBody = (response: AxiosResponse) => response.data;
@AitorAlejandro
AitorAlejandro / orderListByBooleanProperty.js
Created June 22, 2021 12:20
Order list by boolean property and in a immutable way
export function orderListByBooleanProperty({ list = [], propertyName = '', asc = true }) {
const immutableList = [...list];
const sortedList = [...immutableList].sort(function (a, b) {
return asc ? a[propertyName] - b[propertyName] : b[propertyName] - a[propertyName];
});
return sortedList;
}
@AitorAlejandro
AitorAlejandro / pagination.js
Created April 16, 2021 15:11
A way of creating a frontend pagination
// here is an array of 120 items
const items = Array.from({length: 120}, (_, index) => index);
console.log("items ->", items);
function createPagination(items, itemsPerPage) {
const numberOfPages = Math.ceil(items.length / itemsPerPage);
return Array.from({length: numberOfPages}, (_, index) => {
const startIndex = index * itemsPerPage;
return items.slice(startIndex, startIndex + itemsPerPage);
@AitorAlejandro
AitorAlejandro / timestamp.js
Created December 11, 2020 13:13
javascript configurable timestamp
function timestamp(
{
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep','Oct', 'Nov', 'Dec'],
timePad = 2} = {}
) {
const pad = (n) => n.toString().padStart(timePad, '0');
const d = new Date();
const time = [pad(d.getHours()),
pad(d.getMinutes()),
@AitorAlejandro
AitorAlejandro / compose.js
Created November 26, 2020 17:50
Composition Example
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x)
// example
const upperFirst = (srt) => srt.charAt(0).toUpperCase() + srt.slice(1);
const users = [
{ id: 1, name: 'aitor', lastName: 'alejandro' }
];
const first = x => x[0];
const indexBy = (key, xs) => xs.reduce(acc, el) => ({
...acc,
[el[key]]: el,
});
/*
example:
// const users = [
{id: 'id1', name: 'Aritz'},
{id: 'id2', name: 'Asier'},
@AitorAlejandro
AitorAlejandro / uid.js
Created November 24, 2020 07:54
Create a unique ID
const uid = () => performance.now().toString(36) + Math.random().toString(36).substr(2);
@AitorAlejandro
AitorAlejandro / isBrowserTabFocused.js
Created November 15, 2020 13:35
How to find out if the browser tab of the page is focused
const isBrowserTabFocused = () => !document.hidden;
// Example
isBrowserTabFocused(); // true
@AitorAlejandro
AitorAlejandro / copyToClipboard.js
Created November 15, 2020 13:35
How to copy a string to the clipboard
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();