Skip to content

Instantly share code, notes, and snippets.

Avatar
👾
yarn dev

Amadeusz Blanik amadeuszblanik

👾
yarn dev
View GitHub Profile
@amadeuszblanik
amadeuszblanik / idGenerator.ts
Created Mar 3, 2020
[TypeScript] Generate random ID
View idGenerator.ts
export const idGenerator = (length: number) => {
const allowedCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
let response: string = ""
for (let i = 0; i < length; i++) {
const {length: charLength} = allowedCharacters
response += allowedCharacters.charAt(Math.floor(Math.random() * charLength))
}
return response
@amadeuszblanik
amadeuszblanik / regexp.ts
Last active Jan 15, 2020
Regular Expresions
View regexp.ts
/**
* 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
@amadeuszblanik
amadeuszblanik / index.ts
Last active Jan 9, 2020
Sample use of RxJS
View index.ts
export { settingsStore } from "./settings";
export { withSettings } from "./withSettings";
export { useSettings } from "./useSettings";
View dateDiff.ts
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 / forEachObject.ts
Last active Apr 14, 2020
forEach object in TypeScript
View forEachObject.ts
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)) {
View disableIOSScroll.js
const preventDefault = evt => {
evt.preventDefault()
}
const disableScroll = () => {
window.addEventListener("touchmove", preventDefault, { passive: false })
window.addEventListener("touchend", preventDefault, { passive: false })
}
const enableScroll = () => {
@amadeuszblanik
amadeuszblanik / peselVadation.ts
Last active May 18, 2020
Validate polish PESEL / NIP / REGON number — TypeScript — JavaScript ES6 — MIT
View peselVadation.ts
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;
@amadeuszblanik
amadeuszblanik / inlineSVG.ts
Last active Feb 10, 2020
[TypeScript][JavaScript] Inline SVG using Fetch API
View inlineSVG.ts
//
// 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 / tdd_nodeelements.js
Last active Jul 28, 2019
Test if provided element is DOM element
View tdd_nodeelements.js
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 / mobileDetect.js
Last active Jun 5, 2019
Simple function to detect mobile `window.mobile`
View mobileDetect.js
export default class MobileDetect {
constructor() {
this.mount();
}
mount() {
if (typeof window === "object") {
if (window.mobile !== "undefined") {
window.mobile = function () {
let check = false;
You can’t perform that action at this time.