Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
luislobo14rap / is.js (only for detect chrome in mac and ios).js
Created September 15, 2020 15:25
is.js (only for detect chrome in mac and ios).js
/*!
* is.js 0.9.0
* Author: Aras Atasaygin. Adapted by Luis Lobo
*/
// define 'is' object and current version
let is = {};
is.VERSION = '0.9.0';
// store navigator properties to use later
@luislobo14rap
luislobo14rap / math-utils-beta.js
Last active January 29, 2024 14:26
math-utils.js
"Se A é o total, e tenho B, quantos correspondem a 50?"
"Se 'a' é um valor e 'b' é o total, qual a porcentagem que 'a' representa de 'b'?"
// CANSEI CONTINUAR DEPOIS
// math-utils.js v1
Math.avg = (...numbers) => Math.sum(...numbers) / numbers.length
Math.distance = (a, b) => Math.abs(a - b)
Math.isApproximate = (a, b, tolerance = 0) => Math.abs(a - b) <= tolerance
Math.isBetween = (number, min, max) => number >= min && number <= max
Math.isEven = number => number % 2 === 0
@luislobo14rap
luislobo14rap / create-style.js
Last active January 18, 2024 21:42
create-style.js
// create-style.js v1
function createStyle(cssString) {
const head = document.querySelector("head"),
style = document.createElement("style")
style.setAttribute("type", "text/css")
style.append(document.createTextNode(cssString))
head.append(style)
}
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes, shrink-to-fit=no" />
<title></title>
<!-- LIBS -->
@luislobo14rap
luislobo14rap / odd-or-even.js
Last active December 16, 2023 08:58
odd-or-even.js
// odd-or-even.js v1
function oddOrEven(number_){ // 0 = even, 1 = odd
return number_ % 2 === 0 ? 0 : 1;
};
// is-odd.js v1
function isOdd(number_){
return number_ % 2 !== 0;
};
// is-even.js v1
function isEven(number_){
@luislobo14rap
luislobo14rap / plyr-in-chrome.js
Last active October 31, 2023 09:18
plyr-in-chrome.js
// plyr-in-chrome.js v1.4
if(isLocalFile() | isLocalIp()){
const $ = (selector, scope = document) => {
return scope.querySelector(selector);
};
const createElement = (element) => {
return document.createElement(element);
};
@luislobo14rap
luislobo14rap / random-between.js
Last active August 25, 2023 00:50
random-between.js
// random-between.js v1.1
function randomBetween(min, max) {
const range = max + 1 - min
return min + Math.floor(Math.random() * range)
}
// random-between.js v2
function randomBetween(min, max) {
const range = max + 1 - min,
randomValue = (window.crypto || window.msCrypto).getRandomValues(new Uint32Array(1))[0]
@luislobo14rap
luislobo14rap / change-url-without-reload.js
Last active May 12, 2023 20:13
change-url-without-reload.js
// change-url-without-reload.js v1
function changeURLWithoutReload(newUrl){
let url = getURL();
return window.history.pushState(undefined, undefined, url + '/' + newUrl.replace(/^\//, ''));
};
// getURL.js v1
function getURL(url = window.location.href){
return url.split(/[?#&]/)[0].replace(/\/+$/, '');
};
@luislobo14rap
luislobo14rap / setTimesout.js
Last active May 11, 2023 12:12
setTimesout.js
// setTimesout.js v2
function setTimesout(function_ = (time, index) => {}, repeats = [0]) {
repeats = repeats.sort((a, b) => {
return a - b
})
for (let i = 0; i < repeats.length; i++) {
setTimeout(() => {
function_(time = repeats[i], index = i)
}, repeats[i])
}
@luislobo14rap
luislobo14rap / flex-center.css
Last active May 6, 2023 03:17
flex-container.css
/* flex-center.css v1 */
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}