Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
I <3 </>

Tobias Röder tobiasroeder

:octocat:
I <3 </>
View GitHub Profile
@tobiasroeder
tobiasroeder / get1stAdvent.php
Created Jan 14, 2022
Get the date for the first advent.
View get1stAdvent.php
<?php
/**
* @param string $date - [YYYY-MM-DD]
* @return int
*/
function getWeekday( string $date ):int {
// 'w' 0 (for Sunday) through 6 (for Saturday)
$result = (int) date('w', strtotime($date));
@tobiasroeder
tobiasroeder / hashToObject.js
Last active Dec 4, 2021
Convert a string (location.hash) into an object.
View hashToObject.js
/**
* convert string (hash) to object
* @param {string} hash
* @returns {object}
*/
function hashToObject(hash) {
let obj = {};
hash = hash.slice(1);
let params = hash.split(',');
params.forEach(param => {
View chr.php
<?php
$chars = '';
// 65 - 122 (A-Za-z)
// 48 - 122 (0-9A-Za-z)
for ($i = 48; $i <= 122; $i++) {
// skip 58 - 64 and 91 - 96
if ($i >= 58 && $i <= 64 || $i >= 91 && $i <= 96)
continue;
View vigenere.js
/*
* source: https://rosettacode.org/wiki/Vigen%C3%A8re_cipher#JavaScript
*/
// vigenere
function vigenere(text, key, decode) {
// helper
const ordA = a => a.charCodeAt(0) - 65;
// main
let i = 0, b;
@tobiasroeder
tobiasroeder / .htaccess
Created Sep 18, 2021
Very simple PHP Router
View .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
@tobiasroeder
tobiasroeder / hasCssProperty.js
Created May 15, 2021
Check if an element contains a specific css property.
View hasCssProperty.js
// returns a boolean if the element contains that property
const hasCssProperty = (elmt, property) => {
const elmtPropertyValue = window.getComputedStyle(elmt, null).getPropertyValue(property);
return elmtPropertyValue ? true : false;
}
// live example
const elmt = document.querySelector('#video');
if (hasCssProperty(elmt, 'aspect-ratio')) {
@tobiasroeder
tobiasroeder / mixArray.js
Created Jun 20, 2020
A way to mix an array in JavaScript.
View mixArray.js
// function
function mixArray(array) {
let newArray = [],
maxRandNbr = array.length;
for (let i = 0; i = maxRandNbr; i++) {
randNbr = Math.round(Math.random() * (maxRandNbr - 1));
let index = array.indexOf(array[randNbr]);
newArray.push(array[randNbr]);
@tobiasroeder
tobiasroeder / fade.js
Last active Oct 10, 2019
Simple fade in/out function without any library. (pur JavaScript)
View fade.js
// source: https://chrisbuttery.com/articles/fade-in-fade-out-with-javascript/
// fadeOut (fade.js)
function fadeOut(el) {
el.style.opacity = 1;
(function fade() {
var val = parseFloat(el.style.opacity);
if (!((val -= .1) < 0)) {
el.style.opacity = val;
requestAnimationFrame(fade);
@tobiasroeder
tobiasroeder / loadJSON.js
Created Jul 13, 2019
A function which load the content of a json file.
View loadJSON.js
// load JSON
function loadJSON(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
} else {
// callback([{}]);
@tobiasroeder
tobiasroeder / splitter.js
Created Jul 13, 2019
A function which splitter a number.
View splitter.js
// 1234 => 1.234
function splitter(t) {
if (t = '' + t,
t.length > 3) {
var e = t.length % 3,
n = e > 0 ? t.substring(0, e) : '';
for (i = 0; i < Math.floor(t.length / 3); i++) n += 0 == e && 0 == i ? t.substring(e + 3 * i, e + 3 * i + 3) : '.' + t.substring(e + 3 * i, e + 3 * i + 3);
return n;
}