Skip to content

Instantly share code, notes, and snippets.

View ChristianOellers's full-sized avatar
🇨🇾

Christian Oellers ChristianOellers

🇨🇾
View GitHub Profile
@ChristianOellers
ChristianOellers / Format-Number-Locale.ts
Created June 11, 2023 14:36
TypeScript snippets - Locale + Languages. General purpose use cases.
export const formatNumber = (language: string, value: number | bigint): string
=> new Intl.NumberFormat(language).format(value)
@ChristianOellers
ChristianOellers / Currency-Locale.js
Last active June 11, 2023 14:49
Currency for Locale - Render-Optimized (avoid NaN). Example use: React.
/**
* Format currency for locale (which can come from anywhere).
*
* Ignore nullish values (like undefined) to avoid showing 'NaN €' as text, to keep templating simple.
* Example use case: First render occurs prior to data being available (e.g. in React).
*/
const locale = 'de-DE'
export default (val) => {
@ChristianOellers
ChristianOellers / Math-Snippets.js
Created June 11, 2023 13:00
Math utilities + snippets for various use cases. No actual implementation examples, as these are generic applicable.
// Get random of 3 elements
this.spriteVariant = parseInt((Math.random() * 100) % 3);
// On every reached interval (ms), do sth.
// - e.g. increase difficulty in a game
this.time++;
this.interval = 1000;
const difference = (this.time % this.interval) ? 0 : 1;
// Fade away effect over total time (max. to zero).
@ChristianOellers
ChristianOellers / React-Hook-Example.js
Last active June 11, 2023 12:54
useFetch hook - React (JS). Simple implementation example. Could be rewritten in TypeScript, but keep it simple here.
import useFetch from 'useFetch'
const url = 'https://pokeapi.co/api/v2/pokemon/pikachu'
function ExampleComponent () {
const {isLoading, data} = useFetch(url)
return (
<>
{isLoading && <p>Loading ...</p>}
@ChristianOellers
ChristianOellers / Webdriver-Selenium-Scraper.js
Last active June 11, 2023 12:52
Food scraper - Salad bar. Technology demo with Webdriver, Selenium, NodeJS.
/**
* WOYTON Salad bar - Food scraper.
*
* About
* - Tech demo of a functioning real world scraper, that actually worked and resulted in random salad orders.
* Not judging if these actually tasted good or not ;) ... As of 2023, the technology is outdated
* and you might find tools like Puppeteer easier to use, than the messy Selenium + browser stack.
*
* Note
* - Any URLs and personal data have been censored to avoid messing around with their website ;)
@ChristianOellers
ChristianOellers / UNIT-TESTING.md
Last active June 11, 2023 14:50
Unit Testing - 5 Step Method Workflow (by CodeUtopia). Real world example and demo code.

Unit Testing - Intro

TDD-focused mini tutorial for an imaginary, minimal feature described below.

Goals: Deeply think about features, expectations and limitations. Write code last.

Based on the '5 Step Method' described on CodeUtopia.

Example feature

@ChristianOellers
ChristianOellers / Random-Hex-Color-Generator.js
Created November 8, 2020 08:45
Generate a random hexadecimal color string for RGB or ARGB (with transparency).
/**
* Generate random hex color of defined length.
*
* Color function returns 1-2 values on each call.
* The result is filled up from a potentially missing value.
*
* @example
* getRandomHexColor(6); // #ffffff
* getRandomHexColor(8, ''); // ffffffff
* @param {Number} hexLength 6|8 for full RGB|ARGB range.
@ChristianOellers
ChristianOellers / Database-1-PDO-UTF8MB4.php
Last active July 3, 2023 12:29
Minimalist database wrappers for PDO and MySQLI for development purposes. PSR-12 code standards, UTF8-MB4 compatible (PDO).
<?php
declare(strict_types=1);
namespace App;
use PDO;
use function is_array;
@ChristianOellers
ChristianOellers / WordPress-DB-URL-Switch.sql
Created October 1, 2020 15:32
WordPress SQL snippet for database migration operations. Replace examples as needed.
-- CONFIG
SET @prefix = 'wp_XXXX';
-- DEV -> LIVE
SET @url_from = 'http://127.0.0.1';
SET @url_to = 'https://www.example.com';
-- LIVE -> DEV
/* * /
@ChristianOellers
ChristianOellers / WordPress-DB-Change-User-IDs.sql
Last active October 1, 2020 15:33
WordPress SQL snippet for initial setup. Used to secure admin account. Replace examples as needed.
-- Change user IDs for security.
--
-- Also change manually:
-- * AUTO_INCREMENT value of 'users' table, e.g. > 10000
-- * Fields in 'users' and 'usermeta' that disclose the login name
-- example.com
UPDATE wp_1234_users SET ID = 12345 WHERE ID = 1;
UPDATE wp_1234_usermeta SET user_id = 12345 WHERE user_id = 1;
UPDATE wp_1234_posts SET post_author = 12345 WHERE post_author = 1;