Skip to content

Instantly share code, notes, and snippets.

View tobiasroeder's full-sized avatar
:octocat:
I <3 </>

Tobias Röder tobiasroeder

:octocat:
I <3 </>
View GitHub Profile
@tobiasroeder
tobiasroeder / readJSON.js
Created June 5, 2019 14:36
Pur JavaScript to read a JSON file for the YouTube, GitHub or other API
fetch(YOUR_JSON_FILE_URL).then(function(responsive) {
return responsive.json();
}).then(function(obj) {
console.log(obj);
});
@tobiasroeder
tobiasroeder / data.css
Created June 22, 2019 08:22
CSS before/after content property with the content from an attribute.
[data-before]::before {
content: attr(data-before);
}
[data-after]::after {
content: attr(data-after);
}
@tobiasroeder
tobiasroeder / splitter.js
Created July 13, 2019 14:22
A function which splitter a number.
// 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;
}
@tobiasroeder
tobiasroeder / loadJSON.js
Created July 13, 2019 14:23
A function which load the content of a json file.
// 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 / fade.js
Last active October 10, 2019 20:27
Simple fade in/out function without any library. (pur JavaScript)
// 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 / hasCssProperty.js
Created May 15, 2021 13:35
Check if an element contains a specific css property.
// 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 / vigenere.js
Created September 24, 2021 09:15
Vigenère-Chiffre
/*
* 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 / chr.php
Created October 18, 2021 14:02
0-9 A-Z a-z
<?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;
@tobiasroeder
tobiasroeder / hashToObject.js
Last active December 4, 2021 08:29
Convert a string (location.hash) into an object.
/**
* 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 => {
@tobiasroeder
tobiasroeder / get1stAdvent.php
Created January 14, 2022 13:28
Get the date for the first advent.
<?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));