Skip to content

Instantly share code, notes, and snippets.

View dawiidio's full-sized avatar

Dawid Wojda dawiidio

View GitHub Profile
@dawiidio
dawiidio / useDebounce.tsx
Created October 13, 2020 21:58
Dead-simple debounce hook for React.js
function useDebounce(func:() => any, args: Array<any>, delay:number = 250) {
const [timeoutId, setTimeoutId] = useState<number>();
useEffect(() => {
if (timeoutId !== undefined)
clearInterval(timeoutId);
//@ts-ignore
setTimeoutId(setTimeout(() => {
func();
@dawiidio
dawiidio / checkType.ts
Created July 7, 2020 14:58
Check type of class based on string property
interface Entry {
type: string
}
function isTypeOf<T extends Entry>(expectedType:string, testedEntry:T): testedEntry is T {
return expectedType === testedEntry.type;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My test page</title>
<script>
window.appConfig = {
"CLIENT_NAME": "Client",
"PRIMARY_COLOR": "green"
};
</script>
version: '3.4'
services:
client_a:
build:
context: ./
environment:
PRIMARY_COLOR: green
CLIENT_NAME: "Client A"
ports:
FROM node:12
ENV PRIMARY_COLOR ''
ENV CLIENT_NAME ''
COPY . /app/
WORKDIR /app
RUN npm install
RUN npm run build
RUN npm install -g env-serve
EXPOSE 3000
WORKDIR /app/src
const PRIMARY_COLOR = process.env.PRIMARY_COLOR || 'green';
// do some build stuff with webpack etc.
export PROJECT_VERSION=$(package.json | grep version | grep -oh -P '\d{1,2}\.\d{1,3}.\d{1,4}')
@dawiidio
dawiidio / configFromEnv.js
Last active July 2, 2019 11:52
Webpack plugin for config files
const { writeFileSync } = require('fs');
const { default: InjectPlugin } = require('webpack-inject-plugin');
/**
*
* @param config {Object}
*/
const logConfig = config => console.log('\nApp build with config: \n', JSON.stringify(config, null, 4));
/**
@dawiidio
dawiidio / CustomPlugin.js
Last active July 2, 2019 08:34
Webpack custom plugin
class CustomPlugin {
constructor(name, stage, cb) {
this.name = name;
this.cb = cb;
this.stage = stage;
}
apply(compiler) {
compiler.hooks[this.stage].tap(this.name, () => {
this.cb();
@dawiidio
dawiidio / memo.js
Created May 14, 2019 23:16
JS memo function - naive approach
function memo(fn) {
const cache = new Map();
return function(...params) {
const paramsKey = JSON.stringify(params);
if (cache.has(paramsKey))
return cache.get(paramsKey);
const val = fn(...params);