Skip to content

Instantly share code, notes, and snippets.

View amadeuszblanik's full-sized avatar
👨‍💻
Working remotely

Amadeus Blanik amadeuszblanik

👨‍💻
Working remotely
View GitHub Profile
@amadeuszblanik
amadeuszblanik / index.js
Last active November 4, 2019 09:24
Vieport vh trick
// First we get the viewport height and we multiple it by 1% to get a value for a vh unit
const vh = window.innerHeight * 0.01;
// Then we set the value in the --vh custom property to the root of the document
document.documentElement.style.setProperty('--vh', `${vh}px`);
@amadeuszblanik
amadeuszblanik / mobileDetect.js
Last active June 5, 2019 13:24
Simple function to detect mobile `window.mobile`
export default class MobileDetect {
constructor() {
this.mount();
}
mount() {
if (typeof window === "object") {
if (window.mobile !== "undefined") {
window.mobile = function () {
let check = false;
@amadeuszblanik
amadeuszblanik / tdd_nodeelements.js
Last active July 28, 2019 00:54
Test if provided element is DOM element
const isDOMelement = (element, nodeListAllowed = true) => {
if (typeof element !== "object" || element === null) {
console.debug("Element is not an object.\nIt's fine you are in debug mode.");
return false;
} else {
if (!element instanceof HTMLElement) {
console.warn("element is not an instanceof HTMLElement");
return false;
} else if (typeof element.classList !== "object") {
if (!NodeList.prototype.isPrototypeOf(element)) {
@amadeuszblanik
amadeuszblanik / inlineSVG.ts
Last active February 10, 2020 07:54
[TypeScript][JavaScript] Inline SVG using Fetch API
//
// inlineSVG.ts
// Works with React
//
// Created by Amadeusz Blanik on 24/05/2019.
// Copyright © 2019 Amadeusz Blanik. All rights reserved.
//
interface SVGResponse extends Response {
parsedBody?: string;
@amadeuszblanik
amadeuszblanik / peselVadation.ts
Last active November 15, 2023 09:50
Validate polish PESEL / NIP / REGON number — TypeScript — JavaScript ES6 — MIT
const checkSum = (digits: number[]) => {
const digit11 = digits[10];
digits.pop();
const times = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
const reducer = (accumulator, currentValue, index) => accumulator + (currentValue * times[index]);
let sum = digits.reduce(reducer);
sum %= 10;
const preventDefault = evt => {
evt.preventDefault()
}
const disableScroll = () => {
window.addEventListener("touchmove", preventDefault, { passive: false })
window.addEventListener("touchend", preventDefault, { passive: false })
}
const enableScroll = () => {
@amadeuszblanik
amadeuszblanik / forEachObject.ts
Last active April 14, 2020 07:53
forEach object in TypeScript
interface ActionTypeProps<P> {
key: keyof P,
value: any,
}
type actionType<P> = (props: ActionTypeProps<P>) => void
type forEachObjectType<P> = (object: P, action: actionType<P>) => void
export const forEachObject: forEachObjectType<{ [key: string]: any }> = (object, action) => {
for (const [key, value] of Object.entries(object)) {
type DATE = number | Date;
const makeNumber = (value: DATE) => {
if (typeof value !== "number") {
value = value.getTime();
}
return value;
};
const daysDiff = (dateA: DATE, dateB: DATE) => {
@amadeuszblanik
amadeuszblanik / index.ts
Last active January 9, 2020 16:31
Sample use of RxJS
export { settingsStore } from "./settings";
export { withSettings } from "./withSettings";
export { useSettings } from "./useSettings";
@amadeuszblanik
amadeuszblanik / regexp.ts
Last active January 15, 2020 17:43
Regular Expresions
/**
* Regular Expression
* for common use!
*
* @author Amadeusz Blanik
*/
const GoogleAnalytics = /^UA-\d{4,9}-\d{1,4}$/i;
const GoogleTagManager = /^GTM-[A-Z0-9]{1,7}$/i;
const FacebookPages = /(?:https?:\/\/)?(?:www\.)?facebook\.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[\w\-]*\/)*([\w\-\.]*)/ // @author https://gist.github.com/marcgg/733592