Skip to content

Instantly share code, notes, and snippets.

:octocat:
(• ◡•)

Loris Bettazza Pustur

View GitHub Profile
@Pustur
Pustur / trueModulo.js
Created Dec 28, 2018
JavaScript – Modulo function that works for negative numbers
View trueModulo.js
const mod = (n, m) => ((n % m) + m) % m;
@Pustur
Pustur / execAll.js
Created Dec 26, 2018
JavaScript – exec helper function to get all regex matches in one function call
View execAll.js
const execAll = (regex, str, aggregator = []) => {
while ((match = regex.exec(str)) !== null) aggregator.push(match);
return aggregator;
};
@Pustur
Pustur / getElementOfReference.js
Created Sep 12, 2018
JavaScript – Given a DOM element, returns the closest parent that has position !== 'static'. Useful to quickly know what's the reference of your `position: absolute` element
View getElementOfReference.js
function getElementOfReference({ parentElement }) {
if (parentElement === null) {
return document.documentElement;
}
if (getComputedStyle(parentElement).position !== 'static') {
return parentElement;
}
return getElementOfReference(parentElement);
View README.md

Google Fonts downloader

Description

A simple PHP script that lets you download Google Fonts from the command line.
Made mainly to learn more about PHP, it's integration with the command line and how to extract contents from a webpage.
This is not meant to be anything serious, in fact, if you want to download google fonts, there is a better way.

Requirements

Usage

@Pustur
Pustur / icon-list.scss
Created Oct 19, 2016
SCSS – Convenient structure for social icons (lists and loops)
View icon-list.scss
$socials: (
facebook: (13px, 24px),
twitter: (25px, 21px),
youtube: (23px, 26px),
instagram: (22px, 23px)
);
a {
@each $social, $size in $socials {
&[href*='#{$social}'] {
@Pustur
Pustur / unique-dom-elements.js
Last active Apr 3, 2019
JavaScript – Return an object of all the html tag names and the number of times they appear in the document
View unique-dom-elements.js
const uniqueDomElements = () =>
Array.from(document.querySelectorAll('*')).reduce(
(obj, { tagName }) => {
obj[tagName] = obj[tagName] + 1 || 1;
obj._total += 1;
return obj;
},
{ _total: 0 }
);
@Pustur
Pustur / daily-ui.txt
Created Jun 19, 2016
DailyUI – A list of design challenges
View daily-ui.txt
Sign Up
Credit Card Checkout
Landing Page (above the fold)
Calculator
App Icon
User Profile
Settings
404 page
Music Player
Social Share
@Pustur
Pustur / datetime-to-array.js
Last active Jun 19, 2016
JavaScript – Convert datetime into an array of integers. Useful when used to create a new Date()
View datetime-to-array.js
function datetime2array(datetime) {
datetime = datetime.split(/[- :]/g);
var len = datetime.length;
for (var i = 0; i < len; i++) {
datetime[i] = parseInt(datetime[i]);
}
return (len === 3 ? datetime.concat([0, 0, 0]) : datetime);
}
@Pustur
Pustur / sort-by-key.js
Last active Apr 3, 2019
JavaScript – Sort an array on key value
View sort-by-key.js
const sortByKey = (arr, key, dir = 'asc') =>
arr.sort((a, b) => {
const x = a[key];
const y = b[key];
return (dir === 'asc' ? -1 : 1) * (x < y ? 1 : x > y ? -1 : 0);
});
@Pustur
Pustur / timestamp-to-string.js
Last active Jun 22, 2016
JavaScript – Convert a timestamp to a string "X days X hours X minutes X seconds X milliseconds". Useful to show elapsed time since [event]
View timestamp-to-string.js
function timestamp2string(timestamp, useMilliseconds) {
var string = '',
periods = {
day: 86400000,
hour: 3600000,
minute: 60000,
second: 1000,
millisecond: 1
};
You can’t perform that action at this time.